13 struct cmesh_vattrib {
14 int nelem; /* num elements per attribute [1, 4] */
16 unsigned int count; /* number of floats in data */
24 /* istart,icount are valid only when the mesh is indexed, otherwise icount is 0.
25 * vstart,vcount are define the submesh for non-indexed meshes.
26 * For indexed meshes, vstart,vcount denote the range of vertices used by each
31 int nfaces; /* derived from either icount or vcount */
39 unsigned int nverts, nfaces;
41 struct submesh *sublist;
44 /* current value for each attribute for the immediate mode interface */
45 cgm_vec4 cur_val[CMESH_NUM_ATTR];
47 unsigned int buffer_objects[CMESH_NUM_ATTR + 1];
48 struct cmesh_vattrib vattr[CMESH_NUM_ATTR];
62 /* index buffer for wireframe rendering (constructed on demand) */
63 unsigned int wire_ibo;
67 /* axis-aligned bounding box */
68 cgm_vec3 aabb_min, aabb_max;
77 static int clone(struct cmesh *cmdest, const struct cmesh *cmsrc, struct submesh *sub);
78 static int pre_draw(const struct cmesh *cm, int start);
79 static void post_draw(const struct cmesh *cm, int cur_sdr);
80 static void update_buffers(struct cmesh *cm);
82 static void update_wire_ibo(struct cmesh *cm);
84 static void calc_aabb(struct cmesh *cm);
85 static void calc_bsph(struct cmesh *cm);
87 static int def_nelem[CMESH_NUM_ATTR] = {3, 3, 3, 2, 4, 4, 4, 2};
90 static int sdr_loc[CMESH_NUM_ATTR] = {0, 1, 2, 3, 4, 5, 6, 7};
91 static int use_custom_sdr_attr;
95 void cmesh_set_attrib_sdrloc(int attr, int loc)
100 int cmesh_get_attrib_sdrloc(int attr)
102 return sdr_loc[attr];
105 void cmesh_clear_attrib_sdrloc(void)
108 for(i=0; i<CMESH_NUM_ATTR; i++) {
115 struct cmesh *cmesh_alloc(void)
119 if(!(cm = malloc(sizeof *cm))) {
122 if(cmesh_init(cm) == -1) {
129 void cmesh_free(struct cmesh *cm)
135 int cmesh_init(struct cmesh *cm)
141 memset(cm, 0, sizeof *cm);
142 cgm_wcons(cm->cur_val + CMESH_ATTR_COLOR, 1, 1, 1, 1);
145 glGenBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
147 for(i=0; i<CMESH_NUM_ATTR; i++) {
148 cm->vattr[i].vbo = cm->buffer_objects[i];
151 cm->ibo = cm->buffer_objects[CMESH_NUM_ATTR];
156 void cmesh_destroy(struct cmesh *cm)
162 for(i=0; i<CMESH_NUM_ATTR; i++) {
163 free(cm->vattr[i].data);
167 cmesh_clear_submeshes(cm);
170 glDeleteBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
172 glDeleteBuffers(1, &cm->wire_ibo);
177 /*glDeleteList(cm->dlist, 1);*/
178 gaw_free_compiled(cm->dlist);
183 void cmesh_clear(struct cmesh *cm)
187 for(i=0; i<CMESH_NUM_ATTR; i++) {
188 cm->vattr[i].nelem = 0;
190 cm->vattr[i].vbo_valid = 0;
191 cm->vattr[i].data_valid = 0;
193 free(cm->vattr[i].data);
194 cm->vattr[i].data = 0;
195 cm->vattr[i].count = 0;
206 cm->wire_ibo_valid = 0;
208 cm->nverts = cm->nfaces = 0;
212 /*glDeleteList(cm->dlist, 1);*/
213 gaw_free_compiled(cm->dlist);
217 cm->bsph_valid = cm->aabb_valid = 0;
219 cmesh_clear_submeshes(cm);
222 int cmesh_clone(struct cmesh *cmdest, const struct cmesh *cmsrc)
224 return clone(cmdest, cmsrc, 0);
227 static int clone(struct cmesh *cmdest, const struct cmesh *cmsrc, struct submesh *sub)
229 int i, nelem, vstart, vcount, istart, icount;
230 char *srcname, *name = 0;
231 float *varr[CMESH_NUM_ATTR] = {0};
233 unsigned int *iptr, *iarr = 0;
235 /* try do anything that can fail first, before making any changes to cmdest
236 * so we have the option of recovering gracefuly
239 srcname = sub ? sub->name : cmsrc->name;
241 if(!(name = malloc(strlen(srcname) + 1))) {
244 strcpy(name, srcname);
248 vstart = sub->vstart;
249 vcount = sub->vcount;
250 istart = sub->istart;
251 icount = sub->icount;
254 vcount = cmsrc->nverts;
255 icount = cmsrc->icount;
258 if(cmesh_indexed(cmsrc)) {
259 if(!(iarr = malloc(icount * sizeof *iarr))) {
265 for(i=0; i<CMESH_NUM_ATTR; i++) {
266 if(cmesh_has_attrib(cmsrc, i)) {
267 nelem = cmsrc->vattr[i].nelem;
268 if(!(varr[i] = malloc(vcount * nelem * sizeof(float)))) {
279 /* from this point forward nothing can fail */
282 for(i=0; i<CMESH_NUM_ATTR; i++) {
283 free(cmdest->vattr[i].data);
285 if(cmesh_has_attrib(cmsrc, i)) {
286 /* force validation of the actual data on the source mesh */
287 cmesh_attrib((struct cmesh*)cmsrc, i);
289 nelem = cmsrc->vattr[i].nelem;
290 cmdest->vattr[i].nelem = nelem;
291 cmdest->vattr[i].data = varr[i];
292 cmdest->vattr[i].count = vcount * nelem;
293 vptr = cmsrc->vattr[i].data + vstart * nelem;
294 memcpy(cmdest->vattr[i].data, vptr, vcount * nelem * sizeof(float));
295 cmdest->vattr[i].data_valid = 1;
297 cmdest->vattr[i].vbo_valid = 0;
300 memset(cmdest->vattr + i, 0, sizeof cmdest->vattr[i]);
304 if(cmesh_indexed(cmsrc)) {
305 /* force validation .... */
306 cmesh_index((struct cmesh*)cmsrc);
308 cmdest->idata = iarr;
309 cmdest->icount = icount;
311 /* need to offset all vertex indices by -vstart */
312 iptr = cmsrc->idata + istart;
313 for(i=0; i<icount; i++) {
314 cmdest->idata[i] = *iptr++ - vstart;
317 memcpy(cmdest->idata, cmsrc->idata + istart, icount * sizeof *cmdest->idata);
319 cmdest->idata_valid = 1;
322 cmdest->idata_valid = 0;
324 cmdest->ibo_valid = 0;
331 cmdest->nverts = cmsrc->nverts;
332 cmdest->nfaces = sub ? sub->nfaces : cmsrc->nfaces;
334 memcpy(cmdest->cur_val, cmsrc->cur_val, sizeof cmdest->cur_val);
336 cmdest->aabb_min = cmsrc->aabb_min;
337 cmdest->aabb_max = cmsrc->aabb_max;
338 cmdest->aabb_valid = cmsrc->aabb_valid;
339 cmdest->bsph_center = cmsrc->bsph_center;
340 cmdest->bsph_radius = cmsrc->bsph_radius;
341 cmdest->bsph_valid = cmsrc->bsph_valid;
343 /* copy sublist only if we're not cloning a submesh */
345 struct submesh *sm, *n, *head = 0, *tail = 0;
349 if(!(n = malloc(sizeof *n)) || !(name = malloc(strlen(sm->name) + 1))) {
354 strcpy(name, sm->name);
369 cmdest->sublist = head;
370 cmdest->subcount = cmsrc->subcount;
376 int cmesh_set_name(struct cmesh *cm, const char *name)
378 int len = strlen(name);
379 char *tmp = malloc(len + 1);
383 memcpy(cm->name, name, len + 1);
387 const char *cmesh_name(const struct cmesh *cm)
392 int cmesh_has_attrib(const struct cmesh *cm, int attr)
394 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
398 return cm->vattr[attr].vbo_valid | cm->vattr[attr].data_valid;
400 return cm->vattr[attr].data_valid;
404 int cmesh_indexed(const struct cmesh *cm)
407 return cm->ibo_valid | cm->idata_valid;
409 return cm->idata_valid;
413 /* vdata can be 0, in which case only memory is allocated
414 * returns pointer to the attribute array
416 float *cmesh_set_attrib(struct cmesh *cm, int attr, int nelem, unsigned int num,
421 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
424 if(cm->nverts && num != cm->nverts) {
428 if(!(newarr = malloc(num * nelem * sizeof *newarr))) {
432 memcpy(newarr, vdata, num * nelem * sizeof *newarr);
437 free(cm->vattr[attr].data);
438 cm->vattr[attr].data = newarr;
439 cm->vattr[attr].count = num * nelem;
440 cm->vattr[attr].nelem = nelem;
441 cm->vattr[attr].data_valid = 1;
443 cm->vattr[attr].vbo_valid = 0;
447 gaw_free_compiled(cm->dlist);
454 float *cmesh_attrib(struct cmesh *cm, int attr)
456 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
460 cm->vattr[attr].vbo_valid = 0;
464 gaw_free_compiled(cm->dlist);
468 return (float*)cmesh_attrib_ro(cm, attr);
471 const float *cmesh_attrib_ro(const struct cmesh *cm, int attr)
473 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
477 if(!cm->vattr[attr].data_valid) {
478 #if GL_ES_VERSION_2_0 || !defined(USE_VBO)
483 struct cmesh *m = (struct cmesh*)cm;
485 if(!m->vattr[attr].vbo_valid) {
489 /* local data copy unavailable, grab the data from the vbo */
490 nelem = m->vattr[attr].nelem;
491 if(!(m->vattr[attr].data = malloc(m->nverts * nelem * sizeof(float)))) {
494 m->vattr[attr].count = m->nverts * nelem;
496 glBindBuffer(GL_ARRAY_BUFFER, m->vattr[attr].vbo);
497 tmp = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
498 memcpy(m->vattr[attr].data, tmp, m->nverts * nelem * sizeof(float));
499 glUnmapBuffer(GL_ARRAY_BUFFER);
501 m->vattr[attr].data_valid = 1;
504 return cm->vattr[attr].data;
507 float *cmesh_attrib_at(struct cmesh *cm, int attr, int idx)
509 float *vptr = cmesh_attrib(cm, attr);
510 return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
513 const float *cmesh_attrib_at_ro(const struct cmesh *cm, int attr, int idx)
515 const float *vptr = cmesh_attrib_ro(cm, attr);
516 return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
519 int cmesh_attrib_count(const struct cmesh *cm, int attr)
521 return cmesh_has_attrib(cm, attr) ? cm->nverts : 0;
524 int cmesh_attrib_nelem(const struct cmesh *cm, int attr)
526 return cmesh_has_attrib(cm, attr) ? cm->vattr[attr].nelem : 0;
529 int cmesh_push_attrib(struct cmesh *cm, int attr, float *v)
534 if(!cm->vattr[attr].nelem) {
535 cm->vattr[attr].nelem = def_nelem[attr];
538 cursz = cm->vattr[attr].count;
539 newsz = cursz + cm->vattr[attr].nelem;
540 if(!(vptr = realloc(cm->vattr[attr].data, newsz * sizeof(float)))) {
543 cm->vattr[attr].data = vptr;
544 cm->vattr[attr].count = newsz;
547 for(i=0; i<cm->vattr[attr].nelem; i++) {
550 cm->vattr[attr].data_valid = 1;
552 cm->vattr[attr].vbo_valid = 0;
556 gaw_free_compiled(cm->dlist);
561 if(attr == CMESH_ATTR_VERTEX) {
562 cm->nverts = newsz / cm->vattr[attr].nelem;
567 int cmesh_push_attrib1f(struct cmesh *cm, int attr, float x)
573 return cmesh_push_attrib(cm, attr, v);
576 int cmesh_push_attrib2f(struct cmesh *cm, int attr, float x, float y)
583 return cmesh_push_attrib(cm, attr, v);
586 int cmesh_push_attrib3f(struct cmesh *cm, int attr, float x, float y, float z)
593 return cmesh_push_attrib(cm, attr, v);
596 int cmesh_push_attrib4f(struct cmesh *cm, int attr, float x, float y, float z, float w)
603 return cmesh_push_attrib(cm, attr, v);
606 /* indices can be 0, in which case only memory is allocated
607 * returns pointer to the index array
609 unsigned int *cmesh_set_index(struct cmesh *cm, int num, const unsigned int *indices)
612 int nidx = cm->nfaces * 3;
614 if(nidx && num != nidx) {
618 if(!(tmp = malloc(num * sizeof *tmp))) {
622 memcpy(tmp, indices, num * sizeof *tmp);
628 cm->nfaces = num / 3;
635 gaw_free_compiled(cm->dlist);
642 unsigned int *cmesh_index(struct cmesh *cm)
649 gaw_free_compiled(cm->dlist);
653 return (unsigned int*)cmesh_index_ro(cm);
656 const unsigned int *cmesh_index_ro(const struct cmesh *cm)
658 if(!cm->idata_valid) {
659 #if GL_ES_VERSION_2_0 || !defined(USE_VBO)
664 struct cmesh *m = (struct cmesh*)cm;
670 /* local copy is unavailable, grab the data from the ibo */
671 nidx = m->nfaces * 3;
672 if(!(tmp = malloc(nidx * sizeof *m->idata))) {
679 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
680 tmp = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
681 memcpy(m->idata, tmp, nidx * sizeof *m->idata);
682 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
690 int cmesh_index_count(const struct cmesh *cm)
692 return cm->nfaces * 3;
695 int cmesh_push_index(struct cmesh *cm, unsigned int idx)
698 unsigned int cur_sz = cm->icount;
699 if(!(iptr = realloc(cm->idata, (cur_sz + 1) * sizeof *iptr))) {
704 cm->icount = cur_sz + 1;
711 gaw_free_compiled(cm->dlist);
716 cm->nfaces = cm->icount / 3;
720 int cmesh_poly_count(const struct cmesh *cm)
726 return cm->nverts / 3;
731 /* attr can be -1 to invalidate all attributes */
732 void cmesh_invalidate_vbo(struct cmesh *cm, int attr)
737 if(attr >= CMESH_NUM_ATTR) {
742 for(i=0; i<CMESH_NUM_ATTR; i++) {
743 cm->vattr[i].vbo_valid = 0;
746 cm->vattr[attr].vbo_valid = 0;
751 gaw_free_compiled(cm->dlist);
757 void cmesh_invalidate_index(struct cmesh *cm)
764 gaw_free_compiled(cm->dlist);
770 int cmesh_append(struct cmesh *cmdest, const struct cmesh *cmsrc)
772 int i, nelem, newsz, origsz, srcsz;
775 unsigned int idxoffs;
777 if(!cmdest->nverts) {
778 return cmesh_clone(cmdest, cmsrc);
781 for(i=0; i<CMESH_NUM_ATTR; i++) {
782 if(cmesh_has_attrib(cmdest, i) && cmesh_has_attrib(cmsrc, i)) {
783 /* force validation of the data arrays */
784 cmesh_attrib(cmdest, i);
785 cmesh_attrib_ro(cmsrc, i);
787 assert(cmdest->vattr[i].nelem == cmsrc->vattr[i].nelem);
788 nelem = cmdest->vattr[i].nelem;
789 origsz = cmdest->nverts * nelem;
790 newsz = (cmdest->nverts + cmsrc->nverts) * nelem;
792 if(!(vptr = realloc(cmdest->vattr[i].data, newsz * sizeof *vptr))) {
795 memcpy(vptr + origsz, cmsrc->vattr[i].data, cmsrc->nverts * nelem * sizeof(float));
796 cmdest->vattr[i].data = vptr;
797 cmdest->vattr[i].count = newsz;
801 if(cmesh_indexed(cmdest)) {
802 assert(cmesh_indexed(cmsrc));
803 /* force validation ... */
805 cmesh_index_ro(cmsrc);
807 idxoffs = cmdest->nverts;
808 origsz = cmdest->icount;
809 srcsz = cmsrc->icount;
810 newsz = origsz + srcsz;
812 if(!(iptr = realloc(cmdest->idata, newsz * sizeof *iptr))) {
815 cmdest->idata = iptr;
816 cmdest->icount = newsz;
818 /* copy and fixup all the new indices */
820 for(i=0; i<srcsz; i++) {
821 *iptr++ = cmsrc->idata[i] + idxoffs;
825 cmdest->nverts += cmsrc->nverts;
826 cmdest->nfaces += cmsrc->nfaces;
829 cmdest->wire_ibo_valid = 0;
831 cmdest->aabb_valid = 0;
832 cmdest->bsph_valid = 0;
836 void cmesh_clear_submeshes(struct cmesh *cm)
842 cm->sublist = cm->sublist->next;
849 int cmesh_submesh(struct cmesh *cm, const char *name, int fstart, int fcount)
852 unsigned int minv = UINT_MAX, maxv = 0;
856 if(fstart < 0 || fcount < 1 || fstart + fcount > cm->nfaces) {
860 if(!(sm = malloc(sizeof *sm)) || !(sm->name = malloc(strlen(name) + 1))) {
864 strcpy(sm->name, name);
867 if(cmesh_indexed(cm)) {
868 sm->istart = fstart * 3;
869 sm->icount = fcount * 3;
871 /* find out which vertices are used by this submesh */
872 iptr = cm->idata + sm->istart;
873 for(i=0; i<sm->icount; i++) {
874 unsigned int vidx = *iptr++;
875 if(vidx < minv) minv = vidx;
876 if(vidx > maxv) maxv = vidx;
879 sm->vcount = maxv - minv + 1;
881 sm->istart = sm->icount = 0;
882 sm->vstart = fstart * 3;
883 sm->vcount = fcount * 3;
886 sm->next = cm->sublist;
892 int cmesh_remove_submesh(struct cmesh *cm, int idx)
894 struct submesh dummy;
895 struct submesh *prev, *sm;
897 if(idx >= cm->subcount) {
901 dummy.next = cm->sublist;
904 while(prev->next && idx-- > 0) {
908 if(!(sm = prev->next)) return -1;
910 prev->next = sm->next;
915 assert(cm->subcount >= 0);
917 cm->sublist = dummy.next;
921 int cmesh_find_submesh(const struct cmesh *cm, const char *name)
924 struct submesh *sm = cm->sublist;
926 if(strcmp(sm->name, name) == 0) {
927 assert(idx <= cm->subcount);
936 int cmesh_submesh_count(const struct cmesh *cm)
941 static struct submesh *get_submesh(const struct cmesh *m, int idx)
943 struct submesh *sm = m->sublist;
944 while(sm && --idx >= 0) {
950 int cmesh_clone_submesh(struct cmesh *cmdest, const struct cmesh *cm, int subidx)
954 if(!(sub = get_submesh(cm, subidx))) {
957 return clone(cmdest, cm, sub);
961 /* assemble a complete vertex by adding all the useful attributes */
962 int cmesh_vertex(struct cmesh *cm, float x, float y, float z)
966 cgm_wcons(cm->cur_val + CMESH_ATTR_VERTEX, x, y, z, 1.0f);
967 cm->vattr[CMESH_ATTR_VERTEX].data_valid = 1;
968 cm->vattr[CMESH_ATTR_VERTEX].nelem = 3;
970 for(i=0; i<CMESH_NUM_ATTR; i++) {
971 if(cm->vattr[i].nelem > 0) {
972 cmesh_push_attrib(cm, i, &cm->cur_val[i].x);
976 if(cm->idata_valid) {
988 void cmesh_normal(struct cmesh *cm, float nx, float ny, float nz)
990 cgm_wcons(cm->cur_val + CMESH_ATTR_NORMAL, nx, ny, nz, 1.0f);
991 cm->vattr[CMESH_ATTR_NORMAL].nelem = 3;
994 void cmesh_tangent(struct cmesh *cm, float tx, float ty, float tz)
996 cgm_wcons(cm->cur_val + CMESH_ATTR_TANGENT, tx, ty, tz, 1.0f);
997 cm->vattr[CMESH_ATTR_TANGENT].nelem = 3;
1000 void cmesh_texcoord(struct cmesh *cm, float u, float v, float w)
1002 cgm_wcons(cm->cur_val + CMESH_ATTR_TEXCOORD, u, v, w, 1.0f);
1003 cm->vattr[CMESH_ATTR_TEXCOORD].nelem = 3;
1006 void cmesh_boneweights(struct cmesh *cm, float w1, float w2, float w3, float w4)
1008 cgm_wcons(cm->cur_val + CMESH_ATTR_BONEWEIGHTS, w1, w2, w3, w4);
1009 cm->vattr[CMESH_ATTR_BONEWEIGHTS].nelem = 4;
1012 void cmesh_boneidx(struct cmesh *cm, int idx1, int idx2, int idx3, int idx4)
1014 cgm_wcons(cm->cur_val + CMESH_ATTR_BONEIDX, idx1, idx2, idx3, idx4);
1015 cm->vattr[CMESH_ATTR_BONEIDX].nelem = 4;
1018 static float *get_vec4(struct cmesh *cm, int attr, int idx, cgm_vec4 *res)
1022 cgm_wcons(res, 0, 0, 0, 1);
1023 if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
1028 for(i=0; i<cm->vattr[attr].nelem; i++) {
1034 static float *get_vec3(struct cmesh *cm, int attr, int idx, cgm_vec3 *res)
1038 cgm_vcons(res, 0, 0, 0);
1039 if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
1044 for(i=0; i<cm->vattr[attr].nelem; i++) {
1050 /* dir_xform can be null, in which case it's calculated from xform */
1051 void cmesh_apply_xform(struct cmesh *cm, float *xform, float *dir_xform)
1063 for(i=0; i<cm->nverts; i++) {
1064 if(!(vptr = get_vec4(cm, CMESH_ATTR_VERTEX, i, &v))) {
1067 cgm_wmul_m4v4(&v, xform);
1068 for(j=0; j<cm->vattr[CMESH_ATTR_VERTEX].nelem; j++) {
1069 *vptr++ = (&v.x)[j];
1072 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
1073 if((vptr = get_vec3(cm, CMESH_ATTR_NORMAL, i, &n))) {
1074 cgm_vmul_m3v3(&n, dir_xform);
1075 for(j=0; j<cm->vattr[CMESH_ATTR_NORMAL].nelem; j++) {
1076 *vptr++ = (&n.x)[j];
1080 if(cmesh_has_attrib(cm, CMESH_ATTR_TANGENT)) {
1081 if((vptr = get_vec3(cm, CMESH_ATTR_TANGENT, i, &t))) {
1082 cgm_vmul_m3v3(&t, dir_xform);
1083 for(j=0; j<cm->vattr[CMESH_ATTR_TANGENT].nelem; j++) {
1084 *vptr++ = (&t.x)[j];
1091 void cmesh_flip(struct cmesh *cm)
1093 cmesh_flip_faces(cm);
1094 cmesh_flip_normals(cm);
1097 void cmesh_flip_faces(struct cmesh *cm)
1099 int i, j, idxnum, vnum, nelem;
1100 unsigned int *indices;
1101 float *verts, *vptr;
1103 if(cmesh_indexed(cm)) {
1104 if(!(indices = cmesh_index(cm))) {
1107 idxnum = cmesh_index_count(cm);
1108 for(i=0; i<idxnum; i+=3) {
1109 unsigned int tmp = indices[i + 2];
1110 indices[i + 2] = indices[i + 1];
1111 indices[i + 1] = tmp;
1114 if(!(verts = cmesh_attrib(cm, CMESH_ATTR_VERTEX))) {
1117 vnum = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
1118 nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1119 for(i=0; i<vnum; i+=3) {
1120 for(j=0; j<nelem; j++) {
1122 vptr = verts + (i + 1) * nelem + j;
1124 vptr[nelem] = vptr[0];
1130 void cmesh_flip_normals(struct cmesh *cm)
1133 float *nptr = cmesh_attrib(cm, CMESH_ATTR_NORMAL);
1136 num = cm->nverts * cm->vattr[CMESH_ATTR_NORMAL].nelem;
1137 for(i=0; i<num; i++) {
1143 int cmesh_explode(struct cmesh *cm)
1145 int i, j, k, idxnum, nnverts;
1146 unsigned int *indices;
1148 if(!cmesh_indexed(cm)) return 0;
1150 indices = cmesh_index(cm);
1153 idxnum = cmesh_index_count(cm);
1156 for(i=0; i<CMESH_NUM_ATTR; i++) {
1157 const float *srcbuf;
1158 float *tmpbuf, *dstptr;
1160 if(!cmesh_has_attrib(cm, i)) continue;
1162 srcbuf = cmesh_attrib(cm, i);
1163 if(!(tmpbuf = malloc(nnverts * cm->vattr[i].nelem * sizeof(float)))) {
1168 for(j=0; j<idxnum; j++) {
1169 unsigned int idx = indices[j];
1170 const float *srcptr = srcbuf + idx * cm->vattr[i].nelem;
1172 for(k=0; k<cm->vattr[i].nelem; k++) {
1173 *dstptr++ = *srcptr++;
1177 free(cm->vattr[i].data);
1178 cm->vattr[i].data = tmpbuf;
1179 cm->vattr[i].count = nnverts * cm->vattr[i].nelem;
1180 cm->vattr[i].data_valid = 1;
1188 gaw_free_compiled(cm->dlist);
1192 cm->idata_valid = 0;
1197 cm->nverts = nnverts;
1198 cm->nfaces = idxnum / 3;
1202 void cmesh_calc_face_normals(struct cmesh *cm)
1207 static int pre_draw(const struct cmesh *cm, int start)
1213 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1218 update_buffers((struct cmesh*)cm);
1221 if(!cm->vattr[CMESH_ATTR_VERTEX].vbo_valid) {
1226 if(cur_sdr && use_custom_sdr_attr) {
1227 if(sdr_loc[CMESH_ATTR_VERTEX] == -1) {
1231 for(i=0; i<CMESH_NUM_ATTR; i++) {
1233 if(loc >= 0 && cm->vattr[i].vbo_valid) {
1234 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
1235 glVertexAttribPointer(loc, cm->vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
1236 glEnableVertexAttribArray(loc);
1240 #endif /* USE_SDR */
1241 #ifndef GL_ES_VERSION_2_0
1242 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_VERTEX].vbo);
1243 glVertexPointer(cm->vattr[CMESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
1244 glEnableClientState(GL_VERTEX_ARRAY);
1246 if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
1247 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_NORMAL].vbo);
1248 glNormalPointer(GL_FLOAT, 0, 0);
1249 glEnableClientState(GL_NORMAL_ARRAY);
1251 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
1252 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD].vbo);
1253 glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
1254 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1256 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
1257 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_COLOR].vbo);
1258 glColorPointer(cm->vattr[CMESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
1259 glEnableClientState(GL_COLOR_ARRAY);
1261 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
1262 glClientActiveTexture(GL_TEXTURE1);
1263 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD2].vbo);
1264 glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD2].nelem, GL_FLOAT, 0, 0);
1265 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1266 glClientActiveTexture(GL_TEXTURE0);
1268 #endif /* GL_ES_VERSION_2_0 */
1272 glBindBuffer(GL_ARRAY_BUFFER, 0);
1274 #else /* !USE_VBO */
1277 if(cm->dlist && !start) {
1282 const struct cmesh_vattrib *vattr;
1284 vattr = cm->vattr + CMESH_ATTR_VERTEX;
1285 gaw_vertex_array(vattr->nelem, 0, vattr->data + start * vattr->nelem);
1287 vattr = cm->vattr + CMESH_ATTR_NORMAL;
1288 if(vattr->data_valid) {
1289 gaw_normal_array(0, vattr->data + start * 3);
1292 vattr = cm->vattr + CMESH_ATTR_TEXCOORD;
1293 if(vattr->data_valid) {
1294 gaw_texcoord_array(vattr->nelem, 0, vattr->data + start * vattr->nelem);
1297 vattr = cm->vattr + CMESH_ATTR_COLOR;
1298 if(vattr->data_valid) {
1299 gaw_color_array(vattr->nelem, 0, vattr->data + start * vattr->nelem);
1302 #endif /* !USE_VBO */
1306 void cmesh_draw(const struct cmesh *cm)
1310 if((cur_sdr = pre_draw(cm, 0)) == -1) {
1316 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1317 glDrawElements(GL_TRIANGLES, cm->nfaces * 3, GL_UNSIGNED_INT, 0);
1318 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1320 glDrawArrays(GL_TRIANGLES, 0, cm->nverts);
1325 gaw_draw_compiled(cm->dlist);
1328 if(cm->idata_valid) {
1329 /*glDrawElements(GL_TRIANGLES, cm->nfaces * 3, GL_UNSIGNED_INT, cm->idata);*/
1330 gaw_draw_indexed(GAW_TRIANGLES, cm->idata, cm->nfaces * 3);
1332 /*glDrawArrays(GL_TRIANGLES, 0, cm->nverts);*/
1333 gaw_draw(GAW_TRIANGLES, cm->nverts);
1337 post_draw(cm, cur_sdr);
1340 void cmesh_draw_range(const struct cmesh *cm, int start, int count)
1344 if((cur_sdr = pre_draw(cm, start)) == -1) {
1350 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1351 glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (void*)(intptr_t)(start * 4));
1352 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1354 glDrawArrays(GL_TRIANGLES, start, count);
1357 if(cm->idata_valid) {
1358 gaw_draw_indexed(GAW_TRIANGLES, cm->idata + start, count);
1360 gaw_draw(GAW_TRIANGLES, count);
1364 post_draw(cm, cur_sdr);
1367 void cmesh_draw_submesh(const struct cmesh *cm, int subidx)
1369 struct submesh *sm = cm->sublist;
1371 while(sm && subidx-- > 0) {
1377 cmesh_draw_range(cm, sm->istart, sm->icount);
1379 cmesh_draw_range(cm, sm->vstart, sm->vcount);
1383 static void post_draw(const struct cmesh *cm, int cur_sdr)
1387 if(cur_sdr && use_custom_sdr_attr) {
1389 for(i=0; i<CMESH_NUM_ATTR; i++) {
1390 int loc = sdr_loc[i];
1391 if(loc >= 0 && cm->vattr[i].vbo_valid) {
1392 glDisableVertexAttribArray(loc);
1396 #endif /* USE_SDR */
1397 #ifndef GL_ES_VERSION_2_0
1398 glDisableClientState(GL_VERTEX_ARRAY);
1399 if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
1400 glDisableClientState(GL_NORMAL_ARRAY);
1402 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
1403 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1405 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
1406 glDisableClientState(GL_COLOR_ARRAY);
1408 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
1409 glClientActiveTexture(GL_TEXTURE1);
1410 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1411 glClientActiveTexture(GL_TEXTURE0);
1413 #endif /* GL_ES_VERSION_2_0 */
1417 #else /* !USE_VBO */
1418 gaw_vertex_array(0, 0, 0);
1419 gaw_normal_array(0, 0);
1420 gaw_texcoord_array(0, 0, 0);
1421 gaw_color_array(0, 0, 0);
1425 void cmesh_draw_wire(const struct cmesh *cm, float linesz)
1428 int cur_sdr, nfaces;
1430 if((cur_sdr = pre_draw(cm, 0)) == -1) {
1433 update_wire_ibo((struct cmesh*)cm);
1435 nfaces = cmesh_poly_count(cm);
1436 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
1437 glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0);
1438 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1440 post_draw(cm, cur_sdr);
1444 void cmesh_draw_vertices(const struct cmesh *cm, float ptsz)
1447 if((cur_sdr = pre_draw(cm, 0)) == -1) {
1452 glPushAttrib(GL_POINT_BIT);
1454 glDrawArrays(GL_POINTS, 0, cm->nverts);
1457 gaw_draw(GAW_POINTS, cm->nverts);
1459 post_draw(cm, cur_sdr);
1462 void cmesh_draw_normals(const struct cmesh *cm, float len)
1464 #ifndef GL_ES_VERSION_2_0
1465 int i, vert_nelem, norm_nelem;
1467 int cur_sdr, loc = -1;
1469 const float *varr, *norm;
1471 varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1472 norm = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
1473 if(!varr || !norm) return;
1475 vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1476 norm_nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1479 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1480 if(cur_sdr && use_custom_sdr_attr) {
1481 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1487 gaw_begin(GAW_LINES);
1488 for(i=0; i<cm->nverts; i++) {
1489 float x, y, z, endx, endy, endz;
1491 x = varr[i * vert_nelem];
1492 y = varr[i * vert_nelem + 1];
1493 z = varr[i * vert_nelem + 2];
1494 endx = x + norm[i * norm_nelem] * len;
1495 endy = y + norm[i * norm_nelem + 1] * len;
1496 endz = z + norm[i * norm_nelem + 2] * len;
1501 gaw_vertex3f(x, y, z);
1502 gaw_vertex3f(endx, endy, endz);
1505 glVertexAttrib3f(loc, x, y, z);
1506 glVertexAttrib3f(loc, endx, endy, endz);
1511 #endif /* GL_ES_VERSION_2_0 */
1514 void cmesh_draw_tangents(const struct cmesh *cm, float len)
1516 #ifndef GL_ES_VERSION_2_0
1517 int i, vert_nelem, tang_nelem;
1519 int cur_sdr, loc = -1;
1521 const float *varr, *tang;
1523 varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1524 tang = cmesh_attrib_ro(cm, CMESH_ATTR_TANGENT);
1525 if(!varr || !tang) return;
1527 vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1528 tang_nelem = cm->vattr[CMESH_ATTR_TANGENT].nelem;
1531 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1532 if(cur_sdr && use_custom_sdr_attr) {
1533 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1539 gaw_begin(GAW_LINES);
1540 for(i=0; i<cm->nverts; i++) {
1541 float x, y, z, endx, endy, endz;
1543 x = varr[i * vert_nelem];
1544 y = varr[i * vert_nelem + 1];
1545 z = varr[i * vert_nelem + 2];
1546 endx = x + tang[i * tang_nelem] * len;
1547 endy = y + tang[i * tang_nelem + 1] * len;
1548 endz = z + tang[i * tang_nelem + 2] * len;
1553 gaw_vertex3f(x, y, z);
1554 gaw_vertex3f(endx, endy, endz);
1557 glVertexAttrib3f(loc, x, y, z);
1558 glVertexAttrib3f(loc, endx, endy, endz);
1563 #endif /* GL_ES_VERSION_2_0 */
1566 static void update_buffers(struct cmesh *cm)
1570 for(i=0; i<CMESH_NUM_ATTR; i++) {
1571 if(cmesh_has_attrib(cm, i) && !cm->vattr[i].vbo_valid) {
1572 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
1573 glBufferData(GL_ARRAY_BUFFER, cm->nverts * cm->vattr[i].nelem * sizeof(float),
1574 cm->vattr[i].data, GL_STATIC_DRAW);
1575 cm->vattr[i].vbo_valid = 1;
1578 glBindBuffer(GL_ARRAY_BUFFER, 0);
1580 if(cm->idata_valid && !cm->ibo_valid) {
1581 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1582 glBufferData(GL_ELEMENT_ARRAY_BUFFER, cm->nfaces * 3 * sizeof(unsigned int),
1583 cm->idata, GL_STATIC_DRAW);
1585 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1588 #elif defined(USE_DLIST)
1589 static int updating;
1591 if(!cm->dlist && !updating) {
1592 int dlist = gaw_compile_begin();
1603 static void update_wire_ibo(struct cmesh *cm)
1606 unsigned int *wire_idxarr, *dest;
1610 if(cm->wire_ibo_valid) return;
1613 glGenBuffers(1, &cm->wire_ibo);
1615 num_faces = cmesh_poly_count(cm);
1617 if(!(wire_idxarr = malloc(num_faces * 6 * sizeof *wire_idxarr))) {
1623 /* we're dealing with an indexed mesh */
1624 const unsigned int *idxarr = cmesh_index_ro(cm);
1626 for(i=0; i<num_faces; i++) {
1627 *dest++ = idxarr[0];
1628 *dest++ = idxarr[1];
1629 *dest++ = idxarr[1];
1630 *dest++ = idxarr[2];
1631 *dest++ = idxarr[2];
1632 *dest++ = idxarr[0];
1636 /* not an indexed mesh */
1637 for(i=0; i<num_faces; i++) {
1648 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
1649 glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_faces * 6 * sizeof(unsigned int),
1650 wire_idxarr, GL_STATIC_DRAW);
1652 cm->wire_ibo_valid = 1;
1653 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1655 #endif /* USE_VBO */
1657 static void calc_aabb(struct cmesh *cm)
1661 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1665 cgm_vcons(&cm->aabb_min, FLT_MAX, FLT_MAX, FLT_MAX);
1666 cgm_vcons(&cm->aabb_max, -FLT_MAX, -FLT_MAX, -FLT_MAX);
1668 for(i=0; i<cm->nverts; i++) {
1669 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1670 for(j=0; j<3; j++) {
1671 if(v[j] < (&cm->aabb_min.x)[j]) {
1672 (&cm->aabb_min.x)[j] = v[j];
1674 if(v[j] > (&cm->aabb_max.x)[j]) {
1675 (&cm->aabb_max.x)[j] = v[j];
1682 void cmesh_aabbox(const struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax)
1684 if(!cm->aabb_valid) {
1685 calc_aabb((struct cmesh*)cm);
1687 *vmin = cm->aabb_min;
1688 *vmax = cm->aabb_max;
1691 static void calc_bsph(struct cmesh *cm)
1696 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1700 cgm_vcons(&cm->bsph_center, 0, 0, 0);
1702 /* first find the center */
1703 for(i=0; i<cm->nverts; i++) {
1704 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1705 cm->bsph_center.x += v[0];
1706 cm->bsph_center.y += v[1];
1707 cm->bsph_center.z += v[2];
1709 s = 1.0f / (float)cm->nverts;
1710 cm->bsph_center.x *= s;
1711 cm->bsph_center.y *= s;
1712 cm->bsph_center.z *= s;
1714 cm->bsph_radius = 0.0f;
1715 for(i=0; i<cm->nverts; i++) {
1716 const cgm_vec3 *v = (const cgm_vec3*)cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1717 if((dist_sq = cgm_vdist_sq(v, &cm->bsph_center)) > cm->bsph_radius) {
1718 cm->bsph_radius = dist_sq;
1721 cm->bsph_radius = sqrt(cm->bsph_radius);
1725 float cmesh_bsphere(const struct cmesh *cm, cgm_vec3 *center, float *rad)
1727 if(!cm->bsph_valid) {
1728 calc_bsph((struct cmesh*)cm);
1730 if(center) *center = cm->bsph_center;
1731 if(rad) *rad = cm->bsph_radius;
1732 return cm->bsph_radius;
1736 void cmesh_texcoord_apply_xform(struct cmesh *cm, float *xform);
1737 void cmesh_texcoord_gen_plane(struct cmesh *cm, cgm_vec3 *norm, cgm_vec3 *tang);
1738 void cmesh_texcoord_gen_box(struct cmesh *cm);
1739 void cmesh_texcoord_gen_cylinder(struct cmesh *cm);
1741 int cmesh_dump(const struct cmesh *cm, const char *fname)
1743 FILE *fp = fopen(fname, "wb");
1745 int res = cmesh_dump_file(cm, fp);
1752 int cmesh_dump_file(const struct cmesh *cm, FILE *fp)
1754 static const char *label[] = { "pos", "nor", "tan", "tex", "col", "bw", "bid", "tex2" };
1755 static const char *elemfmt[] = { 0, " %s(%g)", " %s(%g, %g)", " %s(%g, %g, %g)", " %s(%g, %g, %g, %g)", 0 };
1758 if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1762 fprintf(fp, "VERTEX ATTRIBUTES\n");
1764 for(i=0; i<cm->nverts; i++) {
1765 fprintf(fp, "%5u:", i);
1766 for(j=0; j<CMESH_NUM_ATTR; j++) {
1767 if(cmesh_has_attrib(cm, j)) {
1768 const float *v = cmesh_attrib_at_ro(cm, j, i);
1769 int nelem = cm->vattr[j].nelem;
1770 fprintf(fp, elemfmt[nelem], label[j], v[0], nelem > 1 ? v[1] : 0.0f,
1771 nelem > 2 ? v[2] : 0.0f, nelem > 3 ? v[3] : 0.0f);
1777 if(cmesh_indexed(cm)) {
1778 const unsigned int *idx = cmesh_index_ro(cm);
1779 int numidx = cmesh_index_count(cm);
1780 int numtri = numidx / 3;
1781 assert(numidx % 3 == 0);
1783 fprintf(fp, "FACES\n");
1785 for(i=0; i<numtri; i++) {
1786 fprintf(fp, "%5d: %d %d %d\n", i, idx[0], idx[1], idx[2]);
1793 int cmesh_dump_obj(const struct cmesh *cm, const char *fname)
1795 FILE *fp = fopen(fname, "wb");
1797 int res = cmesh_dump_obj_file(cm, fp, 0);
1807 int cmesh_dump_obj_file(const struct cmesh *cm, FILE *fp, int voffs)
1809 static const char *fmtstr[] = {" %u", " %u//%u", " %u/%u", " %u/%u/%u"};
1810 int i, j, num, nelem;
1811 unsigned int aflags = 0;
1813 if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1818 nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1819 if((num = cm->vattr[CMESH_ATTR_VERTEX].count) != cm->nverts * nelem) {
1820 fprintf(stderr, "vertex array size (%d) != nverts (%d)\n", num, cm->nverts);
1822 for(i=0; i<cm->nverts; i++) {
1823 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1824 fprintf(fp, "v %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
1827 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
1829 nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1830 if((num = cm->vattr[CMESH_ATTR_NORMAL].count) != cm->nverts * nelem) {
1831 fprintf(stderr, "normal array size (%d) != nverts (%d)\n", num, cm->nverts);
1833 for(i=0; i<cm->nverts; i++) {
1834 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_NORMAL, i);
1835 fprintf(fp, "vn %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
1839 if(cmesh_has_attrib(cm, CMESH_ATTR_TEXCOORD)) {
1841 nelem = cm->vattr[CMESH_ATTR_TEXCOORD].nelem;
1842 if((num = cm->vattr[CMESH_ATTR_TEXCOORD].count) != cm->nverts * nelem) {
1843 fprintf(stderr, "texcoord array size (%d) != nverts (%d)\n", num, cm->nverts);
1845 for(i=0; i<cm->nverts; i++) {
1846 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_TEXCOORD, i);
1847 fprintf(fp, "vt %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f);
1851 if(cmesh_indexed(cm)) {
1852 const unsigned int *idxptr = cmesh_index_ro(cm);
1853 int numidx = cmesh_index_count(cm);
1854 int numtri = numidx / 3;
1855 assert(numidx % 3 == 0);
1857 for(i=0; i<numtri; i++) {
1859 for(j=0; j<3; j++) {
1860 unsigned int idx = *idxptr++ + 1 + voffs;
1861 fprintf(fp, fmtstr[aflags], idx, idx, idx);
1866 int numtri = cm->nverts / 3;
1867 unsigned int idx = 1 + voffs;
1868 for(i=0; i<numtri; i++) {
1870 for(j=0; j<3; j++) {
1871 fprintf(fp, fmtstr[aflags], idx, idx, idx);