gba-x3dtest

view src/x3d.c @ 7:158d23956801

started adding rendering pipeline stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 18 Jun 2014 06:20:07 +0300
parents
children fb0a0d6a8b52
line source
1 #include <string.h>
2 #include "x3d.h"
3 #include "fixed.h"
4 #include "sincos.h"
6 #define MAT_STACK_SIZE 4
8 struct matrix {
9 int32_t m[3][4];
10 };
12 static int32_t proj_fov = M_PI_X16;
13 static int32_t proj_aspect = 65536;
14 static int32_t proj_near = ftox16(0.5);
15 static int32_t proj_far = 500 << 16;
17 static struct matrix identity = {
18 {{65536, 0, 0, 0}, {0, 65536, 0, 0}, {0, 0, 65536, 0}}
19 };
21 static short mtop;
22 static struct matrix mstack[MAT_STACK_SIZE] = {
23 {{{65536, 0, 0, 0}, {0, 65536, 0, 0}, {0, 0, 65536, 0}}},
24 {{{65536, 0, 0, 0}, {0, 65536, 0, 0}, {0, 0, 65536, 0}}}
25 };
27 void x3d_projection(int32_t fov, int32_t aspect, int32_t nearz, int32_t farz)
28 {
29 proj_fov = fov;
30 proj_aspect = aspect;
31 proj_near = nearz;
32 proj_far = farz;
33 }
35 int x3d_push_matrix(void)
36 {
37 short newtop = mtop + 1;
38 if(newtop >= MAT_STACK_SIZE) {
39 return -1;
40 }
41 memcpy(mstack + newtop, mstack + mtop, sizeof *mstack);
42 mtop = newtop;
43 return 0;
44 }
46 int x3d_pop_matrix(void)
47 {
48 if(mtop <= 0) {
49 return -1;
50 }
51 --mtop;
52 return 0;
53 }
55 void x3d_load_matrix(int32_t *m)
56 {
57 memcpy(mstack[mtop].m[0], m, sizeof *mstack);
58 }
61 #define M(i,j) (((i) << 2) + (j))
62 void x3d_mult_matrix(int32_t *m)
63 {
64 int i, j;
65 struct matrix tmp;
67 memcpy(tmp.m[0], mstack[mtop].m[0], sizeof tmp);
69 for(i=0; i<3; i++) {
70 for(j=0; j<4; j++) {
71 mstack[mtop].m[i][j] = x16mul(tmp.m[0][j], m[M(i, 0)]) +
72 x16mul(tmp.m[1][j], m[M(i, 1)]) +
73 x16mul(tmp.m[2][j], m[M(i, 2)]) +
74 m[M(i, 3)];
75 }
76 }
77 }
79 void x3d_load_identity(void)
80 {
81 memcpy(mstack[mtop].m[0], identity.m[0], sizeof identity);
82 }
84 /* TODO cont... */