10 struct cmesh_vattrib {
11 int nelem; /* num elements per attribute [1, 4] */
12 float *data; /* dynarr */
14 int vbo_valid, data_valid;
20 unsigned int nverts, nfaces;
22 /* current value for each attribute for the immediate mode interface */
23 cgm_vec4 cur_val[CMESH_NUM_ATTR];
25 unsigned int buffer_objects[CMESH_NUM_ATTR + 1];
26 struct cmesh_vattrib vattr[CMESH_NUM_ATTR];
28 unsigned int *idata; /* dynarr */
30 int ibo_valid, idata_valid;
32 /* index buffer for wireframe rendering (constructed on demand) */
33 unsigned int wire_ibo;
36 /* axis-aligned bounding box */
37 cgm_vec3 aabb_min, aabb_max;
46 static int pre_draw(struct cmesh *cm);
47 static void post_draw(struct cmesh *cm, int cur_sdr);
48 static void update_buffers(struct cmesh *cm);
49 static void update_wire_ibo(struct cmesh *cm);
50 static void calc_aabb(struct cmesh *cm);
51 static void calc_bsph(struct cmesh *cm);
53 static int def_nelem[CMESH_NUM_ATTR] = {3, 3, 3, 2, 4, 4, 4, 2};
55 static int sdr_loc[CMESH_NUM_ATTR] = {0, 1, 2, 3, 4, 5, 6, 7};
56 static int use_custom_sdr_attr;
60 void cmesh_set_attrib_sdrloc(int attr, int loc)
65 int cmesh_get_attrib_sdrloc(int attr)
70 void cmesh_clear_attrib_sdrloc(void)
73 for(i=0; i<CMESH_NUM_ATTR; i++) {
79 struct cmesh *cmesh_alloc(void)
83 if(!(cm = malloc(sizeof *cm))) {
86 if(cmesh_init(cm) == -1) {
93 void cmesh_free(struct cmesh *cm)
99 int cmesh_init(struct cmesh *cm)
103 memset(cm, 0, sizeof *cm);
104 cgm_wcons(cm->cur_val + CMESH_ATTR_COLOR, 1, 1, 1, 1);
106 glGenBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
108 for(i=0; i<CMESH_NUM_ATTR; i++) {
109 if(!(cm->vattr[i].data = dynarr_alloc(0, sizeof(float)))) {
113 cm->vattr[i].vbo = cm->buffer_objects[i];
116 cm->ibo = cm->buffer_objects[CMESH_NUM_ATTR];
117 if(!(cm->idata = dynarr_alloc(0, sizeof *cm->idata))) {
124 void cmesh_destroy(struct cmesh *cm)
130 for(i=0; i<CMESH_NUM_ATTR; i++) {
131 dynarr_free(cm->vattr[i].data);
133 dynarr_free(cm->idata);
135 glDeleteBuffers(CMESH_NUM_ATTR + 1, cm->buffer_objects);
137 glDeleteBuffers(1, &cm->wire_ibo);
141 void cmesh_clear(struct cmesh *cm)
145 for(i=0; i<CMESH_NUM_ATTR; i++) {
146 cm->vattr[i].nelem = 0;
147 cm->vattr[i].vbo_valid = 0;
148 cm->vattr[i].data_valid = 0;
149 cm->vattr[i].data = dynarr_clear(cm->vattr[i].data);
151 cm->ibo_valid = cm->idata_valid = 0;
152 cm->idata = dynarr_clear(cm->idata);
154 cm->wire_ibo_valid = 0;
155 cm->nverts = cm->nfaces = 0;
157 cm->bsph_valid = cm->aabb_valid = 0;
160 int cmesh_clone(struct cmesh *cmdest, struct cmesh *cmsrc)
164 float *varr[CMESH_NUM_ATTR] = {0};
165 unsigned int *iarr = 0;
167 /* do anything that can fail first, before making any changes to cmdest
168 * so we have the option of recovering gracefuly
171 if(!(name = malloc(strlen(cmsrc->name)))) {
174 strcpy(name, cmsrc->name);
176 if(cmesh_indexed(cmsrc)) {
177 num = dynarr_size(cmsrc->idata);
178 if(!(iarr = dynarr_alloc(num, sizeof *iarr))) {
183 for(i=0; i<CMESH_NUM_ATTR; i++) {
184 if(cmesh_has_attrib(cmsrc, i)) {
185 nelem = cmsrc->vattr[i].nelem;
186 num = dynarr_size(cmsrc->vattr[i].data);
187 if(!(varr[i] = dynarr_alloc(num * nelem, sizeof(float)))) {
189 dynarr_free(varr[i]);
200 for(i=0; i<CMESH_NUM_ATTR; i++) {
201 dynarr_free(cmdest->vattr[i].data);
203 if(cmesh_has_attrib(cmsrc, i)) {
204 cmesh_attrib(cmsrc, i); /* force validation of the actual data on the source mesh */
206 nelem = cmsrc->vattr[i].nelem;
207 cmdest->vattr[i].nelem = nelem;
208 num = dynarr_size(cmsrc->vattr[i].data);
209 cmdest->vattr[i].data = varr[i];
210 memcpy(cmdest->vattr[i].data, cmsrc->vattr[i].data, num * nelem * sizeof(float));
211 cmdest->vattr[i].data_valid = 1;
212 cmdest->vattr[i].vbo_valid = 0;
214 memset(cmdest->vattr + i, 0, sizeof cmdest->vattr[i]);
218 dynarr_free(cmdest->idata);
219 if(cmesh_indexed(cmsrc)) {
220 cmesh_index(cmsrc); /* force validation .... */
222 num = dynarr_size(cmsrc->idata);
223 cmdest->idata = iarr;
224 memcpy(cmdest->idata, cmsrc->idata, num * sizeof *cmdest->idata);
225 cmdest->idata_valid = 1;
228 cmdest->idata_valid = cmdest->ibo_valid = 0;
234 cmdest->nverts = cmsrc->nverts;
235 cmdest->nfaces = cmsrc->nfaces;
237 memcpy(cmdest->cur_val, cmsrc->cur_val, sizeof cmdest->cur_val);
239 cmdest->aabb_min = cmsrc->aabb_min;
240 cmdest->aabb_max = cmsrc->aabb_max;
241 cmdest->aabb_valid = cmsrc->aabb_valid;
242 cmdest->bsph_center = cmsrc->bsph_center;
243 cmdest->bsph_radius = cmsrc->bsph_radius;
244 cmdest->bsph_valid = cmsrc->bsph_valid;
249 int cmesh_set_name(struct cmesh *cm, const char *name)
251 int len = strlen(name);
252 char *tmp = malloc(len + 1);
256 memcpy(cm->name, name, len + 1);
260 const char *cmesh_name(struct cmesh *cm)
265 int cmesh_has_attrib(struct cmesh *cm, int attr)
267 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
270 return cm->vattr[attr].vbo_valid | cm->vattr[attr].data_valid;
273 int cmesh_indexed(struct cmesh *cm)
275 return cm->ibo_valid | cm->idata_valid;
278 /* vdata can be 0, in which case only memory is allocated
279 * returns pointer to the attribute array
281 float *cmesh_set_attrib(struct cmesh *cm, int attr, int nelem, unsigned int num,
286 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
289 if(cm->nverts && num != cm->nverts) {
293 if(!(newarr = dynarr_alloc(num * nelem, sizeof *newarr))) {
297 memcpy(newarr, vdata, num * nelem * sizeof *newarr);
302 dynarr_free(cm->vattr[attr].data);
303 cm->vattr[attr].data = newarr;
304 cm->vattr[attr].nelem = nelem;
305 cm->vattr[attr].data_valid = 1;
306 cm->vattr[attr].vbo_valid = 0;
310 float *cmesh_attrib(struct cmesh *cm, int attr)
312 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
315 cm->vattr[attr].vbo_valid = 0;
316 return (float*)cmesh_attrib_ro(cm, attr);
319 const float *cmesh_attrib_ro(struct cmesh *cm, int attr)
324 if(attr < 0 || attr >= CMESH_NUM_ATTR) {
328 if(!cm->vattr[attr].data_valid) {
329 #if GL_ES_VERSION_2_0
332 if(!cm->vattr[attr].vbo_valid) {
336 /* local data copy unavailable, grab the data from the vbo */
337 nelem = cm->vattr[attr].nelem;
338 if(!(tmp = dynarr_resize(cm->vattr[attr].data, cm->nverts * nelem))) {
341 cm->vattr[attr].data = tmp;
343 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[attr].vbo);
344 tmp = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
345 memcpy(cm->vattr[attr].data, tmp, cm->nverts * nelem * sizeof(float));
346 glUnmapBuffer(GL_ARRAY_BUFFER);
348 cm->vattr[attr].data_valid = 1;
351 return cm->vattr[attr].data;
354 float *cmesh_attrib_at(struct cmesh *cm, int attr, int idx)
356 float *vptr = cmesh_attrib(cm, attr);
357 return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
360 const float *cmesh_attrib_at_ro(struct cmesh *cm, int attr, int idx)
362 const float *vptr = cmesh_attrib_ro(cm, attr);
363 return vptr ? vptr + idx * cm->vattr[attr].nelem : 0;
366 int cmesh_attrib_count(struct cmesh *cm, int attr)
368 return cmesh_has_attrib(cm, attr) ? cm->nverts : 0;
371 int cmesh_push_attrib(struct cmesh *cm, int attr, float *v)
376 if(!cm->vattr[attr].nelem) {
377 cm->vattr[attr].nelem = def_nelem[attr];
380 cursz = dynarr_size(cm->vattr[attr].data);
381 newsz = cursz + cm->vattr[attr].nelem;
382 if(!(vptr = dynarr_resize(cm->vattr[attr].data, newsz))) {
385 cm->vattr[attr].data = vptr;
388 for(i=0; i<cm->vattr[attr].nelem; i++) {
391 cm->vattr[attr].data_valid = 1;
392 cm->vattr[attr].vbo_valid = 0;
394 if(attr == CMESH_ATTR_VERTEX) {
395 cm->nverts = newsz / cm->vattr[attr].nelem;
400 int cmesh_push_attrib1f(struct cmesh *cm, int attr, float x)
406 return cmesh_push_attrib(cm, attr, v);
409 int cmesh_push_attrib2f(struct cmesh *cm, int attr, float x, float y)
416 return cmesh_push_attrib(cm, attr, v);
419 int cmesh_push_attrib3f(struct cmesh *cm, int attr, float x, float y, float z)
426 return cmesh_push_attrib(cm, attr, v);
429 int cmesh_push_attrib4f(struct cmesh *cm, int attr, float x, float y, float z, float w)
436 return cmesh_push_attrib(cm, attr, v);
439 /* indices can be 0, in which case only memory is allocated
440 * returns pointer to the index array
442 unsigned int *cmesh_set_index(struct cmesh *cm, int num, const unsigned int *indices)
445 int nidx = cm->nfaces * 3;
447 if(nidx && num != nidx) {
451 if(!(tmp = dynarr_alloc(num, sizeof *tmp))) {
455 memcpy(tmp, indices, num * sizeof *tmp);
458 dynarr_free(cm->idata);
465 unsigned int *cmesh_index(struct cmesh *cm)
468 return (unsigned int*)cmesh_index_ro(cm);
471 const unsigned int *cmesh_index_ro(struct cmesh *cm)
476 if(!cm->idata_valid) {
477 #if GL_ES_VERSION_2_0
484 /* local copy is unavailable, grab the data from the ibo */
485 nidx = cm->nfaces * 3;
486 if(!(tmp = dynarr_alloc(nidx, sizeof *cm->idata))) {
489 dynarr_free(cm->idata);
492 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
493 tmp = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
494 memcpy(cm->idata, tmp, nidx * sizeof *cm->idata);
495 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
503 int cmesh_index_count(struct cmesh *cm)
505 return cm->nfaces * 3;
508 int cmesh_push_index(struct cmesh *cm, unsigned int idx)
511 if(!(iptr = dynarr_push(cm->idata, &idx))) {
518 cm->nfaces = dynarr_size(cm->idata) / 3;
522 int cmesh_poly_count(struct cmesh *cm)
528 return cm->nverts / 3;
533 /* attr can be -1 to invalidate all attributes */
534 void cmesh_invalidate_vbo(struct cmesh *cm, int attr)
538 if(attr >= CMESH_NUM_ATTR) {
543 for(i=0; i<CMESH_NUM_ATTR; i++) {
544 cm->vattr[i].vbo_valid = 0;
547 cm->vattr[attr].vbo_valid = 0;
551 void cmesh_invalidate_index(struct cmesh *cm)
556 int cmesh_append(struct cmesh *cmdest, struct cmesh *cmsrc)
558 int i, nelem, newsz, origsz, srcsz;
561 unsigned int idxoffs;
563 if(!cmdest->nverts) {
564 return cmesh_clone(cmdest, cmsrc);
567 for(i=0; i<CMESH_NUM_ATTR; i++) {
568 if(cmesh_has_attrib(cmdest, i) && cmesh_has_attrib(cmsrc, i)) {
569 /* force validation of the data arrays */
570 cmesh_attrib(cmdest, i);
571 cmesh_attrib_ro(cmsrc, i);
573 assert(cmdest->vattr[i].nelem == cmsrc->vattr[i].nelem);
574 nelem = cmdest->vattr[i].nelem;
575 origsz = cmdest->nverts * nelem;
576 newsz = cmdest->nverts + cmsrc->nverts * nelem;
578 if(!(vptr = dynarr_resize(cmdest->vattr[i].data, newsz))) {
581 memcpy(vptr + origsz, cmsrc->vattr[i].data, cmsrc->nverts * nelem * sizeof(float));
582 cmdest->vattr[i].data = vptr;
586 if(cmesh_indexed(cmdest)) {
587 assert(cmesh_indexed(cmsrc));
588 /* force validation ... */
590 cmesh_index_ro(cmsrc);
592 idxoffs = cmdest->nverts;
593 origsz = dynarr_size(cmdest->idata);
594 srcsz = dynarr_size(cmsrc->idata);
595 newsz = origsz + srcsz;
597 if(!(iptr = dynarr_resize(cmdest->idata, newsz))) {
600 cmdest->idata = iptr;
602 /* copy and fixup all the new indices */
604 for(i=0; i<srcsz; i++) {
605 *iptr++ = cmsrc->idata[i] + idxoffs;
609 cmdest->wire_ibo_valid = 0;
610 cmdest->aabb_valid = 0;
611 cmdest->bsph_valid = 0;
615 /* assemble a complete vertex by adding all the useful attributes */
616 int cmesh_vertex(struct cmesh *cm, float x, float y, float z)
620 cgm_wcons(cm->cur_val + CMESH_ATTR_VERTEX, x, y, z, 1.0f);
621 cm->vattr[CMESH_ATTR_VERTEX].data_valid = 1;
622 cm->vattr[CMESH_ATTR_VERTEX].nelem = 3;
624 for(i=0; i<CMESH_ATTR_VERTEX; i++) {
625 if(cm->vattr[i].data_valid) {
626 for(j=0; j<cm->vattr[CMESH_ATTR_VERTEX].nelem; j++) {
627 float *tmp = dynarr_push(cm->vattr[i].data, &cm->cur_val[i].x + j);
629 cm->vattr[i].data = tmp;
632 cm->vattr[i].vbo_valid = 0;
633 cm->vattr[i].data_valid = 1;
636 if(cm->idata_valid) {
637 cm->idata = dynarr_clear(cm->idata);
639 cm->ibo_valid = cm->idata_valid = 0;
643 void cmesh_normal(struct cmesh *cm, float nx, float ny, float nz)
645 cgm_wcons(cm->cur_val + CMESH_ATTR_NORMAL, nx, ny, nz, 1.0f);
646 cm->vattr[CMESH_ATTR_NORMAL].nelem = 3;
649 void cmesh_tangent(struct cmesh *cm, float tx, float ty, float tz)
651 cgm_wcons(cm->cur_val + CMESH_ATTR_TANGENT, tx, ty, tz, 1.0f);
652 cm->vattr[CMESH_ATTR_TANGENT].nelem = 3;
655 void cmesh_texcoord(struct cmesh *cm, float u, float v, float w)
657 cgm_wcons(cm->cur_val + CMESH_ATTR_TEXCOORD, u, v, w, 1.0f);
658 cm->vattr[CMESH_ATTR_TEXCOORD].nelem = 3;
661 void cmesh_boneweights(struct cmesh *cm, float w1, float w2, float w3, float w4)
663 cgm_wcons(cm->cur_val + CMESH_ATTR_BONEWEIGHTS, w1, w2, w3, w4);
664 cm->vattr[CMESH_ATTR_BONEWEIGHTS].nelem = 4;
667 void cmesh_boneidx(struct cmesh *cm, int idx1, int idx2, int idx3, int idx4)
669 cgm_wcons(cm->cur_val + CMESH_ATTR_BONEIDX, idx1, idx2, idx3, idx4);
670 cm->vattr[CMESH_ATTR_BONEIDX].nelem = 4;
673 static float *get_vec4(struct cmesh *cm, int attr, int idx, cgm_vec4 *res)
677 cgm_wcons(res, 0, 0, 0, 1);
678 if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
683 for(i=0; i<cm->vattr[attr].nelem; i++) {
689 static float *get_vec3(struct cmesh *cm, int attr, int idx, cgm_vec3 *res)
693 cgm_vcons(res, 0, 0, 0);
694 if(!(sptr = cmesh_attrib_at(cm, attr, idx))) {
699 for(i=0; i<cm->vattr[attr].nelem; i++) {
705 /* dir_xform can be null, in which case it's calculated from xform */
706 void cmesh_apply_xform(struct cmesh *cm, float *xform, float *dir_xform)
714 for(i=0; i<cm->nverts; i++) {
715 if(!(vptr = get_vec4(cm, CMESH_ATTR_VERTEX, i, &v))) {
718 cgm_wmul_m4v4(&v, xform);
719 for(j=0; j<cm->vattr[CMESH_ATTR_VERTEX].nelem; j++) {
723 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
724 if((vptr = get_vec3(cm, CMESH_ATTR_NORMAL, i, &n))) {
725 cgm_vmul_m3v3(&n, dir_xform);
726 for(j=0; j<cm->vattr[CMESH_ATTR_NORMAL].nelem; j++) {
731 if(cmesh_has_attrib(cm, CMESH_ATTR_TANGENT)) {
732 if((vptr = get_vec3(cm, CMESH_ATTR_TANGENT, i, &t))) {
733 cgm_vmul_m3v3(&t, dir_xform);
734 for(j=0; j<cm->vattr[CMESH_ATTR_TANGENT].nelem; j++) {
742 void cmesh_flip(struct cmesh *cm)
744 cmesh_flip_faces(cm);
745 cmesh_flip_normals(cm);
748 void cmesh_flip_faces(struct cmesh *cm)
750 int i, j, idxnum, vnum, nelem;
751 unsigned int *indices;
754 if(cmesh_indexed(cm)) {
755 if(!(indices = cmesh_index(cm))) {
758 idxnum = cmesh_index_count(cm);
759 for(i=0; i<idxnum; i+=3) {
760 unsigned int tmp = indices[i + 2];
761 indices[i + 2] = indices[i + 1];
762 indices[i + 1] = tmp;
765 if(!(verts = cmesh_attrib(cm, CMESH_ATTR_VERTEX))) {
768 vnum = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
769 nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
770 for(i=0; i<vnum; i+=3) {
771 for(j=0; j<nelem; j++) {
772 vptr = verts + (i + 1) * nelem + j;
773 float tmp = vptr[nelem];
774 vptr[nelem] = vptr[0];
780 void cmesh_flip_normals(struct cmesh *cm)
783 float *nptr = cmesh_attrib(cm, CMESH_ATTR_NORMAL);
786 num = cm->nverts * cm->vattr[CMESH_ATTR_NORMAL].nelem;
787 for(i=0; i<num; i++) {
793 int cmesh_explode(struct cmesh *cm)
795 int i, j, k, idxnum, nnverts;
796 unsigned int *indices;
798 if(!cmesh_indexed(cm)) return 0;
800 indices = cmesh_index(cm);
803 idxnum = cmesh_index_count(cm);
806 for(i=0; i<CMESH_NUM_ATTR; i++) {
808 float *tmpbuf, *dstptr;
810 if(!cmesh_has_attrib(cm, i)) continue;
812 srcbuf = cmesh_attrib(cm, i);
813 if(!(tmpbuf = dynarr_alloc(nnverts * cm->vattr[i].nelem, sizeof(float)))) {
818 for(j=0; j<idxnum; j++) {
819 unsigned int idx = indices[j];
820 const float *srcptr = srcbuf + idx * cm->vattr[i].nelem;
822 for(k=0; k<cm->vattr[i].nelem; k++) {
823 *dstptr++ = *srcptr++;
827 dynarr_free(cm->vattr[i].data);
828 cm->vattr[i].data = tmpbuf;
829 cm->vattr[i].data_valid = 1;
834 cm->idata = dynarr_clear(cm->idata);
836 cm->nverts = nnverts;
837 cm->nfaces = idxnum / 3;
841 void cmesh_calc_face_normals(struct cmesh *cm)
846 static int pre_draw(struct cmesh *cm)
850 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
854 if(!cm->vattr[CMESH_ATTR_VERTEX].vbo_valid) {
858 if(cur_sdr && use_custom_sdr_attr) {
859 if(sdr_loc[CMESH_ATTR_VERTEX] == -1) {
863 for(i=0; i<CMESH_NUM_ATTR; i++) {
865 if(loc >= 0 && cm->vattr[i].vbo_valid) {
866 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
867 glVertexAttribPointer(loc, cm->vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
868 glEnableVertexAttribArray(loc);
872 #ifndef GL_ES_VERSION_2_0
873 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_VERTEX].vbo);
874 glVertexPointer(cm->vattr[CMESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
875 glEnableClientState(GL_VERTEX_ARRAY);
877 if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
878 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_NORMAL].vbo);
879 glNormalPointer(GL_FLOAT, 0, 0);
880 glEnableClientState(GL_NORMAL_ARRAY);
882 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
883 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD].vbo);
884 glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
885 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
887 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
888 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_COLOR].vbo);
889 glColorPointer(cm->vattr[CMESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
890 glEnableClientState(GL_COLOR_ARRAY);
892 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
893 glClientActiveTexture(GL_TEXTURE1);
894 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD2].vbo);
895 glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD2].nelem, GL_FLOAT, 0, 0);
896 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
897 glClientActiveTexture(GL_TEXTURE0);
899 #endif /* GL_ES_VERSION_2_0 */
901 glBindBuffer(GL_ARRAY_BUFFER, 0);
905 void cmesh_draw(struct cmesh *cm)
909 if((cur_sdr = pre_draw(cm)) == -1) {
914 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
915 glDrawElements(GL_TRIANGLES, cm->nfaces * 3, GL_UNSIGNED_INT, 0);
916 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
918 glDrawArrays(GL_TRIANGLES, 0, cm->nverts);
921 post_draw(cm, cur_sdr);
924 static void post_draw(struct cmesh *cm, int cur_sdr)
928 if(cur_sdr && use_custom_sdr_attr) {
929 for(i=0; i<CMESH_NUM_ATTR; i++) {
930 int loc = sdr_loc[i];
931 if(loc >= 0 && cm->vattr[i].vbo_valid) {
932 glDisableVertexAttribArray(loc);
936 #ifndef GL_ES_VERSION_2_0
937 glDisableClientState(GL_VERTEX_ARRAY);
938 if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
939 glDisableClientState(GL_NORMAL_ARRAY);
941 if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
942 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
944 if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
945 glDisableClientState(GL_COLOR_ARRAY);
947 if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
948 glClientActiveTexture(GL_TEXTURE1);
949 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
950 glClientActiveTexture(GL_TEXTURE0);
952 #endif /* GL_ES_VERSION_2_0 */
956 void cmesh_draw_wire(struct cmesh *cm, float linesz)
960 if((cur_sdr = pre_draw(cm)) == -1) {
965 nfaces = cmesh_poly_count(cm);
966 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
967 glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0);
968 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
970 post_draw(cm, cur_sdr);
973 void cmesh_draw_vertices(struct cmesh *cm, float ptsz)
976 if((cur_sdr = pre_draw(cm)) == -1) {
980 glPushAttrib(GL_POINT_BIT);
982 glDrawArrays(GL_POINTS, 0, cm->nverts);
985 post_draw(cm, cur_sdr);
988 void cmesh_draw_normals(struct cmesh *cm, float len)
990 #ifndef GL_ES_VERSION_2_0
991 int i, cur_sdr, vert_nelem, norm_nelem;
993 const float *varr, *norm;
995 varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
996 norm = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
997 if(!varr || !norm) return;
999 vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1000 norm_nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1002 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1003 if(cur_sdr && use_custom_sdr_attr) {
1004 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1010 for(i=0; i<cm->nverts; i++) {
1011 float x, y, z, endx, endy, endz;
1013 x = varr[i * vert_nelem];
1014 y = varr[i * vert_nelem + 1];
1015 z = varr[i * vert_nelem + 2];
1016 endx = x + norm[i * norm_nelem] * len;
1017 endy = y + norm[i * norm_nelem + 1] * len;
1018 endz = z + norm[i * norm_nelem + 2] * len;
1021 glVertex3f(x, y, z);
1022 glVertex3f(endx, endy, endz);
1024 glVertexAttrib3f(loc, x, y, z);
1025 glVertexAttrib3f(loc, endx, endy, endz);
1029 #endif /* GL_ES_VERSION_2_0 */
1032 void cmesh_draw_tangents(struct cmesh *cm, float len)
1034 #ifndef GL_ES_VERSION_2_0
1035 int i, cur_sdr, vert_nelem, tang_nelem;
1037 const float *varr, *tang;
1039 varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
1040 tang = cmesh_attrib_ro(cm, CMESH_ATTR_TANGENT);
1041 if(!varr || !tang) return;
1043 vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1044 tang_nelem = cm->vattr[CMESH_ATTR_TANGENT].nelem;
1046 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
1047 if(cur_sdr && use_custom_sdr_attr) {
1048 if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
1054 for(i=0; i<cm->nverts; i++) {
1055 float x, y, z, endx, endy, endz;
1057 x = varr[i * vert_nelem];
1058 y = varr[i * vert_nelem + 1];
1059 z = varr[i * vert_nelem + 2];
1060 endx = x + tang[i * tang_nelem] * len;
1061 endy = y + tang[i * tang_nelem + 1] * len;
1062 endz = z + tang[i * tang_nelem + 2] * len;
1065 glVertex3f(x, y, z);
1066 glVertex3f(endx, endy, endz);
1068 glVertexAttrib3f(loc, x, y, z);
1069 glVertexAttrib3f(loc, endx, endy, endz);
1073 #endif /* GL_ES_VERSION_2_0 */
1076 static void update_buffers(struct cmesh *cm)
1080 for(i=0; i<CMESH_NUM_ATTR; i++) {
1081 if(cmesh_has_attrib(cm, i) && !cm->vattr[i].vbo_valid) {
1082 glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
1083 glBufferData(GL_ARRAY_BUFFER, cm->nverts * cm->vattr[i].nelem * sizeof(float),
1084 cm->vattr[i].data, GL_STATIC_DRAW);
1085 cm->vattr[i].vbo_valid = 1;
1088 glBindBuffer(GL_ARRAY_BUFFER, 0);
1090 if(cm->idata_valid && !cm->ibo_valid) {
1091 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
1092 glBufferData(GL_ELEMENT_ARRAY_BUFFER, cm->nfaces * 3 * sizeof(unsigned int),
1093 cm->idata, GL_STATIC_DRAW);
1095 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1099 static void update_wire_ibo(struct cmesh *cm)
1102 unsigned int *wire_idxarr, *dest;
1106 if(cm->wire_ibo_valid) return;
1109 glGenBuffers(1, &cm->wire_ibo);
1111 num_faces = cmesh_poly_count(cm);
1113 if(!(wire_idxarr = malloc(num_faces * 6 * sizeof *wire_idxarr))) {
1119 /* we're dealing with an indexed mesh */
1120 const unsigned int *idxarr = cmesh_index_ro(cm);
1122 for(i=0; i<num_faces; i++) {
1123 *dest++ = idxarr[0];
1124 *dest++ = idxarr[1];
1125 *dest++ = idxarr[1];
1126 *dest++ = idxarr[2];
1127 *dest++ = idxarr[2];
1128 *dest++ = idxarr[0];
1132 /* not an indexed mesh */
1133 for(i=0; i<num_faces; i++) {
1144 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
1145 glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_faces * 6 * sizeof(unsigned int),
1146 wire_idxarr, GL_STATIC_DRAW);
1148 cm->wire_ibo_valid = 1;
1149 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1152 static void calc_aabb(struct cmesh *cm)
1156 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1160 cgm_vcons(&cm->aabb_min, FLT_MAX, FLT_MAX, FLT_MAX);
1161 cgm_vcons(&cm->aabb_max, -FLT_MAX, -FLT_MAX, -FLT_MAX);
1163 for(i=0; i<cm->nverts; i++) {
1164 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1165 for(j=0; j<3; j++) {
1166 if(v[j] < (&cm->aabb_min.x)[j]) {
1167 (&cm->aabb_min.x)[j] = v[j];
1169 if(v[j] > (&cm->aabb_max.x)[j]) {
1170 (&cm->aabb_max.x)[j] = v[j];
1177 void cmesh_aabbox(struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax)
1179 if(!cm->aabb_valid) {
1182 *vmin = cm->aabb_min;
1183 *vmax = cm->aabb_max;
1186 static void calc_bsph(struct cmesh *cm)
1191 if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
1195 cgm_vcons(&cm->bsph_center, 0, 0, 0);
1197 /* first find the center */
1198 for(i=0; i<cm->nverts; i++) {
1199 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1200 cm->bsph_center.x += v[0];
1201 cm->bsph_center.y += v[1];
1202 cm->bsph_center.z += v[2];
1204 s = 1.0f / (float)cm->nverts;
1205 cm->bsph_center.x *= s;
1206 cm->bsph_center.y *= s;
1207 cm->bsph_center.z *= s;
1209 cm->bsph_radius = 0.0f;
1210 for(i=0; i<cm->nverts; i++) {
1211 const cgm_vec3 *v = (const cgm_vec3*)cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1212 if((dist_sq = cgm_vdist_sq(v, &cm->bsph_center)) > cm->bsph_radius) {
1213 cm->bsph_radius = dist_sq;
1216 cm->bsph_radius = sqrt(cm->bsph_radius);
1220 float cmesh_bsphere(struct cmesh *cm, cgm_vec3 *center, float *rad)
1222 if(!cm->bsph_valid) {
1225 *center = cm->bsph_center;
1226 *rad = cm->bsph_radius;
1227 return cm->bsph_radius;
1231 void cmesh_texcoord_apply_xform(struct cmesh *cm, float *xform);
1232 void cmesh_texcoord_gen_plane(struct cmesh *cm, cgm_vec3 *norm, cgm_vec3 *tang);
1233 void cmesh_texcoord_gen_box(struct cmesh *cm);
1234 void cmesh_texcoord_gen_cylinder(struct cmesh *cm);
1236 int cmesh_dump(struct cmesh *cm, const char *fname)
1238 FILE *fp = fopen(fname, "wb");
1240 int res = cmesh_dump_file(cm, fp);
1247 int cmesh_dump_file(struct cmesh *cm, FILE *fp)
1249 static const char *label[] = { "pos", "nor", "tan", "tex", "col", "bw", "bid", "tex2" };
1250 static const char *elemfmt[] = { 0, " %s(%g)", " %s(%g, %g)", " %s(%g, %g, %g)", " %s(%g, %g, %g, %g)", 0 };
1253 if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1257 fprintf(fp, "VERTEX ATTRIBUTES\n");
1259 for(i=0; i<cm->nverts; i++) {
1260 fprintf(fp, "%5u:", i);
1261 for(j=0; j<CMESH_NUM_ATTR; j++) {
1262 if(cmesh_has_attrib(cm, j)) {
1263 const float *v = cmesh_attrib_at_ro(cm, j, i);
1264 int nelem = cm->vattr[j].nelem;
1265 fprintf(fp, elemfmt[nelem], label[j], v[0], nelem > 1 ? v[1] : 0.0f,
1266 nelem > 2 ? v[2] : 0.0f, nelem > 3 ? v[3] : 0.0f);
1272 if(cmesh_indexed(cm)) {
1273 const unsigned int *idx = cmesh_index_ro(cm);
1274 int numidx = cmesh_index_count(cm);
1275 int numtri = numidx / 3;
1276 assert(numidx % 3 == 0);
1278 fprintf(fp, "FACES\n");
1280 for(i=0; i<numtri; i++) {
1281 fprintf(fp, "%5d: %d %d %d\n", i, idx[0], idx[1], idx[2]);
1288 int cmesh_dump_obj(struct cmesh *cm, const char *fname)
1290 FILE *fp = fopen(fname, "wb");
1292 int res = cmesh_dump_obj_file(cm, fp, 0);
1299 int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs)
1301 int i, j, num, nelem;
1303 if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
1308 nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
1309 if((num = dynarr_size(cm->vattr[CMESH_ATTR_VERTEX].data)) != cm->nverts * nelem) {
1310 warning_log("vertex array size (%d) != nverts (%d)\n", num, cm->nverts);
1312 for(i=0; i<cm->nverts; i++) {
1313 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
1314 fprintf(fp, "v %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
1317 if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
1318 nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
1319 if((num = dynarr_size(cm->vattr[CMESH_ATTR_NORMAL].data)) != cm->nverts * nelem) {
1320 warning_log("normal array size (%d) != nverts (%d)\n", num, cm->nverts);
1322 for(i=0; i<cm->nverts; i++) {
1323 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_NORMAL, i);
1324 fprintf(fp, "vn %f %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f, nelem > 2 ? v[2] : 0.0f);
1328 if(cmesh_has_attrib(cm, CMESH_ATTR_TEXCOORD)) {
1329 nelem = cm->vattr[CMESH_ATTR_TEXCOORD].nelem;
1330 if((num = dynarr_size(cm->vattr[CMESH_ATTR_TEXCOORD].data)) != cm->nverts * nelem) {
1331 warning_log("texcoord array size (%d) != nverts (%d)\n", num, cm->nverts);
1333 for(i=0; i<cm->nverts; i++) {
1334 const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_TEXCOORD, i);
1335 fprintf(fp, "vt %f %f\n", v[0], nelem > 1 ? v[1] : 0.0f);
1339 if(cmesh_indexed(cm)) {
1340 const unsigned int *idxptr = cmesh_index_ro(cm);
1341 int numidx = cmesh_index_count(cm);
1342 int numtri = numidx / 3;
1343 assert(numidx % 3 == 0);
1345 for(i=0; i<numtri; i++) {
1347 for(j=0; j<3; j++) {
1348 unsigned int idx = *idxptr++ + 1 + voffs;
1349 fprintf(fp, " %u/%u/%u", idx, idx, idx);
1354 int numtri = cm->nverts / 3;
1355 unsigned int idx = 1 + voffs;
1356 for(i=0; i<numtri; i++) {
1358 for(j=0; j<3; j++) {
1359 fprintf(fp, " %u/%u/%u", idx, idx, idx);