mesh done? starting obj loader (port from dosdemo)
authorJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 27 Feb 2019 03:41:06 +0000 (05:41 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 27 Feb 2019 03:41:06 +0000 (05:41 +0200)
src/cmesh.c
src/cmesh.h
src/meshload.c [new file with mode: 0644]

index 35600bc..4d74251 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <float.h>
 #include <assert.h>
 #include "opengl.h"
 #include "cmesh.h"
@@ -40,7 +41,17 @@ struct cmesh {
        int bsph_valid;
 };
 
+
+static int pre_draw(struct cmesh *cm);
+static void post_draw(struct cmesh *cm, int cur_sdr);
+static void update_buffers(struct cmesh *cm);
+static void update_wire_ibo(struct cmesh *cm);
+static void calc_aabb(struct cmesh *cm);
+static void calc_bsph(struct cmesh *cm);
+
+
 static int sdr_loc[CMESH_NUM_ATTR] = {0, 1, 2, 3, 4, 5, 6, 7};
+static int use_custom_sdr_attr;
 
 
 /* global state */
@@ -424,7 +435,7 @@ int cmesh_index_count(struct cmesh *cm)
        return cm->nfaces * 3;
 }
 
-int get_poly_count(struct cmesh *cm)
+int cmesh_poly_count(struct cmesh *cm)
 {
        if(cm->nfaces) {
                return cm->nfaces;
@@ -652,32 +663,487 @@ void cmesh_flip(struct cmesh *cm)
 
 void cmesh_flip_faces(struct cmesh *cm)
 {
+       int i, j, idxnum, vnum, nelem;
+       unsigned int *indices;
+       float *verts, *vptr;
+
+       if(cmesh_indexed(cm)) {
+               if(!(indices = cmesh_index(cm))) {
+                       return;
+               }
+               idxnum = cmesh_index_count(cm);
+               for(i=0; i<idxnum; i+=3) {
+                       unsigned int tmp = indices[i + 2];
+                       indices[i + 2] = indices[i + 1];
+                       indices[i + 1] = tmp;
+               }
+       } else {
+               if(!(verts = cmesh_attrib(cm, CMESH_ATTR_VERTEX))) {
+                       return;
+               }
+               vnum = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
+               nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
+               for(i=0; i<vnum; i+=3) {
+                       for(j=0; j<nelem; j++) {
+                               vptr = verts + (i + 1) * nelem + j;
+                               float tmp = vptr[nelem];
+                               vptr[nelem] = vptr[0];
+                               vptr[0] = tmp;
+                       }
+               }
+       }
 }
 void cmesh_flip_normals(struct cmesh *cm)
 {
+       int i, num;
+       float *nptr = cmesh_attrib(cm, CMESH_ATTR_NORMAL);
+       if(!nptr) return;
+
+       num = cm->nverts * cm->vattr[CMESH_ATTR_NORMAL].nelem;
+       for(i=0; i<num; i++) {
+               *nptr = -*nptr;
+               nptr++;
+       }
 }
 
-void cmesh_explode(struct cmesh *cm);  /* undo all vertex sharing */
+int cmesh_explode(struct cmesh *cm)
+{
+       int i, j, k, idxnum, nnverts;
+       unsigned int *indices;
+
+       if(!cmesh_indexed(cm)) return 0;
 
-/* this is only guaranteed to work on an exploded mesh */
-void cmesh_calc_face_normals(struct cmesh *cm);
+       indices = cmesh_index(cm);
+       assert(indices);
 
-void cmesh_draw(struct cmesh *cm);
-void cmesh_draw_wire(struct cmesh *cm, float linesz);
-void cmesh_draw_vertices(struct cmesh *cm, float ptsz);
-void cmesh_draw_normals(struct cmesh *cm, float len);
-void cmesh_draw_tangents(struct cmesh *cm, float len);
+       idxnum = cmesh_index_count(cm);
+       nnverts = idxnum;
 
-/* get the bounding box in local space. The result will be cached and subsequent
- * calls will return the same box. The cache gets invalidated by any functions that
- * can affect the vertex data
- */
-void cmesh_aabbox(struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax);
+       for(i=0; i<CMESH_NUM_ATTR; i++) {
+               const float *srcbuf;
+               float *tmpbuf, *dstptr;
+
+               if(!cmesh_has_attrib(cm, i)) continue;
+
+               srcbuf = cmesh_attrib(cm, i);
+               if(!(tmpbuf = dynarr_alloc(nnverts * cm->vattr[i].nelem, sizeof(float)))) {
+                       return -1;
+               }
+               dstptr = tmpbuf;
+
+               for(j=0; j<idxnum; j++) {
+                       unsigned int idx = indices[j];
+                       const float *srcptr = srcbuf + idx * cm->vattr[i].nelem;
+
+                       for(k=0; k<cm->vattr[i].nelem; k++) {
+                               *dstptr++ = *srcptr++;
+                       }
+               }
+
+               dynarr_free(cm->vattr[i].data);
+               cm->vattr[i].data = tmpbuf;
+               cm->vattr[i].data_valid = 1;
+       }
+
+       cm->ibo_valid = 0;
+       cm->idata_valid = 0;
+       cm->idata = dynarr_clear(cm->idata);
+
+       cm->nverts = nnverts;
+       cm->nfaces = idxnum / 3;
+       return 0;
+}
+
+void cmesh_calc_face_normals(struct cmesh *cm)
+{
+       /* TODO */
+}
+
+static int pre_draw(struct cmesh *cm)
+{
+       int i, loc, cur_sdr;
+
+       glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
+
+       update_buffers(cm);
+
+       if(!cm->vattr[CMESH_ATTR_VERTEX].vbo_valid) {
+               return -1;
+       }
+
+       if(cur_sdr && use_custom_sdr_attr) {
+               if(sdr_loc[CMESH_ATTR_VERTEX] == -1) {
+                       return -1;
+               }
+
+               for(i=0; i<CMESH_NUM_ATTR; i++) {
+                       loc = sdr_loc[i];
+                       if(loc >= 0 && cm->vattr[i].vbo_valid) {
+                               glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
+                               glVertexAttribPointer(loc, cm->vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
+                               glEnableVertexAttribArray(loc);
+                       }
+               }
+       } else {
+#ifndef GL_ES_VERSION_2_0
+               glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_VERTEX].vbo);
+               glVertexPointer(cm->vattr[CMESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
+               glEnableClientState(GL_VERTEX_ARRAY);
+
+               if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
+                       glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_NORMAL].vbo);
+                       glNormalPointer(GL_FLOAT, 0, 0);
+                       glEnableClientState(GL_NORMAL_ARRAY);
+               }
+               if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
+                       glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD].vbo);
+                       glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
+                       glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+               }
+               if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
+                       glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_COLOR].vbo);
+                       glColorPointer(cm->vattr[CMESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
+                       glEnableClientState(GL_COLOR_ARRAY);
+               }
+               if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
+                       glClientActiveTexture(GL_TEXTURE1);
+                       glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[CMESH_ATTR_TEXCOORD2].vbo);
+                       glTexCoordPointer(cm->vattr[CMESH_ATTR_TEXCOORD2].nelem, GL_FLOAT, 0, 0);
+                       glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+                       glClientActiveTexture(GL_TEXTURE0);
+               }
+#endif /* GL_ES_VERSION_2_0 */
+       }
+       glBindBuffer(GL_ARRAY_BUFFER, 0);
+       return cur_sdr;
+}
+
+void cmesh_draw(struct cmesh *cm)
+{
+       int cur_sdr;
+
+       if((cur_sdr = pre_draw(cm)) == -1) {
+               return;
+       }
+
+       if(cm->ibo_valid) {
+               glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
+               glDrawElements(GL_TRIANGLES, cm->nfaces * 3, GL_UNSIGNED_INT, 0);
+               glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+       } else {
+               glDrawArrays(GL_TRIANGLES, 0, cm->nverts);
+       }
+
+       post_draw(cm, cur_sdr);
+}
+
+static void post_draw(struct cmesh *cm, int cur_sdr)
+{
+       int i;
+
+       if(cur_sdr && use_custom_sdr_attr) {
+               for(i=0; i<CMESH_NUM_ATTR; i++) {
+                       int loc = sdr_loc[i];
+                       if(loc >= 0 && cm->vattr[i].vbo_valid) {
+                               glDisableVertexAttribArray(loc);
+                       }
+               }
+       } else {
+#ifndef GL_ES_VERSION_2_0
+               glDisableClientState(GL_VERTEX_ARRAY);
+               if(cm->vattr[CMESH_ATTR_NORMAL].vbo_valid) {
+                       glDisableClientState(GL_NORMAL_ARRAY);
+               }
+               if(cm->vattr[CMESH_ATTR_TEXCOORD].vbo_valid) {
+                       glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+               }
+               if(cm->vattr[CMESH_ATTR_COLOR].vbo_valid) {
+                       glDisableClientState(GL_COLOR_ARRAY);
+               }
+               if(cm->vattr[CMESH_ATTR_TEXCOORD2].vbo_valid) {
+                       glClientActiveTexture(GL_TEXTURE1);
+                       glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+                       glClientActiveTexture(GL_TEXTURE0);
+               }
+#endif /* GL_ES_VERSION_2_0 */
+       }
+}
+
+void cmesh_draw_wire(struct cmesh *cm, float linesz)
+{
+       int cur_sdr, nfaces;
+
+       if((cur_sdr = pre_draw(cm)) == -1) {
+               return;
+       }
+       update_wire_ibo(cm);
+
+       nfaces = cmesh_poly_count(cm);
+       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
+       glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0);
+       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+
+       post_draw(cm, cur_sdr);
+}
+
+void cmesh_draw_vertices(struct cmesh *cm, float ptsz)
+{
+       int cur_sdr;
+       if((cur_sdr = pre_draw(cm)) == -1) {
+               return;
+       }
+
+       glPushAttrib(GL_POINT_BIT);
+       glPointSize(ptsz);
+       glDrawArrays(GL_POINTS, 0, cm->nverts);
+       glPopAttrib();
+
+       post_draw(cm, cur_sdr);
+}
+
+void cmesh_draw_normals(struct cmesh *cm, float len)
+{
+#ifndef GL_ES_VERSION_2_0
+       int i, cur_sdr, vert_nelem, norm_nelem;
+       int loc = -1;
+       const float *varr, *norm;
+
+       varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
+       norm = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
+       if(!varr || !norm) return;
+
+       vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
+       norm_nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
+
+       glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
+       if(cur_sdr && use_custom_sdr_attr) {
+               if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
+                       return;
+               }
+       }
+
+       glBegin(GL_LINES);
+       for(i=0; i<cm->nverts; i++) {
+               float x, y, z, endx, endy, endz;
+
+               x = varr[i * vert_nelem];
+               y = varr[i * vert_nelem + 1];
+               z = varr[i * vert_nelem + 2];
+               endx = x + norm[i * norm_nelem] * len;
+               endy = y + norm[i * norm_nelem + 1] * len;
+               endz = z + norm[i * norm_nelem + 2] * len;
+
+               if(loc == -1) {
+                       glVertex3f(x, y, z);
+                       glVertex3f(endx, endy, endz);
+               } else {
+                       glVertexAttrib3f(loc, x, y, z);
+                       glVertexAttrib3f(loc, endx, endy, endz);
+               }
+       }
+       glEnd();
+#endif /* GL_ES_VERSION_2_0 */
+}
+
+void cmesh_draw_tangents(struct cmesh *cm, float len)
+{
+#ifndef GL_ES_VERSION_2_0
+       int i, cur_sdr, vert_nelem, tang_nelem;
+       int loc = -1;
+       const float *varr, *tang;
+
+       varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
+       tang = cmesh_attrib_ro(cm, CMESH_ATTR_TANGENT);
+       if(!varr || !tang) return;
+
+       vert_nelem = cm->vattr[CMESH_ATTR_VERTEX].nelem;
+       tang_nelem = cm->vattr[CMESH_ATTR_TANGENT].nelem;
+
+       glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
+       if(cur_sdr && use_custom_sdr_attr) {
+               if((loc = sdr_loc[CMESH_ATTR_VERTEX]) < 0) {
+                       return;
+               }
+       }
+
+       glBegin(GL_LINES);
+       for(i=0; i<cm->nverts; i++) {
+               float x, y, z, endx, endy, endz;
+
+               x = varr[i * vert_nelem];
+               y = varr[i * vert_nelem + 1];
+               z = varr[i * vert_nelem + 2];
+               endx = x + tang[i * tang_nelem] * len;
+               endy = y + tang[i * tang_nelem + 1] * len;
+               endz = z + tang[i * tang_nelem + 2] * len;
+
+               if(loc == -1) {
+                       glVertex3f(x, y, z);
+                       glVertex3f(endx, endy, endz);
+               } else {
+                       glVertexAttrib3f(loc, x, y, z);
+                       glVertexAttrib3f(loc, endx, endy, endz);
+               }
+       }
+       glEnd();
+#endif /* GL_ES_VERSION_2_0 */
+}
+
+static void update_buffers(struct cmesh *cm)
+{
+       int i;
+
+       for(i=0; i<CMESH_NUM_ATTR; i++) {
+               if(cmesh_has_attrib(cm, i) && !cm->vattr[i].vbo_valid) {
+                       glBindBuffer(GL_ARRAY_BUFFER, cm->vattr[i].vbo);
+                       glBufferData(GL_ARRAY_BUFFER, cm->nverts * cm->vattr[i].nelem * sizeof(float),
+                                       cm->vattr[i].data, GL_STATIC_DRAW);
+                       cm->vattr[i].vbo_valid = 1;
+               }
+       }
+       glBindBuffer(GL_ARRAY_BUFFER, 0);
+
+       if(cm->idata_valid && !cm->ibo_valid) {
+               glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->ibo);
+               glBufferData(GL_ELEMENT_ARRAY_BUFFER, cm->nfaces * 3 * sizeof(unsigned int),
+                               cm->idata, GL_STATIC_DRAW);
+               cm->ibo_valid = 1;
+               glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+       }
+}
+
+static void update_wire_ibo(struct cmesh *cm)
+{
+       int i, num_faces;
+       unsigned int *wire_idxarr, *dest;
+
+       update_buffers(cm);
+
+       if(cm->wire_ibo_valid) return;
 
-/* get the bounding sphere in local space. The result will be cached ... see above */
-float cmesh_bsphere(struct cmesh *cm, cgm_vec3 *center, float *rad);
+       if(!cm->wire_ibo) {
+               glGenBuffers(1, &cm->wire_ibo);
+       }
+       num_faces = cmesh_poly_count(cm);
+
+       if(!(wire_idxarr = malloc(num_faces * 6 * sizeof *wire_idxarr))) {
+               return;
+       }
+       dest = wire_idxarr;
+
+       if(cm->ibo_valid) {
+               /* we're dealing with an indexed mesh */
+               const unsigned int *idxarr = cmesh_index_ro(cm);
+
+               for(i=0; i<num_faces; i++) {
+                       *dest++ = idxarr[0];
+                       *dest++ = idxarr[1];
+                       *dest++ = idxarr[1];
+                       *dest++ = idxarr[2];
+                       *dest++ = idxarr[2];
+                       *dest++ = idxarr[0];
+                       idxarr += 3;
+               }
+       } else {
+               /* not an indexed mesh */
+               for(i=0; i<num_faces; i++) {
+                       int vidx = i * 3;
+                       *dest++ = vidx;
+                       *dest++ = vidx + 1;
+                       *dest++ = vidx + 1;
+                       *dest++ = vidx + 2;
+                       *dest++ = vidx + 2;
+                       *dest++ = vidx;
+               }
+       }
+
+       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cm->wire_ibo);
+       glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_faces * 6 * sizeof(unsigned int),
+                       wire_idxarr, GL_STATIC_DRAW);
+       free(wire_idxarr);
+       cm->wire_ibo_valid = 1;
+       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+}
+
+static void calc_aabb(struct cmesh *cm)
+{
+       int i, j;
+
+       if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
+               return;
+       }
+
+       cgm_vcons(&cm->aabb_min, FLT_MAX, FLT_MAX, FLT_MAX);
+       cgm_vcons(&cm->aabb_max, -FLT_MAX, -FLT_MAX, -FLT_MAX);
+
+       for(i=0; i<cm->nverts; i++) {
+               const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
+               for(j=0; j<3; j++) {
+                       if(v[j] < (&cm->aabb_min.x)[j]) {
+                               (&cm->aabb_min.x)[j] = v[j];
+                       }
+                       if(v[j] > (&cm->aabb_max.x)[j]) {
+                               (&cm->aabb_max.x)[j] = v[j];
+                       }
+               }
+       }
+       cm->aabb_valid = 1;
+}
+
+void cmesh_aabbox(struct cmesh *cm, cgm_vec3 *vmin, cgm_vec3 *vmax)
+{
+       if(!cm->aabb_valid) {
+               calc_aabb(cm);
+       }
+       *vmin = cm->aabb_min;
+       *vmax = cm->aabb_max;
+}
+
+static void calc_bsph(struct cmesh *cm)
+{
+       int i;
+       float s, dist_sq;
+
+       if(!cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX)) {
+               return;
+       }
+
+       cgm_vcons(&cm->bsph_center, 0, 0, 0);
+
+       /* first find the center */
+       for(i=0; i<cm->nverts; i++) {
+               const float *v = cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
+               cm->bsph_center.x += v[0];
+               cm->bsph_center.y += v[1];
+               cm->bsph_center.z += v[2];
+       }
+       s = 1.0f / (float)cm->nverts;
+       cm->bsph_center.x *= s;
+       cm->bsph_center.y *= s;
+       cm->bsph_center.z *= s;
+
+       cm->bsph_radius = 0.0f;
+       for(i=0; i<cm->nverts; i++) {
+               const cgm_vec3 *v = (const cgm_vec3*)cmesh_attrib_at_ro(cm, CMESH_ATTR_VERTEX, i);
+               if((dist_sq = cgm_vdist_sq(v, &cm->bsph_center)) > cm->bsph_radius) {
+                       cm->bsph_radius = dist_sq;
+               }
+       }
+       cm->bsph_radius = sqrt(cm->bsph_radius);
+       cm->bsph_valid = 1;
+}
+
+float cmesh_bsphere(struct cmesh *cm, cgm_vec3 *center, float *rad)
+{
+       if(!cm->bsph_valid) {
+               calc_bsph(cm);
+       }
+       *center = cm->bsph_center;
+       *rad = cm->bsph_radius;
+       return cm->bsph_radius;
+}
 
-/* texture coordinate manipulation */
+/* TODO */
 void cmesh_texcoord_apply_xform(struct cmesh *cm, float *xform);
 void cmesh_texcoord_gen_plane(struct cmesh *cm, cgm_vec3 *norm, cgm_vec3 *tang);
 void cmesh_texcoord_gen_box(struct cmesh *cm);
index 2039e10..a73cd20 100644 (file)
@@ -60,7 +60,7 @@ unsigned int *cmesh_index(struct cmesh *cm);  /* invalidates IBO */
 const unsigned int *cmesh_index_ro(struct cmesh *cm);  /* doesn't invalidate */
 int cmesh_index_count(struct cmesh *cm);
 
-int get_poly_count(struct cmesh *cm);
+int cmesh_poly_count(struct cmesh *cm);
 
 /* attr can be -1 to invalidate all attributes */
 void cmesh_invalidate_vbo(struct cmesh *cm, int attr);
@@ -83,7 +83,7 @@ void cmesh_flip(struct cmesh *cm);    /* flip faces (winding) and normals */
 void cmesh_flip_faces(struct cmesh *cm);
 void cmesh_flip_normals(struct cmesh *cm);
 
-void cmesh_explode(struct cmesh *cm);  /* undo all vertex sharing */
+int cmesh_explode(struct cmesh *cm);   /* undo all vertex sharing */
 
 /* this is only guaranteed to work on an exploded mesh */
 void cmesh_calc_face_normals(struct cmesh *cm);
diff --git a/src/meshload.c b/src/meshload.c
new file mode 100644 (file)
index 0000000..f9ee26f
--- /dev/null
@@ -0,0 +1,350 @@
+#if 0
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <assert.h>
+#include "mesh.h"
+#include "dynarr.h"
+#include "rbtree.h"
+#include "util.h"
+
+struct vertex_pos_color {
+       float x, y, z;
+       float r, g, b, a;
+};
+
+struct facevertex {
+       int vidx, tidx, nidx;
+};
+
+static char *clean_line(char *s);
+static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn);
+static int cmp_facevert(const void *ap, const void *bp);
+static void free_rbnode_key(struct rbnode *n, void *cls);
+
+/* merge of different indices per attribute happens during face processing.
+ *
+ * A triplet of (vertex index/texcoord index/normal index) is used as the key
+ * to search in a balanced binary search tree for vertex buffer index assigned
+ * to the same triplet if it has been encountered before. That index is
+ * appended to the index buffer.
+ *
+ * If a particular triplet has not been encountered before, a new g3d_vertex is
+ * appended to the vertex buffer. The index of this new vertex is appended to
+ * the index buffer, and also inserted into the tree for future searches.
+ */
+int load_mesh(struct g3d_mesh *mesh, const char *fname)
+{
+       int i, line_num = 0, result = -1;
+       int found_quad = 0;
+       FILE *fp = 0;
+       char buf[256];
+       struct vertex_pos_color *varr = 0;
+       vec3_t *narr = 0;
+       vec2_t *tarr = 0;
+       struct rbtree *rbtree = 0;
+
+       if(!(fp = fopen(fname, "rb"))) {
+               fprintf(stderr, "load_mesh: failed to open file: %s\n", fname);
+               goto err;
+       }
+
+       if(!(rbtree = rb_create(cmp_facevert))) {
+               fprintf(stderr, "load_mesh: failed to create facevertex binary search tree\n");
+               goto err;
+       }
+       rb_set_delete_func(rbtree, free_rbnode_key, 0);
+
+       if(!(mesh->varr = dynarr_alloc(0, sizeof *mesh->varr)) ||
+                       !(mesh->iarr = dynarr_alloc(0, sizeof *mesh->iarr))) {
+               fprintf(stderr, "load_mesh: failed to allocate resizable mesh arrays\n");
+               goto err;
+       }
+       if(!(varr = dynarr_alloc(0, sizeof *varr)) ||
+                       !(narr = dynarr_alloc(0, sizeof *narr)) ||
+                       !(tarr = dynarr_alloc(0, sizeof *tarr))) {
+               fprintf(stderr, "load_mesh: failed to allocate resizable vertex array\n");
+               goto err;
+       }
+
+       while(fgets(buf, sizeof buf, fp)) {
+               char *line = clean_line(buf);
+               ++line_num;
+
+               if(!*line) continue;
+
+               switch(line[0]) {
+               case 'v':
+                       if(isspace(line[1])) {
+                               /* vertex */
+                               struct vertex_pos_color v;
+                               int num;
+
+                               num = sscanf(line + 2, "%f %f %f %f %f %f %f", &v.x, &v.y, &v.z, &v.r, &v.g, &v.b, &v.a);
+                               if(num < 3) {
+                                       fprintf(stderr, "%s:%d: invalid vertex definition: \"%s\"\n", fname, line_num, line);
+                                       goto err;
+                               }
+                               switch(num) {
+                               case 3:
+                                       v.r = 1.0f;
+                               case 4:
+                                       v.g = 1.0f;
+                               case 5:
+                                       v.b = 1.0f;
+                               case 6:
+                                       v.a = 1.0f;
+                               }
+                               if(!(varr = dynarr_push(varr, &v))) {
+                                       fprintf(stderr, "load_mesh: failed to resize vertex buffer\n");
+                                       goto err;
+                               }
+
+                       } else if(line[1] == 't' && isspace(line[2])) {
+                               /* texcoord */
+                               vec2_t tc;
+                               if(sscanf(line + 3, "%f %f", &tc.x, &tc.y) != 2) {
+                                       fprintf(stderr, "%s:%d: invalid texcoord definition: \"%s\"\n", fname, line_num, line);
+                                       goto err;
+                               }
+                               if(!(tarr = dynarr_push(tarr, &tc))) {
+                                       fprintf(stderr, "load_mesh: failed to resize texcoord buffer\n");
+                                       goto err;
+                               }
+
+                       } else if(line[1] == 'n' && isspace(line[2])) {
+                               /* normal */
+                               vec3_t norm;
+                               if(sscanf(line + 3, "%f %f %f", &norm.x, &norm.y, &norm.z) != 3) {
+                                       fprintf(stderr, "%s:%d: invalid normal definition: \"%s\"\n", fname, line_num, line);
+                                       goto err;
+                               }
+                               if(!(narr = dynarr_push(narr, &norm))) {
+                                       fprintf(stderr, "load_mesh: failed to resize normal buffer\n");
+                                       goto err;
+                               }
+                       }
+                       break;
+
+               case 'f':
+                       if(isspace(line[1])) {
+                               /* face */
+                               char *ptr = line + 2;
+                               struct facevertex fv;
+                               struct rbnode *node;
+                               int vsz = dynarr_size(varr);
+                               int tsz = dynarr_size(tarr);
+                               int nsz = dynarr_size(narr);
+
+                               for(i=0; i<4; i++) {
+                                       if(!(ptr = parse_face_vert(ptr, &fv, vsz, tsz, nsz))) {
+                                               if(i < 3 || found_quad) {
+                                                       fprintf(stderr, "%s:%d: invalid face definition: \"%s\"\n", fname, line_num, line);
+                                                       goto err;
+                                               } else {
+                                                       break;
+                                               }
+                                       }
+
+                                       if((node = rb_find(rbtree, &fv))) {
+                                               uint16_t idx = (int)(intptr_t)node->data;
+                                               if(!(mesh->iarr = dynarr_push(mesh->iarr, &idx))) {
+                                                       fprintf(stderr, "load_mesh: failed to resize index array\n");
+                                                       goto err;
+                                               }
+                                       } else {
+                                               uint16_t newidx = dynarr_size(mesh->varr);
+                                               struct g3d_vertex v;
+                                               struct facevertex *newfv;
+
+                                               v.x = varr[fv.vidx].x;
+                                               v.y = varr[fv.vidx].y;
+                                               v.z = varr[fv.vidx].z;
+                                               v.w = 1.0f;
+                                               v.r = cround64(varr[fv.vidx].r * 255.0);
+                                               v.g = cround64(varr[fv.vidx].g * 255.0);
+                                               v.b = cround64(varr[fv.vidx].b * 255.0);
+                                               v.a = cround64(varr[fv.vidx].a * 255.0);
+                                               if(fv.tidx >= 0) {
+                                                       v.u = tarr[fv.tidx].x;
+                                                       v.v = tarr[fv.tidx].y;
+                                               } else {
+                                                       v.u = v.x;
+                                                       v.v = v.y;
+                                               }
+                                               if(fv.nidx >= 0) {
+                                                       v.nx = narr[fv.nidx].x;
+                                                       v.ny = narr[fv.nidx].y;
+                                                       v.nz = narr[fv.nidx].z;
+                                               } else {
+                                                       v.nx = v.ny = 0.0f;
+                                                       v.nz = 1.0f;
+                                               }
+
+                                               if(!(mesh->varr = dynarr_push(mesh->varr, &v))) {
+                                                       fprintf(stderr, "load_mesh: failed to resize combined vertex array\n");
+                                                       goto err;
+                                               }
+                                               if(!(mesh->iarr = dynarr_push(mesh->iarr, &newidx))) {
+                                                       fprintf(stderr, "load_mesh: failed to resize index array\n");
+                                                       goto err;
+                                               }
+
+                                               if((newfv = malloc(sizeof *newfv))) {
+                                                       *newfv = fv;
+                                               }
+                                               if(!newfv || rb_insert(rbtree, newfv, (void*)(intptr_t)newidx) == -1) {
+                                                       fprintf(stderr, "load_mesh: failed to insert facevertex to the binary search tree\n");
+                                                       goto err;
+                                               }
+                                       }
+                               }
+                               if(i > 3) found_quad = 1;
+                       }
+                       break;
+
+               default:
+                       break;
+               }
+       }
+
+       mesh->prim = found_quad ? G3D_QUADS : G3D_TRIANGLES;
+       mesh->vcount = dynarr_size(mesh->varr);
+       mesh->icount = dynarr_size(mesh->iarr);
+       mesh->varr = dynarr_finalize(mesh->varr);
+       mesh->iarr = dynarr_finalize(mesh->iarr);
+       result = 0;     /* success */
+
+       printf("loaded %s mesh: %s: %d vertices, %d faces\n", found_quad ? "quad" : "triangle",
+                       fname, mesh->vcount, mesh->icount / mesh->prim);
+
+err:
+       if(fp) fclose(fp);
+       dynarr_free(varr);
+       dynarr_free(narr);
+       dynarr_free(tarr);
+       if(result == -1) {
+               dynarr_free(mesh->varr);
+               dynarr_free(mesh->iarr);
+       }
+       rb_free(rbtree);
+       return result;
+}
+
+int save_mesh(struct g3d_mesh *mesh, const char *fname)
+{
+       int i, fvcount;
+       FILE *fp;
+
+       if(!(fp = fopen(fname, "wb"))) {
+               fprintf(stderr, "save_mesh: failed to open %s for writing\n", fname);
+               return -1;
+       }
+       fprintf(fp, "# Wavefront OBJ file shoved in your FACE by Mindlapse. Deal with it\n");
+
+       for(i=0; i<mesh->vcount; i++) {
+               struct g3d_vertex *v = mesh->varr + i;
+               fprintf(fp, "v %f %f %f %f %f %f %f\n", v->x, v->y, v->z, v->r / 255.0f, v->g / 255.0f,
+                               v->b / 255.0f, v->a / 255.0f);
+       }
+       for(i=0; i<mesh->vcount; i++) {
+               fprintf(fp, "vn %f %f %f\n", mesh->varr[i].nx, mesh->varr[i].ny, mesh->varr[i].nz);
+       }
+       for(i=0; i<mesh->vcount; i++) {
+               fprintf(fp, "vt %f %f\n", mesh->varr[i].u, mesh->varr[i].v);
+       }
+
+       fvcount = mesh->prim;
+       for(i=0; i<mesh->icount; i++) {
+               int idx = mesh->iarr[i] + 1;
+
+               if(fvcount == mesh->prim) {
+                       fprintf(fp, "\nf");
+                       fvcount = 0;
+               }
+               fprintf(fp, " %d/%d/%d", idx, idx, idx);
+               ++fvcount;
+       }
+       fprintf(fp, "\n");
+
+       fclose(fp);
+       return 0;
+}
+
+static char *clean_line(char *s)
+{
+       char *end;
+
+       while(*s && isspace(*s)) ++s;
+       if(!*s) return 0;
+
+       end = s;
+       while(*end && *end != '#') ++end;
+       *end = 0;
+
+       while(end > s && isspace(*end)) --end;
+       *end = 0;
+
+       return s;
+}
+
+static char *parse_idx(char *ptr, int *idx, int arrsz)
+{
+       char *endp;
+       int val = strtol(ptr, &endp, 10);
+       if(endp == ptr) return 0;
+
+       if(val < 0) {   /* convert negative indices */
+               *idx = arrsz + val;
+       } else {
+               *idx = val - 1; /* indices in obj are 1-based */
+       }
+       return endp;
+}
+
+/* possible face-vertex definitions:
+ * 1. vertex
+ * 2. vertex/texcoord
+ * 3. vertex//normal
+ * 4. vertex/texcoord/normal
+ */
+static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn)
+{
+       if(!(ptr = parse_idx(ptr, &fv->vidx, numv)))
+               return 0;
+       if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
+
+       if(*++ptr == '/') {     /* no texcoord */
+               fv->tidx = -1;
+               ++ptr;
+       } else {
+               if(!(ptr = parse_idx(ptr, &fv->tidx, numt)))
+                       return 0;
+               if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
+               ++ptr;
+       }
+
+       if(!(ptr = parse_idx(ptr, &fv->nidx, numn)))
+               return 0;
+       return (!*ptr || isspace(*ptr)) ? ptr : 0;
+}
+
+static int cmp_facevert(const void *ap, const void *bp)
+{
+       const struct facevertex *a = ap;
+       const struct facevertex *b = bp;
+
+       if(a->vidx == b->vidx) {
+               if(a->tidx == b->tidx) {
+                       return a->nidx - b->nidx;
+               }
+               return a->tidx - b->tidx;
+       }
+       return a->vidx - b->vidx;
+}
+
+static void free_rbnode_key(struct rbnode *n, void *cls)
+{
+       free(n->key);
+}
+#endif /* 0 */