a442893e1cadd3f1dfac77be490432f981a5d512
[gba_blender] / tools / meshdump / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "cmesh.h"
5
6 int main(int argc, char **argv)
7 {
8         int i;
9         struct cmesh *cm;
10
11         for(i=1; i<argc; i++) {
12                 if(!(cm = cmesh_alloc())) {
13                         perror("failed to allocate mesh");
14                         return 1;
15                 }
16                 if(cmesh_load(cm, argv[i]) == -1) {
17                         fprintf(stderr, "failed to load: %s\n", argv[i]);
18                         return 1;
19                 }
20
21                 dump(cm);
22
23                 cmesh_free(cm);
24         }
25         return 0;
26 }
27
28 int dump(struct cmesh *cm)
29 {
30         int i, nverts, nidx, voffs;
31         const float *varr, *narr;
32         const unsigned int *iarr;
33
34         varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
35         narr = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
36         iarr = cmesh_index_ro(cm);
37         nverts = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
38         nidx = cmesh_index_count(cm);
39
40         printf("static struct xvertex mesh[] = {\n");
41         for(i=0; i<nidx; i++) {
42                 voffs = iarr[i] * 3;
43                 printf("\t{%d, %d, %d,", (int)(varr[voffs] * 65536.0f),
44                                 (int)(varr[voffs + 1] * 65536.0f), (int)(varr[voffs + 2] * 65536.0f));
45                 printf("\t%d, %d, %d,", (int)(narr[voffs] * 65536.0f),
46                                 (int)(narr[voffs + 1] * 65536.0f), (int)(narr[voffs + 2] * 65536.0f));
47                 printf("\t0xff}%c\n", i < nidx - 1 ? ',' : '\n');
48         }
49         printf("};\n");
50
51         return 0;
52 }