source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/interleave.cpp @ 983

Revision 983, 10.1 KB checked in by gumbau, 18 years ago (diff)
Line 
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
11namespace       VMI
12{
13
14VertexIL *intertwined = NULL;
15
16static Vertex_ *buf_vertices = NULL;
17static Color   *buf_colors = NULL;
18
19static GLuint vertex_buf = 0,
20       color_buf = 0;
21}
22
23using namespace VMI;
24       
25void 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
98void VMI::setupVertexBufferObjects(Mesh *mesh, Color *colors) {
99   
100    setupVertexArray(mesh, colors);
101   
102    // Disable the vertex array functionality:
103    glDisableClientState(GL_VERTEX_ARRAY);
104    glDisableClientState(GL_COLOR_ARRAY);
105   
106    glDeleteBuffersARB(1, &vertex_buf);
107   
108    glGenBuffersARB(1, &vertex_buf);
109    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf);
110    glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->currentNumTriangles * 3 * 3 * sizeof(float), buf_vertices, GL_STATIC_DRAW_ARB); //upload data
111   
112    glDeleteBuffersARB(1, &color_buf);
113   
114    glGenBuffersARB(1, &color_buf);
115    glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf);
116    glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->currentNumTriangles * 3 * sizeof(Color), buf_colors, GL_STATIC_DRAW_ARB); //upload data
117   
118    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf);
119    glVertexPointer(3, GL_FLOAT, 0,0);
120   
121    glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf);
122    glColorPointer(4, GL_UNSIGNED_BYTE, 0, 0);
123   
124    // Enable the vertex array functionality:
125    glEnableClientState(GL_VERTEX_ARRAY);
126    glEnableClientState(GL_COLOR_ARRAY);
127   
128    free(buf_vertices);
129    buf_vertices = NULL;
130   
131    free(buf_colors);
132    buf_colors = NULL;
133}
134
135void VMI::updateVertexArray(Mesh *mesh)
136{
137    GLuint i, v1, v2, v3, n = 0;
138
139    if (buf_vertices != NULL) free(buf_vertices);
140
141    buf_vertices = (Vertex_ *)malloc(mesh->currentNumTriangles * 3 * sizeof(Vertex_));
142
143    if (buf_vertices == NULL) {
144        fprintf(stderr, "Error allocating memory\n");
145        exit(1);
146    }
147
148    for (i=0; i<mesh->numTriangles; i++) {
149       
150        if (mesh->triangles[i].enable == TRUE) {
151           
152            v1= mesh->triangles[i].indices[0];
153            v2= mesh->triangles[i].indices[1];
154            v3= mesh->triangles[i].indices[2];
155           
156            buf_vertices[n].x = mesh->vertices[v1].x;
157            buf_vertices[n].y = mesh->vertices[v1].y;
158            buf_vertices[n].z = mesh->vertices[v1].z;
159            n++;
160           
161            buf_vertices[n].x = mesh->vertices[v2].x;
162            buf_vertices[n].y = mesh->vertices[v2].y;
163            buf_vertices[n].z = mesh->vertices[v2].z;
164            n++;
165           
166            buf_vertices[n].x = mesh->vertices[v3].x;
167            buf_vertices[n].y = mesh->vertices[v3].y;
168            buf_vertices[n].z = mesh->vertices[v3].z;
169            n++;
170        }
171    }
172#ifdef VERTEX_ARRAY
173    // Enable the buf_vertices vector
174    glEnableClientState(GL_VERTEX_ARRAY);
175   
176    glVertexPointer(3, GL_FLOAT, 0, buf_vertices);
177#endif
178}
179
180void VMI::updateVertexBufferObjects(Mesh *mesh) {
181   
182    updateVertexArray(mesh);
183   
184    // Disable the vertex array functionality:
185    glDisableClientState(GL_VERTEX_ARRAY);
186   
187    glDeleteBuffersARB(1, &vertex_buf);
188   
189    glGenBuffersARB(1, &vertex_buf);
190    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf);
191    glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->currentNumTriangles * 3 * 3 * sizeof(float), buf_vertices, GL_STATIC_DRAW_ARB); //upload data
192   
193    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf);
194    glVertexPointer(3, GL_FLOAT, 0,0);
195   
196    // Enable the vertex array functionality:
197    glEnableClientState(GL_VERTEX_ARRAY);
198   
199    free(buf_vertices);
200    buf_vertices = NULL;
201}
202
203void VMI::setupInterleave(Mesh *mesh, Color *colors)
204{
205    GLuint i, v1, v2, v3, n = 0;
206    GLubyte r, g, b, a;
207   
208    if (intertwined != NULL) free(intertwined);
209   
210    intertwined = (VertexIL *)malloc(mesh->currentNumTriangles * 3 * sizeof(VertexIL));
211   
212    if (intertwined == NULL) {
213        fprintf(stderr, "Error allocating memory\n");
214        exit(1);
215    }
216   
217    for (i=0; i<mesh->numTriangles; i++) {
218       
219        if (mesh->triangles[i].enable == TRUE) {
220            v1= mesh->triangles[i].indices[0];
221            v2= mesh->triangles[i].indices[1];
222            v3= mesh->triangles[i].indices[2];
223           
224            r = colors[i].r;
225            g = colors[i].g;
226            b = colors[i].b;
227            a = colors[i].a;
228           
229            intertwined[n].tx = 0.0f;
230            intertwined[n].ty = 0.0f;
231            intertwined[n].r = r;
232            intertwined[n].g = g;
233            intertwined[n].b = b;
234            intertwined[n].a = a;
235            intertwined[n].x = mesh->vertices[v1].x;
236            intertwined[n].y = mesh->vertices[v1].y;
237            intertwined[n].z = mesh->vertices[v1].z;
238            n++;
239           
240            intertwined[n].tx = 0.0f;
241            intertwined[n].ty = 0.0f;
242            intertwined[n].r = r;
243            intertwined[n].g = g;
244            intertwined[n].b = b;
245            intertwined[n].a = a;
246            intertwined[n].x = mesh->vertices[v2].x;
247            intertwined[n].y = mesh->vertices[v2].y;
248            intertwined[n].z = mesh->vertices[v2].z;
249            n++;
250           
251            intertwined[n].tx = 0.0f;
252            intertwined[n].ty = 0.0f;
253            intertwined[n].r = r;
254            intertwined[n].g = g;
255            intertwined[n].b = b;
256            intertwined[n].a = a;
257            intertwined[n].x = mesh->vertices[v3].x;
258            intertwined[n].y = mesh->vertices[v3].y;
259            intertwined[n].z = mesh->vertices[v3].z;
260            n++;
261        }
262    }
263}
264
265void VMI::printInterleave(int numTriangles)
266{
267    int i;
268
269    printf("\n");
270    for (i=0; i<numTriangles * 3; i++) {
271        printf("Interleave%d (%d, %d, %d ,%d, %f, %f ,%f)\n", i,
272            intertwined[i].r, intertwined[i].g, intertwined[i].b, intertwined[i].a,
273            intertwined[i].x , intertwined[i].y, intertwined[i].z);
274    }
275}
276
277void VMI::updateAllColorInterleave(Mesh *mesh, Color *colors)
278{
279    GLuint i, n = 0;
280    GLubyte r, g, b, a;
281
282    for (i=1; i<=mesh->numTriangles; i++) { // Triangles start at 1
283
284        r = colors[i].r;
285        g = colors[i].g;
286        b = colors[i].b;
287        a = colors[i].a;
288
289        intertwined[n].r = r;
290        intertwined[n].g = g;
291        intertwined[n].b = b;
292        intertwined[n].a = a;
293        n++;
294
295        intertwined[n].r = r;
296        intertwined[n].g = g;
297        intertwined[n].b = b;
298        intertwined[n].a = a;
299        n++;
300
301        intertwined[n].r = r;
302        intertwined[n].g = g;
303        intertwined[n].b = b;
304        intertwined[n].a = a;
305        n++;
306    }
307}
308
309void VMI::updateColorInterleave(Mesh *mesh, Color *colors, GLuint begin, GLuint end)
310{
311    GLuint i, n;
312    GLubyte r, g, b, a;
313
314    if (end > mesh->numTriangles) return;
315
316    for (i=begin; i<end; i++) {
317       
318        n = i * 3;
319
320        r = colors[i].r;
321        g = colors[i].g;
322        b = colors[i].b;
323        a = colors[i].a;
324
325        intertwined[n].r = r;
326        intertwined[n].g = g;
327        intertwined[n].b = b;
328        intertwined[n].a = a;
329
330        n++;
331        intertwined[n].r = r;
332        intertwined[n].g = g;
333        intertwined[n].b = b;
334        intertwined[n].a = a;
335
336        n++;
337        intertwined[n].r = r;
338        intertwined[n].g = g;
339        intertwined[n].b = b;
340        intertwined[n].a = a;
341    }
342}
343
344void VMI::fillColorInterleave(Mesh *mesh, GLuint begin, GLuint end, GLubyte color)
345{
346    GLuint i, n;
347
348    if (end > mesh->numTriangles) return;
349
350    for (i=begin; i<end; i++) {
351       
352        n = i * 3;
353        intertwined[n].r = color;
354        intertwined[n].g = color;
355        intertwined[n].b = color;
356        intertwined[n].a = color;
357
358        n++;
359        intertwined[n].r = color;
360        intertwined[n].g = color;
361        intertwined[n].b = color;
362        intertwined[n].a = color;
363
364        n++;
365        intertwined[n].r = color;
366        intertwined[n].g = color;
367        intertwined[n].b = color;
368        intertwined[n].a = color;
369    }
370}
Note: See TracBrowser for help on using the repository browser.