gba-x3dtest

diff src/x3d.c @ 12:ecc022a21279

more tuff
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Jun 2014 06:44:04 +0300
parents b0ed38f13261
children 2070a81127f2
line diff
     1.1 --- a/src/x3d.c	Sun Jun 22 07:54:29 2014 +0300
     1.2 +++ b/src/x3d.c	Mon Jun 23 06:44:04 2014 +0300
     1.3 @@ -1,5 +1,6 @@
     1.4  #include "config.h"
     1.5  #include <string.h>
     1.6 +#include <math.h>
     1.7  #include "x3d.h"
     1.8  #include "fixed.h"
     1.9  #include "sincos.h"
    1.10 @@ -20,6 +21,7 @@
    1.11  static int32_t proj_aspect = 65536;
    1.12  static int32_t proj_near = ftox16(0.5);
    1.13  static int32_t proj_far = 500 << 16;
    1.14 +static int32_t tan_half_xfov, tan_half_yfov;
    1.15  
    1.16  #define ID_INIT {65536, 0, 0, 0, 0, 65536, 0, 0, 0, 0, 65536, 0}
    1.17  
    1.18 @@ -36,12 +38,15 @@
    1.19  static int32_t im_color[3];
    1.20  static uint8_t im_color_index;
    1.21  
    1.22 -void x3d_projection(int32_t fov, int32_t aspect, int32_t nearz, int32_t farz)
    1.23 +void x3d_projection(int fov, int32_t aspect, int32_t nearz, int32_t farz)
    1.24  {
    1.25 -	proj_fov = fov;
    1.26 +	proj_fov = (M_PI_X16 * fov) / 180;
    1.27  	proj_aspect = aspect;
    1.28  	proj_near = nearz;
    1.29  	proj_far = farz;
    1.30 +
    1.31 +	tan_half_yfov = (int32_t)(tan(0.5 * proj_fov / 65536.0) * 65536.0);
    1.32 +	tan_half_xfov = x16mul(tan_half_yfov, aspect);
    1.33  }
    1.34  
    1.35  int x3d_push_matrix(void)
    1.36 @@ -152,7 +157,7 @@
    1.37  	color_count = count;
    1.38  }
    1.39  
    1.40 -int x3d_draw_arrays(int prim, int vnum)
    1.41 +int x3d_draw(int prim, int vnum)
    1.42  {
    1.43  	int i, j, pverts = prim;
    1.44  	const int32_t *vptr = vertex_array;
    1.45 @@ -182,6 +187,11 @@
    1.46  
    1.47  		for(j=0; j<pverts; j++) {
    1.48  			proc_vertex(vptr, cptr, vpos + j, col + j);
    1.49 +
    1.50 +			if(vpos[j].z <= proj_near) {
    1.51 +				goto skip_prim;
    1.52 +			}
    1.53 +
    1.54  			vptr += 3;
    1.55  			if(cptr) cptr += 3;
    1.56  		}
    1.57 @@ -200,6 +210,19 @@
    1.58  		color = RGB(cr, cg, cb);
    1.59  #endif
    1.60  
    1.61 +		/* project & viewport */
    1.62 +		for(j=0; j<pverts; j++) {
    1.63 +			int32_t x, y;
    1.64 +
    1.65 +			x = x16mul(vpos[j].x, tan_half_xfov);
    1.66 +			x = x16div(x, vpos[j].z);
    1.67 +			vpos[j].x = (x + 65536) * (WIDTH / 2);
    1.68 +
    1.69 +			y = x16mul(vpos[j].y, tan_half_yfov);
    1.70 +			y = x16div(y, vpos[j].z);
    1.71 +			vpos[j].y = (65536 - y) * (HEIGHT / 2);
    1.72 +		}
    1.73 +
    1.74  		switch(pverts) {
    1.75  		case X3D_POINTS:
    1.76  			draw_point(vpos, color);
    1.77 @@ -213,6 +236,7 @@
    1.78  			draw_poly(pverts, vpos, color);
    1.79  			break;
    1.80  		}
    1.81 +skip_prim: ;
    1.82  	}
    1.83  	return 0;
    1.84  }