gba-trycatch
view src/ggen.c @ 15:b755fb002f17
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 25 Jun 2014 17:02:48 +0300 |
parents | c398d834d64a |
children |
line source
1 #include <stdlib.h>
2 #include "ggen.h"
3 #include "mesh.h"
5 #define X1 65536
6 #define VERTEX(x, y, z) \
7 do { \
8 *vptr++ = x; \
9 *vptr++ = y; \
10 *vptr++ = z; \
11 } while(0)
13 #define COLOR(r, g, b) \
14 do { \
15 *cptr++ = r; \
16 *cptr++ = g; \
17 *cptr++ = b; \
18 } while(0)
20 #define TEXCOORD(u, v) \
21 do { \
22 *tptr++ = u; \
23 *tptr++ = v; \
24 } while(0)
26 int gen_box(struct mesh *m)
27 {
28 int i;
29 int num_faces = 6;
30 int num_verts = num_faces * 4;
31 int32_t v[8][3] = {
32 {-X1, -X1, -X1}, {X1, -X1, -X1}, {X1, -X1, X1}, {-X1, -X1, X1},
33 {-X1, X1, -X1}, {X1, X1, -X1}, {X1, X1, X1}, {-X1, X1, X1}
34 };
35 int32_t *vptr, *cptr, *tptr;
38 m->prim = X3D_QUADS;
39 m->nverts = num_verts;
40 if(!(m->verts = malloc(num_verts * 3 * sizeof *m->verts))) {
41 return -1;
42 }
43 vptr = m->verts;
45 if(!(m->colors = malloc(num_verts * 3 * sizeof *m->colors))) {
46 return -1;
47 }
48 cptr = m->colors;
50 if(!(m->texcoords = malloc(num_verts * 2 * sizeof *m->texcoords))) {
51 return -1;
52 }
53 tptr = m->texcoords;
55 /* -Y */
56 VERTEX(v[0][0], v[0][1], v[0][2]);
57 VERTEX(v[1][0], v[1][1], v[1][2]);
58 VERTEX(v[2][0], v[2][1], v[2][2]);
59 VERTEX(v[3][0], v[3][1], v[3][2]);
60 TEXCOORD(65536, 0);
61 TEXCOORD(0, 0);
62 TEXCOORD(0, 65536);
63 TEXCOORD(65536, 65536);
64 for(i=0; i<4; i++) COLOR(65536, 0, 65536);
66 /* +Y */
67 VERTEX(v[4][0], v[4][1], v[4][2]);
68 VERTEX(v[7][0], v[7][1], v[7][2]);
69 VERTEX(v[6][0], v[6][1], v[6][2]);
70 VERTEX(v[5][0], v[5][1], v[5][2]);
71 TEXCOORD(0, 0);
72 TEXCOORD(0, 65536);
73 TEXCOORD(65536, 65536);
74 TEXCOORD(65536, 0);
75 for(i=0; i<4; i++) COLOR(0, 65536, 0);
77 /* -Z */
78 VERTEX(v[0][0], v[0][1], v[0][2]);
79 VERTEX(v[4][0], v[4][1], v[4][2]);
80 VERTEX(v[5][0], v[5][1], v[5][2]);
81 VERTEX(v[1][0], v[1][1], v[1][2]);
82 TEXCOORD(0, 0);
83 TEXCOORD(0, 65536);
84 TEXCOORD(65536, 65536);
85 TEXCOORD(65536, 0);
86 for(i=0; i<4; i++) COLOR(65536, 65536, 0);
88 /* +Z */
89 VERTEX(v[2][0], v[2][1], v[2][2]);
90 VERTEX(v[6][0], v[6][1], v[6][2]);
91 VERTEX(v[7][0], v[7][1], v[7][2]);
92 VERTEX(v[3][0], v[3][1], v[3][2]);
93 TEXCOORD(0, 0);
94 TEXCOORD(0, 65536);
95 TEXCOORD(65536, 65536);
96 TEXCOORD(65536, 0);
97 for(i=0; i<4; i++) COLOR(0, 0, 65536);
99 /* +X */
100 VERTEX(v[1][0], v[1][1], v[1][2]);
101 VERTEX(v[5][0], v[5][1], v[5][2]);
102 VERTEX(v[6][0], v[6][1], v[6][2]);
103 VERTEX(v[2][0], v[2][1], v[2][2]);
104 TEXCOORD(0, 0);
105 TEXCOORD(0, 65536);
106 TEXCOORD(65536, 65536);
107 TEXCOORD(65536, 0);
108 for(i=0; i<4; i++) COLOR(65536, 0, 0);
110 /* -X */
111 VERTEX(v[3][0], v[3][1], v[3][2]);
112 VERTEX(v[7][0], v[7][1], v[7][2]);
113 VERTEX(v[4][0], v[4][1], v[4][2]);
114 VERTEX(v[0][0], v[0][1], v[0][2]);
115 TEXCOORD(0, 0);
116 TEXCOORD(0, 65536);
117 TEXCOORD(65536, 65536);
118 TEXCOORD(65536, 0);
119 for(i=0; i<4; i++) COLOR(0, 65536, 65536);
121 return 0;
122 }