add missing tools/pngdump to the repo
[gbajam22] / 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, *tarr;
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         tarr = cmesh_attrib_ro(cm, CMESH_ATTR_TEXCOORD);
52         iarr = cmesh_index(cm);
53         nverts = cmesh_attrib_count(cm, CMESH_ATTR_VERTEX);
54         nidx = cmesh_index_count(cm);
55
56         qsort(iarr, nidx / 3, sizeof *iarr * 3, zcmp);
57
58         printf("static struct xvertex mesh[] = {\n");
59         for(i=0; i<nidx; i++) {
60                 voffs = iarr[i] * 3;
61                 printf("\t{%7d, %7d, %7d,", (int)(varr[voffs] * 65536.0f),
62                                 (int)(varr[voffs + 1] * 65536.0f), -(int)(varr[voffs + 2] * 65536.0f));
63                 printf("  %7d, %7d, %7d,", (int)(narr[voffs] * 65536.0f),
64                                 (int)(narr[voffs + 1] * 65536.0f), -(int)(narr[voffs + 2] * 65536.0f));
65                 printf("  %7d, %7d,", (int)(tarr[voffs] * 65536.0f), (int)(tarr[voffs] * 65536.0f));
66                 printf("  0xff}%c\n", i < nidx - 1 ? ',' : '\n');
67         }
68         printf("};\n");
69
70         return 0;
71 }