13 struct cmesh_vattrib {
14 int nelem; /* num elements per attribute [1, 4] */
18 int vbo_valid, data_valid;
21 /* istart,icount are valid only when the mesh is indexed, otherwise icount is 0.
22 * vstart,vcount are define the submesh for non-indexed meshes.
23 * For indexed meshes, vstart,vcount denote the range of vertices used by each
28 int nfaces; /* derived from either icount or vcount */
36 unsigned int nverts, nfaces;
38 struct submesh *sublist;
41 /* current value for each attribute for the immediate mode interface */
42 cgm_vec4 cur_val[CMESH_NUM_ATTR];
44 unsigned int buffer_objects[CMESH_NUM_ATTR + 1];
45 struct cmesh_vattrib vattr[CMESH_NUM_ATTR];
50 int ibo_valid, idata_valid;
52 /* index buffer for wireframe rendering (constructed on demand) */
53 unsigned int wire_ibo;
56 /* axis-aligned bounding box */
57 cgm_vec3 aabb_min, aabb_max;
66 static int clone(struct cmesh *cmdest, struct cmesh *cmsrc, struct submesh *sub);
67 static int pre_draw(struct cmesh *cm);
68 static void post_draw(struct cmesh *cm, int cur_sdr);
69 static void update_buffers(struct cmesh *cm);
70 static void update_wire_ibo(struct cmesh *cm);
71 static void calc_aabb(struct cmesh *cm);
72 static void calc_bsph(struct cmesh *cm);
74 static int def_nelem[CMESH_NUM_ATTR] = {3, 3, 3, 2, 4, 4, 4, 2};
76 static int sdr_loc[CMESH_NUM_ATTR] = {0, 1, 2, 3, 4, 5, 6, 7};
77 static int use_custom_sdr_attr;
81 void cmesh_set_attrib_sdrloc(int attr, int loc)
86 int cmesh_get_attrib_sdrloc(int attr)
91 void cmesh_clear_attrib_sdrloc(void)
94 for(i=0; i<CMESH_NUM_ATTR; i++) {
100 struct cmesh *cmesh_alloc(void)
104 if(!(cm = malloc(sizeof *cm))) {
107 if(cmesh_init(cm) == -1) {
114 void cmesh_free(struct cmesh *cm)
120 int cmesh_init(struct cmesh *cm)
124 memset(cm, 0, sizeof *cm);
125 cgm_wcons(cm->cur_val + CMESH_ATTR_COLOR, 1, 1, 1, 1);
128 glGenBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
130 for(i=0; i<CMESH_NUM_ATTR; i++) {
131 cm->vattr[i].vbo = cm->buffer_objects[i];
134 cm->ibo = cm->buffer_objects[CMESH_NUM_ATTR];
139 void cmesh_destroy(struct cmesh *cm)
145 for(i=0; i<CMESH_NUM_ATTR; i++) {
146 free(cm->vattr[i].data);
150 cmesh_clear_submeshes(cm);
153 glDeleteBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
155 glDeleteBuffers(1, &cm->wire_ibo);
160 void cmesh_clear(struct cmesh *cm)
164 for(i=0; i<CMESH_NUM_ATTR; i++) {
165 cm->vattr[i].nelem = 0;
166 cm->vattr[i].vbo_valid = 0;
167 cm->vattr[i].data_valid = 0;
168 free(cm->vattr[i].data);
169 cm->vattr[i].data = 0;
170 cm->vattr[i].count = 0;
172 cm->ibo_valid = cm->idata_valid = 0;
177 cm->wire_ibo_valid = 0;
178 cm->nverts = cm->nfaces = 0;
180 cm->bsph_valid = cm->aabb_valid = 0;
182 cmesh_clear_submeshes(cm);
185 int cmesh_clone(struct cmesh *cmdest, struct cmesh *cmsrc)
187 return clone(cmdest, cmsrc, 0);
190 static int clone(struct cmesh *cmdest, struct cmesh *cmsrc, struct submesh *sub)
192 int i, nelem, vstart, vcount, istart, icount;
193 char *srcname, *name = 0;
194 float *varr[CMESH_NUM_ATTR] = {0};
196 unsigned int *iptr, *iarr = 0;
198 /* try do anything that can fail first, before making any changes to cmdest
199 * so we have the option of recovering gracefuly
202 srcname = sub ? sub->name : cmsrc->name;
204 if(!(name = malloc(strlen(srcname) + 1))) {
207 strcpy(name, srcname);
211 vstart = sub->vstart;
212 vcount = sub->vcount;
213 istart = sub->istart;
214 icount = sub->icount;
217 vcount = cmsrc->nverts;
218 icount = cmsrc->icount;
221 if(cmesh_indexed(cmsrc)) {
222 if(!(iarr = malloc(icount * sizeof *iarr))) {
228 for(i=0; i<CMESH_NUM_ATTR; i++) {
229 if(cmesh_has_attrib(cmsrc, i)) {
230 nelem = cmsrc->vattr[i].nelem;
231 if(!(varr[i] = malloc(vcount * nelem * sizeof(float)))) {
242 /* from this point forward nothing can fail */
245 for(i=0; i<CMESH_NUM_ATTR; i++) {
246 free(cmdest->vattr[i].data);
248 if(cmesh_has_attrib(cmsrc, i)) {
249 cmesh_attrib(cmsrc, i); /* force validation of the actual data on the source mesh */
251 nelem = cmsrc->vattr[i].nelem;
252 cmdest->vattr[i].nelem = nelem;
253 cmdest->vattr[i].data = varr[i];
254 cmdest->vattr[i].count = vcount;
255 vptr = cmsrc->vattr[i].data + vstart * nelem;
256 memcpy(cmdest->vattr[i].data, vptr, vcount * nelem * sizeof(float));
257 cmdest->vattr[i].data_valid = 1;
258 cmdest->vattr[i].vbo_valid = 0;
260 memset(cmdest->vattr + i, 0, sizeof cmdest->vattr[i]);
264 if(cmesh_indexed(cmsrc)) {
265 cmesh_index(cmsrc); /* force validation .... */
267 cmdest->idata = iarr;
268 cmdest->icount = icount;
270 /* need to offset all vertex indices by -vstart */
271 iptr = cmsrc->idata + istart;
272 for(i=0; i<icount; i++) {
273 cmdest->idata[i] = *iptr++ - vstart;
276 memcpy(cmdest->idata, cmsrc->idata + istart, icount * sizeof *cmdest->idata);
278 cmdest->idata_valid = 1;
281 cmdest->idata_valid = cmdest->ibo_valid = 0;
287 cmdest->nverts = cmsrc->nverts;
288 cmdest->nfaces = sub ? sub->nfaces : cmsrc->nfaces;
290 memcpy(cmdest->cur_val, cmsrc->cur_val, sizeof cmdest->cur_val);
292 cmdest->aabb_min = cmsrc->aabb_min;
293 cmdest->aabb_max = cmsrc->aabb_max;
294 cmdest->aabb_valid = cmsrc->aabb_valid;
295 cmdest->bsph_center = cmsrc->bsph_center;
296 cmdest->bsph_radius = cmsrc->bsph_radius;
297 cmdest->bsph_valid = cmsrc->bsph_valid;
299 /* copy sublist only if we're not cloning a submesh */
301 struct submesh *sm, *n, *head = 0, *tail = 0;
305 if(!(n = malloc(sizeof *n)) || !(name = malloc(strlen(sm->name) + 1))) {
310 strcpy(name, sm->name);
325 cmdest->sublist = head;
326 cmdest->subcount = cmsrc->subcount;
332 int cmesh_set_name(struct cmesh *cm, const char *name)
334 int len = strlen(name);
335 char *tmp = malloc(len + 1);
339 memcpy(cm->name, name, len + 1);
343 const char *cmesh_name(struct cmesh *cm)
348 int cmesh_has_attrib(struct cmesh *cm, int attr)
350 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
353 return cm->vattr[attr].vbo_valid | cm->vattr[attr].data_valid;
356 int cmesh_indexed(struct cmesh *cm)
358 return cm->ibo_valid | cm->idata_valid;
361 /* vdata can be 0, in which case only memory is allocated
362 * returns pointer to the attribute array
364 float *cmesh_set_attrib(struct cmesh *cm, int attr, int nelem, unsigned int num,
369 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
372 if(cm->nverts && num != cm->nverts) {
376 if(!(newarr = malloc(num * nelem * sizeof *newarr))) {
380 memcpy(newarr, vdata, num * nelem * sizeof *newarr);
385 free(cm->vattr[attr].data);
386 cm->vattr[attr].data = newarr;
387 cm->vattr[attr].count = num * nelem;
388 cm->vattr[attr].nelem = nelem;
389 cm->vattr[attr].data_valid = 1;
390 cm->vattr[attr].vbo_valid = 0;
394 float *cmesh_attrib(struct cmesh *cm, int attr)
396 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
399 cm->vattr[attr].vbo_valid = 0;
400 return (float*)cmesh_attrib_ro(cm, attr);
403 const float *cmesh_attrib_ro(struct cmesh *cm, int attr)
408 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
412 if(!cm->vattr[attr].data_valid) {
413 #if defined(GL_ES_VERSION_2_0) || defined(NO_OPENGL)
416 if(!cm->vattr[attr].vbo_valid) {
420 /* local data copy unavailable, grab the data from the vbo */
421 nelem = cm->vattr[attr].nelem;
422 if(!(cm->vattr[attr].data = malloc(cm->nverts * nelem * sizeof(float)))) {
425 cm->vattr[attr].count = cm->nverts * nelem;
427 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[attr].vbo);
428 tmp = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
429 memcpy(cm->vattr[attr].data, tmp, cm->nverts * nelem * sizeof(float));
430 glUnmapBuffer(GL_ARRAY_BUFFER);
432 cm->vattr[attr].data_valid = 1;
435 return cm->vattr[attr].data;
438 float *cmesh_attrib_at(struct cmesh *cm, int attr, int idx)
440 float *vptr = cmesh_attrib(cm, attr);
441 return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
444 const float *cmesh_attrib_at_ro(struct cmesh *cm, int attr, int idx)
446 const float *vptr = cmesh_attrib_ro(cm, attr);
447 return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
450 int cmesh_attrib_count(struct cmesh *cm, int attr)
452 return cmesh_has_attrib(cm, attr) ? cm->nverts : 0;
455 int cmesh_push_attrib(struct cmesh *cm, int attr, float *v)
460 if(!cm->vattr[attr].nelem) {
461 cm->vattr[attr].nelem = def_nelem[attr];
464 cursz = cm->vattr[attr].count;
465 newsz = cursz + cm->vattr[attr].nelem;
466 if(!(vptr = realloc(cm->vattr[attr].data, newsz * sizeof(float)))) {
469 cm->vattr[attr].data = vptr;
470 cm->vattr[attr].count = newsz;
473 for(i=0; i<cm->vattr[attr].nelem; i++) {
476 cm->vattr[attr].data_valid = 1;
477 cm->vattr[attr].vbo_valid = 0;
479 if(attr == CMESH_ATTR_VERTEX) {
480 cm->nverts = newsz / cm->vattr[attr].nelem;
485 int cmesh_push_attrib1f(struct cmesh *cm, int attr, float x)
491 return cmesh_push_attrib(cm, attr, v);
494 int cmesh_push_attrib2f(struct cmesh *cm, int attr, float x, float y)
501 return cmesh_push_attrib(cm, attr, v);
504 int cmesh_push_attrib3f(struct cmesh *cm, int attr, float x, float y, float z)
511 return cmesh_push_attrib(cm, attr, v);
514 int cmesh_push_attrib4f(struct cmesh *cm, int attr, float x, float y, float z, float w)
521 return cmesh_push_attrib(cm, attr, v);
524 /* indices can be 0, in which case only memory is allocated
525 * returns pointer to the index array
527 unsigned int *cmesh_set_index(struct cmesh *cm, int num, const unsigned int *indices)
530 int nidx = cm->nfaces * 3;
532 if(nidx && num != nidx) {
536 if(!(tmp = malloc(num * sizeof *tmp))) {
540 memcpy(tmp, indices, num * sizeof *tmp);
551 unsigned int *cmesh_index(struct cmesh *cm)
554 return (unsigned int*)cmesh_index_ro(cm);
557 const unsigned int *cmesh_index_ro(struct cmesh *cm)
562 if(!cm->idata_valid) {
563 #if defined(GL_ES_VERSION_2_0) || defined(NO_OPENGL)
570 /* local copy is unavailable, grab the data from the ibo */
571 nidx = cm->nfaces * 3;
572 if(!(tmp = malloc(nidx * sizeof *cm->idata))) {
579 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
580 tmp = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
581 memcpy(cm->idata, tmp, nidx * sizeof *cm->idata);
582 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
590 int cmesh_index_count(struct cmesh *cm)
592 return cm->nfaces * 3;
595 int cmesh_push_index(struct cmesh *cm, unsigned int idx)
598 unsigned int cur_sz = cm->icount;
599 if(!(iptr = realloc(cm->idata, (cur_sz + 1) * sizeof *iptr))) {
604 cm->icount = cur_sz + 1;
608 cm->nfaces = cm->icount / 3;
612 int cmesh_poly_count(struct cmesh *cm)
618 return cm->nverts / 3;
623 /* attr can be -1 to invalidate all attributes */
624 void cmesh_invalidate_vbo(struct cmesh *cm, int attr)
628 if(attr >= CMESH_NUM_ATTR) {
633 for(i=0; i<CMESH_NUM_ATTR; i++) {
634 cm->vattr[i].vbo_valid = 0;
637 cm->vattr[attr].vbo_valid = 0;
641 void cmesh_invalidate_index(struct cmesh *cm)
646 int cmesh_append(struct cmesh *cmdest, struct cmesh *cmsrc)
648 int i, nelem, newsz, origsz, srcsz;
651 unsigned int idxoffs;
653 if(!cmdest->nverts) {
654 return cmesh_clone(cmdest, cmsrc);
657 for(i=0; i<CMESH_NUM_ATTR; i++) {
658 if(cmesh_has_attrib(cmdest, i) && cmesh_has_attrib(cmsrc, i)) {
659 /* force validation of the data arrays */
660 cmesh_attrib(cmdest, i);
661 cmesh_attrib_ro(cmsrc, i);
663 assert(cmdest->vattr[i].nelem == cmsrc->vattr[i].nelem);
664 nelem = cmdest->vattr[i].nelem;
665 origsz = cmdest->nverts * nelem;
666 newsz = cmdest->nverts + cmsrc->nverts * nelem;
668 if(!(vptr = realloc(cmdest->vattr[i].data, newsz * sizeof *vptr))) {
671 memcpy(vptr + origsz, cmsrc->vattr[i].data, cmsrc->nverts * nelem * sizeof(float));
672 cmdest->vattr[i].data = vptr;
673 cmdest->vattr[i].count = newsz;
677 if(cmesh_indexed(cmdest)) {
678 assert(cmesh_indexed(cmsrc));
679 /* force validation ... */
681 cmesh_index_ro(cmsrc);
683 idxoffs = cmdest->nverts;
684 origsz = cmdest->icount;
685 srcsz = cmsrc->icount;
686 newsz = origsz + srcsz;
688 if(!(iptr = realloc(cmdest->idata, newsz * sizeof *iptr))) {
691 cmdest->idata = iptr;
692 cmdest->icount = newsz;
694 /* copy and fixup all the new indices */
696 for(i=0; i<srcsz; i++) {
697 *iptr++ = cmsrc->idata[i] + idxoffs;
701 cmdest->wire_ibo_valid = 0;
702 cmdest->aabb_valid = 0;
703 cmdest->bsph_valid = 0;
707 void cmesh_clear_submeshes(struct cmesh *cm)
713 cm->sublist = cm->sublist->next;
720 int cmesh_submesh(struct cmesh *cm, const char *name, int fstart, int fcount)
723 unsigned int minv = UINT_MAX, maxv = 0;
727 if(fstart < 0 || fcount < 1 || fstart + fcount > cm->nfaces) {
731 if(!(sm = malloc(sizeof *sm)) || !(sm->name = malloc(strlen(name) + 1))) {
735 strcpy(sm->name, name);
738 if(cmesh_indexed(cm)) {
739 sm->istart = fstart * 3;
740 sm->icount = fcount * 3;
742 /* find out which vertices are used by this submesh */
743 iptr = cm->idata + sm->istart;
744 for(i=0; i<sm->icount; i++) {
745 unsigned int vidx = *iptr++;
746 if(vidx < minv) minv = vidx;
747 if(vidx > maxv) maxv = vidx;
750 sm->vcount = maxv - minv + 1;
752 sm->istart = sm->icount = 0;
753 sm->vstart = fstart * 3;
754 sm->vcount = fcount * 3;
757 sm->next = cm->sublist;
763 int cmesh_remove_submesh(struct cmesh *cm, int idx)
765 struct submesh dummy;
766 struct submesh *prev, *sm;
768 if(idx >= cm->subcount) {
772 dummy.next = cm->sublist;
775 while(prev->next && idx-- > 0) {
779 if(!(sm = prev->next)) return -1;
781 prev->next = sm->next;
786 assert(cm->subcount >= 0);
788 cm->sublist = dummy.next;
792 int cmesh_find_submesh(struct cmesh *cm, const char *name)
795 struct submesh *sm = cm->sublist;
797 if(strcmp(sm->name, name) == 0) {
798 assert(idx <= cm->subcount);
807 int cmesh_submesh_count(struct cmesh *cm)
812 static struct submesh *get_submesh(struct cmesh *m, int idx)
814 struct submesh *sm = m->sublist;
815 while(sm && --idx >= 0) {
821 int cmesh_clone_submesh(struct cmesh *cmdest, struct cmesh *cm, int subidx)
825 if(!(sub = get_submesh(cm, subidx))) {
828 return clone(cmdest, cm, sub);
832 /* assemble a complete vertex by adding all the useful attributes */
833 int cmesh_vertex(struct cmesh *cm, float x, float y, float z)
837 cgm_wcons(cm->cur_val + CMESH_ATTR_VERTEX, x, y, z, 1.0f);
838 cm->vattr[CMESH_ATTR_VERTEX].data_valid = 1;
839 cm->vattr[CMESH_ATTR_VERTEX].nelem = 3;
841 for(i=0; i<CMESH_NUM_ATTR; i++) {
842 if(cm->vattr[i].data_valid) {
843 int newsz = cm->vattr[i].count + cm->vattr[i].nelem;
844 float *tmp = realloc(cm->vattr[i].data, newsz * sizeof *tmp);
846 tmp += cm->vattr[i].count;
848 cm->vattr[i].data = tmp;
849 cm->vattr[i].count = newsz;
851 for(j=0; j<cm->vattr[i].nelem; j++) {
852 *tmp++ = *(&cm->cur_val[i].x + j);
855 cm->vattr[i].vbo_valid = 0;
856 cm->vattr[i].data_valid = 1;
859 if(cm->idata_valid) {
864 cm->ibo_valid = cm->idata_valid = 0;
868 void cmesh_normal(struct cmesh *cm, float nx, float ny, float nz)
870 cgm_wcons(cm->cur_val + CMESH_ATTR_NORMAL, nx, ny, nz, 1.0f);
871 cm->vattr[CMESH_ATTR_NORMAL].nelem = 3;
874 void cmesh_tangent(struct cmesh *cm, float tx, float ty, float tz)
876 cgm_wcons(cm->cur_val + CMESH_ATTR_TANGENT, tx, ty, tz, 1.0f);
877 cm->vattr[CMESH_ATTR_TANGENT].nelem = 3;
880 void cmesh_texcoord(struct cmesh *cm, float u, float v, float w)
882 cgm_wcons(cm->cur_val + CMESH_ATTR_TEXCOORD, u, v, w, 1.0f);
883 cm->vattr[CMESH_ATTR_TEXCOORD].nelem = 3;
886 void cmesh_boneweights(struct cmesh *cm, float w1, float w2, float w3, float w4)
888 cgm_wcons(cm->cur_val + CMESH_ATTR_BONEWEIGHTS, w1, w2, w3, w4);
889 cm->vattr[CMESH_ATTR_BONEWEIGHTS].nelem = 4;
892 void cmesh_boneidx(struct cmesh *cm, int idx1, int idx2, int idx3, int idx4)
894 cgm_wcons(cm->cur_val + CMESH_ATTR_BONEIDX, idx1, idx2, idx3, idx4);
895 cm->vattr[CMESH_ATTR_BONEIDX].nelem = 4;
898 static float *get_vec4(struct cmesh *cm, int attr, int idx, cgm_vec4 *res)
902 cgm_wcons(res, 0, 0, 0, 1);
903 if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
908 for(i=0; i<cm->vattr[attr].nelem; i++) {
914 static float *get_vec3(struct cmesh *cm, int attr, int idx, cgm_vec3 *res)
918 cgm_vcons(res, 0, 0, 0);
919 if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
924 for(i=0; i<cm->vattr[attr].nelem; i++) {
930 /* dir_xform can be null, in which case it's calculated from xform */
931 void cmesh_apply_xform(struct cmesh *cm, float *xform, float *dir_xform)
939 for(i=0; i<cm->nverts; i++) {
940 if(!(vptr = get_vec4(cm, CMESH_ATTR_VERTEX, i, &v))) {
943 cgm_wmul_m4v4(&v, xform);
944 for(j=0; j<cm->vattr[CMESH_ATTR_VERTEX].nelem; j++) {
948 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
949 if((vptr = get_vec3(cm, CMESH_ATTR_NORMAL, i, &n))) {
950 cgm_vmul_m3v3(&n, dir_xform);
951 for(j=0; j<cm->vattr[CMESH_ATTR_NORMAL].nelem; j++) {
956 if(cmesh_has_attrib(cm, CMESH_ATTR_TANGENT)) {
957 if((vptr = get_vec3(cm, CMESH_ATTR_TANGENT, i, &t))) {
958 cgm_vmul_m3v3(&t, dir_xform);
959 for(j=0; j<cm->vattr[CMESH_ATTR_TANGENT].nelem; j++) {
967 void cmesh_flip(struct cmesh *cm)
969 cmesh_flip_faces(cm);
970 cmesh_flip_normals(cm);
973 void cmesh_flip_faces(struct cmesh *cm)
975 int i, j, idxnum, vnum, nelem;
976 unsigned int *indices;
979 if(cmesh_indexed(cm)) {
980 if(!(indices = cmesh_index(cm))) {
983 idxnum = cmesh_index_count(cm);
984 for(i=0; i<idxnum; i+=3) {
985 unsigned int tmp = indices[i + 2];
986 indices[i + 2] = indices[i + 1];
987 indices[i + 1] = tmp;
990 if(!(verts = cmesh_attrib(cm, CMESH_ATTR_VERTEX))) {
993 vnum = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
994 nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
995 for(i=0; i<vnum; i+=3) {
996 for(j=0; j<nelem; j++) {
997 vptr = verts + (i + 1) * nelem + j;
998 float tmp = vptr[nelem];
999 vptr[nelem] = vptr[0];
1005 void cmesh_flip_normals(struct cmesh *cm)
1008 float *nptr = cmesh_attrib(cm, CMESH_ATTR_NORMAL);
1011 num = cm->nverts * cm->vattr[CMESH_ATTR_NORMAL].nelem;
1012 for(i=0; i<num; i++) {
1018 int cmesh_explode(struct cmesh *cm)
1020 int i, j, k, idxnum, nnverts;
1021 unsigned int *indices;
1023 if(!cmesh_indexed(cm)) return 0;
1025 indices = cmesh_index(cm);
1028 idxnum = cmesh_index_count(cm);
1031 for(i=0; i<CMESH_NUM_ATTR; i++) {
1032 const float *srcbuf;
1033 float *tmpbuf, *dstptr;
1035 if(!cmesh_has_attrib(cm, i)) continue;
1037 srcbuf = cmesh_attrib(cm, i);
1038 if(!(tmpbuf = malloc(nnverts * cm->vattr[i].nelem * sizeof(float)))) {
1043 for(j=0; j<idxnum; j++) {
1044 unsigned int idx = indices[j];
1045 const float *srcptr = srcbuf + idx * cm->vattr[i].nelem;
1047 for(k=0; k<cm->vattr[i].nelem; k++) {
1048 *dstptr++ = *srcptr++;
1052 free(cm->vattr[i].data);
1053 cm->vattr[i].data = tmpbuf;
1054 cm->vattr[i].count = nnverts * cm->vattr[i].nelem;
1055 cm->vattr[i].data_valid = 1;
1059 cm->idata_valid = 0;
1064 cm->nverts = nnverts;
1065 cm->nfaces = idxnum / 3;
1069 void cmesh_calc_face_normals(struct cmesh *cm)
1075 static int pre_draw(struct cmesh *cm)
1077 int i, loc, cur_sdr;
1079 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1083 if(!cm->vattr[CMESH_ATTR_VERTEX].vbo_valid) {
1087 if(cur_sdr && use_custom_sdr_attr) {
1088 if(sdr_loc[CMESH_ATTR_VERTEX] == -1) {
1092 for(i=0; i<CMESH_NUM_ATTR; i++) {
1094 if(loc >= 0 && cm->vattr[i].vbo_valid) {
1095 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
1096 glVertexAttribPointer(loc, cm->vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
1097 glEnableVertexAttribArray(loc);
1101 #ifndef GL_ES_VERSION_2_0
1102 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_VERTEX].vbo);
1103 glVertexPointer(cm->vattr[CMESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
1104 glEnableClientState(GL_VERTEX_ARRAY);
1106 if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
1107 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_NORMAL].vbo);
1108 glNormalPointer(GL_FLOAT, 0, 0);
1109 glEnableClientState(GL_NORMAL_ARRAY);
1111 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
1112 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD].vbo);
1113 glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
1114 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1116 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
1117 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_COLOR].vbo);
1118 glColorPointer(cm->vattr[CMESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
1119 glEnableClientState(GL_COLOR_ARRAY);
1121 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
1122 glClientActiveTexture(GL_TEXTURE1);
1123 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD2].vbo);
1124 glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD2].nelem, GL_FLOAT, 0, 0);
1125 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1126 glClientActiveTexture(GL_TEXTURE0);
1128 #endif /* GL_ES_VERSION_2_0 */
1130 glBindBuffer(GL_ARRAY_BUFFER, 0);
1134 void cmesh_draw(struct cmesh *cm)
1138 if((cur_sdr = pre_draw(cm)) == -1) {
1143 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1144 glDrawElements(GL_TRIANGLES, cm->nfaces * 3, GL_UNSIGNED_INT, 0);
1145 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1147 glDrawArrays(GL_TRIANGLES, 0, cm->nverts);
1150 post_draw(cm, cur_sdr);
1153 void cmesh_draw_range(struct cmesh *cm, int start, int count)
1157 if((cur_sdr = pre_draw(cm)) == -1) {
1162 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1163 glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (void*)(start * 4));
1164 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1166 glDrawArrays(GL_TRIANGLES, start, count);
1169 post_draw(cm, cur_sdr);
1172 void cmesh_draw_submesh(struct cmesh *cm, int subidx)
1174 struct submesh *sm = cm->sublist;
1176 while(sm && subidx-- > 0) {
1182 cmesh_draw_range(cm, sm->istart, sm->icount);
1184 cmesh_draw_range(cm, sm->vstart, sm->vcount);
1188 static void post_draw(struct cmesh *cm, int cur_sdr)
1192 if(cur_sdr && use_custom_sdr_attr) {
1193 for(i=0; i<CMESH_NUM_ATTR; i++) {
1194 int loc = sdr_loc[i];
1195 if(loc >= 0 && cm->vattr[i].vbo_valid) {
1196 glDisableVertexAttribArray(loc);
1200 #ifndef GL_ES_VERSION_2_0
1201 glDisableClientState(GL_VERTEX_ARRAY);
1202 if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
1203 glDisableClientState(GL_NORMAL_ARRAY);
1205 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
1206 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1208 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
1209 glDisableClientState(GL_COLOR_ARRAY);
1211 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
1212 glClientActiveTexture(GL_TEXTURE1);
1213 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1214 glClientActiveTexture(GL_TEXTURE0);
1216 #endif /* GL_ES_VERSION_2_0 */
1220 void cmesh_draw_wire(struct cmesh *cm, float linesz)
1222 int cur_sdr, nfaces;
1224 if((cur_sdr = pre_draw(cm)) == -1) {
1227 update_wire_ibo(cm);
1229 nfaces = cmesh_poly_count(cm);
1230 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
1231 glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0);
1232 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1234 post_draw(cm, cur_sdr);
1237 void cmesh_draw_vertices(struct cmesh *cm, float ptsz)
1240 if((cur_sdr = pre_draw(cm)) == -1) {
1244 glPushAttrib(GL_POINT_BIT);
1246 glDrawArrays(GL_POINTS, 0, cm->nverts);
1249 post_draw(cm, cur_sdr);
1252 void cmesh_draw_normals(struct cmesh *cm, float len)
1254 #ifndef GL_ES_VERSION_2_0
1255 int i, cur_sdr, vert_nelem, norm_nelem;
1257 const float *varr, *norm;
1259 varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1260 norm = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
1261 if(!varr || !norm) return;
1263 vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1264 norm_nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1266 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1267 if(cur_sdr && use_custom_sdr_attr) {
1268 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1274 for(i=0; i<cm->nverts; i++) {
1275 float x, y, z, endx, endy, endz;
1277 x = varr[i * vert_nelem];
1278 y = varr[i * vert_nelem + 1];
1279 z = varr[i * vert_nelem + 2];
1280 endx = x + norm[i * norm_nelem] * len;
1281 endy = y + norm[i * norm_nelem + 1] * len;
1282 endz = z + norm[i * norm_nelem + 2] * len;
1285 glVertex3f(x, y, z);
1286 glVertex3f(endx, endy, endz);
1288 glVertexAttrib3f(loc, x, y, z);
1289 glVertexAttrib3f(loc, endx, endy, endz);
1293 #endif /* GL_ES_VERSION_2_0 */
1296 void cmesh_draw_tangents(struct cmesh *cm, float len)
1298 #ifndef GL_ES_VERSION_2_0
1299 int i, cur_sdr, vert_nelem, tang_nelem;
1301 const float *varr, *tang;
1303 varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1304 tang = cmesh_attrib_ro(cm, CMESH_ATTR_TANGENT);
1305 if(!varr || !tang) return;
1307 vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1308 tang_nelem = cm->vattr[CMESH_ATTR_TANGENT].nelem;
1310 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1311 if(cur_sdr && use_custom_sdr_attr) {
1312 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1318 for(i=0; i<cm->nverts; i++) {
1319 float x, y, z, endx, endy, endz;
1321 x = varr[i * vert_nelem];
1322 y = varr[i * vert_nelem + 1];
1323 z = varr[i * vert_nelem + 2];
1324 endx = x + tang[i * tang_nelem] * len;
1325 endy = y + tang[i * tang_nelem + 1] * len;
1326 endz = z + tang[i * tang_nelem + 2] * len;
1329 glVertex3f(x, y, z);
1330 glVertex3f(endx, endy, endz);
1332 glVertexAttrib3f(loc, x, y, z);
1333 glVertexAttrib3f(loc, endx, endy, endz);
1337 #endif /* GL_ES_VERSION_2_0 */
1340 static void update_buffers(struct cmesh *cm)
1344 for(i=0; i<CMESH_NUM_ATTR; i++) {
1345 if(cmesh_has_attrib(cm, i) && !cm->vattr[i].vbo_valid) {
1346 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
1347 glBufferData(GL_ARRAY_BUFFER, cm->nverts * cm->vattr[i].nelem * sizeof(float),
1348 cm->vattr[i].data, GL_STATIC_DRAW);
1349 cm->vattr[i].vbo_valid = 1;
1352 glBindBuffer(GL_ARRAY_BUFFER, 0);
1354 if(cm->idata_valid && !cm->ibo_valid) {
1355 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1356 glBufferData(GL_ELEMENT_ARRAY_BUFFER, cm->nfaces * 3 * sizeof(unsigned int),
1357 cm->idata, GL_STATIC_DRAW);
1359 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1363 static void update_wire_ibo(struct cmesh *cm)
1366 unsigned int *wire_idxarr, *dest;
1370 if(cm->wire_ibo_valid) return;
1373 glGenBuffers(1, &cm->wire_ibo);
1375 num_faces = cmesh_poly_count(cm);
1377 if(!(wire_idxarr = malloc(num_faces * 6 * sizeof *wire_idxarr))) {
1383 /* we're dealing with an indexed mesh */
1384 const unsigned int *idxarr = cmesh_index_ro(cm);
1386 for(i=0; i<num_faces; i++) {
1387 *dest++ = idxarr[0];
1388 *dest++ = idxarr[1];
1389 *dest++ = idxarr[1];
1390 *dest++ = idxarr[2];
1391 *dest++ = idxarr[2];
1392 *dest++ = idxarr[0];
1396 /* not an indexed mesh */
1397 for(i=0; i<num_faces; i++) {
1408 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
1409 glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_faces * 6 * sizeof(unsigned int),
1410 wire_idxarr, GL_STATIC_DRAW);
1412 cm->wire_ibo_valid = 1;
1413 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1415 #endif /* !def NO_OPENGL */
1417 static void calc_aabb(struct cmesh *cm)
1421 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1425 cgm_vcons(&cm->aabb_min, FLT_MAX, FLT_MAX, FLT_MAX);
1426 cgm_vcons(&cm->aabb_max, -FLT_MAX, -FLT_MAX, -FLT_MAX);
1428 for(i=0; i<cm->nverts; i++) {
1429 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1430 for(j=0; j<3; j++) {
1431 if(v[j] < (&cm->aabb_min.x)[j]) {
1432 (&cm->aabb_min.x)[j] = v[j];
1434 if(v[j] > (&cm->aabb_max.x)[j]) {
1435 (&cm->aabb_max.x)[j] = v[j];
1442 void cmesh_aabbox(struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax)
1444 if(!cm->aabb_valid) {
1447 *vmin = cm->aabb_min;
1448 *vmax = cm->aabb_max;
1451 static void calc_bsph(struct cmesh *cm)
1456 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1460 cgm_vcons(&cm->bsph_center, 0, 0, 0);
1462 /* first find the center */
1463 for(i=0; i<cm->nverts; i++) {
1464 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1465 cm->bsph_center.x += v[0];
1466 cm->bsph_center.y += v[1];
1467 cm->bsph_center.z += v[2];
1469 s = 1.0f / (float)cm->nverts;
1470 cm->bsph_center.x *= s;
1471 cm->bsph_center.y *= s;
1472 cm->bsph_center.z *= s;
1474 cm->bsph_radius = 0.0f;
1475 for(i=0; i<cm->nverts; i++) {
1476 const cgm_vec3 *v = (const cgm_vec3*)cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1477 if((dist_sq = cgm_vdist_sq(v, &cm->bsph_center)) > cm->bsph_radius) {
1478 cm->bsph_radius = dist_sq;
1481 cm->bsph_radius = sqrt(cm->bsph_radius);
1485 float cmesh_bsphere(struct cmesh *cm, cgm_vec3 *center, float *rad)
1487 if(!cm->bsph_valid) {
1490 *center = cm->bsph_center;
1491 *rad = cm->bsph_radius;
1492 return cm->bsph_radius;
1496 void cmesh_texcoord_apply_xform(struct cmesh *cm, float *xform);
1497 void cmesh_texcoord_gen_plane(struct cmesh *cm, cgm_vec3 *norm, cgm_vec3 *tang);
1498 void cmesh_texcoord_gen_box(struct cmesh *cm);
1499 void cmesh_texcoord_gen_cylinder(struct cmesh *cm);
1501 int cmesh_dump(struct cmesh *cm, const char *fname)
1503 FILE *fp = fopen(fname, "wb");
1505 int res = cmesh_dump_file(cm, fp);
1512 int cmesh_dump_file(struct cmesh *cm, FILE *fp)
1514 static const char *label[] = { "pos", "nor", "tan", "tex", "col", "bw", "bid", "tex2" };
1515 static const char *elemfmt[] = { 0, " %s(%g)", " %s(%g, %g)", " %s(%g, %g, %g)", " %s(%g, %g, %g, %g)", 0 };
1518 if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1522 fprintf(fp, "VERTEX ATTRIBUTES\n");
1524 for(i=0; i<cm->nverts; i++) {
1525 fprintf(fp, "%5u:", i);
1526 for(j=0; j<CMESH_NUM_ATTR; j++) {
1527 if(cmesh_has_attrib(cm, j)) {
1528 const float *v = cmesh_attrib_at_ro(cm, j, i);
1529 int nelem = cm->vattr[j].nelem;
1530 fprintf(fp, elemfmt[nelem], label[j], v[0], nelem > 1 ? v[1] : 0.0f,
1531 nelem > 2 ? v[2] : 0.0f, nelem > 3 ? v[3] : 0.0f);
1537 if(cmesh_indexed(cm)) {
1538 const unsigned int *idx = cmesh_index_ro(cm);
1539 int numidx = cmesh_index_count(cm);
1540 int numtri = numidx / 3;
1541 assert(numidx % 3 == 0);
1543 fprintf(fp, "FACES\n");
1545 for(i=0; i<numtri; i++) {
1546 fprintf(fp, "%5d: %d %d %d\n", i, idx[0], idx[1], idx[2]);
1553 int cmesh_dump_obj(struct cmesh *cm, const char *fname)
1555 FILE *fp = fopen(fname, "wb");
1557 int res = cmesh_dump_obj_file(cm, fp, 0);
1567 int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs)
1569 static const char *fmtstr[] = {" %u", " %u//%u", " %u/%u", " %u/%u/%u"};
1570 int i, j, num, nelem;
1571 unsigned int aflags = 0;
1573 if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1578 nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1579 if((num = cm->vattr[CMESH_ATTR_VERTEX].count) != cm->nverts * nelem) {
1580 fprintf(stderr, "vertex array size (%d) != nverts (%d)\n", num, cm->nverts);
1582 for(i=0; i<cm->nverts; i++) {
1583 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1584 fprintf(fp, "v %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
1587 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
1589 nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1590 if((num = cm->vattr[CMESH_ATTR_NORMAL].count) != cm->nverts * nelem) {
1591 fprintf(stderr, "normal array size (%d) != nverts (%d)\n", num, cm->nverts);
1593 for(i=0; i<cm->nverts; i++) {
1594 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_NORMAL, i);
1595 fprintf(fp, "vn %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
1599 if(cmesh_has_attrib(cm, CMESH_ATTR_TEXCOORD)) {
1601 nelem = cm->vattr[CMESH_ATTR_TEXCOORD].nelem;
1602 if((num = cm->vattr[CMESH_ATTR_TEXCOORD].count) != cm->nverts * nelem) {
1603 fprintf(stderr, "texcoord array size (%d) != nverts (%d)\n", num, cm->nverts);
1605 for(i=0; i<cm->nverts; i++) {
1606 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_TEXCOORD, i);
1607 fprintf(fp, "vt %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f);
1611 if(cmesh_indexed(cm)) {
1612 const unsigned int *idxptr = cmesh_index_ro(cm);
1613 int numidx = cmesh_index_count(cm);
1614 int numtri = numidx / 3;
1615 assert(numidx % 3 == 0);
1617 for(i=0; i<numtri; i++) {
1619 for(j=0; j<3; j++) {
1620 unsigned int idx = *idxptr++ + 1 + voffs;
1621 fprintf(fp, fmtstr[aflags], idx, idx, idx);
1626 int numtri = cm->nverts / 3;
1627 unsigned int idx = 1 + voffs;
1628 for(i=0; i<numtri; i++) {
1630 for(j=0; j<3; j++) {
1631 fprintf(fp, fmtstr[aflags], idx, idx, idx);