1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | |
---|
4 | #include "../include/GL/glew.h" |
---|
5 | #ifdef _WIN32 |
---|
6 | #include "../include/GL/wglew.h" |
---|
7 | #endif |
---|
8 | #include "../include/interleave.h" |
---|
9 | #include "../include/global.h" |
---|
10 | |
---|
11 | namespace VMI |
---|
12 | { |
---|
13 | |
---|
14 | VertexIL *intertwined = NULL; |
---|
15 | |
---|
16 | static Vertex_ *buf_vertices = NULL; |
---|
17 | static Color *buf_colors = NULL; |
---|
18 | |
---|
19 | GLuint vertex_buf = 0, |
---|
20 | color_buf = 0; |
---|
21 | } |
---|
22 | |
---|
23 | using namespace VMI; |
---|
24 | |
---|
25 | void VMI::setupVertexArray(Mesh *mesh, Color *colors) |
---|
26 | { |
---|
27 | GLuint i, v1, v2, v3, n = 0; |
---|
28 | GLubyte r, g, b, a; |
---|
29 | |
---|
30 | if (buf_vertices != NULL) free(buf_vertices); |
---|
31 | if (buf_colors != NULL) free(buf_colors); |
---|
32 | |
---|
33 | buf_vertices = (Vertex_ *)malloc(mesh->currentNumTriangles * 3 * sizeof(Vertex_)); |
---|
34 | |
---|
35 | buf_colors = (Color *)malloc(mesh->currentNumTriangles * 3 * sizeof(Color)); |
---|
36 | |
---|
37 | if (buf_vertices == NULL) { |
---|
38 | fprintf(stderr, "Error allocating memory\n"); |
---|
39 | exit(1); |
---|
40 | } |
---|
41 | |
---|
42 | if (buf_colors == NULL) { |
---|
43 | fprintf(stderr, "Error allocating memory\n"); |
---|
44 | exit(1); |
---|
45 | } |
---|
46 | |
---|
47 | for (i=0; i<mesh->numTriangles; i++) { |
---|
48 | |
---|
49 | if (mesh->triangles[i].enable == TRUE) { |
---|
50 | |
---|
51 | v1= mesh->triangles[i].indices[0]; |
---|
52 | v2= mesh->triangles[i].indices[1]; |
---|
53 | v3= mesh->triangles[i].indices[2]; |
---|
54 | |
---|
55 | r = colors[i].r; |
---|
56 | g = colors[i].g; |
---|
57 | b = colors[i].b; |
---|
58 | a = colors[i].a; |
---|
59 | |
---|
60 | buf_colors[n].r = r; |
---|
61 | buf_colors[n].g = g; |
---|
62 | buf_colors[n].b = b; |
---|
63 | buf_colors[n].a = a; |
---|
64 | buf_vertices[n].x = mesh->vertices[v1].x; |
---|
65 | buf_vertices[n].y = mesh->vertices[v1].y; |
---|
66 | buf_vertices[n].z = mesh->vertices[v1].z; |
---|
67 | n++; |
---|
68 | |
---|
69 | buf_colors[n].r = r; |
---|
70 | buf_colors[n].g = g; |
---|
71 | buf_colors[n].b = b; |
---|
72 | buf_colors[n].a = a; |
---|
73 | buf_vertices[n].x = mesh->vertices[v2].x; |
---|
74 | buf_vertices[n].y = mesh->vertices[v2].y; |
---|
75 | buf_vertices[n].z = mesh->vertices[v2].z; |
---|
76 | n++; |
---|
77 | |
---|
78 | buf_colors[n].r = r; |
---|
79 | buf_colors[n].g = g; |
---|
80 | buf_colors[n].b = b; |
---|
81 | buf_colors[n].a = a; |
---|
82 | buf_vertices[n].x = mesh->vertices[v3].x; |
---|
83 | buf_vertices[n].y = mesh->vertices[v3].y; |
---|
84 | buf_vertices[n].z = mesh->vertices[v3].z; |
---|
85 | n++; |
---|
86 | } |
---|
87 | } |
---|
88 | #ifdef VERTEX_ARRAY |
---|
89 | // Enable the buf_vertices vector |
---|
90 | glEnableClientState(GL_COLOR_ARRAY); |
---|
91 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
92 | |
---|
93 | glColorPointer(4, GL_UNSIGNED_BYTE, 0, buf_colors); |
---|
94 | glVertexPointer(3, GL_FLOAT, 0, buf_vertices); |
---|
95 | #endif |
---|
96 | } |
---|
97 | |
---|
98 | void VMI::setupVertexBufferObjects(Mesh *mesh, Color *colors) { |
---|
99 | |
---|
100 | setupVertexArray(mesh, colors); |
---|
101 | |
---|
102 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); |
---|
103 | glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->currentNumTriangles * 3 * 3 * sizeof(float), buf_vertices, GL_STATIC_DRAW_ARB); //upload data |
---|
104 | |
---|
105 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf); |
---|
106 | glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->currentNumTriangles * 3 * sizeof(Color), buf_colors, GL_STATIC_DRAW_ARB); //upload data |
---|
107 | |
---|
108 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); |
---|
109 | glVertexPointer(3, GL_FLOAT, 0,0); |
---|
110 | |
---|
111 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf); |
---|
112 | glColorPointer(4, GL_UNSIGNED_BYTE, 0, 0); |
---|
113 | |
---|
114 | // Enable the vertex array functionality: |
---|
115 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
116 | glEnableClientState(GL_COLOR_ARRAY); |
---|
117 | |
---|
118 | free(buf_vertices); |
---|
119 | buf_vertices = NULL; |
---|
120 | |
---|
121 | free(buf_colors); |
---|
122 | buf_colors = NULL; |
---|
123 | } |
---|
124 | |
---|
125 | void VMI::updateVertexArray(Mesh *mesh) |
---|
126 | { |
---|
127 | GLuint i, v1, v2, v3, n = 0; |
---|
128 | |
---|
129 | if (buf_vertices != NULL) free(buf_vertices); |
---|
130 | |
---|
131 | buf_vertices = (Vertex_ *)malloc(mesh->currentNumTriangles * 3 * sizeof(Vertex_)); |
---|
132 | |
---|
133 | if (buf_vertices == NULL) { |
---|
134 | fprintf(stderr, "Error allocating memory\n"); |
---|
135 | exit(1); |
---|
136 | } |
---|
137 | |
---|
138 | for (i=0; i<mesh->numTriangles; i++) { |
---|
139 | |
---|
140 | if (mesh->triangles[i].enable == TRUE) { |
---|
141 | |
---|
142 | v1= mesh->triangles[i].indices[0]; |
---|
143 | v2= mesh->triangles[i].indices[1]; |
---|
144 | v3= mesh->triangles[i].indices[2]; |
---|
145 | |
---|
146 | buf_vertices[n].x = mesh->vertices[v1].x; |
---|
147 | buf_vertices[n].y = mesh->vertices[v1].y; |
---|
148 | buf_vertices[n].z = mesh->vertices[v1].z; |
---|
149 | n++; |
---|
150 | |
---|
151 | buf_vertices[n].x = mesh->vertices[v2].x; |
---|
152 | buf_vertices[n].y = mesh->vertices[v2].y; |
---|
153 | buf_vertices[n].z = mesh->vertices[v2].z; |
---|
154 | n++; |
---|
155 | |
---|
156 | buf_vertices[n].x = mesh->vertices[v3].x; |
---|
157 | buf_vertices[n].y = mesh->vertices[v3].y; |
---|
158 | buf_vertices[n].z = mesh->vertices[v3].z; |
---|
159 | n++; |
---|
160 | } |
---|
161 | } |
---|
162 | #ifdef VERTEX_ARRAY |
---|
163 | // Enable the buf_vertices vector |
---|
164 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
165 | |
---|
166 | glVertexPointer(3, GL_FLOAT, 0, buf_vertices); |
---|
167 | #endif |
---|
168 | } |
---|
169 | |
---|
170 | void VMI::updateVertexBufferObjects(Mesh *mesh) { |
---|
171 | |
---|
172 | updateVertexArray(mesh); |
---|
173 | |
---|
174 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); |
---|
175 | glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->currentNumTriangles * 3 * 3 * sizeof(float), buf_vertices, GL_STATIC_DRAW_ARB); //upload data |
---|
176 | |
---|
177 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); |
---|
178 | glVertexPointer(3, GL_FLOAT, 0,0); |
---|
179 | |
---|
180 | // Enable the vertex array functionality: |
---|
181 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
182 | |
---|
183 | free(buf_vertices); |
---|
184 | buf_vertices = NULL; |
---|
185 | } |
---|
186 | |
---|
187 | void VMI::setupInterleave(Mesh *mesh, Color *colors) |
---|
188 | { |
---|
189 | GLuint i, v1, v2, v3, n = 0; |
---|
190 | GLubyte r, g, b, a; |
---|
191 | |
---|
192 | if (intertwined != NULL) free(intertwined); |
---|
193 | |
---|
194 | intertwined = (VertexIL *)malloc(mesh->currentNumTriangles * 3 * sizeof(VertexIL)); |
---|
195 | |
---|
196 | if (intertwined == NULL) { |
---|
197 | fprintf(stderr, "Error allocating memory\n"); |
---|
198 | exit(1); |
---|
199 | } |
---|
200 | |
---|
201 | for (i=0; i<mesh->numTriangles; i++) { |
---|
202 | |
---|
203 | if (mesh->triangles[i].enable == TRUE) { |
---|
204 | v1= mesh->triangles[i].indices[0]; |
---|
205 | v2= mesh->triangles[i].indices[1]; |
---|
206 | v3= mesh->triangles[i].indices[2]; |
---|
207 | |
---|
208 | r = colors[i].r; |
---|
209 | g = colors[i].g; |
---|
210 | b = colors[i].b; |
---|
211 | a = colors[i].a; |
---|
212 | |
---|
213 | intertwined[n].tx = 0.0f; |
---|
214 | intertwined[n].ty = 0.0f; |
---|
215 | intertwined[n].r = r; |
---|
216 | intertwined[n].g = g; |
---|
217 | intertwined[n].b = b; |
---|
218 | intertwined[n].a = a; |
---|
219 | intertwined[n].x = mesh->vertices[v1].x; |
---|
220 | intertwined[n].y = mesh->vertices[v1].y; |
---|
221 | intertwined[n].z = mesh->vertices[v1].z; |
---|
222 | n++; |
---|
223 | |
---|
224 | intertwined[n].tx = 0.0f; |
---|
225 | intertwined[n].ty = 0.0f; |
---|
226 | intertwined[n].r = r; |
---|
227 | intertwined[n].g = g; |
---|
228 | intertwined[n].b = b; |
---|
229 | intertwined[n].a = a; |
---|
230 | intertwined[n].x = mesh->vertices[v2].x; |
---|
231 | intertwined[n].y = mesh->vertices[v2].y; |
---|
232 | intertwined[n].z = mesh->vertices[v2].z; |
---|
233 | n++; |
---|
234 | |
---|
235 | intertwined[n].tx = 0.0f; |
---|
236 | intertwined[n].ty = 0.0f; |
---|
237 | intertwined[n].r = r; |
---|
238 | intertwined[n].g = g; |
---|
239 | intertwined[n].b = b; |
---|
240 | intertwined[n].a = a; |
---|
241 | intertwined[n].x = mesh->vertices[v3].x; |
---|
242 | intertwined[n].y = mesh->vertices[v3].y; |
---|
243 | intertwined[n].z = mesh->vertices[v3].z; |
---|
244 | n++; |
---|
245 | } |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | void VMI::printInterleave(int numTriangles) |
---|
250 | { |
---|
251 | int i; |
---|
252 | |
---|
253 | printf("\n"); |
---|
254 | for (i=0; i<numTriangles * 3; i++) { |
---|
255 | printf("Interleave%d (%d, %d, %d ,%d, %f, %f ,%f)\n", i, |
---|
256 | intertwined[i].r, intertwined[i].g, intertwined[i].b, intertwined[i].a, |
---|
257 | intertwined[i].x , intertwined[i].y, intertwined[i].z); |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | void VMI::updateAllColorInterleave(Mesh *mesh, Color *colors) |
---|
262 | { |
---|
263 | GLuint i, n = 0; |
---|
264 | GLubyte r, g, b, a; |
---|
265 | |
---|
266 | for (i=1; i<=mesh->numTriangles; i++) { // Triangles start at 1 |
---|
267 | |
---|
268 | r = colors[i].r; |
---|
269 | g = colors[i].g; |
---|
270 | b = colors[i].b; |
---|
271 | a = colors[i].a; |
---|
272 | |
---|
273 | intertwined[n].r = r; |
---|
274 | intertwined[n].g = g; |
---|
275 | intertwined[n].b = b; |
---|
276 | intertwined[n].a = a; |
---|
277 | n++; |
---|
278 | |
---|
279 | intertwined[n].r = r; |
---|
280 | intertwined[n].g = g; |
---|
281 | intertwined[n].b = b; |
---|
282 | intertwined[n].a = a; |
---|
283 | n++; |
---|
284 | |
---|
285 | intertwined[n].r = r; |
---|
286 | intertwined[n].g = g; |
---|
287 | intertwined[n].b = b; |
---|
288 | intertwined[n].a = a; |
---|
289 | n++; |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | void VMI::updateColorInterleave(Mesh *mesh, Color *colors, GLuint begin, GLuint end) |
---|
294 | { |
---|
295 | GLuint i, n; |
---|
296 | GLubyte r, g, b, a; |
---|
297 | |
---|
298 | if (end > mesh->numTriangles) return; |
---|
299 | |
---|
300 | for (i=begin; i<end; i++) { |
---|
301 | |
---|
302 | n = i * 3; |
---|
303 | |
---|
304 | r = colors[i].r; |
---|
305 | g = colors[i].g; |
---|
306 | b = colors[i].b; |
---|
307 | a = colors[i].a; |
---|
308 | |
---|
309 | intertwined[n].r = r; |
---|
310 | intertwined[n].g = g; |
---|
311 | intertwined[n].b = b; |
---|
312 | intertwined[n].a = a; |
---|
313 | |
---|
314 | n++; |
---|
315 | intertwined[n].r = r; |
---|
316 | intertwined[n].g = g; |
---|
317 | intertwined[n].b = b; |
---|
318 | intertwined[n].a = a; |
---|
319 | |
---|
320 | n++; |
---|
321 | intertwined[n].r = r; |
---|
322 | intertwined[n].g = g; |
---|
323 | intertwined[n].b = b; |
---|
324 | intertwined[n].a = a; |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | void VMI::fillColorInterleave(Mesh *mesh, GLuint begin, GLuint end, GLubyte color) |
---|
329 | { |
---|
330 | GLuint i, n; |
---|
331 | |
---|
332 | if (end > mesh->numTriangles) return; |
---|
333 | |
---|
334 | for (i=begin; i<end; i++) { |
---|
335 | |
---|
336 | n = i * 3; |
---|
337 | intertwined[n].r = color; |
---|
338 | intertwined[n].g = color; |
---|
339 | intertwined[n].b = color; |
---|
340 | intertwined[n].a = color; |
---|
341 | |
---|
342 | n++; |
---|
343 | intertwined[n].r = color; |
---|
344 | intertwined[n].g = color; |
---|
345 | intertwined[n].b = color; |
---|
346 | intertwined[n].a = color; |
---|
347 | |
---|
348 | n++; |
---|
349 | intertwined[n].r = color; |
---|
350 | intertwined[n].g = color; |
---|
351 | intertwined[n].b = color; |
---|
352 | intertwined[n].a = color; |
---|
353 | } |
---|
354 | } |
---|