# HG changeset patch # User John Tsiombikas # Date 1403501308 -10800 # Node ID 2070a81127f21dcdd5ae21f0c042870f61ac81f8 # Parent ecc022a212793da3e3957cd70ab7cc5c8d368b71 foo diff -r ecc022a21279 -r 2070a81127f2 src/game.c --- a/src/game.c Mon Jun 23 06:44:04 2014 +0300 +++ b/src/game.c Mon Jun 23 08:28:28 2014 +0300 @@ -5,6 +5,7 @@ #include "sincos.h" #include "fixed.h" #include "palman.h" +#include "ggen.h" static void draw_rect(int x, int y, int w, int h, uint16_t color); @@ -12,12 +13,24 @@ #define X16FLT(x) ((int32_t)((x) * 65536.0)) static const int32_t poly[] = { - X16FLT(0), X16FLT(1), 0, - X16FLT(1), X16FLT(-0.8), 0, - X16FLT(-1), X16FLT(-0.5), 0 + X16INT(-1), X16INT(-1), 0, + X16INT(-1), X16INT(1), 0, + X16INT(1), X16INT(1), 0, + X16INT(1), X16INT(-1), 0, + X16INT(1), X16INT(-1), 0, + X16INT(1), X16INT(1), 0, + X16INT(-1), X16INT(1), 0, + X16INT(-1), X16INT(-1), 0 +}; +static const int32_t colors[] = { + 65535, 0, 0, 65535, 0, 0, 65535, 0, 0, 65535, 0, 0, + 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0 }; static const short vcount = sizeof poly / sizeof *poly / 3; +static struct mesh box; + + int game_init(void) { sincos_init(); @@ -27,9 +40,13 @@ x3d_projection(45.0, (WIDTH << 16) / HEIGHT, 65536 / 2, 65536 * 500); + init_mesh(&box); + gen_box(&box); + return 0; } +static int32_t cam_theta, cam_phi = 25 << 16; static short keyrot; static int autorot = 1; @@ -48,33 +65,33 @@ } x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);*/ - if(autorot) { - x3d_rotate((msec / 64) << 16, 0, 65536, 0); - } else { - x3d_rotate(keyrot << 16, 0, 65536, 0); - } - x3d_translate(0, 0, X16INT(2)); - - x3d_vertex_array(vcount, poly); + x3d_rotate(cam_phi, 65536, 0, 0); + x3d_rotate(keyrot << 16, 0, 65536, 0); + x3d_translate(0, 0, X16INT(6)); #ifdef PALMODE x3d_color_index(255); #else x3d_color(65536, 65536, 65536); #endif - x3d_draw(X3D_TRIANGLES, vcount); + + draw_mesh(&box); + + /* + x3d_vertex_array(vcount, poly); + x3d_color_array(vcount, colors); + x3d_draw(X3D_QUADS, vcount); + x3d_color_array(0, 0); #ifdef PALMODE x3d_color_index(RGBPAL(0, 255, 0)); #else x3d_color(0, 65536, 0); #endif + x3d_draw(X3D_POINTS, vcount); x3d_vertex_array(0, 0); - - draw_rect(0, 0, WIDTH, HEIGHT, RGBPAL(255, 0, 0)); - draw_rect(1, 1, WIDTH - 2, HEIGHT - 2, RGBPAL(0, 255, 0)); - draw_rect(2, 2, WIDTH - 4, HEIGHT - 4, RGBPAL(32, 64, 255)); + */ flip(); } diff -r ecc022a21279 -r 2070a81127f2 src/ggen.c --- a/src/ggen.c Mon Jun 23 06:44:04 2014 +0300 +++ b/src/ggen.c Mon Jun 23 08:28:28 2014 +0300 @@ -10,54 +10,78 @@ *vptr++ = z; \ } while(0) +#define COLOR(r, g, b) \ + do { \ + *cptr++ = r; \ + *cptr++ = g; \ + *cptr++ = b; \ + } while(0) + int gen_box(struct mesh *m) { + int i; int num_faces = 8; int num_verts = num_faces * 4; int32_t v[8][3] = { {-X1, -X1, -X1}, {X1, -X1, -X1}, {X1, -X1, X1}, {-X1, -X1, X1}, {-X1, X1, -X1}, {X1, X1, -X1}, {X1, X1, X1}, {-X1, X1, X1} }; - int32_t *vptr; + int32_t *vptr, *cptr; m->prim = X3D_QUADS; m->nverts = num_verts; - if(!(m->verts = malloc(num_verts * sizeof *m->verts))) { + if(!(m->verts = malloc(num_verts * 3 * sizeof *m->verts))) { return -1; } vptr = m->verts; + if(!(m->colors = malloc(num_verts * 3 * sizeof *m->colors))) { + return -1; + } + cptr = m->colors; + /* -Y */ VERTEX(v[0][0], v[0][1], v[0][2]); VERTEX(v[1][0], v[1][1], v[1][2]); VERTEX(v[2][0], v[2][1], v[2][2]); VERTEX(v[3][0], v[3][1], v[3][2]); + for(i=0; i<4; i++) COLOR(65536, 0, 65536); + /* +Y */ VERTEX(v[4][0], v[4][1], v[4][2]); VERTEX(v[5][0], v[5][1], v[5][2]); VERTEX(v[6][0], v[6][1], v[6][2]); VERTEX(v[7][0], v[7][1], v[7][2]); + for(i=0; i<4; i++) COLOR(0, 65536, 0); + /* -Z */ VERTEX(v[0][0], v[0][1], v[0][2]); VERTEX(v[4][0], v[4][1], v[4][2]); VERTEX(v[5][0], v[5][1], v[5][2]); VERTEX(v[1][0], v[1][1], v[1][2]); + for(i=0; i<4; i++) COLOR(65536, 65536, 0); + /* +Z */ VERTEX(v[2][0], v[2][1], v[2][2]); VERTEX(v[6][0], v[6][1], v[6][2]); VERTEX(v[7][0], v[7][1], v[7][2]); VERTEX(v[3][0], v[3][1], v[3][2]); + for(i=0; i<4; i++) COLOR(0, 0, 65536); + /* +X */ VERTEX(v[1][0], v[1][1], v[1][2]); VERTEX(v[5][0], v[5][1], v[5][2]); VERTEX(v[6][0], v[6][1], v[6][2]); VERTEX(v[2][0], v[2][1], v[2][2]); + for(i=0; i<4; i++) COLOR(65536, 0, 0); + /* -X */ VERTEX(v[3][0], v[3][1], v[3][2]); VERTEX(v[7][0], v[7][1], v[7][2]); VERTEX(v[4][0], v[4][1], v[4][2]); VERTEX(v[0][0], v[0][1], v[0][2]); + for(i=0; i<4; i++) COLOR(0, 65536, 65536); return 0; } diff -r ecc022a21279 -r 2070a81127f2 src/main_sdl.c --- a/src/main_sdl.c Mon Jun 23 06:44:04 2014 +0300 +++ b/src/main_sdl.c Mon Jun 23 08:28:28 2014 +0300 @@ -85,6 +85,11 @@ for(i=0; i> 3) << 11) | ((green >> 2) << 5) | (red >> 3); for(k=0; kprim = X3D_TRIANGLES; m->verts = 0; + m->colors = 0; m->nverts = 0; } void destroy_mesh(struct mesh *m) { free(m->verts); + free(m->colors); } void draw_mesh(struct mesh *m) { x3d_vertex_array(m->nverts, m->verts); + if(m->colors) { + x3d_color_array(m->nverts, m->colors); + } + x3d_draw(m->prim, m->nverts); + x3d_vertex_array(0, 0); + x3d_color_array(0, 0); } diff -r ecc022a21279 -r 2070a81127f2 src/mesh.h --- a/src/mesh.h Mon Jun 23 06:44:04 2014 +0300 +++ b/src/mesh.h Mon Jun 23 08:28:28 2014 +0300 @@ -8,6 +8,7 @@ int prim; int32_t *verts; + int32_t *colors; int nverts; }; diff -r ecc022a21279 -r 2070a81127f2 src/x3d.c --- a/src/x3d.c Mon Jun 23 06:44:04 2014 +0300 +++ b/src/x3d.c Mon Jun 23 08:28:28 2014 +0300 @@ -19,9 +19,10 @@ static int32_t proj_fov = M_PI_X16; static int32_t proj_aspect = 65536; +static int32_t inv_proj_aspect = 65536; static int32_t proj_near = ftox16(0.5); static int32_t proj_far = 500 << 16; -static int32_t tan_half_xfov, tan_half_yfov; +static int32_t inv_tan_half_xfov, inv_tan_half_yfov; #define ID_INIT {65536, 0, 0, 0, 0, 65536, 0, 0, 0, 0, 65536, 0} @@ -42,11 +43,12 @@ { proj_fov = (M_PI_X16 * fov) / 180; proj_aspect = aspect; + inv_proj_aspect = x16div(65536, proj_aspect); proj_near = nearz; proj_far = farz; - tan_half_yfov = (int32_t)(tan(0.5 * proj_fov / 65536.0) * 65536.0); - tan_half_xfov = x16mul(tan_half_yfov, aspect); + inv_tan_half_yfov = (int32_t)(65536.0 / tan(0.5 * proj_fov / 65536.0)); + inv_tan_half_xfov = x16mul(inv_tan_half_yfov, aspect); } int x3d_push_matrix(void) @@ -214,11 +216,11 @@ for(j=0; j