-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvbo_Utilities.cpp
236 lines (179 loc) · 5.75 KB
/
vbo_Utilities.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "vbo_Utilities.h"
// Constructor :
CVBO_Model::CVBO_Model () {
use_normals = false;
use_colors = false;
use_textures = false;
ready_for_render = false;
return;
}
// vertex attrib functions :
void CVBO_Model::bulk_init_vertices (int numVertices, vector4f Vertices[]) {
this->numVertices = numVertices;
vertices = (vector4f *)malloc (sizeof(vector4f) * numVertices);
for (int i=0; i<numVertices; i++) {
for (int j=0; j<4; j++) {
vertices[i].values[j] = Vertices[i].values[j];
}
}
return;
}
void CVBO_Model::bulk_init_normals (int numVertices, vector3f Normals[]) {
this->numVertices = numVertices;
normals = (vector3f *)malloc (sizeof (vector3f) * numVertices);
for (int i=0; i<numVertices; i++) {
for (int j=0; j<3; j++) {
normals[i].values[j] = Normals[i].values[j];
}
}
use_normals = true;
return;
}
void CVBO_Model::bulk_init_colors (int numVertices, vector4f Colors[]) {
this->numVertices = numVertices;
colors = (vector4f *)malloc (sizeof(vector4f) * numVertices);
for (int i=0; i<numVertices; i++) {
for (int j=0; j<4; j++) {
colors[i].values[j] = Colors[i].values[j];
}
}
use_colors = true;
return;
}
void CVBO_Model::bulk_init_textures (int numVertices, vector2f Textures[]) {
this->numVertices = numVertices;
textures = (vector2f *)malloc (sizeof(vector2f) * numVertices);
for (int i=0; i<numVertices; i++) {
for (int j=0; j<2; j++) {
textures[i].values[j] = Textures[i].values[j];
}
}
use_textures = true;
return;
}
void CVBO_Model::add_vertex (vector4f *Vertex, vector3f *Normal, vector4f *Color, vector2f *Texture) {
vertices = (vector4f *)realloc (vertices, sizeof (vector4f) * (numVertices +1));
normals = (vector3f *)realloc (normals, sizeof (vector3f) * (numVertices +1));
colors = (vector4f *)realloc (colors, sizeof (vector4f) * (numVertices +1));
textures = (vector2f *)realloc (textures, sizeof (vector2f) * (numVertices +1));
for (int j=0; j<4; j++) {
if (Vertex != NULL) {
vertices[numVertices].values[j] = Vertex->values[j];
}
if (Color != NULL) {
colors[numVertices].values[j] = Color->values[j];
}
}
for (int j=0; j<3; j++) {
if (Normal != NULL) {
normals[numVertices].values[j] = Normal->values[j];
}
}
for (int j=0; j<2; j++) {
if (Texture != NULL) {
textures[numVertices].values[j] = Texture->values[j];
}
}
if (Normal) { use_normals = true; }
if (Color) { use_colors = true; }
if (Texture) { use_textures = true; }
numVertices++;
return;
}
void CVBO_Model::set_vertex (int vertex_idx, vector4f Vertex) {
for (int j=0; j<4; j++) {
vertices[vertex_idx].values[j] = Vertex.values[j];
}
return;
}
void CVBO_Model::set_normal (int vertex_idx, vector3f Normal) {
for (int j=0; j<3; j++) {
normals[vertex_idx].values[j] = Normal.values[j];
}
return;
}
void CVBO_Model::set_color (int vertex_idx, vector4f Color) {
for (int j=0; j<4; j++) {
colors[vertex_idx].values[j] = Color.values[j];
}
return;
}
void CVBO_Model::set_texture (int vertex_idx, vector2f Texture) {
for (int j=0; j<2; j++) {
textures[vertex_idx].values[j] = Texture.values[j];
}
return;
}
// indices functions :
void CVBO_Model::bulk_init_indices (int numFaces, GLuint Indices[]) {
this->numFaces = numFaces;
indices = (GLuint *)malloc (sizeof(GLuint) * numFaces * NUM_VERTS_PER_FACE);
for (int i=0; i < (numFaces * NUM_VERTS_PER_FACE); i++) {
indices[i] = Indices[i];
}
return;
}
void CVBO_Model::add_face (GLuint face_indices[NUM_VERTS_PER_FACE]) {
indices = (GLuint *)realloc (indices, sizeof(GLuint) * (numFaces + 1) * NUM_VERTS_PER_FACE);
for (int i=numFaces; i < (numFaces + NUM_VERTS_PER_FACE); i++) {
indices[i] = face_indices[i-numFaces];
}
numFaces++;
return;
}
void CVBO_Model::make_VBOs () {
if (vertex_vbo == (GLuint)0) {
glGenBuffersARB (1, &vertex_vbo);
glBindBufferARB (GL_ARRAY_BUFFER_ARB, vertex_vbo);
glBufferDataARB (GL_ARRAY_BUFFER_ARB, sizeof(vector4f) * numVertices, vertices, GL_STATIC_DRAW_ARB);
}
if (use_normals && normal_vbo == (GLuint)0) {
glGenBuffersARB (1, &normal_vbo);
glBindBufferARB (GL_ARRAY_BUFFER_ARB, normal_vbo);
glBufferDataARB (GL_ARRAY_BUFFER_ARB, sizeof(vector3f) * numVertices, normals, GL_STATIC_DRAW_ARB);
}
if (use_colors && color_vbo == (GLuint)0) {
glGenBuffersARB (1, &color_vbo);
glBindBufferARB (GL_ARRAY_BUFFER_ARB, color_vbo);
glBufferDataARB (GL_ARRAY_BUFFER_ARB, sizeof(vector4f) * numVertices, colors, GL_STATIC_DRAW_ARB);
}
if (use_textures && texture_vbo == (GLuint)0) {
glGenBuffersARB (1, &texture_vbo);
glBindBufferARB (GL_ARRAY_BUFFER_ARB, texture_vbo);
glBufferDataARB (GL_ARRAY_BUFFER_ARB, sizeof(vector2f) * numVertices, textures, GL_STATIC_DRAW_ARB);
}
if (index_vbo == (GLuint)0) {
glGenBuffersARB (1, &index_vbo);
glBindBufferARB (GL_ELEMENT_ARRAY_BUFFER_ARB, index_vbo);
glBufferDataARB (GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(GLuint) * numFaces * NUM_VERTS_PER_FACE, indices, GL_STATIC_DRAW_ARB);
}
ready_for_render = true;
return;
}
void CVBO_Model::draw () {
if (!ready_for_render) {
return;
}
glBindBufferARB (GL_ARRAY_BUFFER_ARB, vertex_vbo);
glEnableClientState (GL_VERTEX_ARRAY);
glVertexPointer (4, GL_FLOAT, 0, 0);
if (use_normals) {
glBindBufferARB (GL_ARRAY_BUFFER_ARB, normal_vbo);
glEnableClientState (GL_NORMAL_ARRAY);
glNormalPointer (GL_FLOAT, 0, 0);
}
if (use_colors) {
glBindBufferARB (GL_ARRAY_BUFFER_ARB, color_vbo);
glEnableClientState (GL_COLOR_ARRAY);
glColorPointer (4, GL_FLOAT, 0, 0);
}
if (use_textures) {
glBindBufferARB (GL_ARRAY_BUFFER_ARB, texture_vbo);
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer (2, GL_FLOAT, 0, 0);
}
glBindBufferARB (GL_ELEMENT_ARRAY_BUFFER_ARB, index_vbo);
glEnableClientState(GL_INDEX_ARRAY);
glDrawElements (GL_QUADS, numFaces * NUM_VERTS_PER_FACE, GL_UNSIGNED_INT, 0);
return;
}