suzanne
[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 dump(struct cmesh *cm);
7
8 int main(int argc, char **argv)
9 {
10         int i;
11         struct cmesh *cm;
12
13         for(i=1; i<argc; i++) {
14                 if(!(cm = cmesh_alloc())) {
15                         perror("failed to allocate mesh");
16                         return 1;
17                 }
18                 if(cmesh_load(cm, argv[i]) == -1) {
19                         fprintf(stderr, "failed to load: %s\n", argv[i]);
20                         return 1;
21                 }
22
23                 dump(cm);
24
25                 cmesh_free(cm);
26         }
27         return 0;
28 }
29
30 static int nverts, nidx, voffs;
31 static const float *varr, *narr;
32 static unsigned int *iarr;
33
34 static int zcmp(const void *a, const void *b)
35 {
36         unsigned int *aidx = (unsigned int*)a;
37         unsigned int *bidx = (unsigned int*)b;
38
39         float az = varr[aidx[0] * 3 + 2] + varr[aidx[1] * 3 + 2] + varr[aidx[2] * 3 + 2];
40         float bz = varr[bidx[0] * 3 + 2] + varr[bidx[1] * 3 + 2] + varr[bidx[2] * 3 + 2];
41
42         return az - bz;
43 }
44
45 int dump(struct cmesh *cm)
46 {
47         int i;
48
49         varr = cmesh_attrib_ro(cm, CMESH_ATTR_VERTEX);
50         narr = cmesh_attrib_ro(cm, CMESH_ATTR_NORMAL);
51         iarr = cmesh_index(cm);
52         nverts = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
53         nidx = cmesh_index_count(cm);
54
55         qsort(iarr, nidx / 3, sizeof *iarr * 3, zcmp);
56
57         printf("static struct xvertex mesh[] = {\n");
58         for(i=0; i<nidx; i++) {
59                 voffs = iarr[i] * 3;
60                 printf("\t{%7d, %7d, %7d,", (int)(varr[voffs] * 65536.0f),
61                                 (int)(varr[voffs + 1] * 65536.0f), -(int)(varr[voffs + 2] * 65536.0f));
62                 printf("  %7d, %7d, %7d,", (int)(narr[voffs] * 65536.0f),
63                                 (int)(narr[voffs + 1] * 65536.0f), -(int)(narr[voffs + 2] * 65536.0f));
64                 printf("  0xff}%c\n", i < nidx - 1 ? ',' : '\n');
65         }
66         printf("};\n");
67
68         return 0;
69 }