# HG changeset patch # User John Tsiombikas # Date 1403495044 -10800 # Node ID ecc022a212793da3e3957cd70ab7cc5c8d368b71 # Parent c47e54796b821164e1bd13716b30872f5b5f4032 more tuff diff -r c47e54796b82 -r ecc022a21279 src/game.c --- a/src/game.c Sun Jun 22 07:54:29 2014 +0300 +++ b/src/game.c Mon Jun 23 06:44:04 2014 +0300 @@ -9,11 +9,12 @@ static void draw_rect(int x, int y, int w, int h, uint16_t color); #define X16INT(x) ((x) << 16) +#define X16FLT(x) ((int32_t)((x) * 65536.0)) static const int32_t poly[] = { - X16INT(120), X16INT(20), 0, - X16INT(200), X16INT(130), 0, - X16INT(40), X16INT(100), 0 + X16FLT(0), X16FLT(1), 0, + X16FLT(1), X16FLT(-0.8), 0, + X16FLT(-1), X16FLT(-0.5), 0 }; static const short vcount = sizeof poly / sizeof *poly / 3; @@ -24,6 +25,8 @@ palman_init(); #endif + x3d_projection(45.0, (WIDTH << 16) / HEIGHT, 65536 / 2, 65536 * 500); + return 0; } @@ -37,13 +40,20 @@ clear_buffer(back_buffer, 0); x3d_load_identity(); - x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0); + /*x3d_translate(-itox16(WIDTH / 2), -itox16(HEIGHT / 2), 0); if(autorot) { x3d_rotate((msec / 64) << 16, 0, 0, 65536); } else { x3d_rotate(keyrot << 16, 0, 0, 65536); } - x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0); + 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); @@ -52,14 +62,14 @@ #else x3d_color(65536, 65536, 65536); #endif - x3d_draw_arrays(X3D_TRIANGLES, vcount); + x3d_draw(X3D_TRIANGLES, vcount); #ifdef PALMODE x3d_color_index(RGBPAL(0, 255, 0)); #else x3d_color(0, 65536, 0); #endif - x3d_draw_arrays(X3D_POINTS, vcount); + x3d_draw(X3D_POINTS, vcount); x3d_vertex_array(0, 0); draw_rect(0, 0, WIDTH, HEIGHT, RGBPAL(255, 0, 0)); diff -r c47e54796b82 -r ecc022a21279 src/ggen.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ggen.c Mon Jun 23 06:44:04 2014 +0300 @@ -0,0 +1,63 @@ +#include +#include "ggen.h" +#include "mesh.h" + +#define X1 65536 +#define VERTEX(x, y, z) \ + do { \ + *vptr++ = x; \ + *vptr++ = y; \ + *vptr++ = z; \ + } while(0) + +int gen_box(struct mesh *m) +{ + 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; + + + m->prim = X3D_QUADS; + m->nverts = num_verts; + if(!(m->verts = malloc(num_verts * sizeof *m->verts))) { + return -1; + } + vptr = m->verts; + + /* -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]); + /* +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]); + /* -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]); + /* +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]); + /* +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]); + /* -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]); + + return 0; +} diff -r c47e54796b82 -r ecc022a21279 src/ggen.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ggen.h Mon Jun 23 06:44:04 2014 +0300 @@ -0,0 +1,8 @@ +#ifndef GGEN_H_ +#define GGEN_H_ + +#include "mesh.h" + +int gen_box(struct mesh *m); + +#endif /* GGEN_H_ */ diff -r c47e54796b82 -r ecc022a21279 src/mesh.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mesh.c Mon Jun 23 06:44:04 2014 +0300 @@ -0,0 +1,22 @@ +#include +#include "mesh.h" +#include "x3d.h" + +void init_mesh(struct mesh *m) +{ + m->prim = X3D_TRIANGLES; + m->verts = 0; + m->nverts = 0; +} + +void destroy_mesh(struct mesh *m) +{ + free(m->verts); +} + +void draw_mesh(struct mesh *m) +{ + x3d_vertex_array(m->nverts, m->verts); + x3d_draw(m->prim, m->nverts); + x3d_vertex_array(0, 0); +} diff -r c47e54796b82 -r ecc022a21279 src/mesh.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mesh.h Mon Jun 23 06:44:04 2014 +0300 @@ -0,0 +1,19 @@ +#ifndef MESH_H_ +#define MESH_H_ + +#include +#include "x3d.h" + +struct mesh { + int prim; + + int32_t *verts; + int nverts; +}; + +void init_mesh(struct mesh *m); +void destroy_mesh(struct mesh *m); + +void draw_mesh(struct mesh *m); + +#endif /* MESH_H_ */ diff -r c47e54796b82 -r ecc022a21279 src/x3d.c --- a/src/x3d.c Sun Jun 22 07:54:29 2014 +0300 +++ b/src/x3d.c Mon Jun 23 06:44:04 2014 +0300 @@ -1,5 +1,6 @@ #include "config.h" #include +#include #include "x3d.h" #include "fixed.h" #include "sincos.h" @@ -20,6 +21,7 @@ static int32_t 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; #define ID_INIT {65536, 0, 0, 0, 0, 65536, 0, 0, 0, 0, 65536, 0} @@ -36,12 +38,15 @@ static int32_t im_color[3]; static uint8_t im_color_index; -void x3d_projection(int32_t fov, int32_t aspect, int32_t nearz, int32_t farz) +void x3d_projection(int fov, int32_t aspect, int32_t nearz, int32_t farz) { - proj_fov = fov; + proj_fov = (M_PI_X16 * fov) / 180; proj_aspect = 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); } int x3d_push_matrix(void) @@ -152,7 +157,7 @@ color_count = count; } -int x3d_draw_arrays(int prim, int vnum) +int x3d_draw(int prim, int vnum) { int i, j, pverts = prim; const int32_t *vptr = vertex_array; @@ -182,6 +187,11 @@ for(j=0; j