clray

changeset 21:bd6c2b25f6e7

fixed, now we need to start with optimizations
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 10 Aug 2010 07:24:18 +0100
parents 63a6b46f58a0
children 6c44e4b1726d
files rt.cl src/clray.cc src/mesh.cc src/rt.cc
diffstat 4 files changed, 38 insertions(+), 19 deletions(-) [+]
line diff
     1.1 --- a/rt.cl	Mon Aug 09 12:55:40 2010 +0100
     1.2 +++ b/rt.cl	Tue Aug 10 07:24:18 2010 +0100
     1.3 @@ -53,7 +53,7 @@
     1.4  };
     1.5  
     1.6  #define MIN_ENERGY	0.001
     1.7 -#define EPSILON		1e-6
     1.8 +#define EPSILON		1e-5
     1.9  
    1.10  float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp);
    1.11  bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *sp);
    1.12 @@ -204,7 +204,7 @@
    1.13  
    1.14  	sp->t = t;
    1.15  	sp->pos = pt;
    1.16 -	sp->norm = -normalize(face->v[0].normal * bc.x + face->v[1].normal * bc.y + face->v[2].normal * bc.z);
    1.17 +	sp->norm = normalize(face->v[0].normal * bc.x + face->v[1].normal * bc.y + face->v[2].normal * bc.z);
    1.18  	sp->obj = face;
    1.19  	sp->dbg = bc;
    1.20  	return true;
     2.1 --- a/src/clray.cc	Mon Aug 09 12:55:40 2010 +0100
     2.2 +++ b/src/clray.cc	Tue Aug 10 07:24:18 2010 +0100
     2.3 @@ -167,7 +167,9 @@
     2.4  	case 'd':
     2.5  		dbg_glrender = !dbg_glrender;
     2.6  		if(dbg_glrender) {
     2.7 -			printf("DEBUG GL RENDER\n");
     2.8 +			printf("Debug OpenGL rendering\n");
     2.9 +		} else {
    2.10 +			printf("Raytracing\n");
    2.11  		}
    2.12  		glutPostRedisplay();
    2.13  		break;
     3.1 --- a/src/mesh.cc	Mon Aug 09 12:55:40 2010 +0100
     3.2 +++ b/src/mesh.cc	Tue Aug 10 07:24:18 2010 +0100
     3.3 @@ -165,7 +165,6 @@
     3.4  	for(size_t i=0; i<meshes.size(); i++) {
     3.5  		num_faces += meshes[i]->faces.size();
     3.6  	}
     3.7 -	printf("get_num_faces() = %d\n", num_faces);
     3.8  	return num_faces;
     3.9  }
    3.10  
     4.1 --- a/src/rt.cc	Mon Aug 09 12:55:40 2010 +0100
     4.2 +++ b/src/rt.cc	Tue Aug 10 07:24:18 2010 +0100
     4.3 @@ -115,7 +115,7 @@
     4.4  
     4.5  bool render()
     4.6  {
     4.7 -	printf("Running kernel...");
     4.8 +	printf("Running kernel... ");
     4.9  	fflush(stdout);
    4.10  	if(!prog->run(1, global_size)) {
    4.11  		return false;
    4.12 @@ -135,17 +135,35 @@
    4.13  	return true;
    4.14  }
    4.15  
    4.16 +static void dbg_set_gl_material(Material *mat)
    4.17 +{
    4.18 +	static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
    4.19 +
    4.20 +	if(!mat) mat = &def_mat;
    4.21 +
    4.22 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
    4.23 +	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
    4.24 +	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat->spow);
    4.25 +}
    4.26 +
    4.27  void dbg_render_gl(Scene *scn)
    4.28  {
    4.29 -	float lpos[] = {-1, 1, 10, 0};
    4.30  	glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
    4.31  
    4.32 +	for(int i=0; i<rinf.num_lights; i++) {
    4.33 +		float lpos[4];
    4.34 +
    4.35 +		memcpy(lpos, lightlist[i].pos, sizeof lpos);
    4.36 +		lpos[3] = 1.0;
    4.37 +
    4.38 +		glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
    4.39 +		glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
    4.40 +	}
    4.41 +
    4.42  	glDisable(GL_TEXTURE_2D);
    4.43  	glEnable(GL_DEPTH_TEST);
    4.44  	glEnable(GL_LIGHTING);
    4.45  	glEnable(GL_LIGHT0);
    4.46 -	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
    4.47 -	glEnable(GL_COLOR_MATERIAL);
    4.48  
    4.49  	glMatrixMode(GL_PROJECTION);
    4.50  	glPushMatrix();
    4.51 @@ -154,22 +172,22 @@
    4.52  
    4.53  	Material *materials = scn->get_materials();
    4.54  
    4.55 -	glBegin(GL_TRIANGLES);
    4.56  	int num_faces = scn->get_num_faces();
    4.57 +	int cur_mat = -1;
    4.58 +
    4.59  	for(int i=0; i<num_faces; i++) {
    4.60 -		Material *mat = materials ? materials + faces[i].matid : 0;
    4.61 -
    4.62 -		if(mat) {
    4.63 -			glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
    4.64 -		} else {
    4.65 -			glColor3f(1, 1, 1);
    4.66 +		if(faces[i].matid != cur_mat) {
    4.67 +			if(cur_mat != -1) {
    4.68 +				glEnd();
    4.69 +			}
    4.70 +			dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
    4.71 +			cur_mat = faces[i].matid;
    4.72 +			glBegin(GL_TRIANGLES);
    4.73  		}
    4.74  
    4.75  		for(int j=0; j<3; j++) {
    4.76 -			float *pos = faces[i].v[j].pos;
    4.77 -			float *norm = faces[i].normal;
    4.78 -			glNormal3fv(norm);
    4.79 -			glVertex3fv(pos);
    4.80 +			glNormal3fv(faces[i].v[j].normal);
    4.81 +			glVertex3fv(faces[i].v[j].pos);
    4.82  		}
    4.83  	}
    4.84  	glEnd();