foo
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Fri, 1 Mar 2019 13:44:40 +0000 (15:44 +0200)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Fri, 1 Mar 2019 13:44:40 +0000 (15:44 +0200)
src/cmesh.c
src/gamescr.c
src/meshload.c

index 993fa2c..e40e4f4 100644 (file)
@@ -1296,9 +1296,14 @@ int cmesh_dump_obj(struct cmesh *cm, const char *fname)
        return -1;
 }
 
+#define HAS_VN 1
+#define HAS_VT 2
+
 int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs)
 {
+       static const char *fmtstr[] = {" %u", " %u//%u", " %u/%u", " %u/%u/%u"};
        int i, j, num, nelem;
+       unsigned int aflags = 0;
 
        if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) {
                return -1;
@@ -1315,6 +1320,7 @@ int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs)
        }
 
        if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) {
+               aflags |= HAS_VN;
                nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem;
                if((num = dynarr_size(cm->vattr[CMESH_ATTR_NORMAL].data)) != cm->nverts * nelem) {
                        warning_log("normal array size (%d) != nverts (%d)\n", num, cm->nverts);
@@ -1326,6 +1332,7 @@ int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs)
        }
 
        if(cmesh_has_attrib(cm, CMESH_ATTR_TEXCOORD)) {
+               aflags |= HAS_VT;
                nelem = cm->vattr[CMESH_ATTR_TEXCOORD].nelem;
                if((num = dynarr_size(cm->vattr[CMESH_ATTR_TEXCOORD].data)) != cm->nverts * nelem) {
                        warning_log("texcoord array size (%d) != nverts (%d)\n", num, cm->nverts);
@@ -1346,7 +1353,7 @@ int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs)
                        fputc('f', fp);
                        for(j=0; j<3; j++) {
                                unsigned int idx = *idxptr++ + 1 + voffs;
-                               fprintf(fp, " %u/%u/%u", idx, idx, idx);
+                               fprintf(fp, fmtstr[aflags], idx, idx, idx);
                        }
                        fputc('\n', fp);
                }
@@ -1356,7 +1363,7 @@ int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs)
                for(i=0; i<numtri; i++) {
                        fputc('f', fp);
                        for(j=0; j<3; j++) {
-                               fprintf(fp, " %u/%u/%u", idx, idx, idx);
+                               fprintf(fp, fmtstr[aflags], idx, idx, idx);
                                ++idx;
                        }
                        fputc('\n', fp);
index cc1a837..5fb7b02 100644 (file)
@@ -45,6 +45,7 @@ static int init(void)
                fprintf(stderr, "failed to load block model\n");
                return -1;
        }
+       cmesh_dump(blkmesh, "dump");
        cmesh_dump_obj(blkmesh, "dump.obj");
        return 0;
 }
index ef596a1..ff398aa 100644 (file)
@@ -35,6 +35,7 @@ int cmesh_load(struct cmesh *mesh, const char *fname)
 {
        int i, line_num = 0, result = -1;
        int found_quad = 0;
+       int found_color = 0;
        FILE *fp = 0;
        char buf[256];
        struct vertex_pos_color *varr = 0;
@@ -78,6 +79,7 @@ int cmesh_load(struct cmesh *mesh, const char *fname)
                                        fprintf(stderr, "%s:%d: invalid vertex definition: \"%s\"\n", fname, line_num, line);
                                        goto err;
                                }
+                               if(num > 3) found_color = 1;
                                switch(num) {
                                case 3:
                                        v.r = 1.0f;
@@ -141,6 +143,7 @@ int cmesh_load(struct cmesh *mesh, const char *fname)
 
                                        if((node = rb_find(rbtree, &fv))) {
                                                unsigned int idx = (unsigned int)node->data;
+                                               assert(idx < cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX));
                                                if(cmesh_push_index(mesh, idx) == -1) {
                                                        fprintf(stderr, "load_mesh: failed to resize index array\n");
                                                        goto err;
@@ -149,26 +152,33 @@ int cmesh_load(struct cmesh *mesh, const char *fname)
                                                unsigned int newidx = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX);
                                                struct facevertex *newfv;
                                                struct vertex_pos_color *vptr = varr + fv.vidx;
-                                               float tu, tv;
 
                                                if(cmesh_push_attrib3f(mesh, CMESH_ATTR_VERTEX, vptr->x, vptr->y, vptr->z) == -1) {
                                                        fprintf(stderr, "load_mesh: failed to resize vertex array\n");
                                                        goto err;
                                                }
-                                               if(cmesh_push_attrib(mesh, CMESH_ATTR_COLOR, &vptr->r) == -1) {
-                                                       fprintf(stderr, "load_mesh: failed to resize color array\n");
-                                                       goto err;
+                                               if(found_color) {
+                                                       if(cmesh_push_attrib(mesh, CMESH_ATTR_COLOR, &vptr->r) == -1) {
+                                                               fprintf(stderr, "load_mesh: failed to resize color array\n");
+                                                               goto err;
+                                                       }
                                                }
-                                               if(fv.tidx >= 0) {
-                                                       tu = tarr[fv.tidx].x;
-                                                       tv = tarr[fv.tidx].y;
-                                               } else {
-                                                       tu = vptr->x;
-                                                       tv = vptr->y;
+                                               if(fv.nidx >= 0) {
+                                                       float nx = narr[fv.nidx].x;
+                                                       float ny = narr[fv.nidx].y;
+                                                       float nz = narr[fv.nidx].z;
+                                                       if(cmesh_push_attrib3f(mesh, CMESH_ATTR_NORMAL, nx, ny, nz) == -1) {
+                                                               fprintf(stderr, "load_mesh: failed to resize normal array\n");
+                                                               goto err;
+                                                       }
                                                }
-                                               if(cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, tu, tv) == -1) {
-                                                       fprintf(stderr, "load_mesh: failed to resize texcoord array\n");
-                                                       goto err;
+                                               if(fv.tidx >= 0) {
+                                                       float tu = tarr[fv.tidx].x;
+                                                       float tv = tarr[fv.tidx].y;
+                                                       if(cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, tu, tv) == -1) {
+                                                               fprintf(stderr, "load_mesh: failed to resize texcoord array\n");
+                                                               goto err;
+                                                       }
                                                }
 
                                                if((newfv = malloc(sizeof *newfv))) {