shell mesh
[meshfrac] / src / cmesh.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <limits.h>
5 #include <float.h>
6 #include <assert.h>
7 #include "opengl.h"
8 #include "cmesh.h"
9
10
11 struct cmesh_vattrib {
12         int nelem;      /* num elements per attribute [1, 4] */
13         float *data;
14         unsigned int count;
15         unsigned int vbo;
16         int vbo_valid, data_valid;
17 };
18
19 /* istart,icount are valid only when the mesh is indexed, otherwise icount is 0.
20  * vstart,vcount are define the submesh for non-indexed meshes.
21  * For indexed meshes, vstart,vcount denote the range of vertices used by each
22  * submesh.
23  */
24 struct submesh {
25         char *name;
26         int nfaces;     /* derived from either icount or vcount */
27         int istart, icount;
28         int vstart, vcount;
29         struct submesh *next;
30 };
31
32 struct cmesh {
33         char *name;
34         unsigned int nverts, nfaces;
35
36         struct submesh *sublist;
37         int subcount;
38
39         /* current value for each attribute for the immediate mode interface */
40         cgm_vec4 cur_val[CMESH_NUM_ATTR];
41
42         unsigned int buffer_objects[CMESH_NUM_ATTR + 1];
43         struct cmesh_vattrib vattr[CMESH_NUM_ATTR];
44
45         unsigned int *idata;
46         unsigned int icount;
47         unsigned int ibo;
48         int ibo_valid, idata_valid;
49
50         /* index buffer for wireframe rendering (constructed on demand) */
51         unsigned int wire_ibo;
52         int wire_ibo_valid;
53
54         /* axis-aligned bounding box */
55         cgm_vec3 aabb_min, aabb_max;
56         int aabb_valid;
57         /* bounding sphere */
58         cgm_vec3 bsph_center;
59         float bsph_radius;
60         int bsph_valid;
61 };
62
63
64 static int clone(struct cmesh *cmdest, const struct cmesh *cmsrc, struct submesh *sub);
65 static int pre_draw(const struct cmesh *cm);
66 static void post_draw(const struct cmesh *cm, int cur_sdr);
67 static void update_buffers(struct cmesh *cm);
68 static void update_wire_ibo(struct cmesh *cm);
69 static void calc_aabb(struct cmesh *cm);
70 static void calc_bsph(struct cmesh *cm);
71
72 static int def_nelem[CMESH_NUM_ATTR] = {3, 3, 3, 2, 4, 4, 4, 2};
73
74 static int sdr_loc[CMESH_NUM_ATTR] = {0, 1, 2, 3, 4, 5, 6, 7};
75 static int use_custom_sdr_attr;
76
77
78 /* global state */
79 void cmesh_set_attrib_sdrloc(int attr, int loc)
80 {
81         sdr_loc[attr] = loc;
82 }
83
84 int cmesh_get_attrib_sdrloc(int attr)
85 {
86         return sdr_loc[attr];
87 }
88
89 void cmesh_clear_attrib_sdrloc(void)
90 {
91         int i;
92         for(i=0; i<CMESH_NUM_ATTR; i++) {
93                 sdr_loc[i] = -1;
94         }
95 }
96
97 /* mesh functions */
98 struct cmesh *cmesh_alloc(void)
99 {
100         struct cmesh *cm;
101
102         if(!(cm = malloc(sizeof *cm))) {
103                 return 0;
104         }
105         if(cmesh_init(cm) == -1) {
106                 free(cm);
107                 return 0;
108         }
109         return cm;
110 }
111
112 void cmesh_free(struct cmesh *cm)
113 {
114         cmesh_destroy(cm);
115         free(cm);
116 }
117
118 int cmesh_init(struct cmesh *cm)
119 {
120         int i;
121
122         memset(cm, 0, sizeof *cm);
123         cgm_wcons(cm->cur_val + CMESH_ATTR_COLOR, 1, 1, 1, 1);
124
125         glGenBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
126
127         for(i=0; i<CMESH_NUM_ATTR; i++) {
128                 cm->vattr[i].vbo = cm->buffer_objects[i];
129         }
130
131         cm->ibo = cm->buffer_objects[CMESH_NUM_ATTR];
132         return 0;
133 }
134
135 void cmesh_destroy(struct cmesh *cm)
136 {
137         int i;
138
139         free(cm->name);
140
141         for(i=0; i<CMESH_NUM_ATTR; i++) {
142                 free(cm->vattr[i].data);
143         }
144         free(cm->idata);
145
146         cmesh_clear_submeshes(cm);
147
148         glDeleteBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
149         if(cm->wire_ibo) {
150                 glDeleteBuffers(1, &cm->wire_ibo);
151         }
152 }
153
154 void cmesh_clear(struct cmesh *cm)
155 {
156         int i;
157
158         for(i=0; i<CMESH_NUM_ATTR; i++) {
159                 cm->vattr[i].nelem = 0;
160                 cm->vattr[i].vbo_valid = 0;
161                 cm->vattr[i].data_valid = 0;
162                 free(cm->vattr[i].data);
163                 cm->vattr[i].data = 0;
164                 cm->vattr[i].count = 0;
165         }
166         cm->ibo_valid = cm->idata_valid = 0;
167         free(cm->idata);
168         cm->idata = 0;
169         cm->icount = 0;
170
171         cm->wire_ibo_valid = 0;
172         cm->nverts = cm->nfaces = 0;
173
174         cm->bsph_valid = cm->aabb_valid = 0;
175
176         cmesh_clear_submeshes(cm);
177 }
178
179 int cmesh_clone(struct cmesh *cmdest, const struct cmesh *cmsrc)
180 {
181         return clone(cmdest, cmsrc, 0);
182 }
183
184 static int clone(struct cmesh *cmdest, const struct cmesh *cmsrc, struct submesh *sub)
185 {
186         int i, nelem, vstart, vcount, istart, icount;
187         char *srcname, *name = 0;
188         float *varr[CMESH_NUM_ATTR] = {0};
189         float *vptr;
190         unsigned int *iptr, *iarr = 0;
191
192         /* try do anything that can fail first, before making any changes to cmdest
193          * so we have the option of recovering gracefuly
194          */
195
196         srcname = sub ? sub->name : cmsrc->name;
197         if(srcname) {
198                 if(!(name = malloc(strlen(srcname) + 1))) {
199                         return -1;
200                 }
201                 strcpy(name, srcname);
202         }
203
204         if(sub) {
205                 vstart = sub->vstart;
206                 vcount = sub->vcount;
207                 istart = sub->istart;
208                 icount = sub->icount;
209         } else {
210                 vstart = istart = 0;
211                 vcount = cmsrc->nverts;
212                 icount = cmsrc->icount;
213         }
214
215         if(cmesh_indexed(cmsrc)) {
216                 if(!(iarr = malloc(icount * sizeof *iarr))) {
217                         free(name);
218                         return -1;
219                 }
220         }
221
222         for(i=0; i<CMESH_NUM_ATTR; i++) {
223                 if(cmesh_has_attrib(cmsrc, i)) {
224                         nelem = cmsrc->vattr[i].nelem;
225                         if(!(varr[i] = malloc(vcount * nelem * sizeof(float)))) {
226                                 while(--i >= 0) {
227                                         free(varr[i]);
228                                 }
229                                 free(iarr);
230                                 free(name);
231                                 return -1;
232                         }
233                 }
234         }
235
236         /* from this point forward nothing can fail */
237         cmesh_clear(cmdest);
238
239         for(i=0; i<CMESH_NUM_ATTR; i++) {
240                 free(cmdest->vattr[i].data);
241
242                 if(cmesh_has_attrib(cmsrc, i)) {
243                         /* force validation of the actual data on the source mesh */
244                         cmesh_attrib((struct cmesh*)cmsrc, i);
245
246                         nelem = cmsrc->vattr[i].nelem;
247                         cmdest->vattr[i].nelem = nelem;
248                         cmdest->vattr[i].data = varr[i];
249                         cmdest->vattr[i].count = vcount;
250                         vptr = cmsrc->vattr[i].data + vstart * nelem;
251                         memcpy(cmdest->vattr[i].data, vptr, vcount * nelem * sizeof(float));
252                         cmdest->vattr[i].data_valid = 1;
253                         cmdest->vattr[i].vbo_valid = 0;
254                 } else {
255                         memset(cmdest->vattr + i, 0, sizeof cmdest->vattr[i]);
256                 }
257         }
258
259         if(cmesh_indexed(cmsrc)) {
260                 /* force validation .... */
261                 cmesh_index((struct cmesh*)cmsrc);
262
263                 cmdest->idata = iarr;
264                 cmdest->icount = icount;
265                 if(sub) {
266                         /* need to offset all vertex indices by -vstart */
267                         iptr = cmsrc->idata + istart;
268                         for(i=0; i<icount; i++) {
269                                 cmdest->idata[i] = *iptr++ - vstart;
270                         }
271                 } else {
272                         memcpy(cmdest->idata, cmsrc->idata + istart, icount * sizeof *cmdest->idata);
273                 }
274                 cmdest->idata_valid = 1;
275         } else {
276                 cmdest->idata = 0;
277                 cmdest->idata_valid = cmdest->ibo_valid = 0;
278         }
279
280         free(cmdest->name);
281         cmdest->name = name;
282
283         cmdest->nverts = cmsrc->nverts;
284         cmdest->nfaces = sub ? sub->nfaces : cmsrc->nfaces;
285
286         memcpy(cmdest->cur_val, cmsrc->cur_val, sizeof cmdest->cur_val);
287
288         cmdest->aabb_min = cmsrc->aabb_min;
289         cmdest->aabb_max = cmsrc->aabb_max;
290         cmdest->aabb_valid = cmsrc->aabb_valid;
291         cmdest->bsph_center = cmsrc->bsph_center;
292         cmdest->bsph_radius = cmsrc->bsph_radius;
293         cmdest->bsph_valid = cmsrc->bsph_valid;
294
295         /* copy sublist only if we're not cloning a submesh */
296         if(!sub) {
297                 struct submesh *sm, *n, *head = 0, *tail = 0;
298
299                 sm = cmsrc->sublist;
300                 while(sm) {
301                         if(!(n = malloc(sizeof *n)) || !(name = malloc(strlen(sm->name) + 1))) {
302                                 free(n);
303                                 sm = sm->next;
304                                 continue;
305                         }
306                         strcpy(name, sm->name);
307                         *n = *sm;
308                         n->name = name;
309                         n->next = 0;
310
311                         if(head) {
312                                 tail->next = n;
313                                 tail = n;
314                         } else {
315                                 head = tail = n;
316                         }
317
318                         sm = sm->next;
319                 }
320
321                 cmdest->sublist = head;
322                 cmdest->subcount = cmsrc->subcount;
323         }
324
325         return 0;
326 }
327
328 int cmesh_set_name(struct cmesh *cm, const char *name)
329 {
330         int len = strlen(name);
331         char *tmp = malloc(len + 1);
332         if(!tmp) return -1;
333         free(cm->name);
334         cm->name = tmp;
335         memcpy(cm->name, name, len + 1);
336         return 0;
337 }
338
339 const char *cmesh_name(const struct cmesh *cm)
340 {
341         return cm->name;
342 }
343
344 int cmesh_has_attrib(const struct cmesh *cm, int attr)
345 {
346         if(attr < 0 || attr >= CMESH_NUM_ATTR) {
347                 return 0;
348         }
349         return cm->vattr[attr].vbo_valid | cm->vattr[attr].data_valid;
350 }
351
352 int cmesh_indexed(const struct cmesh *cm)
353 {
354         return cm->ibo_valid | cm->idata_valid;
355 }
356
357 /* vdata can be 0, in which case only memory is allocated
358  * returns pointer to the attribute array
359  */
360 float *cmesh_set_attrib(struct cmesh *cm, int attr, int nelem, unsigned int num,
361                 const float *vdata)
362 {
363         float *newarr;
364
365         if(attr < 0 || attr >= CMESH_NUM_ATTR) {
366                 return 0;
367         }
368         if(cm->nverts && num != cm->nverts) {
369                 return 0;
370         }
371
372         if(!(newarr = malloc(num * nelem * sizeof *newarr))) {
373                 return 0;
374         }
375         if(vdata) {
376                 memcpy(newarr, vdata, num * nelem * sizeof *newarr);
377         }
378
379         cm->nverts = num;
380
381         free(cm->vattr[attr].data);
382         cm->vattr[attr].data = newarr;
383         cm->vattr[attr].count = num * nelem;
384         cm->vattr[attr].nelem = nelem;
385         cm->vattr[attr].data_valid = 1;
386         cm->vattr[attr].vbo_valid = 0;
387         return newarr;
388 }
389
390 float *cmesh_attrib(struct cmesh *cm, int attr)
391 {
392         if(attr < 0 || attr >= CMESH_NUM_ATTR) {
393                 return 0;
394         }
395         cm->vattr[attr].vbo_valid = 0;
396         return (float*)cmesh_attrib_ro(cm, attr);
397 }
398
399 const float *cmesh_attrib_ro(const struct cmesh *cm, int attr)
400 {
401         void *tmp;
402         int nelem;
403
404         if(attr < 0 || attr >= CMESH_NUM_ATTR) {
405                 return 0;
406         }
407
408         if(!cm->vattr[attr].data_valid) {
409 #if GL_ES_VERSION_2_0
410                 return 0;
411 #else
412                 struct cmesh *m = (struct cmesh*)cm;
413
414                 if(!m->vattr[attr].vbo_valid) {
415                         return 0;
416                 }
417
418                 /* local data copy unavailable, grab the data from the vbo */
419                 nelem = m->vattr[attr].nelem;
420                 if(!(m->vattr[attr].data = malloc(m->nverts * nelem * sizeof(float)))) {
421                         return 0;
422                 }
423                 m->vattr[attr].count = m->nverts * nelem;
424
425                 glBindBuffer(GL_ARRAY_BUFFER, m->vattr[attr].vbo);
426                 tmp = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
427                 memcpy(m->vattr[attr].data, tmp, m->nverts * nelem * sizeof(float));
428                 glUnmapBuffer(GL_ARRAY_BUFFER);
429
430                 m->vattr[attr].data_valid = 1;
431 #endif
432         }
433         return cm->vattr[attr].data;
434 }
435
436 float *cmesh_attrib_at(struct cmesh *cm, int attr, int idx)
437 {
438         float *vptr = cmesh_attrib(cm, attr);
439         return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
440 }
441
442 const float *cmesh_attrib_at_ro(const struct cmesh *cm, int attr, int idx)
443 {
444         const float *vptr = cmesh_attrib_ro(cm, attr);
445         return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
446 }
447
448 int cmesh_attrib_count(const struct cmesh *cm, int attr)
449 {
450         return cmesh_has_attrib(cm, attr) ? cm->nverts : 0;
451 }
452
453 int cmesh_attrib_nelem(const struct cmesh *cm, int attr)
454 {
455         return cmesh_has_attrib(cm, attr) ? cm->vattr[attr].nelem : 0;
456 }
457
458 int cmesh_push_attrib(struct cmesh *cm, int attr, float *v)
459 {
460         float *vptr;
461         int i, cursz, newsz;
462
463         if(!cm->vattr[attr].nelem) {
464                 cm->vattr[attr].nelem = def_nelem[attr];
465         }
466
467         cursz = cm->vattr[attr].count;
468         newsz = cursz + cm->vattr[attr].nelem;
469         if(!(vptr = realloc(cm->vattr[attr].data, newsz * sizeof(float)))) {
470                 return -1;
471         }
472         cm->vattr[attr].data = vptr;
473         cm->vattr[attr].count = newsz;
474         vptr += cursz;
475
476         for(i=0; i<cm->vattr[attr].nelem; i++) {
477                 *vptr++ = *v++;
478         }
479         cm->vattr[attr].data_valid = 1;
480         cm->vattr[attr].vbo_valid = 0;
481
482         if(attr == CMESH_ATTR_VERTEX) {
483                 cm->nverts = newsz / cm->vattr[attr].nelem;
484         }
485         return 0;
486 }
487
488 int cmesh_push_attrib1f(struct cmesh *cm, int attr, float x)
489 {
490         float v[4];
491         v[0] = x;
492         v[1] = v[2] = 0.0f;
493         v[3] = 1.0f;
494         return cmesh_push_attrib(cm, attr, v);
495 }
496
497 int cmesh_push_attrib2f(struct cmesh *cm, int attr, float x, float y)
498 {
499         float v[4];
500         v[0] = x;
501         v[1] = y;
502         v[2] = 0.0f;
503         v[3] = 1.0f;
504         return cmesh_push_attrib(cm, attr, v);
505 }
506
507 int cmesh_push_attrib3f(struct cmesh *cm, int attr, float x, float y, float z)
508 {
509         float v[4];
510         v[0] = x;
511         v[1] = y;
512         v[2] = z;
513         v[3] = 1.0f;
514         return cmesh_push_attrib(cm, attr, v);
515 }
516
517 int cmesh_push_attrib4f(struct cmesh *cm, int attr, float x, float y, float z, float w)
518 {
519         float v[4];
520         v[0] = x;
521         v[1] = y;
522         v[2] = z;
523         v[3] = w;
524         return cmesh_push_attrib(cm, attr, v);
525 }
526
527 /* indices can be 0, in which case only memory is allocated
528  * returns pointer to the index array
529  */
530 unsigned int *cmesh_set_index(struct cmesh *cm, int num, const unsigned int *indices)
531 {
532         unsigned int *tmp;
533         int nidx = cm->nfaces * 3;
534
535         if(nidx && num != nidx) {
536                 return 0;
537         }
538
539         if(!(tmp = malloc(num * sizeof *tmp))) {
540                 return 0;
541         }
542         if(indices) {
543                 memcpy(tmp, indices, num * sizeof *tmp);
544         }
545
546         free(cm->idata);
547         cm->idata = tmp;
548         cm->icount = num;
549         cm->nfaces = num / 3;
550         cm->idata_valid = 1;
551         cm->ibo_valid = 0;
552         return tmp;
553 }
554
555 unsigned int *cmesh_index(struct cmesh *cm)
556 {
557         cm->ibo_valid = 0;
558         return (unsigned int*)cmesh_index_ro(cm);
559 }
560
561 const unsigned int *cmesh_index_ro(const struct cmesh *cm)
562 {
563         int nidx;
564         unsigned int *tmp;
565
566         if(!cm->idata_valid) {
567 #if GL_ES_VERSION_2_0
568                 return 0;
569 #else
570                 struct cmesh *m = (struct cmesh*)cm;
571
572                 if(!m->ibo_valid) {
573                         return 0;
574                 }
575
576                 /* local copy is unavailable, grab the data from the ibo */
577                 nidx = m->nfaces * 3;
578                 if(!(tmp = malloc(nidx * sizeof *m->idata))) {
579                         return 0;
580                 }
581                 free(m->idata);
582                 m->idata = tmp;
583                 m->icount = nidx;
584
585                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
586                 tmp = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
587                 memcpy(m->idata, tmp, nidx * sizeof *m->idata);
588                 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
589
590                 m->idata_valid = 1;
591 #endif
592         }
593         return cm->idata;
594 }
595
596 int cmesh_index_count(const struct cmesh *cm)
597 {
598         return cm->nfaces * 3;
599 }
600
601 int cmesh_push_index(struct cmesh *cm, unsigned int idx)
602 {
603         unsigned int *iptr;
604         unsigned int cur_sz = cm->icount;
605         if(!(iptr = realloc(cm->idata, (cur_sz + 1) * sizeof *iptr))) {
606                 return -1;
607         }
608         iptr[cur_sz] = idx;
609         cm->idata = iptr;
610         cm->icount = cur_sz + 1;
611         cm->idata_valid = 1;
612         cm->ibo_valid = 0;
613
614         cm->nfaces = cm->icount / 3;
615         return 0;
616 }
617
618 int cmesh_poly_count(const struct cmesh *cm)
619 {
620         if(cm->nfaces) {
621                 return cm->nfaces;
622         }
623         if(cm->nverts) {
624                 return cm->nverts / 3;
625         }
626         return 0;
627 }
628
629 /* attr can be -1 to invalidate all attributes */
630 void cmesh_invalidate_vbo(struct cmesh *cm, int attr)
631 {
632         int i;
633
634         if(attr >= CMESH_NUM_ATTR) {
635                 return;
636         }
637
638         if(attr < 0) {
639                 for(i=0; i<CMESH_NUM_ATTR; i++) {
640                         cm->vattr[i].vbo_valid = 0;
641                 }
642         } else {
643                 cm->vattr[attr].vbo_valid = 0;
644         }
645 }
646
647 void cmesh_invalidate_index(struct cmesh *cm)
648 {
649         cm->ibo_valid = 0;
650 }
651
652 int cmesh_append(struct cmesh *cmdest, const struct cmesh *cmsrc)
653 {
654         int i, nelem, newsz, origsz, srcsz;
655         float *vptr;
656         unsigned int *iptr;
657         unsigned int idxoffs;
658
659         if(!cmdest->nverts) {
660                 return cmesh_clone(cmdest, cmsrc);
661         }
662
663         for(i=0; i<CMESH_NUM_ATTR; i++) {
664                 if(cmesh_has_attrib(cmdest, i) && cmesh_has_attrib(cmsrc, i)) {
665                         /* force validation of the data arrays */
666                         cmesh_attrib(cmdest, i);
667                         cmesh_attrib_ro(cmsrc, i);
668
669                         assert(cmdest->vattr[i].nelem == cmsrc->vattr[i].nelem);
670                         nelem = cmdest->vattr[i].nelem;
671                         origsz = cmdest->nverts * nelem;
672                         newsz = cmdest->nverts + cmsrc->nverts * nelem;
673
674                         if(!(vptr = realloc(cmdest->vattr[i].data, newsz * sizeof *vptr))) {
675                                 return -1;
676                         }
677                         memcpy(vptr + origsz, cmsrc->vattr[i].data, cmsrc->nverts * nelem * sizeof(float));
678                         cmdest->vattr[i].data = vptr;
679                         cmdest->vattr[i].count = newsz;
680                 }
681         }
682
683         if(cmesh_indexed(cmdest)) {
684                 assert(cmesh_indexed(cmsrc));
685                 /* force validation ... */
686                 cmesh_index(cmdest);
687                 cmesh_index_ro(cmsrc);
688
689                 idxoffs = cmdest->nverts;
690                 origsz = cmdest->icount;
691                 srcsz = cmsrc->icount;
692                 newsz = origsz + srcsz;
693
694                 if(!(iptr = realloc(cmdest->idata, newsz * sizeof *iptr))) {
695                         return -1;
696                 }
697                 cmdest->idata = iptr;
698                 cmdest->icount = newsz;
699
700                 /* copy and fixup all the new indices */
701                 iptr += origsz;
702                 for(i=0; i<srcsz; i++) {
703                         *iptr++ = cmsrc->idata[i] + idxoffs;
704                 }
705         }
706
707         cmdest->wire_ibo_valid = 0;
708         cmdest->aabb_valid = 0;
709         cmdest->bsph_valid = 0;
710         return 0;
711 }
712
713 void cmesh_clear_submeshes(struct cmesh *cm)
714 {
715         struct submesh *sm;
716
717         while(cm->sublist) {
718                 sm = cm->sublist;
719                 cm->sublist = cm->sublist->next;
720                 free(sm->name);
721                 free(sm);
722         }
723         cm->subcount = 0;
724 }
725
726 int cmesh_submesh(struct cmesh *cm, const char *name, int fstart, int fcount)
727 {
728         int i;
729         unsigned int minv = UINT_MAX, maxv = 0;
730         unsigned int *iptr;
731         struct submesh *sm;
732
733         if(fstart < 0 || fcount < 1 || fstart + fcount > cm->nfaces) {
734                 return -1;
735         }
736
737         if(!(sm = malloc(sizeof *sm)) || !(sm->name = malloc(strlen(name) + 1))) {
738                 free(sm);
739                 return -1;
740         }
741         strcpy(sm->name, name);
742         sm->nfaces = fcount;
743
744         if(cmesh_indexed(cm)) {
745                 sm->istart = fstart * 3;
746                 sm->icount = fcount * 3;
747
748                 /* find out which vertices are used by this submesh */
749                 iptr = cm->idata + sm->istart;
750                 for(i=0; i<sm->icount; i++) {
751                         unsigned int vidx = *iptr++;
752                         if(vidx < minv) minv = vidx;
753                         if(vidx > maxv) maxv = vidx;
754                 }
755                 sm->vstart = minv;
756                 sm->vcount = maxv - minv + 1;
757         } else {
758                 sm->istart = sm->icount = 0;
759                 sm->vstart = fstart * 3;
760                 sm->vcount = fcount * 3;
761         }
762
763         sm->next = cm->sublist;
764         cm->sublist = sm;
765         cm->subcount++;
766         return 0;
767 }
768
769 int cmesh_remove_submesh(struct cmesh *cm, int idx)
770 {
771         struct submesh dummy;
772         struct submesh *prev, *sm;
773
774         if(idx >= cm->subcount) {
775                 return -1;
776         }
777
778         dummy.next = cm->sublist;
779         prev = &dummy;
780
781         while(prev->next && idx-- > 0) {
782                 prev = prev->next;
783         }
784
785         if(!(sm = prev->next)) return -1;
786
787         prev->next = sm->next;
788         free(sm->name);
789         free(sm);
790
791         cm->subcount--;
792         assert(cm->subcount >= 0);
793
794         cm->sublist = dummy.next;
795         return 0;
796 }
797
798 int cmesh_find_submesh(const struct cmesh *cm, const char *name)
799 {
800         int idx = 0;
801         struct submesh *sm = cm->sublist;
802         while(sm) {
803                 if(strcmp(sm->name, name) == 0) {
804                         assert(idx <= cm->subcount);
805                         return idx;
806                 }
807                 idx++;
808                 sm = sm->next;
809         }
810         return -1;
811 }
812
813 int cmesh_submesh_count(const struct cmesh *cm)
814 {
815         return cm->subcount;
816 }
817
818 static struct submesh *get_submesh(const struct cmesh *m, int idx)
819 {
820         struct submesh *sm = m->sublist;
821         while(sm && --idx >= 0) {
822                 sm = sm->next;
823         }
824         return sm;
825 }
826
827 int cmesh_clone_submesh(struct cmesh *cmdest, const struct cmesh *cm, int subidx)
828 {
829         struct submesh *sub;
830
831         if(!(sub = get_submesh(cm, subidx))) {
832                 return -1;
833         }
834         return clone(cmdest, cm, sub);
835 }
836
837
838 /* assemble a complete vertex by adding all the useful attributes */
839 int cmesh_vertex(struct cmesh *cm, float x, float y, float z)
840 {
841         int i;
842
843         cgm_wcons(cm->cur_val + CMESH_ATTR_VERTEX, x, y, z, 1.0f);
844         cm->vattr[CMESH_ATTR_VERTEX].data_valid = 1;
845         cm->vattr[CMESH_ATTR_VERTEX].nelem = 3;
846
847         for(i=0; i<CMESH_NUM_ATTR; i++) {
848                 if(cm->vattr[i].nelem > 0) {
849                         cmesh_push_attrib(cm, i, &cm->cur_val[i].x);
850                 }
851         }
852
853         if(cm->idata_valid) {
854                 free(cm->idata);
855                 cm->idata = 0;
856                 cm->icount = 0;
857         }
858         cm->ibo_valid = cm->idata_valid = 0;
859         return 0;
860 }
861
862 void cmesh_normal(struct cmesh *cm, float nx, float ny, float nz)
863 {
864         cgm_wcons(cm->cur_val + CMESH_ATTR_NORMAL, nx, ny, nz, 1.0f);
865         cm->vattr[CMESH_ATTR_NORMAL].nelem = 3;
866 }
867
868 void cmesh_tangent(struct cmesh *cm, float tx, float ty, float tz)
869 {
870         cgm_wcons(cm->cur_val + CMESH_ATTR_TANGENT, tx, ty, tz, 1.0f);
871         cm->vattr[CMESH_ATTR_TANGENT].nelem = 3;
872 }
873
874 void cmesh_texcoord(struct cmesh *cm, float u, float v, float w)
875 {
876         cgm_wcons(cm->cur_val + CMESH_ATTR_TEXCOORD, u, v, w, 1.0f);
877         cm->vattr[CMESH_ATTR_TEXCOORD].nelem = 3;
878 }
879
880 void cmesh_boneweights(struct cmesh *cm, float w1, float w2, float w3, float w4)
881 {
882         cgm_wcons(cm->cur_val + CMESH_ATTR_BONEWEIGHTS, w1, w2, w3, w4);
883         cm->vattr[CMESH_ATTR_BONEWEIGHTS].nelem = 4;
884 }
885
886 void cmesh_boneidx(struct cmesh *cm, int idx1, int idx2, int idx3, int idx4)
887 {
888         cgm_wcons(cm->cur_val + CMESH_ATTR_BONEIDX, idx1, idx2, idx3, idx4);
889         cm->vattr[CMESH_ATTR_BONEIDX].nelem = 4;
890 }
891
892 static float *get_vec4(struct cmesh *cm, int attr, int idx, cgm_vec4 *res)
893 {
894         int i;
895         float *sptr, *dptr;
896         cgm_wcons(res, 0, 0, 0, 1);
897         if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
898                 return 0;
899         }
900         dptr = &res->x;
901
902         for(i=0; i<cm->vattr[attr].nelem; i++) {
903                 *dptr++ = sptr[i];
904         }
905         return sptr;
906 }
907
908 static float *get_vec3(struct cmesh *cm, int attr, int idx, cgm_vec3 *res)
909 {
910         int i;
911         float *sptr, *dptr;
912         cgm_vcons(res, 0, 0, 0);
913         if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
914                 return 0;
915         }
916         dptr = &res->x;
917
918         for(i=0; i<cm->vattr[attr].nelem; i++) {
919                 *dptr++ = sptr[i];
920         }
921         return sptr;
922 }
923
924 /* dir_xform can be null, in which case it's calculated from xform */
925 void cmesh_apply_xform(struct cmesh *cm, float *xform, float *dir_xform)
926 {
927         unsigned int i;
928         int j;
929         cgm_vec4 v;
930         cgm_vec3 n, t;
931         float *vptr;
932
933         for(i=0; i<cm->nverts; i++) {
934                 if(!(vptr = get_vec4(cm, CMESH_ATTR_VERTEX, i, &v))) {
935                         return;
936                 }
937                 cgm_wmul_m4v4(&v, xform);
938                 for(j=0; j<cm->vattr[CMESH_ATTR_VERTEX].nelem; j++) {
939                         *vptr++ = (&v.x)[j];
940                 }
941
942                 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
943                         if((vptr = get_vec3(cm, CMESH_ATTR_NORMAL, i, &n))) {
944                                 cgm_vmul_m3v3(&n, dir_xform);
945                                 for(j=0; j<cm->vattr[CMESH_ATTR_NORMAL].nelem; j++) {
946                                         *vptr++ = (&n.x)[j];
947                                 }
948                         }
949                 }
950                 if(cmesh_has_attrib(cm, CMESH_ATTR_TANGENT)) {
951                         if((vptr = get_vec3(cm, CMESH_ATTR_TANGENT, i, &t))) {
952                                 cgm_vmul_m3v3(&t, dir_xform);
953                                 for(j=0; j<cm->vattr[CMESH_ATTR_TANGENT].nelem; j++) {
954                                         *vptr++ = (&t.x)[j];
955                                 }
956                         }
957                 }
958         }
959 }
960
961 void cmesh_flip(struct cmesh *cm)
962 {
963         cmesh_flip_faces(cm);
964         cmesh_flip_normals(cm);
965 }
966
967 void cmesh_flip_faces(struct cmesh *cm)
968 {
969         int i, j, idxnum, vnum, nelem;
970         unsigned int *indices;
971         float *verts, *vptr;
972
973         if(cmesh_indexed(cm)) {
974                 if(!(indices = cmesh_index(cm))) {
975                         return;
976                 }
977                 idxnum = cmesh_index_count(cm);
978                 for(i=0; i<idxnum; i+=3) {
979                         unsigned int tmp = indices[i + 2];
980                         indices[i + 2] = indices[i + 1];
981                         indices[i + 1] = tmp;
982                 }
983         } else {
984                 if(!(verts = cmesh_attrib(cm, CMESH_ATTR_VERTEX))) {
985                         return;
986                 }
987                 vnum = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
988                 nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
989                 for(i=0; i<vnum; i+=3) {
990                         for(j=0; j<nelem; j++) {
991                                 vptr = verts + (i + 1) * nelem + j;
992                                 float tmp = vptr[nelem];
993                                 vptr[nelem] = vptr[0];
994                                 vptr[0] = tmp;
995                         }
996                 }
997         }
998 }
999 void cmesh_flip_normals(struct cmesh *cm)
1000 {
1001         int i, num;
1002         float *nptr = cmesh_attrib(cm, CMESH_ATTR_NORMAL);
1003         if(!nptr) return;
1004
1005         num = cm->nverts * cm->vattr[CMESH_ATTR_NORMAL].nelem;
1006         for(i=0; i<num; i++) {
1007                 *nptr = -*nptr;
1008                 nptr++;
1009         }
1010 }
1011
1012 int cmesh_explode(struct cmesh *cm)
1013 {
1014         int i, j, k, idxnum, nnverts;
1015         unsigned int *indices;
1016
1017         if(!cmesh_indexed(cm)) return 0;
1018
1019         indices = cmesh_index(cm);
1020         assert(indices);
1021
1022         idxnum = cmesh_index_count(cm);
1023         nnverts = idxnum;
1024
1025         for(i=0; i<CMESH_NUM_ATTR; i++) {
1026                 const float *srcbuf;
1027                 float *tmpbuf, *dstptr;
1028
1029                 if(!cmesh_has_attrib(cm, i)) continue;
1030
1031                 srcbuf = cmesh_attrib(cm, i);
1032                 if(!(tmpbuf = malloc(nnverts * cm->vattr[i].nelem * sizeof(float)))) {
1033                         return -1;
1034                 }
1035                 dstptr = tmpbuf;
1036
1037                 for(j=0; j<idxnum; j++) {
1038                         unsigned int idx = indices[j];
1039                         const float *srcptr = srcbuf + idx * cm->vattr[i].nelem;
1040
1041                         for(k=0; k<cm->vattr[i].nelem; k++) {
1042                                 *dstptr++ = *srcptr++;
1043                         }
1044                 }
1045
1046                 free(cm->vattr[i].data);
1047                 cm->vattr[i].data = tmpbuf;
1048                 cm->vattr[i].count = nnverts * cm->vattr[i].nelem;
1049                 cm->vattr[i].data_valid = 1;
1050         }
1051
1052         cm->ibo_valid = 0;
1053         cm->idata_valid = 0;
1054         free(cm->idata);
1055         cm->idata = 0;
1056         cm->icount = 0;
1057
1058         cm->nverts = nnverts;
1059         cm->nfaces = idxnum / 3;
1060         return 0;
1061 }
1062
1063 void cmesh_calc_face_normals(struct cmesh *cm)
1064 {
1065         /* TODO */
1066 }
1067
1068 static int pre_draw(const struct cmesh *cm)
1069 {
1070         int i, loc, cur_sdr;
1071
1072         glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1073
1074         update_buffers((struct cmesh*)cm);
1075
1076         if(!cm->vattr[CMESH_ATTR_VERTEX].vbo_valid) {
1077                 return -1;
1078         }
1079
1080         if(cur_sdr && use_custom_sdr_attr) {
1081                 if(sdr_loc[CMESH_ATTR_VERTEX] == -1) {
1082                         return -1;
1083                 }
1084
1085                 for(i=0; i<CMESH_NUM_ATTR; i++) {
1086                         loc = sdr_loc[i];
1087                         if(loc >= 0 && cm->vattr[i].vbo_valid) {
1088                                 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
1089                                 glVertexAttribPointer(loc, cm->vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
1090                                 glEnableVertexAttribArray(loc);
1091                         }
1092                 }
1093         } else {
1094 #ifndef GL_ES_VERSION_2_0
1095                 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_VERTEX].vbo);
1096                 glVertexPointer(cm->vattr[CMESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
1097                 glEnableClientState(GL_VERTEX_ARRAY);
1098
1099                 if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
1100                         glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_NORMAL].vbo);
1101                         glNormalPointer(GL_FLOAT, 0, 0);
1102                         glEnableClientState(GL_NORMAL_ARRAY);
1103                 }
1104                 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
1105                         glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD].vbo);
1106                         glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
1107                         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1108                 }
1109                 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
1110                         glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_COLOR].vbo);
1111                         glColorPointer(cm->vattr[CMESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
1112                         glEnableClientState(GL_COLOR_ARRAY);
1113                 }
1114                 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
1115                         glClientActiveTexture(GL_TEXTURE1);
1116                         glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD2].vbo);
1117                         glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD2].nelem, GL_FLOAT, 0, 0);
1118                         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1119                         glClientActiveTexture(GL_TEXTURE0);
1120                 }
1121 #endif  /* GL_ES_VERSION_2_0 */
1122         }
1123         glBindBuffer(GL_ARRAY_BUFFER, 0);
1124         return cur_sdr;
1125 }
1126
1127 void cmesh_draw(const struct cmesh *cm)
1128 {
1129         int cur_sdr;
1130
1131         if((cur_sdr = pre_draw(cm)) == -1) {
1132                 return;
1133         }
1134
1135         if(cm->ibo_valid) {
1136                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1137                 glDrawElements(GL_TRIANGLES, cm->nfaces * 3, GL_UNSIGNED_INT, 0);
1138                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1139         } else {
1140                 glDrawArrays(GL_TRIANGLES, 0, cm->nverts);
1141         }
1142
1143         post_draw(cm, cur_sdr);
1144 }
1145
1146 void cmesh_draw_range(const struct cmesh *cm, int start, int count)
1147 {
1148         int cur_sdr;
1149
1150         if((cur_sdr = pre_draw(cm)) == -1) {
1151                 return;
1152         }
1153
1154         if(cm->ibo_valid) {
1155                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1156                 glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (void*)(intptr_t)(start * 4));
1157                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1158         } else {
1159                 glDrawArrays(GL_TRIANGLES, start, count);
1160         }
1161
1162         post_draw(cm, cur_sdr);
1163 }
1164
1165 void cmesh_draw_submesh(const struct cmesh *cm, int subidx)
1166 {
1167         struct submesh *sm = cm->sublist;
1168
1169         while(sm && subidx-- > 0) {
1170                 sm = sm->next;
1171         }
1172         if(!sm) return;
1173
1174         if(sm->icount) {
1175                 cmesh_draw_range(cm, sm->istart, sm->icount);
1176         } else {
1177                 cmesh_draw_range(cm, sm->vstart, sm->vcount);
1178         }
1179 }
1180
1181 static void post_draw(const struct cmesh *cm, int cur_sdr)
1182 {
1183         int i;
1184
1185         if(cur_sdr && use_custom_sdr_attr) {
1186                 for(i=0; i<CMESH_NUM_ATTR; i++) {
1187                         int loc = sdr_loc[i];
1188                         if(loc >= 0 && cm->vattr[i].vbo_valid) {
1189                                 glDisableVertexAttribArray(loc);
1190                         }
1191                 }
1192         } else {
1193 #ifndef GL_ES_VERSION_2_0
1194                 glDisableClientState(GL_VERTEX_ARRAY);
1195                 if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
1196                         glDisableClientState(GL_NORMAL_ARRAY);
1197                 }
1198                 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
1199                         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1200                 }
1201                 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
1202                         glDisableClientState(GL_COLOR_ARRAY);
1203                 }
1204                 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
1205                         glClientActiveTexture(GL_TEXTURE1);
1206                         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1207                         glClientActiveTexture(GL_TEXTURE0);
1208                 }
1209 #endif  /* GL_ES_VERSION_2_0 */
1210         }
1211 }
1212
1213 void cmesh_draw_wire(const struct cmesh *cm, float linesz)
1214 {
1215         int cur_sdr, nfaces;
1216
1217         if((cur_sdr = pre_draw(cm)) == -1) {
1218                 return;
1219         }
1220         update_wire_ibo((struct cmesh*)cm);
1221
1222         nfaces = cmesh_poly_count(cm);
1223         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
1224         glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0);
1225         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1226
1227         post_draw(cm, cur_sdr);
1228 }
1229
1230 void cmesh_draw_vertices(const struct cmesh *cm, float ptsz)
1231 {
1232         int cur_sdr;
1233         if((cur_sdr = pre_draw(cm)) == -1) {
1234                 return;
1235         }
1236
1237         glPushAttrib(GL_POINT_BIT);
1238         glPointSize(ptsz);
1239         glDrawArrays(GL_POINTS, 0, cm->nverts);
1240         glPopAttrib();
1241
1242         post_draw(cm, cur_sdr);
1243 }
1244
1245 void cmesh_draw_normals(const struct cmesh *cm, float len)
1246 {
1247 #ifndef GL_ES_VERSION_2_0
1248         int i, cur_sdr, vert_nelem, norm_nelem;
1249         int loc = -1;
1250         const float *varr, *norm;
1251
1252         varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1253         norm = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
1254         if(!varr || !norm) return;
1255
1256         vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1257         norm_nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1258
1259         glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1260         if(cur_sdr && use_custom_sdr_attr) {
1261                 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1262                         return;
1263                 }
1264         }
1265
1266         glBegin(GL_LINES);
1267         for(i=0; i<cm->nverts; i++) {
1268                 float x, y, z, endx, endy, endz;
1269
1270                 x = varr[i * vert_nelem];
1271                 y = varr[i * vert_nelem + 1];
1272                 z = varr[i * vert_nelem + 2];
1273                 endx = x + norm[i * norm_nelem] * len;
1274                 endy = y + norm[i * norm_nelem + 1] * len;
1275                 endz = z + norm[i * norm_nelem + 2] * len;
1276
1277                 if(loc == -1) {
1278                         glVertex3f(x, y, z);
1279                         glVertex3f(endx, endy, endz);
1280                 } else {
1281                         glVertexAttrib3f(loc, x, y, z);
1282                         glVertexAttrib3f(loc, endx, endy, endz);
1283                 }
1284         }
1285         glEnd();
1286 #endif  /* GL_ES_VERSION_2_0 */
1287 }
1288
1289 void cmesh_draw_tangents(const struct cmesh *cm, float len)
1290 {
1291 #ifndef GL_ES_VERSION_2_0
1292         int i, cur_sdr, vert_nelem, tang_nelem;
1293         int loc = -1;
1294         const float *varr, *tang;
1295
1296         varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1297         tang = cmesh_attrib_ro(cm, CMESH_ATTR_TANGENT);
1298         if(!varr || !tang) return;
1299
1300         vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1301         tang_nelem = cm->vattr[CMESH_ATTR_TANGENT].nelem;
1302
1303         glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1304         if(cur_sdr && use_custom_sdr_attr) {
1305                 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1306                         return;
1307                 }
1308         }
1309
1310         glBegin(GL_LINES);
1311         for(i=0; i<cm->nverts; i++) {
1312                 float x, y, z, endx, endy, endz;
1313
1314                 x = varr[i * vert_nelem];
1315                 y = varr[i * vert_nelem + 1];
1316                 z = varr[i * vert_nelem + 2];
1317                 endx = x + tang[i * tang_nelem] * len;
1318                 endy = y + tang[i * tang_nelem + 1] * len;
1319                 endz = z + tang[i * tang_nelem + 2] * len;
1320
1321                 if(loc == -1) {
1322                         glVertex3f(x, y, z);
1323                         glVertex3f(endx, endy, endz);
1324                 } else {
1325                         glVertexAttrib3f(loc, x, y, z);
1326                         glVertexAttrib3f(loc, endx, endy, endz);
1327                 }
1328         }
1329         glEnd();
1330 #endif  /* GL_ES_VERSION_2_0 */
1331 }
1332
1333 static void update_buffers(struct cmesh *cm)
1334 {
1335         int i;
1336
1337         for(i=0; i<CMESH_NUM_ATTR; i++) {
1338                 if(cmesh_has_attrib(cm, i) && !cm->vattr[i].vbo_valid) {
1339                         glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
1340                         glBufferData(GL_ARRAY_BUFFER, cm->nverts * cm->vattr[i].nelem * sizeof(float),
1341                                         cm->vattr[i].data, GL_STATIC_DRAW);
1342                         cm->vattr[i].vbo_valid = 1;
1343                 }
1344         }
1345         glBindBuffer(GL_ARRAY_BUFFER, 0);
1346
1347         if(cm->idata_valid && !cm->ibo_valid) {
1348                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1349                 glBufferData(GL_ELEMENT_ARRAY_BUFFER, cm->nfaces * 3 * sizeof(unsigned int),
1350                                 cm->idata, GL_STATIC_DRAW);
1351                 cm->ibo_valid = 1;
1352                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1353         }
1354 }
1355
1356 static void update_wire_ibo(struct cmesh *cm)
1357 {
1358         int i, num_faces;
1359         unsigned int *wire_idxarr, *dest;
1360
1361         update_buffers(cm);
1362
1363         if(cm->wire_ibo_valid) return;
1364
1365         if(!cm->wire_ibo) {
1366                 glGenBuffers(1, &cm->wire_ibo);
1367         }
1368         num_faces = cmesh_poly_count(cm);
1369
1370         if(!(wire_idxarr = malloc(num_faces * 6 * sizeof *wire_idxarr))) {
1371                 return;
1372         }
1373         dest = wire_idxarr;
1374
1375         if(cm->ibo_valid) {
1376                 /* we're dealing with an indexed mesh */
1377                 const unsigned int *idxarr = cmesh_index_ro(cm);
1378
1379                 for(i=0; i<num_faces; i++) {
1380                         *dest++ = idxarr[0];
1381                         *dest++ = idxarr[1];
1382                         *dest++ = idxarr[1];
1383                         *dest++ = idxarr[2];
1384                         *dest++ = idxarr[2];
1385                         *dest++ = idxarr[0];
1386                         idxarr += 3;
1387                 }
1388         } else {
1389                 /* not an indexed mesh */
1390                 for(i=0; i<num_faces; i++) {
1391                         int vidx = i * 3;
1392                         *dest++ = vidx;
1393                         *dest++ = vidx + 1;
1394                         *dest++ = vidx + 1;
1395                         *dest++ = vidx + 2;
1396                         *dest++ = vidx + 2;
1397                         *dest++ = vidx;
1398                 }
1399         }
1400
1401         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
1402         glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_faces * 6 * sizeof(unsigned int),
1403                         wire_idxarr, GL_STATIC_DRAW);
1404         free(wire_idxarr);
1405         cm->wire_ibo_valid = 1;
1406         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1407 }
1408
1409 static void calc_aabb(struct cmesh *cm)
1410 {
1411         int i, j;
1412
1413         if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1414                 return;
1415         }
1416
1417         cgm_vcons(&cm->aabb_min, FLT_MAX, FLT_MAX, FLT_MAX);
1418         cgm_vcons(&cm->aabb_max, -FLT_MAX, -FLT_MAX, -FLT_MAX);
1419
1420         for(i=0; i<cm->nverts; i++) {
1421                 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1422                 for(j=0; j<3; j++) {
1423                         if(v[j] < (&cm->aabb_min.x)[j]) {
1424                                 (&cm->aabb_min.x)[j] = v[j];
1425                         }
1426                         if(v[j] > (&cm->aabb_max.x)[j]) {
1427                                 (&cm->aabb_max.x)[j] = v[j];
1428                         }
1429                 }
1430         }
1431         cm->aabb_valid = 1;
1432 }
1433
1434 void cmesh_aabbox(const struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax)
1435 {
1436         if(!cm->aabb_valid) {
1437                 calc_aabb((struct cmesh*)cm);
1438         }
1439         *vmin = cm->aabb_min;
1440         *vmax = cm->aabb_max;
1441 }
1442
1443 static void calc_bsph(struct cmesh *cm)
1444 {
1445         int i;
1446         float s, dist_sq;
1447
1448         if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1449                 return;
1450         }
1451
1452         cgm_vcons(&cm->bsph_center, 0, 0, 0);
1453
1454         /* first find the center */
1455         for(i=0; i<cm->nverts; i++) {
1456                 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1457                 cm->bsph_center.x += v[0];
1458                 cm->bsph_center.y += v[1];
1459                 cm->bsph_center.z += v[2];
1460         }
1461         s = 1.0f / (float)cm->nverts;
1462         cm->bsph_center.x *= s;
1463         cm->bsph_center.y *= s;
1464         cm->bsph_center.z *= s;
1465
1466         cm->bsph_radius = 0.0f;
1467         for(i=0; i<cm->nverts; i++) {
1468                 const cgm_vec3 *v = (const cgm_vec3*)cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1469                 if((dist_sq = cgm_vdist_sq(v, &cm->bsph_center)) > cm->bsph_radius) {
1470                         cm->bsph_radius = dist_sq;
1471                 }
1472         }
1473         cm->bsph_radius = sqrt(cm->bsph_radius);
1474         cm->bsph_valid = 1;
1475 }
1476
1477 float cmesh_bsphere(const struct cmesh *cm, cgm_vec3 *center, float *rad)
1478 {
1479         if(!cm->bsph_valid) {
1480                 calc_bsph((struct cmesh*)cm);
1481         }
1482         if(center) *center = cm->bsph_center;
1483         if(rad) *rad = cm->bsph_radius;
1484         return cm->bsph_radius;
1485 }
1486
1487 /* TODO */
1488 void cmesh_texcoord_apply_xform(struct cmesh *cm, float *xform);
1489 void cmesh_texcoord_gen_plane(struct cmesh *cm, cgm_vec3 *norm, cgm_vec3 *tang);
1490 void cmesh_texcoord_gen_box(struct cmesh *cm);
1491 void cmesh_texcoord_gen_cylinder(struct cmesh *cm);
1492
1493 int cmesh_dump(const struct cmesh *cm, const char *fname)
1494 {
1495         FILE *fp = fopen(fname, "wb");
1496         if(fp) {
1497                 int res = cmesh_dump_file(cm, fp);
1498                 fclose(fp);
1499                 return res;
1500         }
1501         return -1;
1502 }
1503
1504 int cmesh_dump_file(const struct cmesh *cm, FILE *fp)
1505 {
1506         static const char *label[] = { "pos", "nor", "tan", "tex", "col", "bw", "bid", "tex2" };
1507         static const char *elemfmt[] = { 0, " %s(%g)", " %s(%g, %g)", " %s(%g, %g, %g)", " %s(%g, %g, %g, %g)", 0 };
1508         int i, j;
1509
1510         if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1511                 return -1;
1512         }
1513
1514         fprintf(fp, "VERTEX ATTRIBUTES\n");
1515
1516         for(i=0; i<cm->nverts; i++) {
1517                 fprintf(fp, "%5u:", i);
1518                 for(j=0; j<CMESH_NUM_ATTR; j++) {
1519                         if(cmesh_has_attrib(cm, j)) {
1520                                 const float *v = cmesh_attrib_at_ro(cm, j, i);
1521                                 int nelem = cm->vattr[j].nelem;
1522                                 fprintf(fp, elemfmt[nelem], label[j], v[0], nelem > 1 ? v[1] : 0.0f,
1523                                                 nelem > 2 ? v[2] : 0.0f, nelem > 3 ? v[3] : 0.0f);
1524                         }
1525                 }
1526                 fputc('\n', fp);
1527         }
1528
1529         if(cmesh_indexed(cm)) {
1530                 const unsigned int *idx = cmesh_index_ro(cm);
1531                 int numidx = cmesh_index_count(cm);
1532                 int numtri = numidx / 3;
1533                 assert(numidx % 3 == 0);
1534
1535                 fprintf(fp, "FACES\n");
1536
1537                 for(i=0; i<numtri; i++) {
1538                         fprintf(fp, "%5d: %d %d %d\n", i, idx[0], idx[1], idx[2]);
1539                         idx += 3;
1540                 }
1541         }
1542         return 0;
1543 }
1544
1545 int cmesh_dump_obj(const struct cmesh *cm, const char *fname)
1546 {
1547         FILE *fp = fopen(fname, "wb");
1548         if(fp) {
1549                 int res = cmesh_dump_obj_file(cm, fp, 0);
1550                 fclose(fp);
1551                 return res;
1552         }
1553         return -1;
1554 }
1555
1556 #define HAS_VN  1
1557 #define HAS_VT  2
1558
1559 int cmesh_dump_obj_file(const struct cmesh *cm, FILE *fp, int voffs)
1560 {
1561         static const char *fmtstr[] = {" %u", " %u//%u", " %u/%u", " %u/%u/%u"};
1562         int i, j, num, nelem;
1563         unsigned int aflags = 0;
1564
1565         if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1566                 return -1;
1567         }
1568
1569
1570         nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1571         if((num = cm->vattr[CMESH_ATTR_VERTEX].count) != cm->nverts * nelem) {
1572                 fprintf(stderr, "vertex array size (%d) != nverts (%d)\n", num, cm->nverts);
1573         }
1574         for(i=0; i<cm->nverts; i++) {
1575                 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1576                 fprintf(fp, "v %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
1577         }
1578
1579         if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
1580                 aflags |= HAS_VN;
1581                 nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1582                 if((num = cm->vattr[CMESH_ATTR_NORMAL].count) != cm->nverts * nelem) {
1583                         fprintf(stderr, "normal array size (%d) != nverts (%d)\n", num, cm->nverts);
1584                 }
1585                 for(i=0; i<cm->nverts; i++) {
1586                         const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_NORMAL, i);
1587                         fprintf(fp, "vn %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
1588                 }
1589         }
1590
1591         if(cmesh_has_attrib(cm, CMESH_ATTR_TEXCOORD)) {
1592                 aflags |= HAS_VT;
1593                 nelem = cm->vattr[CMESH_ATTR_TEXCOORD].nelem;
1594                 if((num = cm->vattr[CMESH_ATTR_TEXCOORD].count) != cm->nverts * nelem) {
1595                         fprintf(stderr, "texcoord array size (%d) != nverts (%d)\n", num, cm->nverts);
1596                 }
1597                 for(i=0; i<cm->nverts; i++) {
1598                         const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_TEXCOORD, i);
1599                         fprintf(fp, "vt %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f);
1600                 }
1601         }
1602
1603         if(cmesh_indexed(cm)) {
1604                 const unsigned int *idxptr = cmesh_index_ro(cm);
1605                 int numidx = cmesh_index_count(cm);
1606                 int numtri = numidx / 3;
1607                 assert(numidx % 3 == 0);
1608
1609                 for(i=0; i<numtri; i++) {
1610                         fputc('f', fp);
1611                         for(j=0; j<3; j++) {
1612                                 unsigned int idx = *idxptr++ + 1 + voffs;
1613                                 fprintf(fp, fmtstr[aflags], idx, idx, idx);
1614                         }
1615                         fputc('\n', fp);
1616                 }
1617         } else {
1618                 int numtri = cm->nverts / 3;
1619                 unsigned int idx = 1 + voffs;
1620                 for(i=0; i<numtri; i++) {
1621                         fputc('f', fp);
1622                         for(j=0; j<3; j++) {
1623                                 fprintf(fp, fmtstr[aflags], idx, idx, idx);
1624                                 ++idx;
1625                         }
1626                         fputc('\n', fp);
1627                 }
1628         }
1629         return 0;
1630 }