10 struct cmesh_vattrib {
11 int nelem; /* num elements per attribute [1, 4] */
15 int vbo_valid, data_valid;
18 /* istart,icount are valid only when the mesh is indexed, otherwise icount is 0.
19 * vstart,vcount are define the submesh for non-indexed meshes.
20 * For indexed meshes, vstart,vcount denote the range of vertices used by each
25 int nfaces; /* derived from either icount or vcount */
33 unsigned int nverts, nfaces;
35 struct submesh *sublist;
38 /* current value for each attribute for the immediate mode interface */
39 cgm_vec4 cur_val[CMESH_NUM_ATTR];
41 unsigned int vbuf[CMESH_NUM_ATTR + 1];
42 struct cmesh_vattrib vattr[CMESH_NUM_ATTR];
47 int ibo_valid, idata_valid;
49 /* index buffer for wireframe rendering (constructed on demand) */
50 unsigned int wire_ibo;
53 /* axis-aligned bounding box */
54 cgm_vec3 aabb_min, aabb_max;
63 static int clone(struct cmesh *cmdest, struct cmesh *cmsrc, struct submesh *sub);
64 static int pre_draw(struct cmesh *cm);
65 static void post_draw(struct cmesh *cm, int cur_sdr);
66 static void update_buffers(struct cmesh *cm);
67 static void update_wire_ibo(struct cmesh *cm);
68 static void calc_aabb(struct cmesh *cm);
69 static void calc_bsph(struct cmesh *cm);
71 static int def_nelem[CMESH_NUM_ATTR] = {3, 3, 3, 2, 4, 4, 4, 2};
73 static int sdr_loc[CMESH_NUM_ATTR] = {0, 1, 2, 3, 4, 5, 6, 7};
74 static int use_custom_sdr_attr;
78 void cmesh_set_attrib_sdrloc(int attr, int loc)
83 int cmesh_get_attrib_sdrloc(int attr)
88 void cmesh_clear_attrib_sdrloc(void)
91 for(i=0; i<CMESH_NUM_ATTR; i++) {
97 struct cmesh *cmesh_alloc(void)
101 if(!(cm = malloc(sizeof *cm))) {
104 if(cmesh_init(cm) == -1) {
111 void cmesh_free(struct cmesh *cm)
117 int cmesh_init(struct cmesh *cm)
121 memset(cm, 0, sizeof *cm);
122 cgm_wcons(cm->cur_val + CMESH_ATTR_COLOR, 1, 1, 1, 1);
124 glGenBuffers(CMESH_NUM_ATTR + 1, cm->vbuf);
126 for(i=0; i<CMESH_NUM_ATTR; i++) {
127 cm->vattr[i].vbo = cm->vbuf[i];
130 cm->ibo = cm->vbuf[CMESH_NUM_ATTR];
134 void cmesh_destroy(struct cmesh *cm)
140 for(i=0; i<CMESH_NUM_ATTR; i++) {
141 free(cm->vattr[i].data);
145 cmesh_clear_submeshes(cm);
147 glDeleteBuffers(CMESH_NUM_ATTR + 1, cm->vbuf);
149 glDeleteBuffers(1, &cm->wire_ibo);
153 void cmesh_clear(struct cmesh *cm)
157 for(i=0; i<CMESH_NUM_ATTR; i++) {
158 cm->vattr[i].nelem = 0;
159 cm->vattr[i].vbo_valid = 0;
160 cm->vattr[i].data_valid = 0;
161 free(cm->vattr[i].data);
162 cm->vattr[i].data = 0;
163 cm->vattr[i].count = 0;
165 cm->ibo_valid = cm->idata_valid = 0;
170 cm->wire_ibo_valid = 0;
171 cm->nverts = cm->nfaces = 0;
173 cm->bsph_valid = cm->aabb_valid = 0;
175 cmesh_clear_submeshes(cm);
178 int cmesh_clone(struct cmesh *cmdest, struct cmesh *cmsrc)
180 return clone(cmdest, cmsrc, 0);
183 static int clone(struct cmesh *cmdest, struct cmesh *cmsrc, struct submesh *sub)
185 int i, nelem, vstart, vcount, istart, icount;
186 char *srcname, *name = 0;
187 float *varr[CMESH_NUM_ATTR] = {0};
189 unsigned int *iptr, *iarr = 0;
191 /* try do anything that can fail first, before making any changes to cmdest
192 * so we have the option of recovering gracefuly
195 srcname = sub ? sub->name : cmsrc->name;
197 if(!(name = malloc(strlen(srcname) + 1))) {
200 strcpy(name, srcname);
204 vstart = sub->vstart;
205 vcount = sub->vcount;
206 istart = sub->istart;
207 icount = sub->icount;
210 vcount = cmsrc->nverts;
211 icount = cmsrc->icount;
214 if(cmesh_indexed(cmsrc)) {
215 if(!(iarr = malloc(icount * sizeof *iarr))) {
221 for(i=0; i<CMESH_NUM_ATTR; i++) {
222 if(cmesh_has_attrib(cmsrc, i)) {
223 nelem = cmsrc->vattr[i].nelem;
224 if(!(varr[i] = malloc(vcount * nelem * sizeof(float)))) {
235 /* from this point forward nothing can fail */
238 for(i=0; i<CMESH_NUM_ATTR; i++) {
239 free(cmdest->vattr[i].data);
241 if(cmesh_has_attrib(cmsrc, i)) {
242 cmesh_attrib(cmsrc, i); /* force validation of the actual data on the source mesh */
244 nelem = cmsrc->vattr[i].nelem;
245 cmdest->vattr[i].nelem = nelem;
246 cmdest->vattr[i].data = varr[i];
247 cmdest->vattr[i].count = vcount;
248 vptr = cmsrc->vattr[i].data + vstart * nelem;
249 memcpy(cmdest->vattr[i].data, vptr, vcount * nelem * sizeof(float));
250 cmdest->vattr[i].data_valid = 1;
251 cmdest->vattr[i].vbo_valid = 0;
253 memset(cmdest->vattr + i, 0, sizeof cmdest->vattr[i]);
257 if(cmesh_indexed(cmsrc)) {
258 cmesh_index(cmsrc); /* force validation .... */
260 cmdest->idata = iarr;
261 cmdest->icount = icount;
263 /* need to offset all vertex indices by -vstart */
264 iptr = cmsrc->idata + istart;
265 for(i=0; i<icount; i++) {
266 cmdest->idata[i] = *iptr++ - vstart;
269 memcpy(cmdest->idata, cmsrc->idata + istart, icount * sizeof *cmdest->idata);
271 cmdest->idata_valid = 1;
274 cmdest->idata_valid = cmdest->ibo_valid = 0;
280 cmdest->nverts = cmsrc->nverts;
281 cmdest->nfaces = sub ? sub->nfaces : cmsrc->nfaces;
283 memcpy(cmdest->cur_val, cmsrc->cur_val, sizeof cmdest->cur_val);
285 cmdest->aabb_min = cmsrc->aabb_min;
286 cmdest->aabb_max = cmsrc->aabb_max;
287 cmdest->aabb_valid = cmsrc->aabb_valid;
288 cmdest->bsph_center = cmsrc->bsph_center;
289 cmdest->bsph_radius = cmsrc->bsph_radius;
290 cmdest->bsph_valid = cmsrc->bsph_valid;
292 /* copy sublist only if we're not cloning a submesh */
294 struct submesh *sm, *n, *head = 0, *tail = 0;
298 if(!(n = malloc(sizeof *n)) || !(name = malloc(strlen(sm->name) + 1))) {
303 strcpy(name, sm->name);
318 cmdest->sublist = head;
319 cmdest->subcount = cmsrc->subcount;
325 int cmesh_set_name(struct cmesh *cm, const char *name)
327 int len = strlen(name);
328 char *tmp = malloc(len + 1);
332 memcpy(cm->name, name, len + 1);
336 const char *cmesh_name(struct cmesh *cm)
341 int cmesh_has_attrib(struct cmesh *cm, int attr)
343 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
346 return cm->vattr[attr].vbo_valid | cm->vattr[attr].data_valid;
349 int cmesh_indexed(struct cmesh *cm)
351 return cm->ibo_valid | cm->idata_valid;
354 /* vdata can be 0, in which case only memory is allocated
355 * returns pointer to the attribute array
357 float *cmesh_set_attrib(struct cmesh *cm, int attr, int nelem, unsigned int num,
362 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
365 if(cm->nverts && num != cm->nverts) {
369 if(!(newarr = malloc(num * nelem * sizeof *newarr))) {
373 memcpy(newarr, vdata, num * nelem * sizeof *newarr);
378 free(cm->vattr[attr].data);
379 cm->vattr[attr].data = newarr;
380 cm->vattr[attr].count = num * nelem;
381 cm->vattr[attr].nelem = nelem;
382 cm->vattr[attr].data_valid = 1;
383 cm->vattr[attr].vbo_valid = 0;
387 float *cmesh_attrib(struct cmesh *cm, int attr)
389 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
392 cm->vattr[attr].vbo_valid = 0;
393 return (float*)cmesh_attrib_ro(cm, attr);
396 const float *cmesh_attrib_ro(struct cmesh *cm, int attr)
401 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
405 if(!cm->vattr[attr].data_valid) {
406 #if GL_ES_VERSION_2_0
409 if(!cm->vattr[attr].vbo_valid) {
413 /* local data copy unavailable, grab the data from the vbo */
414 nelem = cm->vattr[attr].nelem;
415 if(!(cm->vattr[attr].data = malloc(cm->nverts * nelem * sizeof(float)))) {
418 cm->vattr[attr].count = cm->nverts * nelem;
420 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[attr].vbo);
421 tmp = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
422 memcpy(cm->vattr[attr].data, tmp, cm->nverts * nelem * sizeof(float));
423 glUnmapBuffer(GL_ARRAY_BUFFER);
425 cm->vattr[attr].data_valid = 1;
428 return cm->vattr[attr].data;
431 float *cmesh_attrib_at(struct cmesh *cm, int attr, int idx)
433 float *vptr = cmesh_attrib(cm, attr);
434 return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
437 const float *cmesh_attrib_at_ro(struct cmesh *cm, int attr, int idx)
439 const float *vptr = cmesh_attrib_ro(cm, attr);
440 return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
443 int cmesh_attrib_count(struct cmesh *cm, int attr)
445 return cmesh_has_attrib(cm, attr) ? cm->nverts : 0;
448 int cmesh_push_attrib(struct cmesh *cm, int attr, float *v)
453 if(!cm->vattr[attr].nelem) {
454 cm->vattr[attr].nelem = def_nelem[attr];
457 cursz = cm->vattr[attr].count;
458 newsz = cursz + cm->vattr[attr].nelem;
459 if(!(vptr = realloc(cm->vattr[attr].data, newsz * sizeof(float)))) {
462 cm->vattr[attr].data = vptr;
463 cm->vattr[attr].count = newsz;
466 for(i=0; i<cm->vattr[attr].nelem; i++) {
469 cm->vattr[attr].data_valid = 1;
470 cm->vattr[attr].vbo_valid = 0;
472 if(attr == CMESH_ATTR_VERTEX) {
473 cm->nverts = newsz / cm->vattr[attr].nelem;
478 int cmesh_push_attrib1f(struct cmesh *cm, int attr, float x)
484 return cmesh_push_attrib(cm, attr, v);
487 int cmesh_push_attrib2f(struct cmesh *cm, int attr, float x, float y)
494 return cmesh_push_attrib(cm, attr, v);
497 int cmesh_push_attrib3f(struct cmesh *cm, int attr, float x, float y, float z)
504 return cmesh_push_attrib(cm, attr, v);
507 int cmesh_push_attrib4f(struct cmesh *cm, int attr, float x, float y, float z, float w)
514 return cmesh_push_attrib(cm, attr, v);
517 /* indices can be 0, in which case only memory is allocated
518 * returns pointer to the index array
520 unsigned int *cmesh_set_index(struct cmesh *cm, int num, const unsigned int *indices)
523 int nidx = cm->nfaces * 3;
525 if(nidx && num != nidx) {
529 if(!(tmp = malloc(num * sizeof *tmp))) {
533 memcpy(tmp, indices, num * sizeof *tmp);
544 unsigned int *cmesh_index(struct cmesh *cm)
547 return (unsigned int*)cmesh_index_ro(cm);
550 const unsigned int *cmesh_index_ro(struct cmesh *cm)
555 if(!cm->idata_valid) {
556 #if GL_ES_VERSION_2_0
563 /* local copy is unavailable, grab the data from the ibo */
564 nidx = cm->nfaces * 3;
565 if(!(tmp = malloc(nidx * sizeof *cm->idata))) {
572 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
573 tmp = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
574 memcpy(cm->idata, tmp, nidx * sizeof *cm->idata);
575 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
583 int cmesh_index_count(struct cmesh *cm)
585 return cm->nfaces * 3;
588 int cmesh_push_index(struct cmesh *cm, unsigned int idx)
591 unsigned int cur_sz = cm->icount;
592 if(!(iptr = realloc(cm->idata, (cur_sz + 1) * sizeof *iptr))) {
597 cm->icount = cur_sz + 1;
601 cm->nfaces = cm->icount / 3;
605 int cmesh_poly_count(struct cmesh *cm)
611 return cm->nverts / 3;
616 /* attr can be -1 to invalidate all attributes */
617 void cmesh_invalidate_vbo(struct cmesh *cm, int attr)
621 if(attr >= CMESH_NUM_ATTR) {
626 for(i=0; i<CMESH_NUM_ATTR; i++) {
627 cm->vattr[i].vbo_valid = 0;
630 cm->vattr[attr].vbo_valid = 0;
634 void cmesh_invalidate_index(struct cmesh *cm)
639 int cmesh_append(struct cmesh *cmdest, struct cmesh *cmsrc)
641 int i, nelem, newsz, origsz, srcsz;
644 unsigned int idxoffs;
646 if(!cmdest->nverts) {
647 return cmesh_clone(cmdest, cmsrc);
650 for(i=0; i<CMESH_NUM_ATTR; i++) {
651 if(cmesh_has_attrib(cmdest, i) && cmesh_has_attrib(cmsrc, i)) {
652 /* force validation of the data arrays */
653 cmesh_attrib(cmdest, i);
654 cmesh_attrib_ro(cmsrc, i);
656 assert(cmdest->vattr[i].nelem == cmsrc->vattr[i].nelem);
657 nelem = cmdest->vattr[i].nelem;
658 origsz = cmdest->nverts * nelem;
659 newsz = cmdest->nverts + cmsrc->nverts * nelem;
661 if(!(vptr = realloc(cmdest->vattr[i].data, newsz * sizeof *vptr))) {
664 memcpy(vptr + origsz, cmsrc->vattr[i].data, cmsrc->nverts * nelem * sizeof(float));
665 cmdest->vattr[i].data = vptr;
666 cmdest->vattr[i].count = newsz;
670 if(cmesh_indexed(cmdest)) {
671 assert(cmesh_indexed(cmsrc));
672 /* force validation ... */
674 cmesh_index_ro(cmsrc);
676 idxoffs = cmdest->nverts;
677 origsz = cmdest->icount;
678 srcsz = cmsrc->icount;
679 newsz = origsz + srcsz;
681 if(!(iptr = realloc(cmdest->idata, newsz * sizeof *iptr))) {
684 cmdest->idata = iptr;
685 cmdest->icount = newsz;
687 /* copy and fixup all the new indices */
689 for(i=0; i<srcsz; i++) {
690 *iptr++ = cmsrc->idata[i] + idxoffs;
694 cmdest->wire_ibo_valid = 0;
695 cmdest->aabb_valid = 0;
696 cmdest->bsph_valid = 0;
700 void cmesh_clear_submeshes(struct cmesh *cm)
706 cm->sublist = cm->sublist->next;
713 int cmesh_submesh(struct cmesh *cm, const char *name, int fstart, int fcount)
716 unsigned int minv = UINT_MAX, maxv = 0;
720 if(fstart < 0 || fcount < 1 || fstart + fcount > cm->nfaces) {
724 if(!(sm = malloc(sizeof *sm)) || !(sm->name = malloc(strlen(name) + 1))) {
728 strcpy(sm->name, name);
731 if(cmesh_indexed(cm)) {
732 sm->istart = fstart * 3;
733 sm->icount = fcount * 3;
735 /* find out which vertices are used by this submesh */
736 iptr = cm->idata + sm->istart;
737 for(i=0; i<sm->icount; i++) {
738 unsigned int vidx = *iptr++;
739 if(vidx < minv) minv = vidx;
740 if(vidx > maxv) maxv = vidx;
743 sm->vcount = maxv - minv + 1;
745 sm->istart = sm->icount = 0;
746 sm->vstart = fstart * 3;
747 sm->vcount = fcount * 3;
750 sm->next = cm->sublist;
756 int cmesh_remove_submesh(struct cmesh *cm, int idx)
758 struct submesh dummy;
759 struct submesh *prev, *sm;
761 if(idx >= cm->subcount) {
765 dummy.next = cm->sublist;
768 while(prev->next && idx-- > 0) {
772 if(!(sm = prev->next)) return -1;
774 prev->next = sm->next;
779 assert(cm->subcount >= 0);
781 cm->sublist = dummy.next;
785 int cmesh_find_submesh(struct cmesh *cm, const char *name)
788 struct submesh *sm = cm->sublist;
790 if(strcmp(sm->name, name) == 0) {
791 assert(idx <= cm->subcount);
800 int cmesh_submesh_count(struct cmesh *cm)
805 static struct submesh *get_submesh(struct cmesh *m, int idx)
807 struct submesh *sm = m->sublist;
808 while(sm && --idx >= 0) {
814 int cmesh_clone_submesh(struct cmesh *cmdest, struct cmesh *cm, int subidx)
818 if(!(sub = get_submesh(cm, subidx))) {
821 return clone(cmdest, cm, sub);
825 /* assemble a complete vertex by adding all the useful attributes */
826 int cmesh_vertex(struct cmesh *cm, float x, float y, float z)
830 cgm_wcons(cm->cur_val + CMESH_ATTR_VERTEX, x, y, z, 1.0f);
831 cm->vattr[CMESH_ATTR_VERTEX].data_valid = 1;
832 cm->vattr[CMESH_ATTR_VERTEX].nelem = 3;
834 for(i=0; i<CMESH_NUM_ATTR; i++) {
835 if(cm->vattr[i].data_valid) {
836 int newsz = cm->vattr[i].count + cm->vattr[i].nelem;
837 float *tmp = realloc(cm->vattr[i].data, newsz * sizeof *tmp);
839 tmp += cm->vattr[i].count;
841 cm->vattr[i].data = tmp;
842 cm->vattr[i].count = newsz;
844 for(j=0; j<cm->vattr[i].nelem; j++) {
845 *tmp++ = *(&cm->cur_val[i].x + j);
848 cm->vattr[i].vbo_valid = 0;
849 cm->vattr[i].data_valid = 1;
852 if(cm->idata_valid) {
857 cm->ibo_valid = cm->idata_valid = 0;
861 void cmesh_normal(struct cmesh *cm, float nx, float ny, float nz)
863 cgm_wcons(cm->cur_val + CMESH_ATTR_NORMAL, nx, ny, nz, 1.0f);
864 cm->vattr[CMESH_ATTR_NORMAL].nelem = 3;
867 void cmesh_tangent(struct cmesh *cm, float tx, float ty, float tz)
869 cgm_wcons(cm->cur_val + CMESH_ATTR_TANGENT, tx, ty, tz, 1.0f);
870 cm->vattr[CMESH_ATTR_TANGENT].nelem = 3;
873 void cmesh_texcoord(struct cmesh *cm, float u, float v, float w)
875 cgm_wcons(cm->cur_val + CMESH_ATTR_TEXCOORD, u, v, w, 1.0f);
876 cm->vattr[CMESH_ATTR_TEXCOORD].nelem = 3;
879 void cmesh_boneweights(struct cmesh *cm, float w1, float w2, float w3, float w4)
881 cgm_wcons(cm->cur_val + CMESH_ATTR_BONEWEIGHTS, w1, w2, w3, w4);
882 cm->vattr[CMESH_ATTR_BONEWEIGHTS].nelem = 4;
885 void cmesh_boneidx(struct cmesh *cm, int idx1, int idx2, int idx3, int idx4)
887 cgm_wcons(cm->cur_val + CMESH_ATTR_BONEIDX, idx1, idx2, idx3, idx4);
888 cm->vattr[CMESH_ATTR_BONEIDX].nelem = 4;
891 static float *get_vec4(struct cmesh *cm, int attr, int idx, cgm_vec4 *res)
895 cgm_wcons(res, 0, 0, 0, 1);
896 if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
901 for(i=0; i<cm->vattr[attr].nelem; i++) {
907 static float *get_vec3(struct cmesh *cm, int attr, int idx, cgm_vec3 *res)
911 cgm_vcons(res, 0, 0, 0);
912 if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
917 for(i=0; i<cm->vattr[attr].nelem; i++) {
923 /* dir_xform can be null, in which case it's calculated from xform */
924 void cmesh_apply_xform(struct cmesh *cm, float *xform, float *dir_xform)
932 for(i=0; i<cm->nverts; i++) {
933 if(!(vptr = get_vec4(cm, CMESH_ATTR_VERTEX, i, &v))) {
936 cgm_wmul_m4v4(&v, xform);
937 for(j=0; j<cm->vattr[CMESH_ATTR_VERTEX].nelem; j++) {
941 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
942 if((vptr = get_vec3(cm, CMESH_ATTR_NORMAL, i, &n))) {
943 cgm_vmul_m3v3(&n, dir_xform);
944 for(j=0; j<cm->vattr[CMESH_ATTR_NORMAL].nelem; j++) {
949 if(cmesh_has_attrib(cm, CMESH_ATTR_TANGENT)) {
950 if((vptr = get_vec3(cm, CMESH_ATTR_TANGENT, i, &t))) {
951 cgm_vmul_m3v3(&t, dir_xform);
952 for(j=0; j<cm->vattr[CMESH_ATTR_TANGENT].nelem; j++) {
960 void cmesh_flip(struct cmesh *cm)
962 cmesh_flip_faces(cm);
963 cmesh_flip_normals(cm);
966 void cmesh_flip_faces(struct cmesh *cm)
968 int i, j, idxnum, vnum, nelem;
969 unsigned int *indices;
972 if(cmesh_indexed(cm)) {
973 if(!(indices = cmesh_index(cm))) {
976 idxnum = cmesh_index_count(cm);
977 for(i=0; i<idxnum; i+=3) {
978 unsigned int tmp = indices[i + 2];
979 indices[i + 2] = indices[i + 1];
980 indices[i + 1] = tmp;
983 if(!(verts = cmesh_attrib(cm, CMESH_ATTR_VERTEX))) {
986 vnum = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
987 nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
988 for(i=0; i<vnum; i+=3) {
989 for(j=0; j<nelem; j++) {
990 vptr = verts + (i + 1) * nelem + j;
991 float tmp = vptr[nelem];
992 vptr[nelem] = vptr[0];
998 void cmesh_flip_normals(struct cmesh *cm)
1001 float *nptr = cmesh_attrib(cm, CMESH_ATTR_NORMAL);
1004 num = cm->nverts * cm->vattr[CMESH_ATTR_NORMAL].nelem;
1005 for(i=0; i<num; i++) {
1011 int cmesh_explode(struct cmesh *cm)
1013 int i, j, k, idxnum, nnverts;
1014 unsigned int *indices;
1016 if(!cmesh_indexed(cm)) return 0;
1018 indices = cmesh_index(cm);
1021 idxnum = cmesh_index_count(cm);
1024 for(i=0; i<CMESH_NUM_ATTR; i++) {
1025 const float *srcbuf;
1026 float *tmpbuf, *dstptr;
1028 if(!cmesh_has_attrib(cm, i)) continue;
1030 srcbuf = cmesh_attrib(cm, i);
1031 if(!(tmpbuf = malloc(nnverts * cm->vattr[i].nelem * sizeof(float)))) {
1036 for(j=0; j<idxnum; j++) {
1037 unsigned int idx = indices[j];
1038 const float *srcptr = srcbuf + idx * cm->vattr[i].nelem;
1040 for(k=0; k<cm->vattr[i].nelem; k++) {
1041 *dstptr++ = *srcptr++;
1045 free(cm->vattr[i].data);
1046 cm->vattr[i].data = tmpbuf;
1047 cm->vattr[i].count = nnverts * cm->vattr[i].nelem;
1048 cm->vattr[i].data_valid = 1;
1052 cm->idata_valid = 0;
1057 cm->nverts = nnverts;
1058 cm->nfaces = idxnum / 3;
1062 void cmesh_calc_face_normals(struct cmesh *cm)
1067 static int pre_draw(struct cmesh *cm)
1069 int i, loc, cur_sdr;
1071 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1075 if(!cm->vattr[CMESH_ATTR_VERTEX].vbo_valid) {
1079 if(cur_sdr && use_custom_sdr_attr) {
1080 if(sdr_loc[CMESH_ATTR_VERTEX] == -1) {
1084 for(i=0; i<CMESH_NUM_ATTR; i++) {
1086 if(loc >= 0 && cm->vattr[i].vbo_valid) {
1087 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
1088 glVertexAttribPointer(loc, cm->vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
1089 glEnableVertexAttribArray(loc);
1093 #ifndef GL_ES_VERSION_2_0
1094 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_VERTEX].vbo);
1095 glVertexPointer(cm->vattr[CMESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
1096 glEnableClientState(GL_VERTEX_ARRAY);
1098 if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
1099 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_NORMAL].vbo);
1100 glNormalPointer(GL_FLOAT, 0, 0);
1101 glEnableClientState(GL_NORMAL_ARRAY);
1103 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
1104 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD].vbo);
1105 glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
1106 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1108 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
1109 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_COLOR].vbo);
1110 glColorPointer(cm->vattr[CMESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
1111 glEnableClientState(GL_COLOR_ARRAY);
1113 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
1114 glClientActiveTexture(GL_TEXTURE1);
1115 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD2].vbo);
1116 glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD2].nelem, GL_FLOAT, 0, 0);
1117 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1118 glClientActiveTexture(GL_TEXTURE0);
1120 #endif /* GL_ES_VERSION_2_0 */
1122 glBindBuffer(GL_ARRAY_BUFFER, 0);
1126 void cmesh_draw(struct cmesh *cm)
1129 cmesh_draw_range(cm, 0, cm->nfaces * 3);
1131 cmesh_draw_range(cm, 0, cm->nverts);
1135 void cmesh_draw_range(struct cmesh *cm, int start, int count)
1139 if((cur_sdr = pre_draw(cm)) == -1) {
1144 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1145 glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (void*)(start * 4));
1146 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1148 glDrawArrays(GL_TRIANGLES, start, count);
1151 post_draw(cm, cur_sdr);
1154 void cmesh_draw_submesh(struct cmesh *cm, int subidx)
1156 struct submesh *sm = cm->sublist;
1158 while(sm && subidx-- > 0) {
1164 cmesh_draw_range(cm, sm->istart, sm->icount);
1166 cmesh_draw_range(cm, sm->vstart, sm->vcount);
1170 static void post_draw(struct cmesh *cm, int cur_sdr)
1174 if(cur_sdr && use_custom_sdr_attr) {
1175 for(i=0; i<CMESH_NUM_ATTR; i++) {
1176 int loc = sdr_loc[i];
1177 if(loc >= 0 && cm->vattr[i].vbo_valid) {
1178 glDisableVertexAttribArray(loc);
1182 #ifndef GL_ES_VERSION_2_0
1183 glDisableClientState(GL_VERTEX_ARRAY);
1184 if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
1185 glDisableClientState(GL_NORMAL_ARRAY);
1187 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
1188 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1190 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
1191 glDisableClientState(GL_COLOR_ARRAY);
1193 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
1194 glClientActiveTexture(GL_TEXTURE1);
1195 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1196 glClientActiveTexture(GL_TEXTURE0);
1198 #endif /* GL_ES_VERSION_2_0 */
1202 void cmesh_draw_wire(struct cmesh *cm, float linesz)
1204 int cur_sdr, nfaces;
1206 if((cur_sdr = pre_draw(cm)) == -1) {
1209 update_wire_ibo(cm);
1211 nfaces = cmesh_poly_count(cm);
1212 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
1213 glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0);
1214 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1216 post_draw(cm, cur_sdr);
1219 void cmesh_draw_vertices(struct cmesh *cm, float ptsz)
1222 if((cur_sdr = pre_draw(cm)) == -1) {
1226 glPushAttrib(GL_POINT_BIT);
1228 glDrawArrays(GL_POINTS, 0, cm->nverts);
1231 post_draw(cm, cur_sdr);
1234 void cmesh_draw_normals(struct cmesh *cm, float len)
1236 #ifndef GL_ES_VERSION_2_0
1237 int i, cur_sdr, vert_nelem, norm_nelem;
1239 const float *varr, *norm;
1241 varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1242 norm = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
1243 if(!varr || !norm) return;
1245 vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1246 norm_nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1248 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1249 if(cur_sdr && use_custom_sdr_attr) {
1250 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1256 for(i=0; i<cm->nverts; i++) {
1257 float x, y, z, endx, endy, endz;
1259 x = varr[i * vert_nelem];
1260 y = varr[i * vert_nelem + 1];
1261 z = varr[i * vert_nelem + 2];
1262 endx = x + norm[i * norm_nelem] * len;
1263 endy = y + norm[i * norm_nelem + 1] * len;
1264 endz = z + norm[i * norm_nelem + 2] * len;
1267 glVertex3f(x, y, z);
1268 glVertex3f(endx, endy, endz);
1270 glVertexAttrib3f(loc, x, y, z);
1271 glVertexAttrib3f(loc, endx, endy, endz);
1275 #endif /* GL_ES_VERSION_2_0 */
1278 void cmesh_draw_tangents(struct cmesh *cm, float len)
1280 #ifndef GL_ES_VERSION_2_0
1281 int i, cur_sdr, vert_nelem, tang_nelem;
1283 const float *varr, *tang;
1285 varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1286 tang = cmesh_attrib_ro(cm, CMESH_ATTR_TANGENT);
1287 if(!varr || !tang) return;
1289 vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1290 tang_nelem = cm->vattr[CMESH_ATTR_TANGENT].nelem;
1292 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1293 if(cur_sdr && use_custom_sdr_attr) {
1294 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1300 for(i=0; i<cm->nverts; i++) {
1301 float x, y, z, endx, endy, endz;
1303 x = varr[i * vert_nelem];
1304 y = varr[i * vert_nelem + 1];
1305 z = varr[i * vert_nelem + 2];
1306 endx = x + tang[i * tang_nelem] * len;
1307 endy = y + tang[i * tang_nelem + 1] * len;
1308 endz = z + tang[i * tang_nelem + 2] * len;
1311 glVertex3f(x, y, z);
1312 glVertex3f(endx, endy, endz);
1314 glVertexAttrib3f(loc, x, y, z);
1315 glVertexAttrib3f(loc, endx, endy, endz);
1319 #endif /* GL_ES_VERSION_2_0 */
1322 static void update_buffers(struct cmesh *cm)
1326 for(i=0; i<CMESH_NUM_ATTR; i++) {
1327 if(cmesh_has_attrib(cm, i) && !cm->vattr[i].vbo_valid) {
1328 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
1329 glBufferData(GL_ARRAY_BUFFER, cm->nverts * cm->vattr[i].nelem * sizeof(float),
1330 cm->vattr[i].data, GL_STATIC_DRAW);
1331 cm->vattr[i].vbo_valid = 1;
1334 glBindBuffer(GL_ARRAY_BUFFER, 0);
1336 if(cm->idata_valid && !cm->ibo_valid) {
1337 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1338 glBufferData(GL_ELEMENT_ARRAY_BUFFER, cm->nfaces * 3 * sizeof(unsigned int),
1339 cm->idata, GL_STATIC_DRAW);
1341 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1345 static void update_wire_ibo(struct cmesh *cm)
1348 unsigned int *wire_idxarr, *dest;
1352 if(cm->wire_ibo_valid) return;
1355 glGenBuffers(1, &cm->wire_ibo);
1357 num_faces = cmesh_poly_count(cm);
1359 if(!(wire_idxarr = malloc(num_faces * 6 * sizeof *wire_idxarr))) {
1365 /* we're dealing with an indexed mesh */
1366 const unsigned int *idxarr = cmesh_index_ro(cm);
1368 for(i=0; i<num_faces; i++) {
1369 *dest++ = idxarr[0];
1370 *dest++ = idxarr[1];
1371 *dest++ = idxarr[1];
1372 *dest++ = idxarr[2];
1373 *dest++ = idxarr[2];
1374 *dest++ = idxarr[0];
1378 /* not an indexed mesh */
1379 for(i=0; i<num_faces; i++) {
1390 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
1391 glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_faces * 6 * sizeof(unsigned int),
1392 wire_idxarr, GL_STATIC_DRAW);
1394 cm->wire_ibo_valid = 1;
1395 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1398 static void calc_aabb(struct cmesh *cm)
1402 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1406 cgm_vcons(&cm->aabb_min, FLT_MAX, FLT_MAX, FLT_MAX);
1407 cgm_vcons(&cm->aabb_max, -FLT_MAX, -FLT_MAX, -FLT_MAX);
1409 for(i=0; i<cm->nverts; i++) {
1410 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1411 for(j=0; j<3; j++) {
1412 if(v[j] < (&cm->aabb_min.x)[j]) {
1413 (&cm->aabb_min.x)[j] = v[j];
1415 if(v[j] > (&cm->aabb_max.x)[j]) {
1416 (&cm->aabb_max.x)[j] = v[j];
1423 void cmesh_aabbox(struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax)
1425 if(!cm->aabb_valid) {
1428 *vmin = cm->aabb_min;
1429 *vmax = cm->aabb_max;
1432 static void calc_bsph(struct cmesh *cm)
1437 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1441 cgm_vcons(&cm->bsph_center, 0, 0, 0);
1443 /* first find the center */
1444 for(i=0; i<cm->nverts; i++) {
1445 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1446 cm->bsph_center.x += v[0];
1447 cm->bsph_center.y += v[1];
1448 cm->bsph_center.z += v[2];
1450 s = 1.0f / (float)cm->nverts;
1451 cm->bsph_center.x *= s;
1452 cm->bsph_center.y *= s;
1453 cm->bsph_center.z *= s;
1455 cm->bsph_radius = 0.0f;
1456 for(i=0; i<cm->nverts; i++) {
1457 const cgm_vec3 *v = (const cgm_vec3*)cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1458 if((dist_sq = cgm_vdist_sq(v, &cm->bsph_center)) > cm->bsph_radius) {
1459 cm->bsph_radius = dist_sq;
1462 cm->bsph_radius = sqrt(cm->bsph_radius);
1466 float cmesh_bsphere(struct cmesh *cm, cgm_vec3 *center, float *rad)
1468 if(!cm->bsph_valid) {
1471 *center = cm->bsph_center;
1472 *rad = cm->bsph_radius;
1473 return cm->bsph_radius;
1477 void cmesh_texcoord_apply_xform(struct cmesh *cm, float *xform);
1478 void cmesh_texcoord_gen_plane(struct cmesh *cm, cgm_vec3 *norm, cgm_vec3 *tang);
1479 void cmesh_texcoord_gen_box(struct cmesh *cm);
1480 void cmesh_texcoord_gen_cylinder(struct cmesh *cm);
1482 int cmesh_dump(struct cmesh *cm, const char *fname)
1484 FILE *fp = fopen(fname, "wb");
1486 int res = cmesh_dump_file(cm, fp);
1493 int cmesh_dump_file(struct cmesh *cm, FILE *fp)
1495 static const char *label[] = { "pos", "nor", "tan", "tex", "col", "bw", "bid", "tex2" };
1496 static const char *elemfmt[] = { 0, " %s(%g)", " %s(%g, %g)", " %s(%g, %g, %g)", " %s(%g, %g, %g, %g)", 0 };
1499 if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1503 fprintf(fp, "VERTEX ATTRIBUTES\n");
1505 for(i=0; i<cm->nverts; i++) {
1506 fprintf(fp, "%5u:", i);
1507 for(j=0; j<CMESH_NUM_ATTR; j++) {
1508 if(cmesh_has_attrib(cm, j)) {
1509 const float *v = cmesh_attrib_at_ro(cm, j, i);
1510 int nelem = cm->vattr[j].nelem;
1511 fprintf(fp, elemfmt[nelem], label[j], v[0], nelem > 1 ? v[1] : 0.0f,
1512 nelem > 2 ? v[2] : 0.0f, nelem > 3 ? v[3] : 0.0f);
1518 if(cmesh_indexed(cm)) {
1519 const unsigned int *idx = cmesh_index_ro(cm);
1520 int numidx = cmesh_index_count(cm);
1521 int numtri = numidx / 3;
1522 assert(numidx % 3 == 0);
1524 fprintf(fp, "FACES\n");
1526 for(i=0; i<numtri; i++) {
1527 fprintf(fp, "%5d: %d %d %d\n", i, idx[0], idx[1], idx[2]);
1534 int cmesh_dump_obj(struct cmesh *cm, const char *fname)
1536 FILE *fp = fopen(fname, "wb");
1538 int res = cmesh_dump_obj_file(cm, fp, 0);
1548 int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs)
1550 static const char *fmtstr[] = {" %u", " %u//%u", " %u/%u", " %u/%u/%u"};
1551 int i, j, num, nelem;
1552 unsigned int aflags = 0;
1554 if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1559 nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1560 if((num = cm->vattr[CMESH_ATTR_VERTEX].count) != cm->nverts * nelem) {
1561 fprintf(stderr, "vertex array size (%d) != nverts (%d)\n", num, cm->nverts);
1563 for(i=0; i<cm->nverts; i++) {
1564 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1565 fprintf(fp, "v %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
1568 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
1570 nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1571 if((num = cm->vattr[CMESH_ATTR_NORMAL].count) != cm->nverts * nelem) {
1572 fprintf(stderr, "normal array size (%d) != nverts (%d)\n", num, cm->nverts);
1574 for(i=0; i<cm->nverts; i++) {
1575 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_NORMAL, i);
1576 fprintf(fp, "vn %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
1580 if(cmesh_has_attrib(cm, CMESH_ATTR_TEXCOORD)) {
1582 nelem = cm->vattr[CMESH_ATTR_TEXCOORD].nelem;
1583 if((num = cm->vattr[CMESH_ATTR_TEXCOORD].count) != cm->nverts * nelem) {
1584 fprintf(stderr, "texcoord array size (%d) != nverts (%d)\n", num, cm->nverts);
1586 for(i=0; i<cm->nverts; i++) {
1587 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_TEXCOORD, i);
1588 fprintf(fp, "vt %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f);
1592 if(cmesh_indexed(cm)) {
1593 const unsigned int *idxptr = cmesh_index_ro(cm);
1594 int numidx = cmesh_index_count(cm);
1595 int numtri = numidx / 3;
1596 assert(numidx % 3 == 0);
1598 for(i=0; i<numtri; i++) {
1600 for(j=0; j<3; j++) {
1601 unsigned int idx = *idxptr++ + 1 + voffs;
1602 fprintf(fp, fmtstr[aflags], idx, idx, idx);
1607 int numtri = cm->nverts / 3;
1608 unsigned int idx = 1 + voffs;
1609 for(i=0; i<numtri; i++) {
1611 for(j=0; j<3; j++) {
1612 fprintf(fp, fmtstr[aflags], idx, idx, idx);