suzanne
[gba_blender] / tools / meshdump / main.c
index a442893..d758deb 100644 (file)
@@ -3,6 +3,8 @@
 #include <string.h>
 #include "cmesh.h"
 
+int dump(struct cmesh *cm);
+
 int main(int argc, char **argv)
 {
        int i;
@@ -25,26 +27,41 @@ int main(int argc, char **argv)
        return 0;
 }
 
+static int nverts, nidx, voffs;
+static const float *varr, *narr;
+static unsigned int *iarr;
+
+static int zcmp(const void *a, const void *b)
+{
+       unsigned int *aidx = (unsigned int*)a;
+       unsigned int *bidx = (unsigned int*)b;
+
+       float az = varr[aidx[0] * 3 + 2] + varr[aidx[1] * 3 + 2] + varr[aidx[2] * 3 + 2];
+       float bz = varr[bidx[0] * 3 + 2] + varr[bidx[1] * 3 + 2] + varr[bidx[2] * 3 + 2];
+
+       return az - bz;
+}
+
 int dump(struct cmesh *cm)
 {
-       int i, nverts, nidx, voffs;
-       const float *varr, *narr;
-       const unsigned int *iarr;
+       int i;
 
        varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
        narr = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
-       iarr = cmesh_index_ro(cm);
+       iarr = cmesh_index(cm);
        nverts = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
        nidx = cmesh_index_count(cm);
 
+       qsort(iarr, nidx / 3, sizeof *iarr * 3, zcmp);
+
        printf("static struct xvertex mesh[] = {\n");
        for(i=0; i<nidx; i++) {
                voffs = iarr[i] * 3;
-               printf("\t{%d, %d, %d,", (int)(varr[voffs] * 65536.0f),
-                               (int)(varr[voffs + 1] * 65536.0f), (int)(varr[voffs + 2] * 65536.0f));
-               printf("\t%d, %d, %d,", (int)(narr[voffs] * 65536.0f),
-                               (int)(narr[voffs + 1] * 65536.0f), (int)(narr[voffs + 2] * 65536.0f));
-               printf("\t0xff}%c\n", i < nidx - 1 ? ',' : '\n');
+               printf("\t{%7d, %7d, %7d,", (int)(varr[voffs] * 65536.0f),
+                               (int)(varr[voffs + 1] * 65536.0f), -(int)(varr[voffs + 2] * 65536.0f));
+               printf("  %7d, %7d, %7d,", (int)(narr[voffs] * 65536.0f),
+                               (int)(narr[voffs + 1] * 65536.0f), -(int)(narr[voffs + 2] * 65536.0f));
+               printf("  0xff}%c\n", i < nidx - 1 ? ',' : '\n');
        }
        printf("};\n");