source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/buffers.cpp @ 2127

Revision 2127, 14.2 KB checked in by gumbau, 17 years ago (diff)
Line 
1#include <stdio.h>
2#include <stdlib.h>
3
4#include "../include/GL/glew.h"
5
6#include "../include/buffers.h"
7#include "../include/global.h"
8
9namespace       VMI
10{
11Vertex_ *buf_vertices = NULL;
12Color   *buf_colors = NULL;
13
14Vertex_ *pbuf_vertices;
15Color   *pbuf_colors;
16
17GLuint vertex_buf = 0,
18       color_buf = 0;
19
20}
21
22using namespace VMI;
23
24void VMI::saveVertexBuffer(Change *c, VertexIL *dest) {
25    int j, t, n = 0, m;
26   
27    // Save vertex coordinates and color for deleted triangles
28    for (j=0; j<c->numDel; j++) {
29       
30        t = c->deleted[j].id;
31       
32        m = 3 * t;
33       
34        dest[n].x = buf_vertices[m + 0].x;
35        dest[n].y = buf_vertices[m + 0].y;
36        dest[n].z = buf_vertices[m + 0].z;
37        dest[n].a = buf_colors[m + 0].a;
38        dest[n].b = buf_colors[m + 0].b;
39        dest[n].r = buf_colors[m + 0].r;
40        dest[n].g = buf_colors[m + 0].g;
41        n++;
42       
43        dest[n].x = buf_vertices[m + 1].x;
44        dest[n].y = buf_vertices[m + 1].y;
45        dest[n].z = buf_vertices[m + 1].z;
46        dest[n].a = buf_colors[m + 1].a;
47        dest[n].b = buf_colors[m + 1].b;
48        dest[n].r = buf_colors[m + 1].r;
49        dest[n].g = buf_colors[m + 1].g;
50        n++;
51       
52        dest[n].x = buf_vertices[m + 2].x;
53        dest[n].y = buf_vertices[m + 2].y;
54        dest[n].z = buf_vertices[m + 2].z;
55        dest[n].a = buf_colors[m + 2].a;
56        dest[n].b = buf_colors[m + 2].b;
57        dest[n].r = buf_colors[m + 2].r;
58        dest[n].g = buf_colors[m + 2].g;
59        n++;
60    }
61    // Save vertex coordinates for modified triangles
62    for (j=0; j<c->numMod; j++) {
63       
64        t = c->modified[j].id;
65       
66        m = 3 * t;
67       
68        dest[n].x = buf_vertices[m + 0].x;
69        dest[n].y = buf_vertices[m + 0].y;
70        dest[n].z = buf_vertices[m + 0].z;
71        n++;
72       
73        dest[n].x = buf_vertices[m + 1].x;
74        dest[n].y = buf_vertices[m + 1].y;
75        dest[n].z = buf_vertices[m + 1].z;
76        n++;
77       
78        dest[n].x = buf_vertices[m + 2].x;
79        dest[n].y = buf_vertices[m + 2].y;
80        dest[n].z = buf_vertices[m + 2].z;
81        n++;
82    }
83}
84
85void VMI::loadVertexBuffer(VertexIL *src, Change *c) {
86    int j, t, n = 0, m;
87   
88    // Load vertex coordinates and color for deleted triangles
89    for (j=0; j<c->numDel; j++) {
90       
91        t = c->deleted[j].id;
92       
93        m = 3 * t;
94
95        buf_vertices[m + 0].x = src[n].x;
96        buf_vertices[m + 0].y = src[n].y;
97        buf_vertices[m + 0].z = src[n].z;
98        buf_colors[m + 0].a = src[n].a;
99        buf_colors[m + 0].b = src[n].b;
100        buf_colors[m + 0].r = src[n].r;
101        buf_colors[m + 0].g = src[n].g;
102        n++;
103
104        buf_vertices[m + 1].x = src[n].x;
105        buf_vertices[m + 1].y = src[n].y;
106        buf_vertices[m + 1].z = src[n].z;
107        buf_colors[m + 1].a = src[n].a;
108        buf_colors[m + 1].b = src[n].b;
109        buf_colors[m + 1].r = src[n].r;
110        buf_colors[m + 1].g = src[n].g;
111        n++;
112
113        buf_vertices[m + 2].x = src[n].x;
114        buf_vertices[m + 2].y = src[n].y;
115        buf_vertices[m + 2].z = src[n].z;
116        buf_colors[m + 2].a = src[n].a;
117        buf_colors[m + 2].b = src[n].b;
118        buf_colors[m + 2].r = src[n].r;
119        buf_colors[m + 2].g = src[n].g;
120        n++;
121    }
122    // Load vertex coordinates for modified triangles
123    for (j=0; j<c->numMod; j++) {
124       
125        t = c->modified[j].id;
126       
127        m = 3 * t;
128
129        buf_vertices[m + 0].x = src[n].x;
130        buf_vertices[m + 0].y = src[n].y;
131        buf_vertices[m + 0].z = src[n].z;
132        n++;
133
134        buf_vertices[m + 1].x = src[n].x;
135        buf_vertices[m + 1].y = src[n].y;
136        buf_vertices[m + 1].z = src[n].z;
137        n++;
138
139        buf_vertices[m + 2].x = src[n].x;
140        buf_vertices[m + 2].y = src[n].y;
141        buf_vertices[m + 2].z = src[n].z;
142        n++;
143    }
144}
145
146void VMI::setupVertexArray(Mesh *mesh, Color *colors)
147{
148    int i, v1, v2, v3, n = 0;
149    GLubyte r, g, b, a;
150
151    if (buf_vertices != NULL) free(buf_vertices);
152    if (buf_colors != NULL) free(buf_colors);
153
154    buf_vertices = (Vertex_ *)malloc(mesh->numTriangles * 3 * sizeof(Vertex_));
155
156    buf_colors = (Color *)malloc(mesh->numTriangles * 3 * sizeof(Color));
157
158    if (buf_vertices == NULL) {
159        fprintf(stderr, "Error allocating memory\n");
160        exit(1);
161    }
162
163    if (buf_colors == NULL) {
164        fprintf(stderr, "Error allocating memory\n");
165        exit(1);
166    }
167
168    for (i=0; i<mesh->numTriangles; i++) {
169
170        v1= mesh->triangles[i].indices[0];
171        v2= mesh->triangles[i].indices[1];
172        v3= mesh->triangles[i].indices[2];
173       
174        if (mesh->triangles[i].enable == TRUE) {
175           
176            r = colors[i].r;
177            g = colors[i].g;
178            b = colors[i].b;
179            a = colors[i].a;
180           
181            buf_colors[n].r = r;
182            buf_colors[n].g = g;
183            buf_colors[n].b = b;
184            buf_colors[n].a = a;
185            buf_vertices[n].x = mesh->vertices[v1].x;
186            buf_vertices[n].y = mesh->vertices[v1].y;
187            buf_vertices[n].z = mesh->vertices[v1].z;
188            n++;
189           
190            buf_colors[n].r = r;
191            buf_colors[n].g = g;
192            buf_colors[n].b = b;
193            buf_colors[n].a = a;
194            buf_vertices[n].x = mesh->vertices[v2].x;
195            buf_vertices[n].y = mesh->vertices[v2].y;
196            buf_vertices[n].z = mesh->vertices[v2].z;
197            n++;
198           
199            buf_colors[n].r = r;
200            buf_colors[n].g = g;
201            buf_colors[n].b = b;
202            buf_colors[n].a = a;
203            buf_vertices[n].x = mesh->vertices[v3].x;
204            buf_vertices[n].y = mesh->vertices[v3].y;
205            buf_vertices[n].z = mesh->vertices[v3].z;
206            n++;
207        } else {
208         
209            buf_colors[n].r = 0;
210            buf_colors[n].g = 0;
211            buf_colors[n].b = 0;
212            buf_colors[n].a = 0;
213            buf_vertices[n].x = 0;
214            buf_vertices[n].y = 0;
215            buf_vertices[n].z = 0;
216            n++;
217           
218            buf_colors[n].r = 0;
219            buf_colors[n].g = 0;
220            buf_colors[n].b = 0;
221            buf_colors[n].a = 0;
222            buf_vertices[n].x = 0;
223            buf_vertices[n].y = 0;
224            buf_vertices[n].z = 0;
225            n++;
226           
227            buf_colors[n].r = 0;
228            buf_colors[n].g = 0;
229            buf_colors[n].b = 0;
230            buf_colors[n].a = 0;
231            buf_vertices[n].x = 0;
232            buf_vertices[n].y = 0;
233            buf_vertices[n].z = 0;
234            n++;
235        }
236    }
237#ifdef VERTEX_ARRAY
238    // Enable the buf_vertices vector
239    glEnableClientState(GL_COLOR_ARRAY);
240    glEnableClientState(GL_VERTEX_ARRAY);
241   
242    glColorPointer(4, GL_UNSIGNED_BYTE, 0, buf_colors);
243    glVertexPointer(3, GL_FLOAT, 0, buf_vertices);
244#endif
245
246    pbuf_colors = buf_colors;
247    pbuf_vertices = buf_vertices;
248}
249
250void VMI::setupVertexBufferObjects(Mesh *mesh, Color *colors) {
251   
252    setupVertexArray(mesh, colors);
253   
254    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf);
255    glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->numTriangles * 3 * 3 * sizeof(float), buf_vertices, GL_STATIC_DRAW_ARB); //upload data
256
257    glVertexPointer(3, GL_FLOAT, 0,0);
258   
259    glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf);
260    glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->numTriangles * 3 * sizeof(Color), buf_colors, GL_STATIC_DRAW_ARB); //upload data
261   
262    glColorPointer(4, GL_UNSIGNED_BYTE, 0, 0);
263
264    // Enable the vertex array functionality:
265    glEnableClientState(GL_VERTEX_ARRAY);
266    glEnableClientState(GL_COLOR_ARRAY);
267   
268    //free(buf_vertices);
269    //buf_vertices = NULL;
270   
271    //free(buf_colors);
272    //buf_colors = NULL;
273}
274
275void VMI::updateVertexArray(Mesh *mesh, Change *c)
276{
277    int i, v1, v2, v3, t, n;
278
279    // The deleted triangles are now in background color
280    for (i=0; i<c->numDel; i++) {
281        t = c->deleted[i].id;
282
283        n = t * 3;
284
285        buf_colors[n + 0].r = 0;
286        buf_colors[n + 0].g = 0;
287        buf_colors[n + 0].b = 0;
288        buf_colors[n + 0].a = 0;
289
290        buf_colors[n + 1].r = 0;
291        buf_colors[n + 1].g = 0;
292        buf_colors[n + 1].b = 0;
293        buf_colors[n + 1].a = 0;
294
295        buf_colors[n + 2].r = 0;
296        buf_colors[n + 2].g = 0;
297        buf_colors[n + 2].b = 0;
298        buf_colors[n + 2].a = 0;
299
300        buf_vertices[n + 0].x = 0;
301        buf_vertices[n + 0].y = 0;
302        buf_vertices[n + 0].z = 0;
303
304        buf_vertices[n + 1].x = 0;
305        buf_vertices[n + 1].y = 0;
306        buf_vertices[n + 1].z = 0;
307
308        buf_vertices[n + 2].x = 0;
309        buf_vertices[n + 2].y = 0;
310        buf_vertices[n + 2].z = 0;
311    }
312
313    // Update vertex coordinates for modified triangles
314    for (i=0; i<c->numMod; i++) {
315        t = c->modified[i].id;
316
317        n = t * 3;
318
319        v1= mesh->triangles[t].indices[0];
320        v2= mesh->triangles[t].indices[1];
321        v3= mesh->triangles[t].indices[2];
322
323        buf_vertices[n + 0].x = mesh->vertices[v1].x;
324        buf_vertices[n + 0].y = mesh->vertices[v1].y;
325        buf_vertices[n + 0].z = mesh->vertices[v1].z;
326
327        buf_vertices[n + 1].x = mesh->vertices[v2].x;
328        buf_vertices[n + 1].y = mesh->vertices[v2].y;
329        buf_vertices[n + 1].z = mesh->vertices[v2].z;
330
331        buf_vertices[n + 2].x = mesh->vertices[v3].x;
332        buf_vertices[n + 2].y = mesh->vertices[v3].y;
333        buf_vertices[n + 2].z = mesh->vertices[v3].z;
334    }
335}
336
337void VMI::updateVertexBufferObjects(Mesh *mesh, Change *c) {
338   
339    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf);
340    // Map the buffer object
341        buf_vertices = ( Vertex_ *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY);
342
343    glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf);
344    // Map the buffer object
345        buf_colors = (Color   *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY);
346
347    updateVertexArray(mesh, c);
348
349    // Unmap buffers
350    glBindBuffer(GL_ARRAY_BUFFER_ARB, vertex_buf);
351    if (!glUnmapBufferARB(GL_ARRAY_BUFFER_ARB)) {
352        // Handle error case
353    }
354    glBindBuffer(GL_ARRAY_BUFFER_ARB, color_buf);
355    if (!glUnmapBufferARB(GL_ARRAY_BUFFER_ARB)) {
356        // Handle error case
357    }
358}
359
360VertexIL *VMI::setupInterleave(Mesh *mesh, Color *colors)
361{
362    int i, v1, v2, v3, n = 0;
363    GLubyte r, g, b, a;
364    VertexIL *interleave = NULL;
365   
366    interleave = (VertexIL *)malloc(mesh->currentNumTriangles * 3 * sizeof(VertexIL));
367   
368    if (interleave == NULL) {
369        fprintf(stderr, "Error allocating memory\n");
370        exit(1);
371    }
372   
373    for (i=0; i<mesh->numTriangles; i++) {
374       
375        if (mesh->triangles[i].enable == TRUE) {
376            v1= mesh->triangles[i].indices[0];
377            v2= mesh->triangles[i].indices[1];
378            v3= mesh->triangles[i].indices[2];
379           
380            r = colors[i].r;
381            g = colors[i].g;
382            b = colors[i].b;
383            a = colors[i].a;
384           
385            interleave[n].tx = 0.0f;
386            interleave[n].ty = 0.0f;
387            interleave[n].r = r;
388            interleave[n].g = g;
389            interleave[n].b = b;
390            interleave[n].a = a;
391            interleave[n].x = mesh->vertices[v1].x;
392            interleave[n].y = mesh->vertices[v1].y;
393            interleave[n].z = mesh->vertices[v1].z;
394            n++;
395           
396            interleave[n].tx = 0.0f;
397            interleave[n].ty = 0.0f;
398            interleave[n].r = r;
399            interleave[n].g = g;
400            interleave[n].b = b;
401            interleave[n].a = a;
402            interleave[n].x = mesh->vertices[v2].x;
403            interleave[n].y = mesh->vertices[v2].y;
404            interleave[n].z = mesh->vertices[v2].z;
405            n++;
406           
407            interleave[n].tx = 0.0f;
408            interleave[n].ty = 0.0f;
409            interleave[n].r = r;
410            interleave[n].g = g;
411            interleave[n].b = b;
412            interleave[n].a = a;
413            interleave[n].x = mesh->vertices[v3].x;
414            interleave[n].y = mesh->vertices[v3].y;
415            interleave[n].z = mesh->vertices[v3].z;
416            n++;
417        }
418    }
419
420    return interleave;
421}
422
423void VMI::printInterleave(VertexIL *interleave, int num)
424{
425    int i;
426
427    printf("\n");
428    for (i=0; i<num * 3; i++) {
429        printf("Interleave%d (%d, %d, %d ,%d, %f, %f ,%f)\n", i,
430            interleave[i].r, interleave[i].g, interleave[i].b, interleave[i].a,
431            interleave[i].x , interleave[i].y, interleave[i].z);
432    }
433}
434
435void VMI::updateAllColorInterleave(VertexIL *interleave, int num, Color *colors)
436{
437    int i, n = 0;
438    GLubyte r, g, b, a;
439
440    for (i=1; i<=num; i++) { // Triangles start at 1
441
442        r = colors[i].r;
443        g = colors[i].g;
444        b = colors[i].b;
445        a = colors[i].a;
446
447        interleave[n].r = r;
448        interleave[n].g = g;
449        interleave[n].b = b;
450        interleave[n].a = a;
451        n++;
452
453        interleave[n].r = r;
454        interleave[n].g = g;
455        interleave[n].b = b;
456        interleave[n].a = a;
457        n++;
458
459        interleave[n].r = r;
460        interleave[n].g = g;
461        interleave[n].b = b;
462        interleave[n].a = a;
463        n++;
464    }
465}
466
467void VMI::updateColorInterleave(VertexIL *interleave, int num, Color *colors, int begin, int end)
468{
469    int i, n;
470    GLubyte r, g, b, a;
471
472    if (end > num) return;
473
474    for (i=begin; i<end; i++) {
475       
476        n = i * 3;
477
478        r = colors[i].r;
479        g = colors[i].g;
480        b = colors[i].b;
481        a = colors[i].a;
482
483        interleave[n].r = r;
484        interleave[n].g = g;
485        interleave[n].b = b;
486        interleave[n].a = a;
487
488        n++;
489        interleave[n].r = r;
490        interleave[n].g = g;
491        interleave[n].b = b;
492        interleave[n].a = a;
493
494        n++;
495        interleave[n].r = r;
496        interleave[n].g = g;
497        interleave[n].b = b;
498        interleave[n].a = a;
499    }
500}
501
502void VMI::fillColorInterleave(VertexIL *interleave, int num, int begin, int end, GLubyte color)
503{
504    int i, n;
505
506    if (end > num) return;
507
508    for (i=begin; i<end; i++) {
509       
510        n = i * 3;
511        interleave[n].r = color;
512        interleave[n].g = color;
513        interleave[n].b = color;
514        interleave[n].a = color;
515
516        n++;
517        interleave[n].r = color;
518        interleave[n].g = color;
519        interleave[n].b = color;
520        interleave[n].a = color;
521
522        n++;
523        interleave[n].r = color;
524        interleave[n].g = color;
525        interleave[n].b = color;
526        interleave[n].a = color;
527    }
528}
Note: See TracBrowser for help on using the repository browser.