rayzor

diff src/m3drast.c @ 9:70e332156d02

moving along
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 10 Apr 2014 02:31:31 +0300
parents 5fcf72837b69
children d94a69933a71
line diff
     1.1 --- a/src/m3drast.c	Mon Apr 07 08:46:06 2014 +0300
     1.2 +++ b/src/m3drast.c	Thu Apr 10 02:31:31 2014 +0300
     1.3 @@ -1,25 +1,56 @@
     1.4  #include "m3dimpl.h"
     1.5 +#include "lines.h"
     1.6  
     1.7  
     1.8 -void draw_point(struct min3d_vertex *v)
     1.9 +void m3d_draw_point(struct min3d_vertex *v)
    1.10  {
    1.11  	int x = v->pos[0] + 0.5;
    1.12  	int y = v->pos[1] + 0.5;
    1.13  	int xsz = m3dctx->cbuf->xsz;
    1.14 -	unsigned char *ptr = m3dctx->cbuf->pixels + (y * xsz + x) * 3;
    1.15 -
    1.16 -	int r = (int)(v->color[0] * 255.0);
    1.17 -	int g = (int)(v->color[1] * 255.0);
    1.18 -	int b = (int)(v->color[2] * 255.0);
    1.19 -	ptr[0] = r > 255 ? 255 : r;
    1.20 -	ptr[1] = g > 255 ? 255 : g;
    1.21 -	ptr[2] = b > 255 ? 255 : b;
    1.22 +	m3dctx->cbuf->pixels[y * xsz + x] = v->color;
    1.23  }
    1.24  
    1.25 -void draw_line(struct min3d_vertex *v)
    1.26 +void m3d_draw_line(struct min3d_vertex *v)
    1.27  {
    1.28 +	int x0, y0, x1, y1;
    1.29 +
    1.30 +	x0 = v[0].pos[0];
    1.31 +	y0 = v[0].pos[1];
    1.32 +	x1 = v[1].pos[0];
    1.33 +	y1 = v[1].pos[1];
    1.34 +
    1.35 +	if(clip_line2d(&x0, &y0, &x1, &y1, m3dctx->vport)) {
    1.36 +		draw_line(m3dctx->cbuf->pixels, m3dctx->cbuf->xsz, v->color, x0, y0, x1, y1);
    1.37 +	}
    1.38  }
    1.39  
    1.40 -void draw_poly(struct min3d_vertex *v, int numv)
    1.41 +void m3d_draw_poly(struct min3d_vertex *v, int numv)
    1.42  {
    1.43 +	int i;
    1.44 +	struct min3d_vertex last[2];
    1.45 +
    1.46 +	if(ENABLED(M3D_CULL_FACE)) {
    1.47 +		float a[2], b[2], crossz = 0;
    1.48 +
    1.49 +		for(i=1; i<numv - 1; i++) {
    1.50 +			int n = i + 1;
    1.51 +			a[0] = v[i].pos[0] - v[0].pos[0];
    1.52 +			a[1] = v[i].pos[1] - v[0].pos[1];
    1.53 +
    1.54 +			b[0] = v[n].pos[0] - v[0].pos[0];
    1.55 +			b[1] = v[n].pos[1] - v[0].pos[1];
    1.56 +
    1.57 +			crossz += a[0] * b[1] - a[1] * b[0];
    1.58 +		}
    1.59 +
    1.60 +		if(crossz < 0) return;
    1.61 +	}
    1.62 +
    1.63 +	last[0] = v[numv - 1];
    1.64 +	last[1] = v[0];
    1.65 +
    1.66 +	for(i=0; i<numv - 1; i++) {
    1.67 +		m3d_draw_line(v++);
    1.68 +	}
    1.69 +	m3d_draw_line(last);
    1.70  }