11 struct cmesh_vattrib {
12 int nelem; /* num elements per attribute [1, 4] */
16 int vbo_valid, data_valid;
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
26 int nfaces; /* derived from either icount or vcount */
34 unsigned int nverts, nfaces;
36 struct submesh *sublist;
39 /* current value for each attribute for the immediate mode interface */
40 cgm_vec4 cur_val[CMESH_NUM_ATTR];
42 unsigned int buffer_objects[CMESH_NUM_ATTR + 1];
43 struct cmesh_vattrib vattr[CMESH_NUM_ATTR];
48 int ibo_valid, idata_valid;
50 /* index buffer for wireframe rendering (constructed on demand) */
51 unsigned int wire_ibo;
54 /* axis-aligned bounding box */
55 cgm_vec3 aabb_min, aabb_max;
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);
72 static int def_nelem[CMESH_NUM_ATTR] = {3, 3, 3, 2, 4, 4, 4, 2};
74 static int sdr_loc[CMESH_NUM_ATTR] = {0, 1, 2, 3, 4, 5, 6, 7};
75 static int use_custom_sdr_attr;
79 void cmesh_set_attrib_sdrloc(int attr, int loc)
84 int cmesh_get_attrib_sdrloc(int attr)
89 void cmesh_clear_attrib_sdrloc(void)
92 for(i=0; i<CMESH_NUM_ATTR; i++) {
98 struct cmesh *cmesh_alloc(void)
102 if(!(cm = malloc(sizeof *cm))) {
105 if(cmesh_init(cm) == -1) {
112 void cmesh_free(struct cmesh *cm)
118 int cmesh_init(struct cmesh *cm)
122 memset(cm, 0, sizeof *cm);
123 cgm_wcons(cm->cur_val + CMESH_ATTR_COLOR, 1, 1, 1, 1);
125 glGenBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
127 for(i=0; i<CMESH_NUM_ATTR; i++) {
128 cm->vattr[i].vbo = cm->buffer_objects[i];
131 cm->ibo = cm->buffer_objects[CMESH_NUM_ATTR];
135 void cmesh_destroy(struct cmesh *cm)
141 for(i=0; i<CMESH_NUM_ATTR; i++) {
142 free(cm->vattr[i].data);
146 cmesh_clear_submeshes(cm);
148 glDeleteBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
150 glDeleteBuffers(1, &cm->wire_ibo);
154 void cmesh_clear(struct cmesh *cm)
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;
166 cm->ibo_valid = cm->idata_valid = 0;
171 cm->wire_ibo_valid = 0;
172 cm->nverts = cm->nfaces = 0;
174 cm->bsph_valid = cm->aabb_valid = 0;
176 cmesh_clear_submeshes(cm);
179 int cmesh_clone(struct cmesh *cmdest, const struct cmesh *cmsrc)
181 return clone(cmdest, cmsrc, 0);
184 static int clone(struct cmesh *cmdest, const struct cmesh *cmsrc, struct submesh *sub)
186 int i, nelem, vstart, vcount, istart, icount;
187 char *srcname, *name = 0;
188 float *varr[CMESH_NUM_ATTR] = {0};
190 unsigned int *iptr, *iarr = 0;
192 /* try do anything that can fail first, before making any changes to cmdest
193 * so we have the option of recovering gracefuly
196 srcname = sub ? sub->name : cmsrc->name;
198 if(!(name = malloc(strlen(srcname) + 1))) {
201 strcpy(name, srcname);
205 vstart = sub->vstart;
206 vcount = sub->vcount;
207 istart = sub->istart;
208 icount = sub->icount;
211 vcount = cmsrc->nverts;
212 icount = cmsrc->icount;
215 if(cmesh_indexed(cmsrc)) {
216 if(!(iarr = malloc(icount * sizeof *iarr))) {
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)))) {
236 /* from this point forward nothing can fail */
239 for(i=0; i<CMESH_NUM_ATTR; i++) {
240 free(cmdest->vattr[i].data);
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);
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;
255 memset(cmdest->vattr + i, 0, sizeof cmdest->vattr[i]);
259 if(cmesh_indexed(cmsrc)) {
260 /* force validation .... */
261 cmesh_index((struct cmesh*)cmsrc);
263 cmdest->idata = iarr;
264 cmdest->icount = icount;
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;
272 memcpy(cmdest->idata, cmsrc->idata + istart, icount * sizeof *cmdest->idata);
274 cmdest->idata_valid = 1;
277 cmdest->idata_valid = cmdest->ibo_valid = 0;
283 cmdest->nverts = cmsrc->nverts;
284 cmdest->nfaces = sub ? sub->nfaces : cmsrc->nfaces;
286 memcpy(cmdest->cur_val, cmsrc->cur_val, sizeof cmdest->cur_val);
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;
295 /* copy sublist only if we're not cloning a submesh */
297 struct submesh *sm, *n, *head = 0, *tail = 0;
301 if(!(n = malloc(sizeof *n)) || !(name = malloc(strlen(sm->name) + 1))) {
306 strcpy(name, sm->name);
321 cmdest->sublist = head;
322 cmdest->subcount = cmsrc->subcount;
328 int cmesh_set_name(struct cmesh *cm, const char *name)
330 int len = strlen(name);
331 char *tmp = malloc(len + 1);
335 memcpy(cm->name, name, len + 1);
339 const char *cmesh_name(const struct cmesh *cm)
344 int cmesh_has_attrib(const struct cmesh *cm, int attr)
346 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
349 return cm->vattr[attr].vbo_valid | cm->vattr[attr].data_valid;
352 int cmesh_indexed(const struct cmesh *cm)
354 return cm->ibo_valid | cm->idata_valid;
357 /* vdata can be 0, in which case only memory is allocated
358 * returns pointer to the attribute array
360 float *cmesh_set_attrib(struct cmesh *cm, int attr, int nelem, unsigned int num,
365 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
368 if(cm->nverts && num != cm->nverts) {
372 if(!(newarr = malloc(num * nelem * sizeof *newarr))) {
376 memcpy(newarr, vdata, num * nelem * sizeof *newarr);
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;
390 float *cmesh_attrib(struct cmesh *cm, int attr)
392 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
395 cm->vattr[attr].vbo_valid = 0;
396 return (float*)cmesh_attrib_ro(cm, attr);
399 const float *cmesh_attrib_ro(const struct cmesh *cm, int attr)
404 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
408 if(!cm->vattr[attr].data_valid) {
409 #if GL_ES_VERSION_2_0
412 struct cmesh *m = (struct cmesh*)cm;
414 if(!m->vattr[attr].vbo_valid) {
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)))) {
423 m->vattr[attr].count = m->nverts * nelem;
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);
430 m->vattr[attr].data_valid = 1;
433 return cm->vattr[attr].data;
436 float *cmesh_attrib_at(struct cmesh *cm, int attr, int idx)
438 float *vptr = cmesh_attrib(cm, attr);
439 return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
442 const float *cmesh_attrib_at_ro(const struct cmesh *cm, int attr, int idx)
444 const float *vptr = cmesh_attrib_ro(cm, attr);
445 return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
448 int cmesh_attrib_count(const struct cmesh *cm, int attr)
450 return cmesh_has_attrib(cm, attr) ? cm->nverts : 0;
453 int cmesh_attrib_nelem(const struct cmesh *cm, int attr)
455 return cmesh_has_attrib(cm, attr) ? cm->vattr[attr].nelem : 0;
458 int cmesh_push_attrib(struct cmesh *cm, int attr, float *v)
463 if(!cm->vattr[attr].nelem) {
464 cm->vattr[attr].nelem = def_nelem[attr];
467 cursz = cm->vattr[attr].count;
468 newsz = cursz + cm->vattr[attr].nelem;
469 if(!(vptr = realloc(cm->vattr[attr].data, newsz * sizeof(float)))) {
472 cm->vattr[attr].data = vptr;
473 cm->vattr[attr].count = newsz;
476 for(i=0; i<cm->vattr[attr].nelem; i++) {
479 cm->vattr[attr].data_valid = 1;
480 cm->vattr[attr].vbo_valid = 0;
482 if(attr == CMESH_ATTR_VERTEX) {
483 cm->nverts = newsz / cm->vattr[attr].nelem;
488 int cmesh_push_attrib1f(struct cmesh *cm, int attr, float x)
494 return cmesh_push_attrib(cm, attr, v);
497 int cmesh_push_attrib2f(struct cmesh *cm, int attr, float x, float y)
504 return cmesh_push_attrib(cm, attr, v);
507 int cmesh_push_attrib3f(struct cmesh *cm, int attr, float x, float y, float z)
514 return cmesh_push_attrib(cm, attr, v);
517 int cmesh_push_attrib4f(struct cmesh *cm, int attr, float x, float y, float z, float w)
524 return cmesh_push_attrib(cm, attr, v);
527 /* indices can be 0, in which case only memory is allocated
528 * returns pointer to the index array
530 unsigned int *cmesh_set_index(struct cmesh *cm, int num, const unsigned int *indices)
533 int nidx = cm->nfaces * 3;
535 if(nidx && num != nidx) {
539 if(!(tmp = malloc(num * sizeof *tmp))) {
543 memcpy(tmp, indices, num * sizeof *tmp);
549 cm->nfaces = num / 3;
555 unsigned int *cmesh_index(struct cmesh *cm)
558 return (unsigned int*)cmesh_index_ro(cm);
561 const unsigned int *cmesh_index_ro(const struct cmesh *cm)
566 if(!cm->idata_valid) {
567 #if GL_ES_VERSION_2_0
570 struct cmesh *m = (struct cmesh*)cm;
576 /* local copy is unavailable, grab the data from the ibo */
577 nidx = m->nfaces * 3;
578 if(!(tmp = malloc(nidx * sizeof *m->idata))) {
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);
596 int cmesh_index_count(const struct cmesh *cm)
598 return cm->nfaces * 3;
601 int cmesh_push_index(struct cmesh *cm, unsigned int idx)
604 unsigned int cur_sz = cm->icount;
605 if(!(iptr = realloc(cm->idata, (cur_sz + 1) * sizeof *iptr))) {
610 cm->icount = cur_sz + 1;
614 cm->nfaces = cm->icount / 3;
618 int cmesh_poly_count(const struct cmesh *cm)
624 return cm->nverts / 3;
629 /* attr can be -1 to invalidate all attributes */
630 void cmesh_invalidate_vbo(struct cmesh *cm, int attr)
634 if(attr >= CMESH_NUM_ATTR) {
639 for(i=0; i<CMESH_NUM_ATTR; i++) {
640 cm->vattr[i].vbo_valid = 0;
643 cm->vattr[attr].vbo_valid = 0;
647 void cmesh_invalidate_index(struct cmesh *cm)
652 int cmesh_append(struct cmesh *cmdest, const struct cmesh *cmsrc)
654 int i, nelem, newsz, origsz, srcsz;
657 unsigned int idxoffs;
659 if(!cmdest->nverts) {
660 return cmesh_clone(cmdest, cmsrc);
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);
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;
674 if(!(vptr = realloc(cmdest->vattr[i].data, newsz * sizeof *vptr))) {
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;
683 if(cmesh_indexed(cmdest)) {
684 assert(cmesh_indexed(cmsrc));
685 /* force validation ... */
687 cmesh_index_ro(cmsrc);
689 idxoffs = cmdest->nverts;
690 origsz = cmdest->icount;
691 srcsz = cmsrc->icount;
692 newsz = origsz + srcsz;
694 if(!(iptr = realloc(cmdest->idata, newsz * sizeof *iptr))) {
697 cmdest->idata = iptr;
698 cmdest->icount = newsz;
700 /* copy and fixup all the new indices */
702 for(i=0; i<srcsz; i++) {
703 *iptr++ = cmsrc->idata[i] + idxoffs;
707 cmdest->wire_ibo_valid = 0;
708 cmdest->aabb_valid = 0;
709 cmdest->bsph_valid = 0;
713 void cmesh_clear_submeshes(struct cmesh *cm)
719 cm->sublist = cm->sublist->next;
726 int cmesh_submesh(struct cmesh *cm, const char *name, int fstart, int fcount)
729 unsigned int minv = UINT_MAX, maxv = 0;
733 if(fstart < 0 || fcount < 1 || fstart + fcount > cm->nfaces) {
737 if(!(sm = malloc(sizeof *sm)) || !(sm->name = malloc(strlen(name) + 1))) {
741 strcpy(sm->name, name);
744 if(cmesh_indexed(cm)) {
745 sm->istart = fstart * 3;
746 sm->icount = fcount * 3;
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;
756 sm->vcount = maxv - minv + 1;
758 sm->istart = sm->icount = 0;
759 sm->vstart = fstart * 3;
760 sm->vcount = fcount * 3;
763 sm->next = cm->sublist;
769 int cmesh_remove_submesh(struct cmesh *cm, int idx)
771 struct submesh dummy;
772 struct submesh *prev, *sm;
774 if(idx >= cm->subcount) {
778 dummy.next = cm->sublist;
781 while(prev->next && idx-- > 0) {
785 if(!(sm = prev->next)) return -1;
787 prev->next = sm->next;
792 assert(cm->subcount >= 0);
794 cm->sublist = dummy.next;
798 int cmesh_find_submesh(const struct cmesh *cm, const char *name)
801 struct submesh *sm = cm->sublist;
803 if(strcmp(sm->name, name) == 0) {
804 assert(idx <= cm->subcount);
813 int cmesh_submesh_count(const struct cmesh *cm)
818 static struct submesh *get_submesh(const struct cmesh *m, int idx)
820 struct submesh *sm = m->sublist;
821 while(sm && --idx >= 0) {
827 int cmesh_clone_submesh(struct cmesh *cmdest, const struct cmesh *cm, int subidx)
831 if(!(sub = get_submesh(cm, subidx))) {
834 return clone(cmdest, cm, sub);
838 /* assemble a complete vertex by adding all the useful attributes */
839 int cmesh_vertex(struct cmesh *cm, float x, float y, float z)
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;
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);
853 if(cm->idata_valid) {
858 cm->ibo_valid = cm->idata_valid = 0;
862 void cmesh_normal(struct cmesh *cm, float nx, float ny, float nz)
864 cgm_wcons(cm->cur_val + CMESH_ATTR_NORMAL, nx, ny, nz, 1.0f);
865 cm->vattr[CMESH_ATTR_NORMAL].nelem = 3;
868 void cmesh_tangent(struct cmesh *cm, float tx, float ty, float tz)
870 cgm_wcons(cm->cur_val + CMESH_ATTR_TANGENT, tx, ty, tz, 1.0f);
871 cm->vattr[CMESH_ATTR_TANGENT].nelem = 3;
874 void cmesh_texcoord(struct cmesh *cm, float u, float v, float w)
876 cgm_wcons(cm->cur_val + CMESH_ATTR_TEXCOORD, u, v, w, 1.0f);
877 cm->vattr[CMESH_ATTR_TEXCOORD].nelem = 3;
880 void cmesh_boneweights(struct cmesh *cm, float w1, float w2, float w3, float w4)
882 cgm_wcons(cm->cur_val + CMESH_ATTR_BONEWEIGHTS, w1, w2, w3, w4);
883 cm->vattr[CMESH_ATTR_BONEWEIGHTS].nelem = 4;
886 void cmesh_boneidx(struct cmesh *cm, int idx1, int idx2, int idx3, int idx4)
888 cgm_wcons(cm->cur_val + CMESH_ATTR_BONEIDX, idx1, idx2, idx3, idx4);
889 cm->vattr[CMESH_ATTR_BONEIDX].nelem = 4;
892 static float *get_vec4(struct cmesh *cm, int attr, int idx, cgm_vec4 *res)
896 cgm_wcons(res, 0, 0, 0, 1);
897 if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
902 for(i=0; i<cm->vattr[attr].nelem; i++) {
908 static float *get_vec3(struct cmesh *cm, int attr, int idx, cgm_vec3 *res)
912 cgm_vcons(res, 0, 0, 0);
913 if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
918 for(i=0; i<cm->vattr[attr].nelem; i++) {
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)
933 for(i=0; i<cm->nverts; i++) {
934 if(!(vptr = get_vec4(cm, CMESH_ATTR_VERTEX, i, &v))) {
937 cgm_wmul_m4v4(&v, xform);
938 for(j=0; j<cm->vattr[CMESH_ATTR_VERTEX].nelem; j++) {
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++) {
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++) {
961 void cmesh_flip(struct cmesh *cm)
963 cmesh_flip_faces(cm);
964 cmesh_flip_normals(cm);
967 void cmesh_flip_faces(struct cmesh *cm)
969 int i, j, idxnum, vnum, nelem;
970 unsigned int *indices;
973 if(cmesh_indexed(cm)) {
974 if(!(indices = cmesh_index(cm))) {
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;
984 if(!(verts = cmesh_attrib(cm, CMESH_ATTR_VERTEX))) {
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];
999 void cmesh_flip_normals(struct cmesh *cm)
1002 float *nptr = cmesh_attrib(cm, CMESH_ATTR_NORMAL);
1005 num = cm->nverts * cm->vattr[CMESH_ATTR_NORMAL].nelem;
1006 for(i=0; i<num; i++) {
1012 int cmesh_explode(struct cmesh *cm)
1014 int i, j, k, idxnum, nnverts;
1015 unsigned int *indices;
1017 if(!cmesh_indexed(cm)) return 0;
1019 indices = cmesh_index(cm);
1022 idxnum = cmesh_index_count(cm);
1025 for(i=0; i<CMESH_NUM_ATTR; i++) {
1026 const float *srcbuf;
1027 float *tmpbuf, *dstptr;
1029 if(!cmesh_has_attrib(cm, i)) continue;
1031 srcbuf = cmesh_attrib(cm, i);
1032 if(!(tmpbuf = malloc(nnverts * cm->vattr[i].nelem * sizeof(float)))) {
1037 for(j=0; j<idxnum; j++) {
1038 unsigned int idx = indices[j];
1039 const float *srcptr = srcbuf + idx * cm->vattr[i].nelem;
1041 for(k=0; k<cm->vattr[i].nelem; k++) {
1042 *dstptr++ = *srcptr++;
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;
1053 cm->idata_valid = 0;
1058 cm->nverts = nnverts;
1059 cm->nfaces = idxnum / 3;
1063 void cmesh_calc_face_normals(struct cmesh *cm)
1068 static int pre_draw(const struct cmesh *cm)
1070 int i, loc, cur_sdr;
1072 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1074 update_buffers((struct cmesh*)cm);
1076 if(!cm->vattr[CMESH_ATTR_VERTEX].vbo_valid) {
1080 if(cur_sdr && use_custom_sdr_attr) {
1081 if(sdr_loc[CMESH_ATTR_VERTEX] == -1) {
1085 for(i=0; i<CMESH_NUM_ATTR; 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);
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);
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);
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);
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);
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);
1121 #endif /* GL_ES_VERSION_2_0 */
1123 glBindBuffer(GL_ARRAY_BUFFER, 0);
1127 void cmesh_draw(const struct cmesh *cm)
1131 if((cur_sdr = pre_draw(cm)) == -1) {
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);
1140 glDrawArrays(GL_TRIANGLES, 0, cm->nverts);
1143 post_draw(cm, cur_sdr);
1146 void cmesh_draw_range(const struct cmesh *cm, int start, int count)
1150 if((cur_sdr = pre_draw(cm)) == -1) {
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);
1159 glDrawArrays(GL_TRIANGLES, start, count);
1162 post_draw(cm, cur_sdr);
1165 void cmesh_draw_submesh(const struct cmesh *cm, int subidx)
1167 struct submesh *sm = cm->sublist;
1169 while(sm && subidx-- > 0) {
1175 cmesh_draw_range(cm, sm->istart, sm->icount);
1177 cmesh_draw_range(cm, sm->vstart, sm->vcount);
1181 static void post_draw(const struct cmesh *cm, int cur_sdr)
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);
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);
1198 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
1199 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1201 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
1202 glDisableClientState(GL_COLOR_ARRAY);
1204 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
1205 glClientActiveTexture(GL_TEXTURE1);
1206 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1207 glClientActiveTexture(GL_TEXTURE0);
1209 #endif /* GL_ES_VERSION_2_0 */
1213 void cmesh_draw_wire(const struct cmesh *cm, float linesz)
1215 int cur_sdr, nfaces;
1217 if((cur_sdr = pre_draw(cm)) == -1) {
1220 update_wire_ibo((struct cmesh*)cm);
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);
1227 post_draw(cm, cur_sdr);
1230 void cmesh_draw_vertices(const struct cmesh *cm, float ptsz)
1233 if((cur_sdr = pre_draw(cm)) == -1) {
1237 glPushAttrib(GL_POINT_BIT);
1239 glDrawArrays(GL_POINTS, 0, cm->nverts);
1242 post_draw(cm, cur_sdr);
1245 void cmesh_draw_normals(const struct cmesh *cm, float len)
1247 #ifndef GL_ES_VERSION_2_0
1248 int i, cur_sdr, vert_nelem, norm_nelem;
1250 const float *varr, *norm;
1252 varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1253 norm = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
1254 if(!varr || !norm) return;
1256 vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1257 norm_nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1259 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1260 if(cur_sdr && use_custom_sdr_attr) {
1261 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1267 for(i=0; i<cm->nverts; i++) {
1268 float x, y, z, endx, endy, endz;
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;
1278 glVertex3f(x, y, z);
1279 glVertex3f(endx, endy, endz);
1281 glVertexAttrib3f(loc, x, y, z);
1282 glVertexAttrib3f(loc, endx, endy, endz);
1286 #endif /* GL_ES_VERSION_2_0 */
1289 void cmesh_draw_tangents(const struct cmesh *cm, float len)
1291 #ifndef GL_ES_VERSION_2_0
1292 int i, cur_sdr, vert_nelem, tang_nelem;
1294 const float *varr, *tang;
1296 varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1297 tang = cmesh_attrib_ro(cm, CMESH_ATTR_TANGENT);
1298 if(!varr || !tang) return;
1300 vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1301 tang_nelem = cm->vattr[CMESH_ATTR_TANGENT].nelem;
1303 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1304 if(cur_sdr && use_custom_sdr_attr) {
1305 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1311 for(i=0; i<cm->nverts; i++) {
1312 float x, y, z, endx, endy, endz;
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;
1322 glVertex3f(x, y, z);
1323 glVertex3f(endx, endy, endz);
1325 glVertexAttrib3f(loc, x, y, z);
1326 glVertexAttrib3f(loc, endx, endy, endz);
1330 #endif /* GL_ES_VERSION_2_0 */
1333 static void update_buffers(struct cmesh *cm)
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;
1345 glBindBuffer(GL_ARRAY_BUFFER, 0);
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);
1352 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1356 static void update_wire_ibo(struct cmesh *cm)
1359 unsigned int *wire_idxarr, *dest;
1363 if(cm->wire_ibo_valid) return;
1366 glGenBuffers(1, &cm->wire_ibo);
1368 num_faces = cmesh_poly_count(cm);
1370 if(!(wire_idxarr = malloc(num_faces * 6 * sizeof *wire_idxarr))) {
1376 /* we're dealing with an indexed mesh */
1377 const unsigned int *idxarr = cmesh_index_ro(cm);
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];
1389 /* not an indexed mesh */
1390 for(i=0; i<num_faces; i++) {
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);
1405 cm->wire_ibo_valid = 1;
1406 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1409 static void calc_aabb(struct cmesh *cm)
1413 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1417 cgm_vcons(&cm->aabb_min, FLT_MAX, FLT_MAX, FLT_MAX);
1418 cgm_vcons(&cm->aabb_max, -FLT_MAX, -FLT_MAX, -FLT_MAX);
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];
1426 if(v[j] > (&cm->aabb_max.x)[j]) {
1427 (&cm->aabb_max.x)[j] = v[j];
1434 void cmesh_aabbox(const struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax)
1436 if(!cm->aabb_valid) {
1437 calc_aabb((struct cmesh*)cm);
1439 *vmin = cm->aabb_min;
1440 *vmax = cm->aabb_max;
1443 static void calc_bsph(struct cmesh *cm)
1448 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1452 cgm_vcons(&cm->bsph_center, 0, 0, 0);
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];
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;
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;
1473 cm->bsph_radius = sqrt(cm->bsph_radius);
1477 float cmesh_bsphere(const struct cmesh *cm, cgm_vec3 *center, float *rad)
1479 if(!cm->bsph_valid) {
1480 calc_bsph((struct cmesh*)cm);
1482 if(center) *center = cm->bsph_center;
1483 if(rad) *rad = cm->bsph_radius;
1484 return cm->bsph_radius;
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);
1493 int cmesh_dump(const struct cmesh *cm, const char *fname)
1495 FILE *fp = fopen(fname, "wb");
1497 int res = cmesh_dump_file(cm, fp);
1504 int cmesh_dump_file(const struct cmesh *cm, FILE *fp)
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 };
1510 if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1514 fprintf(fp, "VERTEX ATTRIBUTES\n");
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);
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);
1535 fprintf(fp, "FACES\n");
1537 for(i=0; i<numtri; i++) {
1538 fprintf(fp, "%5d: %d %d %d\n", i, idx[0], idx[1], idx[2]);
1545 int cmesh_dump_obj(const struct cmesh *cm, const char *fname)
1547 FILE *fp = fopen(fname, "wb");
1549 int res = cmesh_dump_obj_file(cm, fp, 0);
1559 int cmesh_dump_obj_file(const struct cmesh *cm, FILE *fp, int voffs)
1561 static const char *fmtstr[] = {" %u", " %u//%u", " %u/%u", " %u/%u/%u"};
1562 int i, j, num, nelem;
1563 unsigned int aflags = 0;
1565 if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
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);
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);
1579 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
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);
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);
1591 if(cmesh_has_attrib(cm, CMESH_ATTR_TEXCOORD)) {
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);
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);
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);
1609 for(i=0; i<numtri; i++) {
1611 for(j=0; j<3; j++) {
1612 unsigned int idx = *idxptr++ + 1 + voffs;
1613 fprintf(fp, fmtstr[aflags], idx, idx, idx);
1618 int numtri = cm->nverts / 3;
1619 unsigned int idx = 1 + voffs;
1620 for(i=0; i<numtri; i++) {
1622 for(j=0; j<3; j++) {
1623 fprintf(fp, fmtstr[aflags], idx, idx, idx);