gba-x3dtest

diff src/x3d.c @ 14:c398d834d64a

fixed the rendering bugs
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Jun 2014 10:33:24 +0300
parents 2070a81127f2
children b755fb002f17
line diff
     1.1 --- a/src/x3d.c	Mon Jun 23 08:28:28 2014 +0300
     1.2 +++ b/src/x3d.c	Mon Jun 23 10:33:24 2014 +0300
     1.3 @@ -1,4 +1,5 @@
     1.4  #include "config.h"
     1.5 +#include <stdio.h>
     1.6  #include <string.h>
     1.7  #include <math.h>
     1.8  #include "x3d.h"
     1.9 @@ -8,6 +9,8 @@
    1.10  #include "polyfill.h"
    1.11  #include "gbasys.h"
    1.12  
    1.13 +int dbg_fill_dump;
    1.14 +
    1.15  #define MAT_STACK_SIZE	4
    1.16  
    1.17  struct matrix {
    1.18 @@ -15,6 +18,7 @@
    1.19  };
    1.20  
    1.21  static void proc_vertex(const int32_t *vin, const int32_t *cin, pvec3 *vout, pvec3 *cout);
    1.22 +static int dump_frame(struct pixel_buffer *frame);
    1.23  
    1.24  
    1.25  static int32_t proj_fov = M_PI_X16;
    1.26 @@ -88,11 +92,11 @@
    1.27  	for(i=0; i<3; i++) {
    1.28  		for(j=0; j<4; j++) {
    1.29  			mstack[mtop].m[M(i, j)] =
    1.30 -				x16mul(tmp.m[M(0, j)], m[M(i, 0)]) +
    1.31 -				x16mul(tmp.m[M(1, j)], m[M(i, 1)]) +
    1.32 -				x16mul(tmp.m[M(2, j)], m[M(i, 2)]);
    1.33 +				x16mul(m[M(0, j)], tmp.m[M(i, 0)]) +
    1.34 +				x16mul(m[M(1, j)], tmp.m[M(i, 1)]) +
    1.35 +				x16mul(m[M(2, j)], tmp.m[M(i, 2)]);
    1.36  		}
    1.37 -		mstack[mtop].m[M(i, 3)] += m[M(i, 3)];
    1.38 +		mstack[mtop].m[M(i, 3)] += tmp.m[M(i, 3)];
    1.39  	}
    1.40  }
    1.41  
    1.42 @@ -236,10 +240,15 @@
    1.43  		case X3D_TRIANGLES:
    1.44  		case X3D_QUADS:
    1.45  			draw_poly(pverts, vpos, color);
    1.46 +			if(dbg_fill_dump) {
    1.47 +				dump_frame(back_buffer);
    1.48 +			}
    1.49  			break;
    1.50  		}
    1.51  skip_prim: ;
    1.52  	}
    1.53 +
    1.54 +	dbg_fill_dump = 0;
    1.55  	return 0;
    1.56  }
    1.57  
    1.58 @@ -287,3 +296,31 @@
    1.59  	im_color[1] = g;
    1.60  	im_color[2] = b;
    1.61  }
    1.62 +
    1.63 +static int dump_frame(struct pixel_buffer *frame)
    1.64 +{
    1.65 +	static int frameno;
    1.66 +	char buf[128];
    1.67 +	FILE *fp;
    1.68 +	int i, npix;
    1.69 +	uint16_t *ptr = frame->pixels;
    1.70 +
    1.71 +	sprintf(buf, "dump%03d.ppm", ++frameno);
    1.72 +
    1.73 +	if(!(fp = fopen(buf, "wb"))) {
    1.74 +		fprintf(stderr, "failed to dump file: %s\n", buf);
    1.75 +		return -1;
    1.76 +	}
    1.77 +
    1.78 +	fprintf(fp, "P6\n%d %d\n255\n", frame->x, frame->y);
    1.79 +
    1.80 +	npix = frame->x * frame->y;
    1.81 +	for(i=0; i<npix; i++) {
    1.82 +		uint16_t pixel = *ptr++;
    1.83 +		fputc(GET_R(pixel), fp);
    1.84 +		fputc(GET_G(pixel), fp);
    1.85 +		fputc(GET_B(pixel), fp);
    1.86 +	}
    1.87 +	fclose(fp);
    1.88 +	return 0;
    1.89 +}