4d7425182bcd080250745f0d5188db6060bda0fc
[vrtris] / src / cmesh.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <float.h>
4 #include <assert.h>
5 #include "opengl.h"
6 #include "cmesh.h"
7
8
9 struct cmesh_vattrib {
10         int nelem;      /* num elements per attribute [1, 4] */
11         float *data;    /* dynarr */
12         unsigned int vbo;
13         int vbo_valid, data_valid;
14 };
15
16
17 struct cmesh {
18         char *name;
19         unsigned int nverts, nfaces;
20
21         /* current value for each attribute for the immediate mode interface */
22         cgm_vec4 cur_val[CMESH_NUM_ATTR];
23
24         unsigned int buffer_objects[CMESH_NUM_ATTR + 1];
25         struct cmesh_vattrib vattr[CMESH_NUM_ATTR];
26
27         unsigned int *idata;    /* dynarr */
28         unsigned int ibo;
29         int ibo_valid, idata_valid;
30
31         /* index buffer for wireframe rendering (constructed on demand) */
32         unsigned int wire_ibo;
33         int wire_ibo_valid;
34
35         /* axis-aligned bounding box */
36         cgm_vec3 aabb_min, aabb_max;
37         int aabb_valid;
38         /* bounding sphere */
39         cgm_vec3 bsph_center;
40         float bsph_radius;
41         int bsph_valid;
42 };
43
44
45 static int pre_draw(struct cmesh *cm);
46 static void post_draw(struct cmesh *cm, int cur_sdr);
47 static void update_buffers(struct cmesh *cm);
48 static void update_wire_ibo(struct cmesh *cm);
49 static void calc_aabb(struct cmesh *cm);
50 static void calc_bsph(struct cmesh *cm);
51
52
53 static int sdr_loc[CMESH_NUM_ATTR] = {0, 1, 2, 3, 4, 5, 6, 7};
54 static int use_custom_sdr_attr;
55
56
57 /* global state */
58 void cmesh_set_attrib_sdrloc(int attr, int loc)
59 {
60         sdr_loc[attr] = loc;
61 }
62
63 int cmesh_get_attrib_sdrloc(int attr)
64 {
65         return sdr_loc[attr];
66 }
67
68 void cmesh_clear_attrib_sdrloc(void)
69 {
70         int i;
71         for(i=0; i<CMESH_NUM_ATTR; i++) {
72                 sdr_loc[i] = -1;
73         }
74 }
75
76 /* mesh functions */
77 struct cmesh *cmesh_alloc(void)
78 {
79         struct cmesh *cm;
80
81         if(!(cm = malloc(sizeof *cm))) {
82                 return 0;
83         }
84         if(cmesh_init(cm) == -1) {
85                 free(cm);
86                 return 0;
87         }
88         return cm;
89 }
90
91 void cmesh_free(struct cmesh *cm)
92 {
93         cmesh_destroy(cm);
94         free(cm);
95 }
96
97 int cmesh_init(struct cmesh *cm)
98 {
99         int i;
100
101         memset(cm, 0, sizeof *cm);
102         cgm_wcons(cm->cur_val + CMESH_ATTR_COLOR, 1, 1, 1, 1);
103
104         glGenBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
105
106         for(i=0; i<CMESH_NUM_ATTR; i++) {
107                 if(!(cm->vattr[i].data = dynarr_alloc(0, sizeof(float)))) {
108                         cmesh_destroy(cm);
109                         return -1;
110                 }
111                 cm->vattr[i].vbo = cm->buffer_objects[i];
112         }
113
114         cm->ibo = cm->buffer_objects[CMESH_NUM_ATTR];
115         if(!(cm->idata = dynarr_alloc(0, sizeof *cm->idata))) {
116                 cmesh_destroy(cm);
117                 return -1;
118         }
119         return 0;
120 }
121
122 void cmesh_destroy(struct cmesh *cm)
123 {
124         int i;
125
126         free(cm->name);
127
128         for(i=0; i<CMESH_NUM_ATTR; i++) {
129                 dynarr_free(cm->vattr[i].data);
130         }
131         dynarr_free(cm->idata);
132
133         glDeleteBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
134         if(cm->wire_ibo) {
135                 glDeleteBuffers(1, &cm->wire_ibo);
136         }
137 }
138
139 void cmesh_clear(struct cmesh *cm)
140 {
141         int i;
142
143         for(i=0; i<CMESH_NUM_ATTR; i++) {
144                 cm->vattr[i].nelem = 0;
145                 cm->vattr[i].vbo_valid = 0;
146                 cm->vattr[i].data_valid = 0;
147                 cm->vattr[i].data = dynarr_clear(cm->vattr[i].data);
148         }
149         cm->ibo_valid = cm->idata_valid = 0;
150         cm->idata = dynarr_clear(cm->idata);
151
152         cm->wire_ibo_valid = 0;
153         cm->nverts = cm->nfaces = 0;
154
155         cm->bsph_valid = cm->aabb_valid = 0;
156 }
157
158 int cmesh_clone(struct cmesh *cmdest, struct cmesh *cmsrc)
159 {
160         int i, num, nelem;
161         char *name = 0;
162         float *varr[CMESH_NUM_ATTR] = {0};
163         unsigned int *iarr = 0;
164
165         /* do anything that can fail first, before making any changes to cmdest
166          * so we have the option of recovering gracefuly
167          */
168         if(cmsrc->name) {
169                 if(!(name = malloc(strlen(cmsrc->name)))) {
170                         return -1;
171                 }
172                 strcpy(name, cmsrc->name);
173         }
174         if(cmesh_indexed(cmsrc)) {
175                 num = dynarr_size(cmsrc->idata);
176                 if(!(iarr = dynarr_alloc(num, sizeof *iarr))) {
177                         free(name);
178                         return -1;
179                 }
180         }
181         for(i=0; i<CMESH_NUM_ATTR; i++) {
182                 if(cmesh_has_attrib(cmsrc, i)) {
183                         nelem = cmsrc->vattr[i].nelem;
184                         num = dynarr_size(cmsrc->vattr[i].data);
185                         if(!(varr[i] = dynarr_alloc(num * nelem, sizeof(float)))) {
186                                 while(--i >= 0) {
187                                         dynarr_free(varr[i]);
188                                 }
189                                 dynarr_free(iarr);
190                                 free(name);
191                                 return -1;
192                         }
193                 }
194         }
195
196         cmesh_clear(cmdest);
197
198         for(i=0; i<CMESH_NUM_ATTR; i++) {
199                 dynarr_free(cmdest->vattr[i].data);
200
201                 if(cmesh_has_attrib(cmsrc, i)) {
202                         cmesh_attrib(cmsrc, i); /* force validation of the actual data on the source mesh */
203
204                         nelem = cmsrc->vattr[i].nelem;
205                         cmdest->vattr[i].nelem = nelem;
206                         num = dynarr_size(cmsrc->vattr[i].data);
207                         cmdest->vattr[i].data = varr[i];
208                         memcpy(cmdest->vattr[i].data, cmsrc->vattr[i].data, num * nelem * sizeof(float));
209                         cmdest->vattr[i].data_valid = 1;
210                         cmdest->vattr[i].vbo_valid = 0;
211                 } else {
212                         memset(cmdest->vattr + i, 0, sizeof cmdest->vattr[i]);
213                 }
214         }
215
216         dynarr_free(cmdest->idata);
217         if(cmesh_indexed(cmsrc)) {
218                 cmesh_index(cmsrc);     /* force validation .... */
219
220                 num = dynarr_size(cmsrc->idata);
221                 cmdest->idata = iarr;
222                 memcpy(cmdest->idata, cmsrc->idata, num * sizeof *cmdest->idata);
223                 cmdest->idata_valid = 1;
224         } else {
225                 cmdest->idata = 0;
226                 cmdest->idata_valid = cmdest->ibo_valid = 0;
227         }
228
229         free(cmdest->name);
230         cmdest->name = name;
231
232         cmdest->nverts = cmsrc->nverts;
233         cmdest->nfaces = cmsrc->nfaces;
234
235         memcpy(cmdest->cur_val, cmsrc->cur_val, sizeof cmdest->cur_val);
236
237         cmdest->aabb_min = cmsrc->aabb_min;
238         cmdest->aabb_max = cmsrc->aabb_max;
239         cmdest->aabb_valid = cmsrc->aabb_valid;
240         cmdest->bsph_center = cmsrc->bsph_center;
241         cmdest->bsph_radius = cmsrc->bsph_radius;
242         cmdest->bsph_valid = cmsrc->bsph_valid;
243
244         return 0;
245 }
246
247 int cmesh_set_name(struct cmesh *cm, const char *name)
248 {
249         int len = strlen(name);
250         char *tmp = malloc(len + 1);
251         if(!tmp) return -1;
252         free(cm->name);
253         cm->name = tmp;
254         memcpy(cm->name, name, len + 1);
255         return 0;
256 }
257
258 const char *cmesh_name(struct cmesh *cm)
259 {
260         return cm->name;
261 }
262
263 int cmesh_has_attrib(struct cmesh *cm, int attr)
264 {
265         if(attr < 0 || attr >= CMESH_NUM_ATTR) {
266                 return 0;
267         }
268         return cm->vattr[attr].vbo_valid | cm->vattr[attr].data_valid;
269 }
270
271 int cmesh_indexed(struct cmesh *cm)
272 {
273         return cm->ibo_valid | cm->idata_valid;
274 }
275
276 /* vdata can be 0, in which case only memory is allocated
277  * returns pointer to the attribute array
278  */
279 float *cmesh_set_attrib(struct cmesh *cm, int attr, int nelem, unsigned int num,
280                 const float *vdata)
281 {
282         float *newarr;
283
284         if(attr < 0 || attr >= CMESH_NUM_ATTR) {
285                 return 0;
286         }
287         if(cm->nverts && num != cm->nverts) {
288                 return 0;
289         }
290
291         if(!(newarr = dynarr_alloc(num * nelem, sizeof *newarr))) {
292                 return 0;
293         }
294         if(vdata) {
295                 memcpy(newarr, vdata, num * nelem * sizeof *newarr);
296         }
297
298         cm->nverts = num;
299
300         dynarr_free(cm->vattr[attr].data);
301         cm->vattr[attr].data = newarr;
302         cm->vattr[attr].nelem = nelem;
303         cm->vattr[attr].data_valid = 1;
304         cm->vattr[attr].vbo_valid = 0;
305         return newarr;
306 }
307
308 float *cmesh_attrib(struct cmesh *cm, int attr)
309 {
310         if(attr < 0 || attr >= CMESH_NUM_ATTR) {
311                 return 0;
312         }
313         cm->vattr[attr].vbo_valid = 0;
314         return (float*)cmesh_attrib_ro(cm, attr);
315 }
316
317 const float *cmesh_attrib_ro(struct cmesh *cm, int attr)
318 {
319         float *tmp;
320         int nelem;
321
322         if(attr < 0 || attr >= CMESH_NUM_ATTR) {
323                 return 0;
324         }
325
326         if(!cm->vattr[attr].data_valid) {
327 #if GL_ES_VERSION_2_0
328                 return 0;
329 #else
330                 if(!cm->vattr[attr].vbo_valid) {
331                         return 0;
332                 }
333
334                 /* local data copy unavailable, grab the data from the vbo */
335                 nelem = cm->vattr[attr].nelem;
336                 if(!(tmp = dynarr_resize(cm->vattr[attr].data, cm->nverts * nelem))) {
337                         return 0;
338                 }
339                 cm->vattr[attr].data = tmp;
340
341                 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[attr].vbo);
342                 tmp = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
343                 memcpy(cm->vattr[attr].data, tmp, cm->nverts * nelem * sizeof(float));
344                 glUnmapBuffer(GL_ARRAY_BUFFER);
345
346                 cm->vattr[attr].data_valid = 1;
347 #endif
348         }
349         return cm->vattr[attr].data;
350 }
351
352 float *cmesh_attrib_at(struct cmesh *cm, int attr, int idx)
353 {
354         float *vptr = cmesh_attrib(cm, attr);
355         return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
356 }
357
358 const float *cmesh_attrib_at_ro(struct cmesh *cm, int attr, int idx)
359 {
360         const float *vptr = cmesh_attrib_ro(cm, attr);
361         return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
362 }
363
364 int cmesh_attrib_count(struct cmesh *cm, int attr)
365 {
366         return cmesh_has_attrib(cm, attr) ? cm->nverts : 0;
367 }
368
369 /* indices can be 0, in which case only memory is allocated
370  * returns pointer to the index array
371  */
372 unsigned int *cmesh_set_index(struct cmesh *cm, int num, const unsigned int *indices)
373 {
374         unsigned int *tmp;
375         int nidx = cm->nfaces * 3;
376
377         if(nidx && num != nidx) {
378                 return 0;
379         }
380
381         if(!(tmp = dynarr_alloc(num, sizeof *tmp))) {
382                 return 0;
383         }
384         if(indices) {
385                 memcpy(tmp, indices, num * sizeof *tmp);
386         }
387
388         dynarr_free(cm->idata);
389         cm->idata = tmp;
390         cm->idata_valid = 1;
391         cm->ibo_valid = 0;
392         return tmp;
393 }
394
395 unsigned int *cmesh_index(struct cmesh *cm)
396 {
397         cm->ibo_valid = 0;
398         return (unsigned int*)cmesh_index_ro(cm);
399 }
400
401 const unsigned int *cmesh_index_ro(struct cmesh *cm)
402 {
403         int nidx;
404         unsigned int *tmp;
405
406         if(!cm->idata_valid) {
407 #if GL_ES_VERSION_2_0
408                 return 0;
409 #else
410                 if(!cm->ibo_valid) {
411                         return 0;
412                 }
413
414                 /* local copy is unavailable, grab the data from the ibo */
415                 nidx = cm->nfaces * 3;
416                 if(!(tmp = dynarr_alloc(nidx, sizeof *cm->idata))) {
417                         return 0;
418                 }
419                 dynarr_free(cm->idata);
420                 cm->idata = tmp;
421
422                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
423                 tmp = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
424                 memcpy(cm->idata, tmp, nidx * sizeof *cm->idata);
425                 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
426
427                 cm->idata_valid = 1;
428 #endif
429         }
430         return cm->idata;
431 }
432
433 int cmesh_index_count(struct cmesh *cm)
434 {
435         return cm->nfaces * 3;
436 }
437
438 int cmesh_poly_count(struct cmesh *cm)
439 {
440         if(cm->nfaces) {
441                 return cm->nfaces;
442         }
443         if(cm->nverts) {
444                 return cm->nverts / 3;
445         }
446         return 0;
447 }
448
449 /* attr can be -1 to invalidate all attributes */
450 void cmesh_invalidate_vbo(struct cmesh *cm, int attr)
451 {
452         int i;
453
454         if(attr >= CMESH_NUM_ATTR) {
455                 return;
456         }
457
458         if(attr < 0) {
459                 for(i=0; i<CMESH_NUM_ATTR; i++) {
460                         cm->vattr[i].vbo_valid = 0;
461                 }
462         } else {
463                 cm->vattr[attr].vbo_valid = 0;
464         }
465 }
466
467 void cmesh_invalidate_index(struct cmesh *cm)
468 {
469         cm->ibo_valid = 0;
470 }
471
472 int cmesh_append(struct cmesh *cmdest, struct cmesh *cmsrc)
473 {
474         int i, nelem, newsz, origsz, srcsz;
475         float *vptr;
476         unsigned int *iptr;
477         unsigned int idxoffs;
478
479         if(!cmdest->nverts) {
480                 return cmesh_clone(cmdest, cmsrc);
481         }
482
483         for(i=0; i<CMESH_NUM_ATTR; i++) {
484                 if(cmesh_has_attrib(cmdest, i) && cmesh_has_attrib(cmsrc, i)) {
485                         /* force validation of the data arrays */
486                         cmesh_attrib(cmdest, i);
487                         cmesh_attrib_ro(cmsrc, i);
488
489                         assert(cmdest->vattr[i].nelem == cmsrc->vattr[i].nelem);
490                         nelem = cmdest->vattr[i].nelem;
491                         origsz = cmdest->nverts * nelem;
492                         newsz = cmdest->nverts + cmsrc->nverts * nelem;
493
494                         if(!(vptr = dynarr_resize(cmdest->vattr[i].data, newsz))) {
495                                 return -1;
496                         }
497                         memcpy(vptr + origsz, cmsrc->vattr[i].data, cmsrc->nverts * nelem * sizeof(float));
498                         cmdest->vattr[i].data = vptr;
499                 }
500         }
501
502         if(cmesh_indexed(cmdest)) {
503                 assert(cmesh_indexed(cmsrc));
504                 /* force validation ... */
505                 cmesh_index(cmdest);
506                 cmesh_index_ro(cmsrc);
507
508                 idxoffs = cmdest->nverts;
509                 origsz = dynarr_size(cmdest->idata);
510                 srcsz = dynarr_size(cmsrc->idata);
511                 newsz = origsz + srcsz;
512
513                 if(!(iptr = dynarr_resize(cmdest->idata, newsz))) {
514                         return -1;
515                 }
516                 cmdest->idata = iptr;
517
518                 /* copy and fixup all the new indices */
519                 iptr += origsz;
520                 for(i=0; i<srcsz; i++) {
521                         *iptr++ = cmsrc->idata[i] + idxoffs;
522                 }
523         }
524
525         cmdest->wire_ibo_valid = 0;
526         cmdest->aabb_valid = 0;
527         cmdest->bsph_valid = 0;
528         return 0;
529 }
530
531 /* assemble a complete vertex by adding all the useful attributes */
532 int cmesh_vertex(struct cmesh *cm, float x, float y, float z)
533 {
534         int i, j;
535
536         cgm_wcons(cm->cur_val + CMESH_ATTR_VERTEX, x, y, z, 1.0f);
537         cm->vattr[CMESH_ATTR_VERTEX].data_valid = 1;
538         cm->vattr[CMESH_ATTR_VERTEX].nelem = 3;
539
540         for(i=0; i<CMESH_ATTR_VERTEX; i++) {
541                 if(cm->vattr[i].data_valid) {
542                         for(j=0; j<cm->vattr[CMESH_ATTR_VERTEX].nelem; j++) {
543                                 float *tmp = dynarr_push(cm->vattr[i].data, &cm->cur_val[i].x + j);
544                                 if(!tmp) return -1;
545                                 cm->vattr[i].data = tmp;
546                         }
547                 }
548                 cm->vattr[i].vbo_valid = 0;
549                 cm->vattr[i].data_valid = 1;
550         }
551
552         if(cm->idata_valid) {
553                 cm->idata = dynarr_clear(cm->idata);
554         }
555         cm->ibo_valid = cm->idata_valid = 0;
556         return 0;
557 }
558
559 void cmesh_normal(struct cmesh *cm, float nx, float ny, float nz)
560 {
561         cgm_wcons(cm->cur_val + CMESH_ATTR_NORMAL, nx, ny, nz, 1.0f);
562         cm->vattr[CMESH_ATTR_NORMAL].nelem = 3;
563 }
564
565 void cmesh_tangent(struct cmesh *cm, float tx, float ty, float tz)
566 {
567         cgm_wcons(cm->cur_val + CMESH_ATTR_TANGENT, tx, ty, tz, 1.0f);
568         cm->vattr[CMESH_ATTR_TANGENT].nelem = 3;
569 }
570
571 void cmesh_texcoord(struct cmesh *cm, float u, float v, float w)
572 {
573         cgm_wcons(cm->cur_val + CMESH_ATTR_TEXCOORD, u, v, w, 1.0f);
574         cm->vattr[CMESH_ATTR_TEXCOORD].nelem = 3;
575 }
576
577 void cmesh_boneweights(struct cmesh *cm, float w1, float w2, float w3, float w4)
578 {
579         cgm_wcons(cm->cur_val + CMESH_ATTR_BONEWEIGHTS, w1, w2, w3, w4);
580         cm->vattr[CMESH_ATTR_BONEWEIGHTS].nelem = 4;
581 }
582
583 void cmesh_boneidx(struct cmesh *cm, int idx1, int idx2, int idx3, int idx4)
584 {
585         cgm_wcons(cm->cur_val + CMESH_ATTR_BONEIDX, idx1, idx2, idx3, idx4);
586         cm->vattr[CMESH_ATTR_BONEIDX].nelem = 4;
587 }
588
589 static float *get_vec4(struct cmesh *cm, int attr, int idx, cgm_vec4 *res)
590 {
591         int i;
592         float *sptr, *dptr;
593         cgm_wcons(res, 0, 0, 0, 1);
594         if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
595                 return 0;
596         }
597         dptr = &res->x;
598
599         for(i=0; i<cm->vattr[attr].nelem; i++) {
600                 *dptr++ = sptr[i];
601         }
602         return sptr;
603 }
604
605 static float *get_vec3(struct cmesh *cm, int attr, int idx, cgm_vec3 *res)
606 {
607         int i;
608         float *sptr, *dptr;
609         cgm_vcons(res, 0, 0, 0);
610         if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
611                 return 0;
612         }
613         dptr = &res->x;
614
615         for(i=0; i<cm->vattr[attr].nelem; i++) {
616                 *dptr++ = sptr[i];
617         }
618         return sptr;
619 }
620
621 /* dir_xform can be null, in which case it's calculated from xform */
622 void cmesh_apply_xform(struct cmesh *cm, float *xform, float *dir_xform)
623 {
624         unsigned int i;
625         int j;
626         cgm_vec4 v;
627         cgm_vec3 n, t;
628         float *vptr;
629
630         for(i=0; i<cm->nverts; i++) {
631                 if(!(vptr = get_vec4(cm, CMESH_ATTR_VERTEX, i, &v))) {
632                         return;
633                 }
634                 cgm_wmul_m4v4(&v, xform);
635                 for(j=0; j<cm->vattr[CMESH_ATTR_VERTEX].nelem; j++) {
636                         *vptr++ = (&v.x)[j];
637                 }
638
639                 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
640                         if((vptr = get_vec3(cm, CMESH_ATTR_NORMAL, i, &n))) {
641                                 cgm_vmul_m3v3(&n, dir_xform);
642                                 for(j=0; j<cm->vattr[CMESH_ATTR_NORMAL].nelem; j++) {
643                                         *vptr++ = (&n.x)[j];
644                                 }
645                         }
646                 }
647                 if(cmesh_has_attrib(cm, CMESH_ATTR_TANGENT)) {
648                         if((vptr = get_vec3(cm, CMESH_ATTR_TANGENT, i, &t))) {
649                                 cgm_vmul_m3v3(&t, dir_xform);
650                                 for(j=0; j<cm->vattr[CMESH_ATTR_TANGENT].nelem; j++) {
651                                         *vptr++ = (&t.x)[j];
652                                 }
653                         }
654                 }
655         }
656 }
657
658 void cmesh_flip(struct cmesh *cm)
659 {
660         cmesh_flip_faces(cm);
661         cmesh_flip_normals(cm);
662 }
663
664 void cmesh_flip_faces(struct cmesh *cm)
665 {
666         int i, j, idxnum, vnum, nelem;
667         unsigned int *indices;
668         float *verts, *vptr;
669
670         if(cmesh_indexed(cm)) {
671                 if(!(indices = cmesh_index(cm))) {
672                         return;
673                 }
674                 idxnum = cmesh_index_count(cm);
675                 for(i=0; i<idxnum; i+=3) {
676                         unsigned int tmp = indices[i + 2];
677                         indices[i + 2] = indices[i + 1];
678                         indices[i + 1] = tmp;
679                 }
680         } else {
681                 if(!(verts = cmesh_attrib(cm, CMESH_ATTR_VERTEX))) {
682                         return;
683                 }
684                 vnum = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
685                 nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
686                 for(i=0; i<vnum; i+=3) {
687                         for(j=0; j<nelem; j++) {
688                                 vptr = verts + (i + 1) * nelem + j;
689                                 float tmp = vptr[nelem];
690                                 vptr[nelem] = vptr[0];
691                                 vptr[0] = tmp;
692                         }
693                 }
694         }
695 }
696 void cmesh_flip_normals(struct cmesh *cm)
697 {
698         int i, num;
699         float *nptr = cmesh_attrib(cm, CMESH_ATTR_NORMAL);
700         if(!nptr) return;
701
702         num = cm->nverts * cm->vattr[CMESH_ATTR_NORMAL].nelem;
703         for(i=0; i<num; i++) {
704                 *nptr = -*nptr;
705                 nptr++;
706         }
707 }
708
709 int cmesh_explode(struct cmesh *cm)
710 {
711         int i, j, k, idxnum, nnverts;
712         unsigned int *indices;
713
714         if(!cmesh_indexed(cm)) return 0;
715
716         indices = cmesh_index(cm);
717         assert(indices);
718
719         idxnum = cmesh_index_count(cm);
720         nnverts = idxnum;
721
722         for(i=0; i<CMESH_NUM_ATTR; i++) {
723                 const float *srcbuf;
724                 float *tmpbuf, *dstptr;
725
726                 if(!cmesh_has_attrib(cm, i)) continue;
727
728                 srcbuf = cmesh_attrib(cm, i);
729                 if(!(tmpbuf = dynarr_alloc(nnverts * cm->vattr[i].nelem, sizeof(float)))) {
730                         return -1;
731                 }
732                 dstptr = tmpbuf;
733
734                 for(j=0; j<idxnum; j++) {
735                         unsigned int idx = indices[j];
736                         const float *srcptr = srcbuf + idx * cm->vattr[i].nelem;
737
738                         for(k=0; k<cm->vattr[i].nelem; k++) {
739                                 *dstptr++ = *srcptr++;
740                         }
741                 }
742
743                 dynarr_free(cm->vattr[i].data);
744                 cm->vattr[i].data = tmpbuf;
745                 cm->vattr[i].data_valid = 1;
746         }
747
748         cm->ibo_valid = 0;
749         cm->idata_valid = 0;
750         cm->idata = dynarr_clear(cm->idata);
751
752         cm->nverts = nnverts;
753         cm->nfaces = idxnum / 3;
754         return 0;
755 }
756
757 void cmesh_calc_face_normals(struct cmesh *cm)
758 {
759         /* TODO */
760 }
761
762 static int pre_draw(struct cmesh *cm)
763 {
764         int i, loc, cur_sdr;
765
766         glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
767
768         update_buffers(cm);
769
770         if(!cm->vattr[CMESH_ATTR_VERTEX].vbo_valid) {
771                 return -1;
772         }
773
774         if(cur_sdr && use_custom_sdr_attr) {
775                 if(sdr_loc[CMESH_ATTR_VERTEX] == -1) {
776                         return -1;
777                 }
778
779                 for(i=0; i<CMESH_NUM_ATTR; i++) {
780                         loc = sdr_loc[i];
781                         if(loc >= 0 && cm->vattr[i].vbo_valid) {
782                                 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
783                                 glVertexAttribPointer(loc, cm->vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
784                                 glEnableVertexAttribArray(loc);
785                         }
786                 }
787         } else {
788 #ifndef GL_ES_VERSION_2_0
789                 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_VERTEX].vbo);
790                 glVertexPointer(cm->vattr[CMESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
791                 glEnableClientState(GL_VERTEX_ARRAY);
792
793                 if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
794                         glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_NORMAL].vbo);
795                         glNormalPointer(GL_FLOAT, 0, 0);
796                         glEnableClientState(GL_NORMAL_ARRAY);
797                 }
798                 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
799                         glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD].vbo);
800                         glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
801                         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
802                 }
803                 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
804                         glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_COLOR].vbo);
805                         glColorPointer(cm->vattr[CMESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
806                         glEnableClientState(GL_COLOR_ARRAY);
807                 }
808                 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
809                         glClientActiveTexture(GL_TEXTURE1);
810                         glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD2].vbo);
811                         glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD2].nelem, GL_FLOAT, 0, 0);
812                         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
813                         glClientActiveTexture(GL_TEXTURE0);
814                 }
815 #endif  /* GL_ES_VERSION_2_0 */
816         }
817         glBindBuffer(GL_ARRAY_BUFFER, 0);
818         return cur_sdr;
819 }
820
821 void cmesh_draw(struct cmesh *cm)
822 {
823         int cur_sdr;
824
825         if((cur_sdr = pre_draw(cm)) == -1) {
826                 return;
827         }
828
829         if(cm->ibo_valid) {
830                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
831                 glDrawElements(GL_TRIANGLES, cm->nfaces * 3, GL_UNSIGNED_INT, 0);
832                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
833         } else {
834                 glDrawArrays(GL_TRIANGLES, 0, cm->nverts);
835         }
836
837         post_draw(cm, cur_sdr);
838 }
839
840 static void post_draw(struct cmesh *cm, int cur_sdr)
841 {
842         int i;
843
844         if(cur_sdr && use_custom_sdr_attr) {
845                 for(i=0; i<CMESH_NUM_ATTR; i++) {
846                         int loc = sdr_loc[i];
847                         if(loc >= 0 && cm->vattr[i].vbo_valid) {
848                                 glDisableVertexAttribArray(loc);
849                         }
850                 }
851         } else {
852 #ifndef GL_ES_VERSION_2_0
853                 glDisableClientState(GL_VERTEX_ARRAY);
854                 if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
855                         glDisableClientState(GL_NORMAL_ARRAY);
856                 }
857                 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
858                         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
859                 }
860                 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
861                         glDisableClientState(GL_COLOR_ARRAY);
862                 }
863                 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
864                         glClientActiveTexture(GL_TEXTURE1);
865                         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
866                         glClientActiveTexture(GL_TEXTURE0);
867                 }
868 #endif  /* GL_ES_VERSION_2_0 */
869         }
870 }
871
872 void cmesh_draw_wire(struct cmesh *cm, float linesz)
873 {
874         int cur_sdr, nfaces;
875
876         if((cur_sdr = pre_draw(cm)) == -1) {
877                 return;
878         }
879         update_wire_ibo(cm);
880
881         nfaces = cmesh_poly_count(cm);
882         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
883         glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0);
884         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
885
886         post_draw(cm, cur_sdr);
887 }
888
889 void cmesh_draw_vertices(struct cmesh *cm, float ptsz)
890 {
891         int cur_sdr;
892         if((cur_sdr = pre_draw(cm)) == -1) {
893                 return;
894         }
895
896         glPushAttrib(GL_POINT_BIT);
897         glPointSize(ptsz);
898         glDrawArrays(GL_POINTS, 0, cm->nverts);
899         glPopAttrib();
900
901         post_draw(cm, cur_sdr);
902 }
903
904 void cmesh_draw_normals(struct cmesh *cm, float len)
905 {
906 #ifndef GL_ES_VERSION_2_0
907         int i, cur_sdr, vert_nelem, norm_nelem;
908         int loc = -1;
909         const float *varr, *norm;
910
911         varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
912         norm = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
913         if(!varr || !norm) return;
914
915         vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
916         norm_nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
917
918         glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
919         if(cur_sdr && use_custom_sdr_attr) {
920                 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
921                         return;
922                 }
923         }
924
925         glBegin(GL_LINES);
926         for(i=0; i<cm->nverts; i++) {
927                 float x, y, z, endx, endy, endz;
928
929                 x = varr[i * vert_nelem];
930                 y = varr[i * vert_nelem + 1];
931                 z = varr[i * vert_nelem + 2];
932                 endx = x + norm[i * norm_nelem] * len;
933                 endy = y + norm[i * norm_nelem + 1] * len;
934                 endz = z + norm[i * norm_nelem + 2] * len;
935
936                 if(loc == -1) {
937                         glVertex3f(x, y, z);
938                         glVertex3f(endx, endy, endz);
939                 } else {
940                         glVertexAttrib3f(loc, x, y, z);
941                         glVertexAttrib3f(loc, endx, endy, endz);
942                 }
943         }
944         glEnd();
945 #endif  /* GL_ES_VERSION_2_0 */
946 }
947
948 void cmesh_draw_tangents(struct cmesh *cm, float len)
949 {
950 #ifndef GL_ES_VERSION_2_0
951         int i, cur_sdr, vert_nelem, tang_nelem;
952         int loc = -1;
953         const float *varr, *tang;
954
955         varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
956         tang = cmesh_attrib_ro(cm, CMESH_ATTR_TANGENT);
957         if(!varr || !tang) return;
958
959         vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
960         tang_nelem = cm->vattr[CMESH_ATTR_TANGENT].nelem;
961
962         glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
963         if(cur_sdr && use_custom_sdr_attr) {
964                 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
965                         return;
966                 }
967         }
968
969         glBegin(GL_LINES);
970         for(i=0; i<cm->nverts; i++) {
971                 float x, y, z, endx, endy, endz;
972
973                 x = varr[i * vert_nelem];
974                 y = varr[i * vert_nelem + 1];
975                 z = varr[i * vert_nelem + 2];
976                 endx = x + tang[i * tang_nelem] * len;
977                 endy = y + tang[i * tang_nelem + 1] * len;
978                 endz = z + tang[i * tang_nelem + 2] * len;
979
980                 if(loc == -1) {
981                         glVertex3f(x, y, z);
982                         glVertex3f(endx, endy, endz);
983                 } else {
984                         glVertexAttrib3f(loc, x, y, z);
985                         glVertexAttrib3f(loc, endx, endy, endz);
986                 }
987         }
988         glEnd();
989 #endif  /* GL_ES_VERSION_2_0 */
990 }
991
992 static void update_buffers(struct cmesh *cm)
993 {
994         int i;
995
996         for(i=0; i<CMESH_NUM_ATTR; i++) {
997                 if(cmesh_has_attrib(cm, i) && !cm->vattr[i].vbo_valid) {
998                         glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
999                         glBufferData(GL_ARRAY_BUFFER, cm->nverts * cm->vattr[i].nelem * sizeof(float),
1000                                         cm->vattr[i].data, GL_STATIC_DRAW);
1001                         cm->vattr[i].vbo_valid = 1;
1002                 }
1003         }
1004         glBindBuffer(GL_ARRAY_BUFFER, 0);
1005
1006         if(cm->idata_valid && !cm->ibo_valid) {
1007                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1008                 glBufferData(GL_ELEMENT_ARRAY_BUFFER, cm->nfaces * 3 * sizeof(unsigned int),
1009                                 cm->idata, GL_STATIC_DRAW);
1010                 cm->ibo_valid = 1;
1011                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1012         }
1013 }
1014
1015 static void update_wire_ibo(struct cmesh *cm)
1016 {
1017         int i, num_faces;
1018         unsigned int *wire_idxarr, *dest;
1019
1020         update_buffers(cm);
1021
1022         if(cm->wire_ibo_valid) return;
1023
1024         if(!cm->wire_ibo) {
1025                 glGenBuffers(1, &cm->wire_ibo);
1026         }
1027         num_faces = cmesh_poly_count(cm);
1028
1029         if(!(wire_idxarr = malloc(num_faces * 6 * sizeof *wire_idxarr))) {
1030                 return;
1031         }
1032         dest = wire_idxarr;
1033
1034         if(cm->ibo_valid) {
1035                 /* we're dealing with an indexed mesh */
1036                 const unsigned int *idxarr = cmesh_index_ro(cm);
1037
1038                 for(i=0; i<num_faces; i++) {
1039                         *dest++ = idxarr[0];
1040                         *dest++ = idxarr[1];
1041                         *dest++ = idxarr[1];
1042                         *dest++ = idxarr[2];
1043                         *dest++ = idxarr[2];
1044                         *dest++ = idxarr[0];
1045                         idxarr += 3;
1046                 }
1047         } else {
1048                 /* not an indexed mesh */
1049                 for(i=0; i<num_faces; i++) {
1050                         int vidx = i * 3;
1051                         *dest++ = vidx;
1052                         *dest++ = vidx + 1;
1053                         *dest++ = vidx + 1;
1054                         *dest++ = vidx + 2;
1055                         *dest++ = vidx + 2;
1056                         *dest++ = vidx;
1057                 }
1058         }
1059
1060         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
1061         glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_faces * 6 * sizeof(unsigned int),
1062                         wire_idxarr, GL_STATIC_DRAW);
1063         free(wire_idxarr);
1064         cm->wire_ibo_valid = 1;
1065         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1066 }
1067
1068 static void calc_aabb(struct cmesh *cm)
1069 {
1070         int i, j;
1071
1072         if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1073                 return;
1074         }
1075
1076         cgm_vcons(&cm->aabb_min, FLT_MAX, FLT_MAX, FLT_MAX);
1077         cgm_vcons(&cm->aabb_max, -FLT_MAX, -FLT_MAX, -FLT_MAX);
1078
1079         for(i=0; i<cm->nverts; i++) {
1080                 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1081                 for(j=0; j<3; j++) {
1082                         if(v[j] < (&cm->aabb_min.x)[j]) {
1083                                 (&cm->aabb_min.x)[j] = v[j];
1084                         }
1085                         if(v[j] > (&cm->aabb_max.x)[j]) {
1086                                 (&cm->aabb_max.x)[j] = v[j];
1087                         }
1088                 }
1089         }
1090         cm->aabb_valid = 1;
1091 }
1092
1093 void cmesh_aabbox(struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax)
1094 {
1095         if(!cm->aabb_valid) {
1096                 calc_aabb(cm);
1097         }
1098         *vmin = cm->aabb_min;
1099         *vmax = cm->aabb_max;
1100 }
1101
1102 static void calc_bsph(struct cmesh *cm)
1103 {
1104         int i;
1105         float s, dist_sq;
1106
1107         if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1108                 return;
1109         }
1110
1111         cgm_vcons(&cm->bsph_center, 0, 0, 0);
1112
1113         /* first find the center */
1114         for(i=0; i<cm->nverts; i++) {
1115                 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1116                 cm->bsph_center.x += v[0];
1117                 cm->bsph_center.y += v[1];
1118                 cm->bsph_center.z += v[2];
1119         }
1120         s = 1.0f / (float)cm->nverts;
1121         cm->bsph_center.x *= s;
1122         cm->bsph_center.y *= s;
1123         cm->bsph_center.z *= s;
1124
1125         cm->bsph_radius = 0.0f;
1126         for(i=0; i<cm->nverts; i++) {
1127                 const cgm_vec3 *v = (const cgm_vec3*)cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1128                 if((dist_sq = cgm_vdist_sq(v, &cm->bsph_center)) > cm->bsph_radius) {
1129                         cm->bsph_radius = dist_sq;
1130                 }
1131         }
1132         cm->bsph_radius = sqrt(cm->bsph_radius);
1133         cm->bsph_valid = 1;
1134 }
1135
1136 float cmesh_bsphere(struct cmesh *cm, cgm_vec3 *center, float *rad)
1137 {
1138         if(!cm->bsph_valid) {
1139                 calc_bsph(cm);
1140         }
1141         *center = cm->bsph_center;
1142         *rad = cm->bsph_radius;
1143         return cm->bsph_radius;
1144 }
1145
1146 /* TODO */
1147 void cmesh_texcoord_apply_xform(struct cmesh *cm, float *xform);
1148 void cmesh_texcoord_gen_plane(struct cmesh *cm, cgm_vec3 *norm, cgm_vec3 *tang);
1149 void cmesh_texcoord_gen_box(struct cmesh *cm);
1150 void cmesh_texcoord_gen_cylinder(struct cmesh *cm);
1151
1152 int cmesh_dump(struct cmesh *cm, const char *fname);
1153 int cmesh_dump_file(struct cmesh *cm, FILE *fp);
1154 int cmesh_dump_obj(struct cmesh *cm, const char *fname);
1155 int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs);