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 */
31 struct cmesh_material mtl;
37 unsigned int nverts, nfaces;
39 struct submesh *sublist, *subtail;
42 struct cmesh_material mtl;
44 /* current value for each attribute for the immediate mode interface */
45 cgm_vec4 cur_val[CMESH_NUM_ATTR];
47 unsigned int buffer_objects[CMESH_NUM_ATTR + 1];
48 struct cmesh_vattrib vattr[CMESH_NUM_ATTR];
53 int ibo_valid, idata_valid;
55 /* index buffer for wireframe rendering (constructed on demand) */
56 unsigned int wire_ibo;
59 /* axis-aligned bounding box */
60 cgm_vec3 aabb_min, aabb_max;
69 static int clone(struct cmesh *cmdest, struct cmesh *cmsrc, struct submesh *sub);
70 static int pre_draw(struct cmesh *cm);
71 static void post_draw(struct cmesh *cm, int cur_sdr);
72 static void update_buffers(struct cmesh *cm);
73 static void update_wire_ibo(struct cmesh *cm);
74 static void calc_aabb(struct cmesh *cm);
75 static void calc_bsph_noidx(struct cmesh *cm, int start, int count);
76 static void calc_bsph_idx(struct cmesh *cm, int start, int count);
77 static void clear_mtl(struct cmesh_material *mtl);
78 static void clone_mtl(struct cmesh_material *dest, struct cmesh_material *src);
80 static int def_nelem[CMESH_NUM_ATTR] = {3, 3, 3, 2, 4, 4, 4, 2};
82 static int sdr_loc[CMESH_NUM_ATTR] = {0, 1, 2, 3, 4, 5, 6, 7};
83 static int use_custom_sdr_attr;
85 static const struct cmesh_material defmtl = {
86 0, {1, 1, 1}, {0, 0, 0}, {0, 0, 0}, 1, 1, 1
91 void cmesh_set_attrib_sdrloc(int attr, int loc)
96 int cmesh_get_attrib_sdrloc(int attr)
101 void cmesh_clear_attrib_sdrloc(void)
104 for(i=0; i<CMESH_NUM_ATTR; i++) {
109 void cmesh_bind_sdrloc(unsigned int sdr)
111 glBindAttribLocation(sdr, CMESH_ATTR_VERTEX, "attr_vertex");
112 glBindAttribLocation(sdr, CMESH_ATTR_NORMAL, "attr_normal");
113 glBindAttribLocation(sdr, CMESH_ATTR_TANGENT, "attr_tangent");
114 glBindAttribLocation(sdr, CMESH_ATTR_TEXCOORD, "attr_texcoord");
115 glBindAttribLocation(sdr, CMESH_ATTR_COLOR, "attr_color");
116 glBindAttribLocation(sdr, CMESH_ATTR_BONEWEIGHTS, "attr_boneweights");
117 glBindAttribLocation(sdr, CMESH_ATTR_BONEIDX, "attr_boneidx");
118 glBindAttribLocation(sdr, CMESH_ATTR_TEXCOORD2, "attr_texcoord2");
123 struct cmesh *cmesh_alloc(void)
127 cm = malloc_nf(sizeof *cm);
128 if(cmesh_init(cm) == -1) {
135 void cmesh_free(struct cmesh *cm)
141 int cmesh_init(struct cmesh *cm)
145 memset(cm, 0, sizeof *cm);
146 cgm_wcons(cm->cur_val + CMESH_ATTR_COLOR, 1, 1, 1, 1);
148 glGenBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
150 for(i=0; i<CMESH_NUM_ATTR; i++) {
151 cm->vattr[i].vbo = cm->buffer_objects[i];
154 cm->ibo = cm->buffer_objects[CMESH_NUM_ATTR];
160 void cmesh_destroy(struct cmesh *cm)
166 for(i=0; i<CMESH_NUM_ATTR; i++) {
167 free(cm->vattr[i].data);
171 cmesh_clear_submeshes(cm);
173 glDeleteBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
175 glDeleteBuffers(1, &cm->wire_ibo);
181 void cmesh_clear(struct cmesh *cm)
185 for(i=0; i<CMESH_NUM_ATTR; i++) {
186 cm->vattr[i].nelem = 0;
187 cm->vattr[i].vbo_valid = 0;
188 cm->vattr[i].data_valid = 0;
189 free(cm->vattr[i].data);
190 cm->vattr[i].data = 0;
191 cm->vattr[i].count = 0;
193 cm->ibo_valid = cm->idata_valid = 0;
198 cm->wire_ibo_valid = 0;
199 cm->nverts = cm->nfaces = 0;
201 cm->bsph_valid = cm->aabb_valid = 0;
203 cmesh_clear_submeshes(cm);
207 int cmesh_clone(struct cmesh *cmdest, struct cmesh *cmsrc)
209 return clone(cmdest, cmsrc, 0);
212 static int clone(struct cmesh *cmdest, struct cmesh *cmsrc, struct submesh *sub)
214 int i, nelem, vstart, vcount, istart, icount;
215 char *srcname, *name = 0;
216 float *varr[CMESH_NUM_ATTR] = {0};
218 unsigned int *iptr, *iarr = 0;
219 struct cmesh_material mtl;
221 srcname = sub ? sub->name : cmsrc->name;
223 name = malloc_nf(strlen(srcname) + 1);
224 strcpy(name, srcname);
228 vstart = sub->vstart;
229 vcount = sub->vcount;
230 istart = sub->istart;
231 icount = sub->icount;
234 vcount = cmsrc->nverts;
235 icount = cmsrc->icount;
238 if(cmesh_indexed(cmsrc)) {
239 iarr = malloc_nf(icount * sizeof *iarr);
242 clone_mtl(&mtl, sub ? &sub->mtl : &cmsrc->mtl);
244 for(i=0; i<CMESH_NUM_ATTR; i++) {
245 if(cmesh_has_attrib(cmsrc, i)) {
246 nelem = cmsrc->vattr[i].nelem;
247 varr[i] = malloc_nf(vcount * nelem * sizeof(float));
251 /* from this point forward nothing can fail */
254 for(i=0; i<CMESH_NUM_ATTR; i++) {
255 free(cmdest->vattr[i].data);
257 if(cmesh_has_attrib(cmsrc, i)) {
258 cmesh_attrib(cmsrc, i); /* force validation of the actual data on the source mesh */
260 nelem = cmsrc->vattr[i].nelem;
261 cmdest->vattr[i].nelem = nelem;
262 cmdest->vattr[i].data = varr[i];
263 cmdest->vattr[i].count = vcount;
264 vptr = cmsrc->vattr[i].data + vstart * nelem;
265 memcpy(cmdest->vattr[i].data, vptr, vcount * nelem * sizeof(float));
266 cmdest->vattr[i].data_valid = 1;
267 cmdest->vattr[i].vbo_valid = 0;
269 memset(cmdest->vattr + i, 0, sizeof cmdest->vattr[i]);
273 if(cmesh_indexed(cmsrc)) {
274 cmesh_index(cmsrc); /* force validation .... */
276 cmdest->idata = iarr;
277 cmdest->icount = icount;
279 /* need to offset all vertex indices by -vstart */
280 iptr = cmsrc->idata + istart;
281 for(i=0; i<icount; i++) {
282 cmdest->idata[i] = *iptr++ - vstart;
285 memcpy(cmdest->idata, cmsrc->idata + istart, icount * sizeof *cmdest->idata);
287 cmdest->idata_valid = 1;
290 cmdest->idata_valid = cmdest->ibo_valid = 0;
296 cmdest->nverts = cmsrc->nverts;
297 cmdest->nfaces = sub ? sub->nfaces : cmsrc->nfaces;
299 memcpy(cmdest->cur_val, cmsrc->cur_val, sizeof cmdest->cur_val);
301 cmdest->aabb_min = cmsrc->aabb_min;
302 cmdest->aabb_max = cmsrc->aabb_max;
303 cmdest->aabb_valid = cmsrc->aabb_valid;
304 cmdest->bsph_center = cmsrc->bsph_center;
305 cmdest->bsph_radius = cmsrc->bsph_radius;
306 cmdest->bsph_valid = cmsrc->bsph_valid;
308 /* copy sublist only if we're not cloning a submesh */
310 struct submesh *sm, *n, *head = 0, *tail = 0;
314 n = malloc_nf(sizeof *n);
315 name = strdup_nf(sm->name);
330 cmdest->sublist = head;
331 cmdest->subtail = tail;
332 cmdest->subcount = cmsrc->subcount;
338 int cmesh_set_name(struct cmesh *cm, const char *name)
341 cm->name = strdup_nf(name);
345 const char *cmesh_name(struct cmesh *cm)
350 struct cmesh_material *cmesh_material(struct cmesh *cm)
355 struct cmesh_material *cmesh_submesh_material(struct cmesh *cm, int subidx)
357 struct submesh *sm = cm->sublist;
359 while(sm && subidx-- > 0) {
362 return sm ? &sm->mtl : 0;
365 static int load_textures(struct cmesh_material *mtl)
368 for(i=0; i<CMESH_NUM_TEX; i++) {
369 if(mtl->tex[i].name && !(mtl->tex[i].id = get_tex2d(mtl->tex[i].name))) {
376 int cmesh_load_textures(struct cmesh *cm)
381 if(load_textures(&cm->mtl) == -1) {
387 if(load_textures(&sm->mtl) == -1) {
395 int cmesh_has_attrib(struct cmesh *cm, int attr)
397 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
400 return cm->vattr[attr].vbo_valid | cm->vattr[attr].data_valid;
403 int cmesh_indexed(struct cmesh *cm)
405 return cm->ibo_valid | cm->idata_valid;
408 /* vdata can be 0, in which case only memory is allocated
409 * returns pointer to the attribute array
411 float *cmesh_set_attrib(struct cmesh *cm, int attr, int nelem, unsigned int num,
416 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
419 if(cm->nverts && num != cm->nverts) {
423 newarr = malloc_nf(num * nelem * sizeof *newarr);
425 memcpy(newarr, vdata, num * nelem * sizeof *newarr);
430 free(cm->vattr[attr].data);
431 cm->vattr[attr].data = newarr;
432 cm->vattr[attr].count = num * nelem;
433 cm->vattr[attr].nelem = nelem;
434 cm->vattr[attr].data_valid = 1;
435 cm->vattr[attr].vbo_valid = 0;
439 float *cmesh_attrib(struct cmesh *cm, int attr)
441 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
444 cm->vattr[attr].vbo_valid = 0;
445 return (float*)cmesh_attrib_ro(cm, attr);
448 const float *cmesh_attrib_ro(struct cmesh *cm, int attr)
450 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
454 if(!cm->vattr[attr].data_valid) {
455 #if GL_ES_VERSION_2_0
460 if(!cm->vattr[attr].vbo_valid) {
464 /* local data copy unavailable, grab the data from the vbo */
465 nelem = cm->vattr[attr].nelem;
466 cm->vattr[attr].data = malloc_nf(cm->nverts * nelem * sizeof(float));
467 cm->vattr[attr].count = cm->nverts * nelem;
469 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[attr].vbo);
470 tmp = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
471 memcpy(cm->vattr[attr].data, tmp, cm->nverts * nelem * sizeof(float));
472 glUnmapBuffer(GL_ARRAY_BUFFER);
474 cm->vattr[attr].data_valid = 1;
477 return cm->vattr[attr].data;
480 float *cmesh_attrib_at(struct cmesh *cm, int attr, int idx)
482 float *vptr = cmesh_attrib(cm, attr);
483 return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
486 const float *cmesh_attrib_at_ro(struct cmesh *cm, int attr, int idx)
488 const float *vptr = cmesh_attrib_ro(cm, attr);
489 return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
492 int cmesh_attrib_count(struct cmesh *cm, int attr)
494 return cmesh_has_attrib(cm, attr) ? cm->nverts : 0;
497 int cmesh_push_attrib(struct cmesh *cm, int attr, float *v)
502 if(!cm->vattr[attr].nelem) {
503 cm->vattr[attr].nelem = def_nelem[attr];
506 cursz = cm->vattr[attr].count;
507 newsz = cursz + cm->vattr[attr].nelem;
508 vptr = realloc_nf(cm->vattr[attr].data, newsz * sizeof(float));
509 cm->vattr[attr].data = vptr;
510 cm->vattr[attr].count = newsz;
513 for(i=0; i<cm->vattr[attr].nelem; i++) {
516 cm->vattr[attr].data_valid = 1;
517 cm->vattr[attr].vbo_valid = 0;
519 if(attr == CMESH_ATTR_VERTEX) {
520 cm->nverts = newsz / cm->vattr[attr].nelem;
525 int cmesh_push_attrib1f(struct cmesh *cm, int attr, float x)
531 return cmesh_push_attrib(cm, attr, v);
534 int cmesh_push_attrib2f(struct cmesh *cm, int attr, float x, float y)
541 return cmesh_push_attrib(cm, attr, v);
544 int cmesh_push_attrib3f(struct cmesh *cm, int attr, float x, float y, float z)
551 return cmesh_push_attrib(cm, attr, v);
554 int cmesh_push_attrib4f(struct cmesh *cm, int attr, float x, float y, float z, float w)
561 return cmesh_push_attrib(cm, attr, v);
564 /* indices can be 0, in which case only memory is allocated
565 * returns pointer to the index array
567 unsigned int *cmesh_set_index(struct cmesh *cm, int num, const unsigned int *indices)
570 int nidx = cm->nfaces * 3;
572 if(nidx && num != nidx) {
576 tmp = malloc_nf(num * sizeof *tmp);
578 memcpy(tmp, indices, num * sizeof *tmp);
584 cm->nfaces = num / 3;
590 unsigned int *cmesh_index(struct cmesh *cm)
593 return (unsigned int*)cmesh_index_ro(cm);
596 const unsigned int *cmesh_index_ro(struct cmesh *cm)
598 if(!cm->idata_valid) {
599 #if GL_ES_VERSION_2_0
609 /* local copy is unavailable, grab the data from the ibo */
610 nidx = cm->nfaces * 3;
611 tmp = malloc_nf(nidx * sizeof *cm->idata);
616 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
617 tmp = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
618 memcpy(cm->idata, tmp, nidx * sizeof *cm->idata);
619 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
627 int cmesh_index_count(struct cmesh *cm)
629 return cm->nfaces * 3;
632 int cmesh_push_index(struct cmesh *cm, unsigned int idx)
635 unsigned int cur_sz = cm->icount;
636 iptr = realloc_nf(cm->idata, (cur_sz + 1) * sizeof *iptr);
639 cm->icount = cur_sz + 1;
643 cm->nfaces = cm->icount / 3;
647 int cmesh_poly_count(struct cmesh *cm)
653 return cm->nverts / 3;
658 /* attr can be -1 to invalidate all attributes */
659 void cmesh_invalidate_vbo(struct cmesh *cm, int attr)
663 if(attr >= CMESH_NUM_ATTR) {
668 for(i=0; i<CMESH_NUM_ATTR; i++) {
669 cm->vattr[i].vbo_valid = 0;
672 cm->vattr[attr].vbo_valid = 0;
676 void cmesh_invalidate_index(struct cmesh *cm)
681 int cmesh_append(struct cmesh *cmdest, struct cmesh *cmsrc)
683 int i, nelem, newsz, origsz, srcsz;
686 unsigned int idxoffs;
688 if(!cmdest->nverts) {
689 return cmesh_clone(cmdest, cmsrc);
692 for(i=0; i<CMESH_NUM_ATTR; i++) {
693 if(cmesh_has_attrib(cmdest, i) && cmesh_has_attrib(cmsrc, i)) {
694 /* force validation of the data arrays */
695 cmesh_attrib(cmdest, i);
696 cmesh_attrib_ro(cmsrc, i);
698 assert(cmdest->vattr[i].nelem == cmsrc->vattr[i].nelem);
699 nelem = cmdest->vattr[i].nelem;
700 origsz = cmdest->nverts * nelem;
701 newsz = cmdest->nverts + cmsrc->nverts * nelem;
703 vptr = realloc_nf(cmdest->vattr[i].data, newsz * sizeof *vptr);
704 memcpy(vptr + origsz, cmsrc->vattr[i].data, cmsrc->nverts * nelem * sizeof(float));
705 cmdest->vattr[i].data = vptr;
706 cmdest->vattr[i].count = newsz;
710 if(cmesh_indexed(cmdest)) {
711 assert(cmesh_indexed(cmsrc));
712 /* force validation ... */
714 cmesh_index_ro(cmsrc);
716 idxoffs = cmdest->nverts;
717 origsz = cmdest->icount;
718 srcsz = cmsrc->icount;
719 newsz = origsz + srcsz;
721 iptr = realloc_nf(cmdest->idata, newsz * sizeof *iptr);
722 cmdest->idata = iptr;
723 cmdest->icount = newsz;
725 /* copy and fixup all the new indices */
727 for(i=0; i<srcsz; i++) {
728 *iptr++ = cmsrc->idata[i] + idxoffs;
732 cmdest->wire_ibo_valid = 0;
733 cmdest->aabb_valid = 0;
734 cmdest->bsph_valid = 0;
738 void cmesh_clear_submeshes(struct cmesh *cm)
744 cm->sublist = cm->sublist->next;
753 int cmesh_submesh(struct cmesh *cm, const char *name, int fstart, int fcount)
756 unsigned int minv = UINT_MAX, maxv = 0;
760 if(fstart < 0 || fcount < 1 || fstart + fcount > cm->nfaces) {
764 sm = malloc_nf(sizeof *sm);
765 sm->name = strdup_nf(name);
767 clone_mtl(&sm->mtl, &cm->mtl);
769 if(cmesh_indexed(cm)) {
770 sm->istart = fstart * 3;
771 sm->icount = fcount * 3;
773 /* find out which vertices are used by this submesh */
774 iptr = cm->idata + sm->istart;
775 for(i=0; i<sm->icount; i++) {
776 unsigned int vidx = *iptr++;
777 if(vidx < minv) minv = vidx;
778 if(vidx > maxv) maxv = vidx;
781 sm->vcount = maxv - minv + 1;
783 sm->istart = sm->icount = 0;
784 sm->vstart = fstart * 3;
785 sm->vcount = fcount * 3;
790 cm->subtail->next = sm;
793 cm->sublist = cm->subtail = sm;
799 int cmesh_remove_submesh(struct cmesh *cm, int idx)
801 struct submesh dummy;
802 struct submesh *prev, *sm;
804 if(idx >= cm->subcount) {
808 dummy.next = cm->sublist;
811 while(prev->next && idx-- > 0) {
815 if(!(sm = prev->next)) return -1;
817 prev->next = sm->next;
823 assert(cm->subcount >= 0);
825 cm->sublist = dummy.next;
827 cm->sublist = cm->subtail = 0;
829 if(cm->subtail == sm) cm->subtail = prev;
834 int cmesh_find_submesh(struct cmesh *cm, const char *name)
837 struct submesh *sm = cm->sublist;
839 if(strcmp(sm->name, name) == 0) {
840 assert(idx <= cm->subcount);
849 int cmesh_submesh_count(struct cmesh *cm)
854 static struct submesh *get_submesh(struct cmesh *m, int idx)
856 struct submesh *sm = m->sublist;
857 while(sm && --idx >= 0) {
863 int cmesh_clone_submesh(struct cmesh *cmdest, struct cmesh *cm, int subidx)
867 if(!(sub = get_submesh(cm, subidx))) {
870 return clone(cmdest, cm, sub);
873 const char *cmesh_submesh_name(struct cmesh *cm, int idx)
877 if(!(sub = get_submesh(cm, idx))) {
885 /* assemble a complete vertex by adding all the useful attributes */
886 int cmesh_vertex(struct cmesh *cm, float x, float y, float z)
890 cgm_wcons(cm->cur_val + CMESH_ATTR_VERTEX, x, y, z, 1.0f);
891 cm->vattr[CMESH_ATTR_VERTEX].data_valid = 1;
892 cm->vattr[CMESH_ATTR_VERTEX].nelem = 3;
894 for(i=0; i<CMESH_NUM_ATTR; i++) {
895 if(cm->vattr[i].data_valid) {
896 int newsz = cm->vattr[i].count + cm->vattr[i].nelem;
897 float *tmp = realloc_nf(cm->vattr[i].data, newsz * sizeof *tmp);
898 tmp += cm->vattr[i].count;
900 cm->vattr[i].data = tmp;
901 cm->vattr[i].count = newsz;
903 for(j=0; j<cm->vattr[i].nelem; j++) {
904 *tmp++ = *(&cm->cur_val[i].x + j);
907 cm->vattr[i].vbo_valid = 0;
908 cm->vattr[i].data_valid = 1;
911 if(cm->idata_valid) {
916 cm->ibo_valid = cm->idata_valid = 0;
920 void cmesh_normal(struct cmesh *cm, float nx, float ny, float nz)
922 cgm_wcons(cm->cur_val + CMESH_ATTR_NORMAL, nx, ny, nz, 1.0f);
923 cm->vattr[CMESH_ATTR_NORMAL].nelem = 3;
926 void cmesh_tangent(struct cmesh *cm, float tx, float ty, float tz)
928 cgm_wcons(cm->cur_val + CMESH_ATTR_TANGENT, tx, ty, tz, 1.0f);
929 cm->vattr[CMESH_ATTR_TANGENT].nelem = 3;
932 void cmesh_texcoord(struct cmesh *cm, float u, float v, float w)
934 cgm_wcons(cm->cur_val + CMESH_ATTR_TEXCOORD, u, v, w, 1.0f);
935 cm->vattr[CMESH_ATTR_TEXCOORD].nelem = 3;
938 void cmesh_boneweights(struct cmesh *cm, float w1, float w2, float w3, float w4)
940 cgm_wcons(cm->cur_val + CMESH_ATTR_BONEWEIGHTS, w1, w2, w3, w4);
941 cm->vattr[CMESH_ATTR_BONEWEIGHTS].nelem = 4;
944 void cmesh_boneidx(struct cmesh *cm, int idx1, int idx2, int idx3, int idx4)
946 cgm_wcons(cm->cur_val + CMESH_ATTR_BONEIDX, idx1, idx2, idx3, idx4);
947 cm->vattr[CMESH_ATTR_BONEIDX].nelem = 4;
950 static float *get_vec4(struct cmesh *cm, int attr, int idx, cgm_vec4 *res)
954 cgm_wcons(res, 0, 0, 0, 1);
955 if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
960 for(i=0; i<cm->vattr[attr].nelem; i++) {
966 static float *get_vec3(struct cmesh *cm, int attr, int idx, cgm_vec3 *res)
970 cgm_vcons(res, 0, 0, 0);
971 if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
976 for(i=0; i<cm->vattr[attr].nelem; i++) {
982 void cmesh_apply_xform(struct cmesh *cm, float *xform, float *dir_xform)
990 if(!dir_xform) dir_xform = xform;
992 for(i=0; i<cm->nverts; i++) {
993 if(!(vptr = get_vec4(cm, CMESH_ATTR_VERTEX, i, &v))) {
996 cgm_wmul_m4v4(&v, xform);
997 for(j=0; j<cm->vattr[CMESH_ATTR_VERTEX].nelem; j++) {
1001 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
1002 if((vptr = get_vec3(cm, CMESH_ATTR_NORMAL, i, &n))) {
1003 cgm_vmul_m3v3(&n, dir_xform);
1004 for(j=0; j<cm->vattr[CMESH_ATTR_NORMAL].nelem; j++) {
1005 *vptr++ = (&n.x)[j];
1009 if(cmesh_has_attrib(cm, CMESH_ATTR_TANGENT)) {
1010 if((vptr = get_vec3(cm, CMESH_ATTR_TANGENT, i, &t))) {
1011 cgm_vmul_m3v3(&t, dir_xform);
1012 for(j=0; j<cm->vattr[CMESH_ATTR_TANGENT].nelem; j++) {
1013 *vptr++ = (&t.x)[j];
1020 void cmesh_flip(struct cmesh *cm)
1022 cmesh_flip_faces(cm);
1023 cmesh_flip_normals(cm);
1026 void cmesh_flip_faces(struct cmesh *cm)
1028 int i, j, idxnum, vnum, nelem;
1029 unsigned int *indices;
1030 float *verts, *vptr;
1032 if(cmesh_indexed(cm)) {
1033 if(!(indices = cmesh_index(cm))) {
1036 idxnum = cmesh_index_count(cm);
1037 for(i=0; i<idxnum; i+=3) {
1038 unsigned int tmp = indices[i + 2];
1039 indices[i + 2] = indices[i + 1];
1040 indices[i + 1] = tmp;
1043 if(!(verts = cmesh_attrib(cm, CMESH_ATTR_VERTEX))) {
1046 vnum = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
1047 nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1048 for(i=0; i<vnum; i+=3) {
1049 for(j=0; j<nelem; j++) {
1050 vptr = verts + (i + 1) * nelem + j;
1051 float tmp = vptr[nelem];
1052 vptr[nelem] = vptr[0];
1058 void cmesh_flip_normals(struct cmesh *cm)
1061 float *nptr = cmesh_attrib(cm, CMESH_ATTR_NORMAL);
1064 num = cm->nverts * cm->vattr[CMESH_ATTR_NORMAL].nelem;
1065 for(i=0; i<num; i++) {
1071 int cmesh_explode(struct cmesh *cm)
1073 int i, j, k, idxnum, nnverts;
1074 unsigned int *indices;
1076 if(!cmesh_indexed(cm)) return 0;
1078 indices = cmesh_index(cm);
1081 idxnum = cmesh_index_count(cm);
1084 for(i=0; i<CMESH_NUM_ATTR; i++) {
1085 const float *srcbuf;
1086 float *tmpbuf, *dstptr;
1088 if(!cmesh_has_attrib(cm, i)) continue;
1090 srcbuf = cmesh_attrib(cm, i);
1091 tmpbuf = malloc_nf(nnverts * cm->vattr[i].nelem * sizeof(float));
1094 for(j=0; j<idxnum; j++) {
1095 unsigned int idx = indices[j];
1096 const float *srcptr = srcbuf + idx * cm->vattr[i].nelem;
1098 for(k=0; k<cm->vattr[i].nelem; k++) {
1099 *dstptr++ = *srcptr++;
1103 free(cm->vattr[i].data);
1104 cm->vattr[i].data = tmpbuf;
1105 cm->vattr[i].count = nnverts * cm->vattr[i].nelem;
1106 cm->vattr[i].data_valid = 1;
1110 cm->idata_valid = 0;
1115 cm->nverts = nnverts;
1116 cm->nfaces = idxnum / 3;
1120 void cmesh_calc_face_normals(struct cmesh *cm)
1125 static int pre_draw(struct cmesh *cm)
1131 if(!cm->vattr[CMESH_ATTR_VERTEX].vbo_valid) {
1135 if(sdr_loc[CMESH_ATTR_VERTEX] == -1) {
1139 for(i=0; i<CMESH_NUM_ATTR; i++) {
1141 if(loc >= 0 && cm->vattr[i].vbo_valid) {
1142 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
1143 glVertexAttribPointer(loc, cm->vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
1144 glEnableVertexAttribArray(loc);
1147 glBindBuffer(GL_ARRAY_BUFFER, 0);
1151 void cmesh_draw(struct cmesh *cm)
1156 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1157 glDrawElements(GL_TRIANGLES, cm->nfaces * 3, GL_UNSIGNED_INT, 0);
1158 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1160 glDrawArrays(GL_TRIANGLES, 0, cm->nverts);
1166 void cmesh_draw_range(struct cmesh *cm, int start, int count)
1170 if((cur_sdr = pre_draw(cm)) == -1) {
1175 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1176 glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (void*)(uintptr_t)(start * 4));
1177 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1179 glDrawArrays(GL_TRIANGLES, start, count);
1182 post_draw(cm, cur_sdr);
1185 void cmesh_draw_submesh(struct cmesh *cm, int subidx)
1187 struct submesh *sm = cm->sublist;
1189 while(sm && subidx-- > 0) {
1195 cmesh_draw_range(cm, sm->istart, sm->icount);
1197 cmesh_draw_range(cm, sm->vstart, sm->vcount);
1201 static void post_draw(struct cmesh *cm, int cur_sdr)
1205 for(i=0; i<CMESH_NUM_ATTR; i++) {
1206 int loc = sdr_loc[i];
1207 if(loc >= 0 && cm->vattr[i].vbo_valid) {
1208 glDisableVertexAttribArray(loc);
1213 void cmesh_draw_wire(struct cmesh *cm, float linesz)
1215 int cur_sdr, nfaces;
1217 if((cur_sdr = pre_draw(cm)) == -1) {
1220 update_wire_ibo(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(struct cmesh *cm, float ptsz)
1233 if((cur_sdr = pre_draw(cm)) == -1) {
1237 /* TODO use billboards if needed */
1238 glDrawArrays(GL_POINTS, 0, cm->nverts);
1240 post_draw(cm, cur_sdr);
1243 void cmesh_draw_normals(struct cmesh *cm, float len)
1245 #ifndef GL_ES_VERSION_2_0
1246 int i, cur_sdr, vert_nelem, norm_nelem;
1248 const float *varr, *norm;
1250 varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1251 norm = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
1252 if(!varr || !norm) return;
1254 vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1255 norm_nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1257 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1258 if(cur_sdr && use_custom_sdr_attr) {
1259 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1265 for(i=0; i<cm->nverts; i++) {
1266 float x, y, z, endx, endy, endz;
1268 x = varr[i * vert_nelem];
1269 y = varr[i * vert_nelem + 1];
1270 z = varr[i * vert_nelem + 2];
1271 endx = x + norm[i * norm_nelem] * len;
1272 endy = y + norm[i * norm_nelem + 1] * len;
1273 endz = z + norm[i * norm_nelem + 2] * len;
1276 glVertex3f(x, y, z);
1277 glVertex3f(endx, endy, endz);
1279 glVertexAttrib3f(loc, x, y, z);
1280 glVertexAttrib3f(loc, endx, endy, endz);
1284 #endif /* GL_ES_VERSION_2_0 */
1287 void cmesh_draw_tangents(struct cmesh *cm, float len)
1289 #ifndef GL_ES_VERSION_2_0
1290 int i, cur_sdr, vert_nelem, tang_nelem;
1292 const float *varr, *tang;
1294 varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1295 tang = cmesh_attrib_ro(cm, CMESH_ATTR_TANGENT);
1296 if(!varr || !tang) return;
1298 vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1299 tang_nelem = cm->vattr[CMESH_ATTR_TANGENT].nelem;
1301 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1302 if(cur_sdr && use_custom_sdr_attr) {
1303 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1309 for(i=0; i<cm->nverts; i++) {
1310 float x, y, z, endx, endy, endz;
1312 x = varr[i * vert_nelem];
1313 y = varr[i * vert_nelem + 1];
1314 z = varr[i * vert_nelem + 2];
1315 endx = x + tang[i * tang_nelem] * len;
1316 endy = y + tang[i * tang_nelem + 1] * len;
1317 endz = z + tang[i * tang_nelem + 2] * len;
1320 glVertex3f(x, y, z);
1321 glVertex3f(endx, endy, endz);
1323 glVertexAttrib3f(loc, x, y, z);
1324 glVertexAttrib3f(loc, endx, endy, endz);
1328 #endif /* GL_ES_VERSION_2_0 */
1331 static void update_buffers(struct cmesh *cm)
1335 for(i=0; i<CMESH_NUM_ATTR; i++) {
1336 if(cmesh_has_attrib(cm, i) && !cm->vattr[i].vbo_valid) {
1337 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
1338 glBufferData(GL_ARRAY_BUFFER, cm->nverts * cm->vattr[i].nelem * sizeof(float),
1339 cm->vattr[i].data, GL_STATIC_DRAW);
1340 cm->vattr[i].vbo_valid = 1;
1343 glBindBuffer(GL_ARRAY_BUFFER, 0);
1345 if(cm->idata_valid && !cm->ibo_valid) {
1346 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1347 glBufferData(GL_ELEMENT_ARRAY_BUFFER, cm->nfaces * 3 * sizeof(unsigned int),
1348 cm->idata, GL_STATIC_DRAW);
1350 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1354 static void update_wire_ibo(struct cmesh *cm)
1357 unsigned int *wire_idxarr, *dest;
1361 if(cm->wire_ibo_valid) return;
1364 glGenBuffers(1, &cm->wire_ibo);
1366 num_faces = cmesh_poly_count(cm);
1368 wire_idxarr = malloc_nf(num_faces * 6 * sizeof *wire_idxarr);
1372 /* we're dealing with an indexed mesh */
1373 const unsigned int *idxarr = cmesh_index_ro(cm);
1375 for(i=0; i<num_faces; i++) {
1376 *dest++ = idxarr[0];
1377 *dest++ = idxarr[1];
1378 *dest++ = idxarr[1];
1379 *dest++ = idxarr[2];
1380 *dest++ = idxarr[2];
1381 *dest++ = idxarr[0];
1385 /* not an indexed mesh */
1386 for(i=0; i<num_faces; i++) {
1397 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
1398 glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_faces * 6 * sizeof(unsigned int),
1399 wire_idxarr, GL_STATIC_DRAW);
1401 cm->wire_ibo_valid = 1;
1402 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1405 static void calc_aabb(struct cmesh *cm)
1409 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1413 cgm_vcons(&cm->aabb_min, FLT_MAX, FLT_MAX, FLT_MAX);
1414 cgm_vcons(&cm->aabb_max, -FLT_MAX, -FLT_MAX, -FLT_MAX);
1416 for(i=0; i<cm->nverts; i++) {
1417 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1418 for(j=0; j<3; j++) {
1419 if(v[j] < (&cm->aabb_min.x)[j]) {
1420 (&cm->aabb_min.x)[j] = v[j];
1422 if(v[j] > (&cm->aabb_max.x)[j]) {
1423 (&cm->aabb_max.x)[j] = v[j];
1430 void cmesh_aabbox(struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax)
1432 if(!cm->aabb_valid) {
1435 *vmin = cm->aabb_min;
1436 *vmax = cm->aabb_max;
1439 static void calc_bsph_noidx(struct cmesh *cm, int start, int count)
1444 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1447 if(count <= 0) count = cm->nverts;
1449 cgm_vcons(&cm->bsph_center, 0, 0, 0);
1451 /* first find the center */
1452 for(i=0; i<count; i++) {
1453 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i + start);
1454 cm->bsph_center.x += v[0];
1455 cm->bsph_center.y += v[1];
1456 cm->bsph_center.z += v[2];
1458 s = 1.0f / (float)count;
1459 cm->bsph_center.x *= s;
1460 cm->bsph_center.y *= s;
1461 cm->bsph_center.z *= s;
1463 cm->bsph_radius = 0.0f;
1464 for(i=0; i<count; i++) {
1465 const cgm_vec3 *v = (const cgm_vec3*)cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i + start);
1466 if((dist_sq = cgm_vdist_sq(v, &cm->bsph_center)) > cm->bsph_radius) {
1467 cm->bsph_radius = dist_sq;
1470 cm->bsph_radius = sqrt(cm->bsph_radius);
1474 static void calc_bsph_idx(struct cmesh *cm, int start, int count)
1479 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX) || !cmesh_index_ro(cm)) {
1482 if(count <= 0) count = cm->icount;
1484 cgm_vcons(&cm->bsph_center, 0, 0, 0);
1486 /* first find the center */
1487 for(i=0; i<count; i++) {
1488 int idx = cm->idata[i + start];
1489 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, idx);
1490 cm->bsph_center.x += v[0];
1491 cm->bsph_center.y += v[1];
1492 cm->bsph_center.z += v[2];
1494 s = 1.0f / (float)count;
1495 cm->bsph_center.x *= s;
1496 cm->bsph_center.y *= s;
1497 cm->bsph_center.z *= s;
1499 cm->bsph_radius = 0.0f;
1500 for(i=0; i<count; i++) {
1501 int idx = cm->idata[i + start];
1502 const cgm_vec3 *v = (const cgm_vec3*)cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, idx);
1503 if((dist_sq = cgm_vdist_sq(v, &cm->bsph_center)) > cm->bsph_radius) {
1504 cm->bsph_radius = dist_sq;
1507 cm->bsph_radius = sqrt(cm->bsph_radius);
1512 float cmesh_bsphere(struct cmesh *cm, cgm_vec3 *center, float *rad)
1514 if(!cm->bsph_valid) {
1515 calc_bsph_noidx(cm, 0, 0);
1517 if(center) *center = cm->bsph_center;
1518 if(rad) *rad = cm->bsph_radius;
1519 return cm->bsph_radius;
1522 float cmesh_submesh_bsphere(struct cmesh *cm, int subidx, cgm_vec3 *center, float *rad)
1524 struct submesh *sm = get_submesh(cm, subidx);
1526 cgm_vcons(center, 0, 0, 0);
1532 calc_bsph_idx(cm, sm->istart, sm->icount);
1534 calc_bsph_noidx(cm, sm->vstart, sm->vcount);
1538 if(center) *center = cm->bsph_center;
1539 if(rad) *rad = cm->bsph_radius;
1540 return cm->bsph_radius;
1544 void cmesh_texcoord_apply_xform(struct cmesh *cm, float *xform);
1545 void cmesh_texcoord_gen_plane(struct cmesh *cm, cgm_vec3 *norm, cgm_vec3 *tang);
1546 void cmesh_texcoord_gen_box(struct cmesh *cm);
1547 void cmesh_texcoord_gen_cylinder(struct cmesh *cm);
1549 int cmesh_dump(struct cmesh *cm, const char *fname)
1551 FILE *fp = fopen(fname, "wb");
1553 int res = cmesh_dump_file(cm, fp);
1560 int cmesh_dump_file(struct cmesh *cm, FILE *fp)
1562 static const char *label[] = { "pos", "nor", "tan", "tex", "col", "bw", "bid", "tex2" };
1563 static const char *elemfmt[] = { 0, " %s(%g)", " %s(%g, %g)", " %s(%g, %g, %g)", " %s(%g, %g, %g, %g)", 0 };
1566 if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1570 fprintf(fp, "VERTEX ATTRIBUTES\n");
1572 for(i=0; i<cm->nverts; i++) {
1573 fprintf(fp, "%5u:", i);
1574 for(j=0; j<CMESH_NUM_ATTR; j++) {
1575 if(cmesh_has_attrib(cm, j)) {
1576 const float *v = cmesh_attrib_at_ro(cm, j, i);
1577 int nelem = cm->vattr[j].nelem;
1578 fprintf(fp, elemfmt[nelem], label[j], v[0], nelem > 1 ? v[1] : 0.0f,
1579 nelem > 2 ? v[2] : 0.0f, nelem > 3 ? v[3] : 0.0f);
1585 if(cmesh_indexed(cm)) {
1586 const unsigned int *idx = cmesh_index_ro(cm);
1587 int numidx = cmesh_index_count(cm);
1588 int numtri = numidx / 3;
1589 assert(numidx % 3 == 0);
1591 fprintf(fp, "FACES\n");
1593 for(i=0; i<numtri; i++) {
1594 fprintf(fp, "%5d: %d %d %d\n", i, idx[0], idx[1], idx[2]);
1601 int cmesh_dump_obj(struct cmesh *cm, const char *fname)
1603 FILE *fp = fopen(fname, "wb");
1605 int res = cmesh_dump_obj_file(cm, fp, 0);
1615 int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs)
1617 static const char *fmtstr[] = {" %u", " %u//%u", " %u/%u", " %u/%u/%u"};
1618 int i, j, num, nelem;
1619 unsigned int aflags = 0;
1621 if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1626 nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1627 if((num = cm->vattr[CMESH_ATTR_VERTEX].count) != cm->nverts * nelem) {
1628 fprintf(stderr, "vertex array size (%d) != nverts (%d)\n", num, cm->nverts);
1630 for(i=0; i<cm->nverts; i++) {
1631 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1632 fprintf(fp, "v %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
1635 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
1637 nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1638 if((num = cm->vattr[CMESH_ATTR_NORMAL].count) != cm->nverts * nelem) {
1639 fprintf(stderr, "normal array size (%d) != nverts (%d)\n", num, cm->nverts);
1641 for(i=0; i<cm->nverts; i++) {
1642 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_NORMAL, i);
1643 fprintf(fp, "vn %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
1647 if(cmesh_has_attrib(cm, CMESH_ATTR_TEXCOORD)) {
1649 nelem = cm->vattr[CMESH_ATTR_TEXCOORD].nelem;
1650 if((num = cm->vattr[CMESH_ATTR_TEXCOORD].count) != cm->nverts * nelem) {
1651 fprintf(stderr, "texcoord array size (%d) != nverts (%d)\n", num, cm->nverts);
1653 for(i=0; i<cm->nverts; i++) {
1654 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_TEXCOORD, i);
1655 fprintf(fp, "vt %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f);
1659 if(cmesh_indexed(cm)) {
1660 const unsigned int *idxptr = cmesh_index_ro(cm);
1661 int numidx = cmesh_index_count(cm);
1662 int numtri = numidx / 3;
1663 assert(numidx % 3 == 0);
1665 for(i=0; i<numtri; i++) {
1667 for(j=0; j<3; j++) {
1668 unsigned int idx = *idxptr++ + 1 + voffs;
1669 fprintf(fp, fmtstr[aflags], idx, idx, idx);
1674 int numtri = cm->nverts / 3;
1675 unsigned int idx = 1 + voffs;
1676 for(i=0; i<numtri; i++) {
1678 for(j=0; j<3; j++) {
1679 fprintf(fp, fmtstr[aflags], idx, idx, idx);
1688 static void clear_mtl(struct cmesh_material *mtl)
1693 for(i=0; i<CMESH_NUM_TEX; i++) {
1694 free(mtl->tex[i].name);
1699 static void clone_mtl(struct cmesh_material *dest, struct cmesh_material *src)
1703 if(src->name) dest->name = strdup_nf(src->name);
1704 for(i=0; i<CMESH_NUM_TEX; i++) {
1705 if(src->tex[i].name) {
1706 dest->tex[i].name = strdup_nf(src->tex[i].name);