gba-x3dtest

diff src/polyfill.c @ 6:73b5f2e5d18a

first triangle on screen
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 18 Jun 2014 04:13:02 +0300
parents 850be43b3135
children fb0a0d6a8b52
line diff
     1.1 --- a/src/polyfill.c	Mon Jun 16 22:01:45 2014 +0300
     1.2 +++ b/src/polyfill.c	Wed Jun 18 04:13:02 2014 +0300
     1.3 @@ -1,5 +1,6 @@
     1.4  #include "config.h"
     1.5  #include <string.h>
     1.6 +#include <assert.h>
     1.7  #include "polyfill.h"
     1.8  #include "fixed.h"
     1.9  #include "gbasys.h"
    1.10 @@ -32,29 +33,34 @@
    1.11  	lidx[1] = topidx ? topidx - 1 : num - 1;
    1.12  	ridx[1] = (topidx + 1) % num;
    1.13  
    1.14 -	if(ridx[1] < lidx[1]) {
    1.15 +	if(verts[ridx[1]].x < verts[lidx[1]].x) {
    1.16  		return;	/* backface (CCW) */
    1.17  	}
    1.18  
    1.19  	lx = rx = verts[lidx[0]].x;
    1.20  
    1.21 +	/* TODO handle ldy == 0 or rdy == 0 */
    1.22  	ldy = verts[lidx[1]].y - verts[lidx[0]].y;
    1.23  	ldxdy = x16div(verts[lidx[1]].x - lx, ldy);
    1.24  
    1.25 -	rdy = verts[ridx[1]].y - verts[ridx[1]].y;
    1.26 +	rdy = verts[ridx[1]].y - verts[ridx[0]].y;
    1.27  	rdxdy = x16div(verts[ridx[1]].x - rx, rdy);
    1.28  
    1.29  	start = topy >> 16;
    1.30  	end = boty >> 16;
    1.31  
    1.32 +	if(end >= HEIGHT) end = HEIGHT - 1;
    1.33 +
    1.34  	y = topy;
    1.35  	for(i=start; i<end; i++) {
    1.36  		unsigned short x0, x1;
    1.37  
    1.38  		x0 = lx < 0 ? 0 : (lx >> 16);
    1.39 -		x1 = rx >= WIDTH ? WIDTH - 1 : (rx >> 16);
    1.40 +		x1 = (rx >> 16) >= WIDTH ? WIDTH - 1 : (rx >> 16);
    1.41  
    1.42 -		fill_scanline(i, x0, x1, color);
    1.43 +		if(i >= 0 && x1 > x0) {
    1.44 +			fill_scanline(i, x0, x1, color);
    1.45 +		}
    1.46  
    1.47  		if(y >= verts[lidx[1]].y) {
    1.48  			lidx[0] = lidx[1];
    1.49 @@ -77,6 +83,7 @@
    1.50  
    1.51  		lx += ldxdy;
    1.52  		rx += rdxdy;
    1.53 +		y += 65536;
    1.54  	}
    1.55  }
    1.56