# HG changeset patch # User John Tsiombikas # Date 1403061607 -10800 # Node ID 158d23956801a53e30137b03a586aa7e27711acd # Parent 73b5f2e5d18a0a8d3550efedc871c076ab90def1 started adding rendering pipeline stuff diff -r 73b5f2e5d18a -r 158d23956801 src/fixed.h --- a/src/fixed.h Wed Jun 18 04:13:02 2014 +0300 +++ b/src/fixed.h Wed Jun 18 06:20:07 2014 +0300 @@ -3,6 +3,12 @@ #include +#define ftox16(x) (int32_t)((x) * 65536.0f) +#define itox16(x) (int32_t)((x) << 16) + +#define x16tof(x) (float)((x) / 65536.0f) +#define x16toi(x) ((x) >> 16) + #define x16mul(a, b) (int32_t)(((int32_t)(a) >> 8) * ((int32_t)(b) >> 8)) /*int32_t x16div(register int32_t num, register int32_t denom);*/ diff -r 73b5f2e5d18a -r 158d23956801 src/x3d.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/x3d.c Wed Jun 18 06:20:07 2014 +0300 @@ -0,0 +1,84 @@ +#include +#include "x3d.h" +#include "fixed.h" +#include "sincos.h" + +#define MAT_STACK_SIZE 4 + +struct matrix { + int32_t m[3][4]; +}; + +static int32_t proj_fov = M_PI_X16; +static int32_t proj_aspect = 65536; +static int32_t proj_near = ftox16(0.5); +static int32_t proj_far = 500 << 16; + +static struct matrix identity = { + {{65536, 0, 0, 0}, {0, 65536, 0, 0}, {0, 0, 65536, 0}} +}; + +static short mtop; +static struct matrix mstack[MAT_STACK_SIZE] = { + {{{65536, 0, 0, 0}, {0, 65536, 0, 0}, {0, 0, 65536, 0}}}, + {{{65536, 0, 0, 0}, {0, 65536, 0, 0}, {0, 0, 65536, 0}}} +}; + +void x3d_projection(int32_t fov, int32_t aspect, int32_t nearz, int32_t farz) +{ + proj_fov = fov; + proj_aspect = aspect; + proj_near = nearz; + proj_far = farz; +} + +int x3d_push_matrix(void) +{ + short newtop = mtop + 1; + if(newtop >= MAT_STACK_SIZE) { + return -1; + } + memcpy(mstack + newtop, mstack + mtop, sizeof *mstack); + mtop = newtop; + return 0; +} + +int x3d_pop_matrix(void) +{ + if(mtop <= 0) { + return -1; + } + --mtop; + return 0; +} + +void x3d_load_matrix(int32_t *m) +{ + memcpy(mstack[mtop].m[0], m, sizeof *mstack); +} + + +#define M(i,j) (((i) << 2) + (j)) +void x3d_mult_matrix(int32_t *m) +{ + int i, j; + struct matrix tmp; + + memcpy(tmp.m[0], mstack[mtop].m[0], sizeof tmp); + + for(i=0; i<3; i++) { + for(j=0; j<4; j++) { + mstack[mtop].m[i][j] = x16mul(tmp.m[0][j], m[M(i, 0)]) + + x16mul(tmp.m[1][j], m[M(i, 1)]) + + x16mul(tmp.m[2][j], m[M(i, 2)]) + + m[M(i, 3)]; + } + } +} + +void x3d_load_identity(void) +{ + memcpy(mstack[mtop].m[0], identity.m[0], sizeof identity); +} + +/* TODO cont... */ diff -r 73b5f2e5d18a -r 158d23956801 src/x3d.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/x3d.h Wed Jun 18 06:20:07 2014 +0300 @@ -0,0 +1,28 @@ +#ifndef X3D_H_ +#define X3D_H_ + +#include + +enum { + X3D_POINTS = 1, + X3D_LINES = 2, + X3D_TRIANGLES = 3, + X3D_QUADS = 4 +}; + +void x3d_projection(int32_t fov, int32_t aspect, int32_t nearz, int32_t farz); + +int x3d_push_matrix(void); +int x3d_pop_matrix(void); +void x3d_load_matrix(int32_t *m); +void x3d_mult_matrix(int32_t *m); +void x3d_load_identity(void); +void x3d_translate(int32_t x, int32_t y, int32_t z); +void x3d_rotate(int32_t angle, int32_t x, int32_t y, int32_t z); +void x3d_scale(int32_t x, int32_t y, int32_t z); + +void x3d_vertex_array(int count, int32_t *ptr); +void x3d_color_array(int count, int32_t *ptr); +int x3d_draw_arrays(int prim); + +#endif /* X3D_H_ */