clray

changeset 15:754faf15ba36

burp
author John Tsiombikas
date Sun, 08 Aug 2010 09:51:45 +0100
parents 29f9330cfa4b
children 9e4a28063394
files rt.cl src/clray.cc src/mesh.cc src/mesh.h src/rt.cc src/rt.h
diffstat 6 files changed, 86 insertions(+), 4 deletions(-) [+]
line diff
     1.1 --- a/rt.cl	Sat Aug 07 03:36:36 2010 +0100
     1.2 +++ b/rt.cl	Sun Aug 08 09:51:45 2010 +0100
     1.3 @@ -4,6 +4,7 @@
     1.4  	int xsz, ysz;
     1.5  	int num_faces, num_lights;
     1.6  	int max_iter;
     1.7 +	int dbg;
     1.8  };
     1.9  
    1.10  struct Vertex {
    1.11 @@ -59,17 +60,26 @@
    1.12  		global const struct Light *lights,
    1.13  		global const struct Ray *primrays,
    1.14  		global const float *xform,
    1.15 -		global const float *invtrans)
    1.16 +		global const float *invtrans,
    1.17 +		global struct Face *outfaces)
    1.18  {
    1.19  	int idx = get_global_id(0);
    1.20 +	
    1.21 +	if(idx == 0) {
    1.22 +		for(int i=0; i<rinf->num_faces; i++) {
    1.23 +			outfaces[i] = faces[i];
    1.24 +		}
    1.25 +	}
    1.26  
    1.27  	struct Ray ray = transform_ray(primrays + idx, xform, invtrans);
    1.28  
    1.29  	struct SurfPoint sp, sp0;
    1.30  	sp0.t = FLT_MAX;
    1.31  	sp0.obj = 0;
    1.32 +	
    1.33 +	int max_faces = min(rinf->num_faces, rinf->dbg);
    1.34  
    1.35 -	for(int i=0; i<rinf->num_faces; i++) {
    1.36 +	for(int i=0; i<max_faces; i++) {
    1.37  		if(intersect(ray, faces + i, &sp) && sp.t < sp0.t) {
    1.38  			sp0 = sp;
    1.39  		}
    1.40 @@ -94,7 +104,7 @@
    1.41  		entering = false;
    1.42  	}
    1.43  
    1.44 -	float4 dcol = (float4)(0, 0, 0, 0);
    1.45 +	float4 dcol = (float4)(0.07, 0.07, 0.07, 0);
    1.46  	float4 scol = (float4)(0, 0, 0, 0);
    1.47  
    1.48  	for(int i=0; i<num_lights; i++) {
     2.1 --- a/src/clray.cc	Sat Aug 07 03:36:36 2010 +0100
     2.2 +++ b/src/clray.cc	Sun Aug 08 09:51:45 2010 +0100
     2.3 @@ -26,6 +26,7 @@
     2.4  static float cam_dist = 10.0;
     2.5  
     2.6  static bool dbg_glrender = true;
     2.7 +static int dbg_dbg = 8;
     2.8  
     2.9  static Scene scn;
    2.10  
    2.11 @@ -172,6 +173,18 @@
    2.12  		glutPostRedisplay();
    2.13  		break;
    2.14  
    2.15 +	case '=':
    2.16 +		dbg_set_dbg(++dbg_dbg);
    2.17 +		need_update = true;
    2.18 +		glutPostRedisplay();
    2.19 +		break;
    2.20 +
    2.21 +	case '-':
    2.22 +		dbg_set_dbg(--dbg_dbg);
    2.23 +		need_update = true;
    2.24 +		glutPostRedisplay();
    2.25 +		break;
    2.26 +
    2.27  	default:
    2.28  		break;
    2.29  	}
     3.1 --- a/src/mesh.cc	Sat Aug 07 03:36:36 2010 +0100
     3.2 +++ b/src/mesh.cc	Sun Aug 08 09:51:45 2010 +0100
     3.3 @@ -1,6 +1,7 @@
     3.4  #include <stdio.h>
     3.5  #include <stdlib.h>
     3.6  #include <string.h>
     3.7 +#include <math.h>
     3.8  #include <errno.h>
     3.9  #include <limits.h>
    3.10  #include <string>
    3.11 @@ -115,6 +116,26 @@
    3.12  
    3.13  static map<string, int> matnames;
    3.14  
    3.15 +
    3.16 +#define FEQ(a, b)	(fabs((a) - (b)) < 1e-8)
    3.17 +bool Face::operator ==(const Face &f) const
    3.18 +{
    3.19 +	for(int i=0; i<3; i++) {
    3.20 +		for(int j=0; j<3; j++) {
    3.21 +			if(!FEQ(v[i].pos[j], f.v[i].pos[j])) {
    3.22 +				return false;
    3.23 +			}
    3.24 +			if(!FEQ(v[i].normal[j], f.v[i].normal[j])) {
    3.25 +				return false;
    3.26 +			}
    3.27 +		}
    3.28 +		if(!FEQ(normal[i], f.normal[i])) {
    3.29 +			return false;
    3.30 +		}
    3.31 +	}
    3.32 +	return true;
    3.33 +}
    3.34 +
    3.35  bool Scene::add_mesh(Mesh *m)
    3.36  {
    3.37  	// make sure triangles have material ids
     4.1 --- a/src/mesh.h	Sat Aug 07 03:36:36 2010 +0100
     4.2 +++ b/src/mesh.h	Sun Aug 08 09:51:45 2010 +0100
     4.3 @@ -15,6 +15,8 @@
     4.4  	float normal[4];
     4.5  	int matid;
     4.6  	int padding[3];
     4.7 +
     4.8 +	bool operator ==(const Face &f) const;
     4.9  };
    4.10  
    4.11  struct Material {
     5.1 --- a/src/rt.cc	Sat Aug 07 03:36:36 2010 +0100
     5.2 +++ b/src/rt.cc	Sun Aug 08 09:51:45 2010 +0100
     5.3 @@ -16,6 +16,7 @@
     5.4  	KARG_PRIM_RAYS,
     5.5  	KARG_XFORM,
     5.6  	KARG_INVTRANS_XFORM,
     5.7 +	KARG_OUTFACES,	/* DBG */
     5.8  
     5.9  	NUM_KERNEL_ARGS
    5.10  };
    5.11 @@ -24,6 +25,7 @@
    5.12  	int xsz, ysz;
    5.13  	int num_faces, num_lights;
    5.14  	int max_iter;
    5.15 +	int dbg;
    5.16  };
    5.17  
    5.18  struct Ray {
    5.19 @@ -58,6 +60,7 @@
    5.20  	rinf.num_faces = scn->get_num_faces();
    5.21  	rinf.num_lights = sizeof lightlist / sizeof *lightlist;
    5.22  	rinf.max_iter = 6;
    5.23 +	rinf.dbg = 8;
    5.24  
    5.25  	/* calculate primary rays */
    5.26  	prim_rays = new Ray[xsz * ysz];
    5.27 @@ -89,6 +92,7 @@
    5.28  	prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
    5.29  	prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
    5.30  	prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
    5.31 +	prog->set_arg_buffer(KARG_OUTFACES, ARG_WR, rinf.num_faces * sizeof(Face));
    5.32  
    5.33  	if(prog->get_num_args() < NUM_KERNEL_ARGS) {
    5.34  		return false;
    5.35 @@ -114,6 +118,20 @@
    5.36  	}
    5.37  	printf("done\n");
    5.38  
    5.39 +	/* DEBUG */
    5.40 +	CLMemBuffer *dbgbuf = prog->get_arg_buffer(KARG_OUTFACES);
    5.41 +	Face *outfaces = (Face*)map_mem_buffer(dbgbuf, MAP_RD);
    5.42 +	for(int i=0; i<rinf.num_faces; i++) {
    5.43 +		if(!(faces[i] == outfaces[i])) {
    5.44 +			fprintf(stderr, "SKATA %d\n", i);
    5.45 +			return false;
    5.46 +		}
    5.47 +		faces[i] = outfaces[i];
    5.48 +	}
    5.49 +	printf("equality test passed\n");
    5.50 +	unmap_mem_buffer(dbgbuf);
    5.51 +
    5.52 +
    5.53  	CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
    5.54  	void *fb = map_mem_buffer(mbuf, MAP_RD);
    5.55  	if(!fb) {
    5.56 @@ -126,12 +144,27 @@
    5.57  	return true;
    5.58  }
    5.59  
    5.60 +void dbg_set_dbg(int dbg)
    5.61 +{
    5.62 +	printf("setting dbg: %d\n", dbg);
    5.63 +
    5.64 +	CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_RENDER_INFO);
    5.65 +	RendInfo *rinf = (RendInfo*)map_mem_buffer(mbuf, MAP_WR);
    5.66 +	rinf->dbg = dbg;
    5.67 +	unmap_mem_buffer(mbuf);
    5.68 +}
    5.69 +
    5.70  void dbg_render_gl(Scene *scn)
    5.71  {
    5.72 +	float lpos[] = {-1, 1, 10, 0};
    5.73  	glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
    5.74  
    5.75  	glDisable(GL_TEXTURE_2D);
    5.76  	glEnable(GL_DEPTH_TEST);
    5.77 +	glEnable(GL_LIGHTING);
    5.78 +	glEnable(GL_LIGHT0);
    5.79 +	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
    5.80 +	glEnable(GL_COLOR_MATERIAL);
    5.81  
    5.82  	glMatrixMode(GL_PROJECTION);
    5.83  	glPushMatrix();
    5.84 @@ -153,7 +186,9 @@
    5.85  
    5.86  		for(int j=0; j<3; j++) {
    5.87  			float *pos = faces[i].v[j].pos;
    5.88 -			glVertex3f(pos[0], pos[1], pos[2]);
    5.89 +			float *norm = faces[i].normal;
    5.90 +			glNormal3fv(norm);
    5.91 +			glVertex3fv(pos);
    5.92  		}
    5.93  	}
    5.94  
     6.1 --- a/src/rt.h	Sat Aug 07 03:36:36 2010 +0100
     6.2 +++ b/src/rt.h	Sun Aug 08 09:51:45 2010 +0100
     6.3 @@ -8,6 +8,7 @@
     6.4  bool render();
     6.5  void set_xform(float *matrix, float *invtrans);
     6.6  
     6.7 +void dbg_set_dbg(int dbg);
     6.8  void dbg_render_gl(Scene *scn);
     6.9  
    6.10  #endif	/* RT_H_ */