gba-trycatch
changeset 7:158d23956801
started adding rendering pipeline stuff
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 18 Jun 2014 06:20:07 +0300 |
parents | 73b5f2e5d18a |
children | fb0a0d6a8b52 |
files | src/fixed.h src/x3d.c src/x3d.h |
diffstat | 3 files changed, 118 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- a/src/fixed.h Wed Jun 18 04:13:02 2014 +0300 1.2 +++ b/src/fixed.h Wed Jun 18 06:20:07 2014 +0300 1.3 @@ -3,6 +3,12 @@ 1.4 1.5 #include <stdint.h> 1.6 1.7 +#define ftox16(x) (int32_t)((x) * 65536.0f) 1.8 +#define itox16(x) (int32_t)((x) << 16) 1.9 + 1.10 +#define x16tof(x) (float)((x) / 65536.0f) 1.11 +#define x16toi(x) ((x) >> 16) 1.12 + 1.13 #define x16mul(a, b) (int32_t)(((int32_t)(a) >> 8) * ((int32_t)(b) >> 8)) 1.14 /*int32_t x16div(register int32_t num, register int32_t denom);*/ 1.15
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/x3d.c Wed Jun 18 06:20:07 2014 +0300 2.3 @@ -0,0 +1,84 @@ 2.4 +#include <string.h> 2.5 +#include "x3d.h" 2.6 +#include "fixed.h" 2.7 +#include "sincos.h" 2.8 + 2.9 +#define MAT_STACK_SIZE 4 2.10 + 2.11 +struct matrix { 2.12 + int32_t m[3][4]; 2.13 +}; 2.14 + 2.15 +static int32_t proj_fov = M_PI_X16; 2.16 +static int32_t proj_aspect = 65536; 2.17 +static int32_t proj_near = ftox16(0.5); 2.18 +static int32_t proj_far = 500 << 16; 2.19 + 2.20 +static struct matrix identity = { 2.21 + {{65536, 0, 0, 0}, {0, 65536, 0, 0}, {0, 0, 65536, 0}} 2.22 +}; 2.23 + 2.24 +static short mtop; 2.25 +static struct matrix mstack[MAT_STACK_SIZE] = { 2.26 + {{{65536, 0, 0, 0}, {0, 65536, 0, 0}, {0, 0, 65536, 0}}}, 2.27 + {{{65536, 0, 0, 0}, {0, 65536, 0, 0}, {0, 0, 65536, 0}}} 2.28 +}; 2.29 + 2.30 +void x3d_projection(int32_t fov, int32_t aspect, int32_t nearz, int32_t farz) 2.31 +{ 2.32 + proj_fov = fov; 2.33 + proj_aspect = aspect; 2.34 + proj_near = nearz; 2.35 + proj_far = farz; 2.36 +} 2.37 + 2.38 +int x3d_push_matrix(void) 2.39 +{ 2.40 + short newtop = mtop + 1; 2.41 + if(newtop >= MAT_STACK_SIZE) { 2.42 + return -1; 2.43 + } 2.44 + memcpy(mstack + newtop, mstack + mtop, sizeof *mstack); 2.45 + mtop = newtop; 2.46 + return 0; 2.47 +} 2.48 + 2.49 +int x3d_pop_matrix(void) 2.50 +{ 2.51 + if(mtop <= 0) { 2.52 + return -1; 2.53 + } 2.54 + --mtop; 2.55 + return 0; 2.56 +} 2.57 + 2.58 +void x3d_load_matrix(int32_t *m) 2.59 +{ 2.60 + memcpy(mstack[mtop].m[0], m, sizeof *mstack); 2.61 +} 2.62 + 2.63 + 2.64 +#define M(i,j) (((i) << 2) + (j)) 2.65 +void x3d_mult_matrix(int32_t *m) 2.66 +{ 2.67 + int i, j; 2.68 + struct matrix tmp; 2.69 + 2.70 + memcpy(tmp.m[0], mstack[mtop].m[0], sizeof tmp); 2.71 + 2.72 + for(i=0; i<3; i++) { 2.73 + for(j=0; j<4; j++) { 2.74 + mstack[mtop].m[i][j] = x16mul(tmp.m[0][j], m[M(i, 0)]) + 2.75 + x16mul(tmp.m[1][j], m[M(i, 1)]) + 2.76 + x16mul(tmp.m[2][j], m[M(i, 2)]) + 2.77 + m[M(i, 3)]; 2.78 + } 2.79 + } 2.80 +} 2.81 + 2.82 +void x3d_load_identity(void) 2.83 +{ 2.84 + memcpy(mstack[mtop].m[0], identity.m[0], sizeof identity); 2.85 +} 2.86 + 2.87 +/* TODO cont... */
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/x3d.h Wed Jun 18 06:20:07 2014 +0300 3.3 @@ -0,0 +1,28 @@ 3.4 +#ifndef X3D_H_ 3.5 +#define X3D_H_ 3.6 + 3.7 +#include <stdint.h> 3.8 + 3.9 +enum { 3.10 + X3D_POINTS = 1, 3.11 + X3D_LINES = 2, 3.12 + X3D_TRIANGLES = 3, 3.13 + X3D_QUADS = 4 3.14 +}; 3.15 + 3.16 +void x3d_projection(int32_t fov, int32_t aspect, int32_t nearz, int32_t farz); 3.17 + 3.18 +int x3d_push_matrix(void); 3.19 +int x3d_pop_matrix(void); 3.20 +void x3d_load_matrix(int32_t *m); 3.21 +void x3d_mult_matrix(int32_t *m); 3.22 +void x3d_load_identity(void); 3.23 +void x3d_translate(int32_t x, int32_t y, int32_t z); 3.24 +void x3d_rotate(int32_t angle, int32_t x, int32_t y, int32_t z); 3.25 +void x3d_scale(int32_t x, int32_t y, int32_t z); 3.26 + 3.27 +void x3d_vertex_array(int count, int32_t *ptr); 3.28 +void x3d_color_array(int count, int32_t *ptr); 3.29 +int x3d_draw_arrays(int prim); 3.30 + 3.31 +#endif /* X3D_H_ */