gba-x3dtest

annotate src/ggen.c @ 14:c398d834d64a

fixed the rendering bugs
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Jun 2014 10:33:24 +0300
parents 2070a81127f2
children b755fb002f17
rev   line source
nuclear@12 1 #include <stdlib.h>
nuclear@12 2 #include "ggen.h"
nuclear@12 3 #include "mesh.h"
nuclear@12 4
nuclear@12 5 #define X1 65536
nuclear@12 6 #define VERTEX(x, y, z) \
nuclear@12 7 do { \
nuclear@12 8 *vptr++ = x; \
nuclear@12 9 *vptr++ = y; \
nuclear@12 10 *vptr++ = z; \
nuclear@12 11 } while(0)
nuclear@12 12
nuclear@13 13 #define COLOR(r, g, b) \
nuclear@13 14 do { \
nuclear@13 15 *cptr++ = r; \
nuclear@13 16 *cptr++ = g; \
nuclear@13 17 *cptr++ = b; \
nuclear@13 18 } while(0)
nuclear@13 19
nuclear@12 20 int gen_box(struct mesh *m)
nuclear@12 21 {
nuclear@13 22 int i;
nuclear@14 23 int num_faces = 12;
nuclear@14 24 int num_verts = num_faces * 3;
nuclear@12 25 int32_t v[8][3] = {
nuclear@12 26 {-X1, -X1, -X1}, {X1, -X1, -X1}, {X1, -X1, X1}, {-X1, -X1, X1},
nuclear@12 27 {-X1, X1, -X1}, {X1, X1, -X1}, {X1, X1, X1}, {-X1, X1, X1}
nuclear@12 28 };
nuclear@13 29 int32_t *vptr, *cptr;
nuclear@12 30
nuclear@12 31
nuclear@14 32 m->prim = X3D_TRIANGLES;
nuclear@12 33 m->nverts = num_verts;
nuclear@13 34 if(!(m->verts = malloc(num_verts * 3 * sizeof *m->verts))) {
nuclear@12 35 return -1;
nuclear@12 36 }
nuclear@12 37 vptr = m->verts;
nuclear@12 38
nuclear@13 39 if(!(m->colors = malloc(num_verts * 3 * sizeof *m->colors))) {
nuclear@13 40 return -1;
nuclear@13 41 }
nuclear@13 42 cptr = m->colors;
nuclear@13 43
nuclear@12 44 /* -Y */
nuclear@12 45 VERTEX(v[0][0], v[0][1], v[0][2]);
nuclear@12 46 VERTEX(v[1][0], v[1][1], v[1][2]);
nuclear@12 47 VERTEX(v[2][0], v[2][1], v[2][2]);
nuclear@14 48 for(i=0; i<3; i++) COLOR(32768, 0, 32768);
nuclear@14 49
nuclear@14 50 VERTEX(v[0][0], v[0][1], v[0][2]);
nuclear@14 51 VERTEX(v[2][0], v[2][1], v[2][2]);
nuclear@12 52 VERTEX(v[3][0], v[3][1], v[3][2]);
nuclear@14 53 for(i=0; i<3; i++) COLOR(65536, 0, 65536);
nuclear@13 54
nuclear@12 55 /* +Y */
nuclear@14 56 VERTEX(v[7][0], v[7][1], v[7][2]);
nuclear@14 57 VERTEX(v[6][0], v[6][1], v[6][2]);
nuclear@14 58 VERTEX(v[5][0], v[5][1], v[5][2]);
nuclear@14 59 for(i=0; i<3; i++) COLOR(0, 32768, 0);
nuclear@14 60
nuclear@14 61 VERTEX(v[7][0], v[7][1], v[7][2]);
nuclear@14 62 VERTEX(v[5][0], v[5][1], v[5][2]);
nuclear@12 63 VERTEX(v[4][0], v[4][1], v[4][2]);
nuclear@14 64 for(i=0; i<3; i++) COLOR(0, 65536, 0);
nuclear@13 65
nuclear@12 66 /* -Z */
nuclear@12 67 VERTEX(v[0][0], v[0][1], v[0][2]);
nuclear@12 68 VERTEX(v[4][0], v[4][1], v[4][2]);
nuclear@12 69 VERTEX(v[5][0], v[5][1], v[5][2]);
nuclear@14 70 for(i=0; i<3; i++) COLOR(32768, 32768, 0);
nuclear@14 71
nuclear@14 72 VERTEX(v[0][0], v[0][1], v[0][2]);
nuclear@14 73 VERTEX(v[5][0], v[5][1], v[5][2]);
nuclear@12 74 VERTEX(v[1][0], v[1][1], v[1][2]);
nuclear@14 75 for(i=0; i<3; i++) COLOR(65536, 65536, 0);
nuclear@13 76
nuclear@12 77 /* +Z */
nuclear@12 78 VERTEX(v[2][0], v[2][1], v[2][2]);
nuclear@12 79 VERTEX(v[6][0], v[6][1], v[6][2]);
nuclear@12 80 VERTEX(v[7][0], v[7][1], v[7][2]);
nuclear@14 81 for(i=0; i<3; i++) COLOR(0, 0, 32768);
nuclear@14 82
nuclear@14 83 VERTEX(v[2][0], v[2][1], v[2][2]);
nuclear@14 84 VERTEX(v[7][0], v[7][1], v[7][2]);
nuclear@12 85 VERTEX(v[3][0], v[3][1], v[3][2]);
nuclear@14 86 for(i=0; i<3; i++) COLOR(0, 0, 65536);
nuclear@13 87
nuclear@12 88 /* +X */
nuclear@12 89 VERTEX(v[1][0], v[1][1], v[1][2]);
nuclear@12 90 VERTEX(v[5][0], v[5][1], v[5][2]);
nuclear@12 91 VERTEX(v[6][0], v[6][1], v[6][2]);
nuclear@14 92 for(i=0; i<3; i++) COLOR(32768, 0, 0);
nuclear@14 93
nuclear@14 94 VERTEX(v[1][0], v[1][1], v[1][2]);
nuclear@14 95 VERTEX(v[6][0], v[6][1], v[6][2]);
nuclear@12 96 VERTEX(v[2][0], v[2][1], v[2][2]);
nuclear@14 97 for(i=0; i<3; i++) COLOR(65536, 0, 0);
nuclear@13 98
nuclear@12 99 /* -X */
nuclear@12 100 VERTEX(v[3][0], v[3][1], v[3][2]);
nuclear@12 101 VERTEX(v[7][0], v[7][1], v[7][2]);
nuclear@12 102 VERTEX(v[4][0], v[4][1], v[4][2]);
nuclear@14 103 for(i=0; i<3; i++) COLOR(0, 32768, 32768);
nuclear@14 104
nuclear@14 105 VERTEX(v[3][0], v[3][1], v[3][2]);
nuclear@14 106 VERTEX(v[4][0], v[4][1], v[4][2]);
nuclear@12 107 VERTEX(v[0][0], v[0][1], v[0][2]);
nuclear@14 108 for(i=0; i<3; i++) COLOR(0, 65536, 65536);
nuclear@12 109
nuclear@12 110 return 0;
nuclear@12 111 }