gba-x3dtest

annotate 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
rev   line source
nuclear@7 1 #include <string.h>
nuclear@7 2 #include "x3d.h"
nuclear@7 3 #include "fixed.h"
nuclear@7 4 #include "sincos.h"
nuclear@7 5
nuclear@7 6 #define MAT_STACK_SIZE 4
nuclear@7 7
nuclear@7 8 struct matrix {
nuclear@7 9 int32_t m[3][4];
nuclear@7 10 };
nuclear@7 11
nuclear@7 12 static int32_t proj_fov = M_PI_X16;
nuclear@7 13 static int32_t proj_aspect = 65536;
nuclear@7 14 static int32_t proj_near = ftox16(0.5);
nuclear@7 15 static int32_t proj_far = 500 << 16;
nuclear@7 16
nuclear@7 17 static struct matrix identity = {
nuclear@7 18 {{65536, 0, 0, 0}, {0, 65536, 0, 0}, {0, 0, 65536, 0}}
nuclear@7 19 };
nuclear@7 20
nuclear@7 21 static short mtop;
nuclear@7 22 static struct matrix mstack[MAT_STACK_SIZE] = {
nuclear@7 23 {{{65536, 0, 0, 0}, {0, 65536, 0, 0}, {0, 0, 65536, 0}}},
nuclear@7 24 {{{65536, 0, 0, 0}, {0, 65536, 0, 0}, {0, 0, 65536, 0}}}
nuclear@7 25 };
nuclear@7 26
nuclear@7 27 void x3d_projection(int32_t fov, int32_t aspect, int32_t nearz, int32_t farz)
nuclear@7 28 {
nuclear@7 29 proj_fov = fov;
nuclear@7 30 proj_aspect = aspect;
nuclear@7 31 proj_near = nearz;
nuclear@7 32 proj_far = farz;
nuclear@7 33 }
nuclear@7 34
nuclear@7 35 int x3d_push_matrix(void)
nuclear@7 36 {
nuclear@7 37 short newtop = mtop + 1;
nuclear@7 38 if(newtop >= MAT_STACK_SIZE) {
nuclear@7 39 return -1;
nuclear@7 40 }
nuclear@7 41 memcpy(mstack + newtop, mstack + mtop, sizeof *mstack);
nuclear@7 42 mtop = newtop;
nuclear@7 43 return 0;
nuclear@7 44 }
nuclear@7 45
nuclear@7 46 int x3d_pop_matrix(void)
nuclear@7 47 {
nuclear@7 48 if(mtop <= 0) {
nuclear@7 49 return -1;
nuclear@7 50 }
nuclear@7 51 --mtop;
nuclear@7 52 return 0;
nuclear@7 53 }
nuclear@7 54
nuclear@7 55 void x3d_load_matrix(int32_t *m)
nuclear@7 56 {
nuclear@7 57 memcpy(mstack[mtop].m[0], m, sizeof *mstack);
nuclear@7 58 }
nuclear@7 59
nuclear@7 60
nuclear@7 61 #define M(i,j) (((i) << 2) + (j))
nuclear@7 62 void x3d_mult_matrix(int32_t *m)
nuclear@7 63 {
nuclear@7 64 int i, j;
nuclear@7 65 struct matrix tmp;
nuclear@7 66
nuclear@7 67 memcpy(tmp.m[0], mstack[mtop].m[0], sizeof tmp);
nuclear@7 68
nuclear@7 69 for(i=0; i<3; i++) {
nuclear@7 70 for(j=0; j<4; j++) {
nuclear@7 71 mstack[mtop].m[i][j] = x16mul(tmp.m[0][j], m[M(i, 0)]) +
nuclear@7 72 x16mul(tmp.m[1][j], m[M(i, 1)]) +
nuclear@7 73 x16mul(tmp.m[2][j], m[M(i, 2)]) +
nuclear@7 74 m[M(i, 3)];
nuclear@7 75 }
nuclear@7 76 }
nuclear@7 77 }
nuclear@7 78
nuclear@7 79 void x3d_load_identity(void)
nuclear@7 80 {
nuclear@7 81 memcpy(mstack[mtop].m[0], identity.m[0], sizeof identity);
nuclear@7 82 }
nuclear@7 83
nuclear@7 84 /* TODO cont... */