rayzor

changeset 3:9035507275d6

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Apr 2014 20:53:44 +0300
parents c273c6f799a4
children 18bdbcbaee81
files Makefile src/m3dimpl.h src/m3drast.c src/min3d.c
diffstat 4 files changed, 100 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Sat Apr 05 18:31:00 2014 +0300
     1.2 +++ b/Makefile	Sat Apr 05 20:53:44 2014 +0300
     1.3 @@ -1,4 +1,4 @@
     1.4 -modelobj = main.obj min3d.obj logger.obj
     1.5 +modelobj = main.obj min3d.obj m3drast.obj logger.obj
     1.6  rendobj = rend.obj vmath.obj
     1.7  scnobj = scene.obj object.obj
     1.8  sysobj = gfx.obj vbe.obj dpmi.obj timer.obj mouse.obj keyb.obj
     2.1 --- a/src/m3dimpl.h	Sat Apr 05 18:31:00 2014 +0300
     2.2 +++ b/src/m3dimpl.h	Sat Apr 05 20:53:44 2014 +0300
     2.3 @@ -25,6 +25,12 @@
     2.4  
     2.5  	int mmode;	/* matrix mode */
     2.6  	struct min3d_mstack mstack[2];
     2.7 -} *m3dctx;
     2.8 +};
     2.9 +
    2.10 +extern struct min3d_context *m3dctx;
    2.11 +
    2.12 +void draw_point(struct min3d_vertex *v);
    2.13 +void draw_line(struct min3d_vertex *v);
    2.14 +void draw_poly(struct min3d_vertex *v, int numv);
    2.15  
    2.16  #endif	/* M3DIMPL_H_ */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/m3drast.c	Sat Apr 05 20:53:44 2014 +0300
     3.3 @@ -0,0 +1,14 @@
     3.4 +#include "m3dimpl.h"
     3.5 +
     3.6 +
     3.7 +void draw_point(struct min3d_vertex *v)
     3.8 +{
     3.9 +}
    3.10 +
    3.11 +void draw_line(struct min3d_vertex *v)
    3.12 +{
    3.13 +}
    3.14 +
    3.15 +void draw_poly(struct min3d_vertex *v, int numv)
    3.16 +{
    3.17 +}
     4.1 --- a/src/min3d.c	Sat Apr 05 18:31:00 2014 +0300
     4.2 +++ b/src/min3d.c	Sat Apr 05 20:53:44 2014 +0300
     4.3 @@ -6,6 +6,8 @@
     4.4  #define M_PI	3.141592653
     4.5  #endif
     4.6  
     4.7 +struct min3d_context *m3dctx;
     4.8 +
     4.9  int m3d_init(void)
    4.10  {
    4.11  	if(!(m3dctx = malloc(sizeof *m3dctx))) {
    4.12 @@ -178,15 +180,88 @@
    4.13  	vec[3] = w;
    4.14  }
    4.15  
    4.16 -static void proc_vertex(struct min3d_vertex *v)
    4.17 +static int proc_prim(int prim, struct min3d_vertex *res, struct min3d_vertex *v)
    4.18  {
    4.19 -	struct min3d_vertex *tv;
    4.20 +	int i;
    4.21 +	int vcount = prim;
    4.22 +	int mvtop, ptop;
    4.23 +	float *mvmat, *pmat;
    4.24 +
    4.25 +	mvtop = m3dctx->mstack[M3D_MODELVIEW].top;
    4.26 +	mvmat = m3dctx->mstack[M3D_MODELVIEW].m[mvtop];
    4.27 +	ptop = m3dctx->mstack[M3D_PROJECTION].top;
    4.28 +	pmat = m3dctx->mstack[M3D_PROJECTION].m[ptop];
    4.29 +
    4.30 +	/* transform to view space */
    4.31 +	for(i=0; i<vcount; i++) {
    4.32 +		res[i] = v[i];
    4.33 +		xform4(mvmat, res[i].pos);
    4.34 +		/* TODO: normal */
    4.35 +	}
    4.36 +
    4.37 +	/* TODO: lighting */
    4.38 +
    4.39 +	/* project */
    4.40 +	for(i=0; i<vcount; i++) {
    4.41 +		xform4(pmat, res[i].pos);
    4.42 +	}
    4.43 +
    4.44 +	/* clip */
    4.45 +	switch(prim) {
    4.46 +	case M3D_POINTS:
    4.47 +		{
    4.48 +			float w = res[0].pos[3];
    4.49 +			if(res[0].pos[2] < -w || res[0].pos[2] >= w ||
    4.50 +					res[0].pos[0] / w < -1 || res[0].pos[0] / w >= 1 ||
    4.51 +					res[0].pos[1] / w < -1 || res[0].pos[1] / w >= 1) {
    4.52 +				vcount = 0;
    4.53 +			}
    4.54 +		}
    4.55 +		break;
    4.56 +
    4.57 +	default:
    4.58 +		break;	/* TODO */
    4.59 +	}
    4.60 +
    4.61 +	/* perspective division */
    4.62 +	for(i=0; i<vcount; i++) {
    4.63 +		res[i].pos[0] = res[i].pos[3];
    4.64 +		res[i].pos[1] = res[i].pos[3];
    4.65 +		res[i].pos[2] = res[i].pos[3];
    4.66 +	}
    4.67 +	return vcount;
    4.68  }
    4.69  
    4.70  /* drawing */
    4.71  void m3d_draw(int prim, const float *varr, int vcount)
    4.72  {
    4.73 -	/* TODO */
    4.74 +	int i;
    4.75 +	struct min3d_vertex v[4];
    4.76 +	struct min3d_vertex resv[16];
    4.77 +
    4.78 +	for(i=0; i<vcount; i++) {
    4.79 +		int idx = i % prim;
    4.80 +
    4.81 +		v[idx].pos[0] = *varr++;
    4.82 +		v[idx].pos[1] = *varr++;
    4.83 +		v[idx].pos[2] = *varr++;
    4.84 +
    4.85 +		if(idx == prim - 1) {
    4.86 +			int resnum = proc_prim(prim, resv, v);
    4.87 +			switch(resnum) {
    4.88 +			case 1:
    4.89 +				draw_point(resv);
    4.90 +				break;
    4.91 +
    4.92 +			case '2':
    4.93 +				draw_line(resv);
    4.94 +				break;
    4.95 +
    4.96 +			default:
    4.97 +				draw_poly(resv, resnum);
    4.98 +			}
    4.99 +		}
   4.100 +	}
   4.101  }
   4.102  
   4.103  void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount)