gba-x3dtest

changeset 13:2070a81127f2

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Jun 2014 08:28:28 +0300
parents ecc022a21279
children c398d834d64a
files src/game.c src/ggen.c src/main_sdl.c src/mesh.c src/mesh.h src/x3d.c
diffstat 6 files changed, 81 insertions(+), 24 deletions(-) [+]
line diff
     1.1 --- a/src/game.c	Mon Jun 23 06:44:04 2014 +0300
     1.2 +++ b/src/game.c	Mon Jun 23 08:28:28 2014 +0300
     1.3 @@ -5,6 +5,7 @@
     1.4  #include "sincos.h"
     1.5  #include "fixed.h"
     1.6  #include "palman.h"
     1.7 +#include "ggen.h"
     1.8  
     1.9  static void draw_rect(int x, int y, int w, int h, uint16_t color);
    1.10  
    1.11 @@ -12,12 +13,24 @@
    1.12  #define X16FLT(x)	((int32_t)((x) * 65536.0))
    1.13  
    1.14  static const int32_t poly[] = {
    1.15 -	X16FLT(0), X16FLT(1), 0,
    1.16 -	X16FLT(1), X16FLT(-0.8), 0,
    1.17 -	X16FLT(-1), X16FLT(-0.5), 0
    1.18 +	X16INT(-1), X16INT(-1), 0,
    1.19 +	X16INT(-1), X16INT(1), 0,
    1.20 +	X16INT(1), X16INT(1), 0,
    1.21 +	X16INT(1), X16INT(-1), 0,
    1.22 +	X16INT(1), X16INT(-1), 0,
    1.23 +	X16INT(1), X16INT(1), 0,
    1.24 +	X16INT(-1), X16INT(1), 0,
    1.25 +	X16INT(-1), X16INT(-1), 0
    1.26 +};
    1.27 +static const int32_t colors[] = {
    1.28 +	65535, 0, 0, 65535, 0, 0, 65535, 0, 0, 65535, 0, 0,
    1.29 +	65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0, 65536, 65535, 0
    1.30  };
    1.31  static const short vcount = sizeof poly / sizeof *poly / 3;
    1.32  
    1.33 +static struct mesh box;
    1.34 +
    1.35 +
    1.36  int game_init(void)
    1.37  {
    1.38  	sincos_init();
    1.39 @@ -27,9 +40,13 @@
    1.40  
    1.41  	x3d_projection(45.0, (WIDTH << 16) / HEIGHT, 65536 / 2, 65536 * 500);
    1.42  
    1.43 +	init_mesh(&box);
    1.44 +	gen_box(&box);
    1.45 +
    1.46  	return 0;
    1.47  }
    1.48  
    1.49 +static int32_t cam_theta, cam_phi = 25 << 16;
    1.50  static short keyrot;
    1.51  static int autorot = 1;
    1.52  
    1.53 @@ -48,33 +65,33 @@
    1.54  	}
    1.55  	x3d_translate(itox16(WIDTH / 2), itox16(HEIGHT / 2), 0);*/
    1.56  
    1.57 -	if(autorot) {
    1.58 -		x3d_rotate((msec / 64) << 16, 0, 65536, 0);
    1.59 -	} else {
    1.60 -		x3d_rotate(keyrot << 16, 0, 65536, 0);
    1.61 -	}
    1.62 -	x3d_translate(0, 0, X16INT(2));
    1.63 -
    1.64 -	x3d_vertex_array(vcount, poly);
    1.65 +	x3d_rotate(cam_phi, 65536, 0, 0);
    1.66 +	x3d_rotate(keyrot << 16, 0, 65536, 0);
    1.67 +	x3d_translate(0, 0, X16INT(6));
    1.68  
    1.69  #ifdef PALMODE
    1.70  	x3d_color_index(255);
    1.71  #else
    1.72  	x3d_color(65536, 65536, 65536);
    1.73  #endif
    1.74 -	x3d_draw(X3D_TRIANGLES, vcount);
    1.75 +
    1.76 +	draw_mesh(&box);
    1.77 +
    1.78 +	/*
    1.79 +	x3d_vertex_array(vcount, poly);
    1.80 +	x3d_color_array(vcount, colors);
    1.81 +	x3d_draw(X3D_QUADS, vcount);
    1.82 +	x3d_color_array(0, 0);
    1.83  
    1.84  #ifdef PALMODE
    1.85  	x3d_color_index(RGBPAL(0, 255, 0));
    1.86  #else
    1.87  	x3d_color(0, 65536, 0);
    1.88  #endif
    1.89 +
    1.90  	x3d_draw(X3D_POINTS, vcount);
    1.91  	x3d_vertex_array(0, 0);
    1.92 -
    1.93 -	draw_rect(0, 0, WIDTH, HEIGHT, RGBPAL(255, 0, 0));
    1.94 -	draw_rect(1, 1, WIDTH - 2, HEIGHT - 2, RGBPAL(0, 255, 0));
    1.95 -	draw_rect(2, 2, WIDTH - 4, HEIGHT - 4, RGBPAL(32, 64, 255));
    1.96 +	*/
    1.97  
    1.98  	flip();
    1.99  }
     2.1 --- a/src/ggen.c	Mon Jun 23 06:44:04 2014 +0300
     2.2 +++ b/src/ggen.c	Mon Jun 23 08:28:28 2014 +0300
     2.3 @@ -10,54 +10,78 @@
     2.4  		*vptr++ = z; \
     2.5  	} while(0)
     2.6  
     2.7 +#define COLOR(r, g, b) \
     2.8 +	do { \
     2.9 +		*cptr++ = r; \
    2.10 +		*cptr++ = g; \
    2.11 +		*cptr++ = b; \
    2.12 +	} while(0)
    2.13 +
    2.14  int gen_box(struct mesh *m)
    2.15  {
    2.16 +	int i;
    2.17  	int num_faces = 8;
    2.18  	int num_verts = num_faces * 4;
    2.19  	int32_t v[8][3] = {
    2.20  		{-X1, -X1, -X1}, {X1, -X1, -X1}, {X1, -X1, X1}, {-X1, -X1, X1},
    2.21  		{-X1, X1, -X1}, {X1, X1, -X1}, {X1, X1, X1}, {-X1, X1, X1}
    2.22  	};
    2.23 -	int32_t *vptr;
    2.24 +	int32_t *vptr, *cptr;
    2.25  
    2.26  
    2.27  	m->prim = X3D_QUADS;
    2.28  	m->nverts = num_verts;
    2.29 -	if(!(m->verts = malloc(num_verts * sizeof *m->verts))) {
    2.30 +	if(!(m->verts = malloc(num_verts * 3 * sizeof *m->verts))) {
    2.31  		return -1;
    2.32  	}
    2.33  	vptr = m->verts;
    2.34  
    2.35 +	if(!(m->colors = malloc(num_verts * 3 * sizeof *m->colors))) {
    2.36 +		return -1;
    2.37 +	}
    2.38 +	cptr = m->colors;
    2.39 +
    2.40  	/* -Y */
    2.41  	VERTEX(v[0][0], v[0][1], v[0][2]);
    2.42  	VERTEX(v[1][0], v[1][1], v[1][2]);
    2.43  	VERTEX(v[2][0], v[2][1], v[2][2]);
    2.44  	VERTEX(v[3][0], v[3][1], v[3][2]);
    2.45 +	for(i=0; i<4; i++) COLOR(65536, 0, 65536);
    2.46 +
    2.47  	/* +Y */
    2.48  	VERTEX(v[4][0], v[4][1], v[4][2]);
    2.49  	VERTEX(v[5][0], v[5][1], v[5][2]);
    2.50  	VERTEX(v[6][0], v[6][1], v[6][2]);
    2.51  	VERTEX(v[7][0], v[7][1], v[7][2]);
    2.52 +	for(i=0; i<4; i++) COLOR(0, 65536, 0);
    2.53 +
    2.54  	/* -Z */
    2.55  	VERTEX(v[0][0], v[0][1], v[0][2]);
    2.56  	VERTEX(v[4][0], v[4][1], v[4][2]);
    2.57  	VERTEX(v[5][0], v[5][1], v[5][2]);
    2.58  	VERTEX(v[1][0], v[1][1], v[1][2]);
    2.59 +	for(i=0; i<4; i++) COLOR(65536, 65536, 0);
    2.60 +
    2.61  	/* +Z */
    2.62  	VERTEX(v[2][0], v[2][1], v[2][2]);
    2.63  	VERTEX(v[6][0], v[6][1], v[6][2]);
    2.64  	VERTEX(v[7][0], v[7][1], v[7][2]);
    2.65  	VERTEX(v[3][0], v[3][1], v[3][2]);
    2.66 +	for(i=0; i<4; i++) COLOR(0, 0, 65536);
    2.67 +
    2.68  	/* +X */
    2.69  	VERTEX(v[1][0], v[1][1], v[1][2]);
    2.70  	VERTEX(v[5][0], v[5][1], v[5][2]);
    2.71  	VERTEX(v[6][0], v[6][1], v[6][2]);
    2.72  	VERTEX(v[2][0], v[2][1], v[2][2]);
    2.73 +	for(i=0; i<4; i++) COLOR(65536, 0, 0);
    2.74 +
    2.75  	/* -X */
    2.76  	VERTEX(v[3][0], v[3][1], v[3][2]);
    2.77  	VERTEX(v[7][0], v[7][1], v[7][2]);
    2.78  	VERTEX(v[4][0], v[4][1], v[4][2]);
    2.79  	VERTEX(v[0][0], v[0][1], v[0][2]);
    2.80 +	for(i=0; i<4; i++) COLOR(0, 65536, 65536);
    2.81  
    2.82  	return 0;
    2.83  }
     3.1 --- a/src/main_sdl.c	Mon Jun 23 06:44:04 2014 +0300
     3.2 +++ b/src/main_sdl.c	Mon Jun 23 08:28:28 2014 +0300
     3.3 @@ -85,6 +85,11 @@
     3.4  		for(i=0; i<HEIGHT; i++) {
     3.5  			for(j=0; j<WIDTH; j++) {
     3.6  				uint16_t pixel = *src++;
     3.7 +				unsigned int red = GET_R(pixel);
     3.8 +				unsigned int green = GET_G(pixel);
     3.9 +				unsigned int blue = GET_B(pixel);
    3.10 +
    3.11 +				pixel = ((blue >> 3) << 11) | ((green >> 2) << 5) | (red >> 3);
    3.12  
    3.13  				for(k=0; k<sdlscale; k++) {
    3.14  					for(l=0; l<sdlscale; l++) {
     4.1 --- a/src/mesh.c	Mon Jun 23 06:44:04 2014 +0300
     4.2 +++ b/src/mesh.c	Mon Jun 23 08:28:28 2014 +0300
     4.3 @@ -6,17 +6,25 @@
     4.4  {
     4.5  	m->prim = X3D_TRIANGLES;
     4.6  	m->verts = 0;
     4.7 +	m->colors = 0;
     4.8  	m->nverts = 0;
     4.9  }
    4.10  
    4.11  void destroy_mesh(struct mesh *m)
    4.12  {
    4.13  	free(m->verts);
    4.14 +	free(m->colors);
    4.15  }
    4.16  
    4.17  void draw_mesh(struct mesh *m)
    4.18  {
    4.19  	x3d_vertex_array(m->nverts, m->verts);
    4.20 +	if(m->colors) {
    4.21 +		x3d_color_array(m->nverts, m->colors);
    4.22 +	}
    4.23 +
    4.24  	x3d_draw(m->prim, m->nverts);
    4.25 +
    4.26  	x3d_vertex_array(0, 0);
    4.27 +	x3d_color_array(0, 0);
    4.28  }
     5.1 --- a/src/mesh.h	Mon Jun 23 06:44:04 2014 +0300
     5.2 +++ b/src/mesh.h	Mon Jun 23 08:28:28 2014 +0300
     5.3 @@ -8,6 +8,7 @@
     5.4  	int prim;
     5.5  
     5.6  	int32_t *verts;
     5.7 +	int32_t *colors;
     5.8  	int nverts;
     5.9  };
    5.10  
     6.1 --- a/src/x3d.c	Mon Jun 23 06:44:04 2014 +0300
     6.2 +++ b/src/x3d.c	Mon Jun 23 08:28:28 2014 +0300
     6.3 @@ -19,9 +19,10 @@
     6.4  
     6.5  static int32_t proj_fov = M_PI_X16;
     6.6  static int32_t proj_aspect = 65536;
     6.7 +static int32_t inv_proj_aspect = 65536;
     6.8  static int32_t proj_near = ftox16(0.5);
     6.9  static int32_t proj_far = 500 << 16;
    6.10 -static int32_t tan_half_xfov, tan_half_yfov;
    6.11 +static int32_t inv_tan_half_xfov, inv_tan_half_yfov;
    6.12  
    6.13  #define ID_INIT {65536, 0, 0, 0, 0, 65536, 0, 0, 0, 0, 65536, 0}
    6.14  
    6.15 @@ -42,11 +43,12 @@
    6.16  {
    6.17  	proj_fov = (M_PI_X16 * fov) / 180;
    6.18  	proj_aspect = aspect;
    6.19 +	inv_proj_aspect = x16div(65536, proj_aspect);
    6.20  	proj_near = nearz;
    6.21  	proj_far = farz;
    6.22  
    6.23 -	tan_half_yfov = (int32_t)(tan(0.5 * proj_fov / 65536.0) * 65536.0);
    6.24 -	tan_half_xfov = x16mul(tan_half_yfov, aspect);
    6.25 +	inv_tan_half_yfov = (int32_t)(65536.0 / tan(0.5 * proj_fov / 65536.0));
    6.26 +	inv_tan_half_xfov = x16mul(inv_tan_half_yfov, aspect);
    6.27  }
    6.28  
    6.29  int x3d_push_matrix(void)
    6.30 @@ -214,11 +216,11 @@
    6.31  		for(j=0; j<pverts; j++) {
    6.32  			int32_t x, y;
    6.33  
    6.34 -			x = x16mul(vpos[j].x, tan_half_xfov);
    6.35 +			x = x16mul(vpos[j].x, inv_tan_half_xfov);
    6.36  			x = x16div(x, vpos[j].z);
    6.37 -			vpos[j].x = (x + 65536) * (WIDTH / 2);
    6.38 +			vpos[j].x = (x16mul(x, inv_proj_aspect) + 65536) * (WIDTH / 2);
    6.39  
    6.40 -			y = x16mul(vpos[j].y, tan_half_yfov);
    6.41 +			y = x16mul(vpos[j].y, inv_tan_half_yfov);
    6.42  			y = x16div(y, vpos[j].z);
    6.43  			vpos[j].y = (65536 - y) * (HEIGHT / 2);
    6.44  		}