suzanne
[gba_blender] / tools / meshdump / main.c
diff --git a/tools/meshdump/main.c b/tools/meshdump/main.c
new file mode 100644 (file)
index 0000000..a442893
--- /dev/null
@@ -0,0 +1,52 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "cmesh.h"
+
+int main(int argc, char **argv)
+{
+       int i;
+       struct cmesh *cm;
+
+       for(i=1; i<argc; i++) {
+               if(!(cm = cmesh_alloc())) {
+                       perror("failed to allocate mesh");
+                       return 1;
+               }
+               if(cmesh_load(cm, argv[i]) == -1) {
+                       fprintf(stderr, "failed to load: %s\n", argv[i]);
+                       return 1;
+               }
+
+               dump(cm);
+
+               cmesh_free(cm);
+       }
+       return 0;
+}
+
+int dump(struct cmesh *cm)
+{
+       int i, nverts, nidx, voffs;
+       const float *varr, *narr;
+       const unsigned int *iarr;
+
+       varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
+       narr = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
+       iarr = cmesh_index_ro(cm);
+       nverts = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
+       nidx = cmesh_index_count(cm);
+
+       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("};\n");
+
+       return 0;
+}