clray

diff src/rt.cc @ 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
line diff
     1.1 --- a/src/rt.cc	Mon Aug 09 12:55:40 2010 +0100
     1.2 +++ b/src/rt.cc	Tue Aug 10 07:24:18 2010 +0100
     1.3 @@ -115,7 +115,7 @@
     1.4  
     1.5  bool render()
     1.6  {
     1.7 -	printf("Running kernel...");
     1.8 +	printf("Running kernel... ");
     1.9  	fflush(stdout);
    1.10  	if(!prog->run(1, global_size)) {
    1.11  		return false;
    1.12 @@ -135,17 +135,35 @@
    1.13  	return true;
    1.14  }
    1.15  
    1.16 +static void dbg_set_gl_material(Material *mat)
    1.17 +{
    1.18 +	static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
    1.19 +
    1.20 +	if(!mat) mat = &def_mat;
    1.21 +
    1.22 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
    1.23 +	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
    1.24 +	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat->spow);
    1.25 +}
    1.26 +
    1.27  void dbg_render_gl(Scene *scn)
    1.28  {
    1.29 -	float lpos[] = {-1, 1, 10, 0};
    1.30  	glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
    1.31  
    1.32 +	for(int i=0; i<rinf.num_lights; i++) {
    1.33 +		float lpos[4];
    1.34 +
    1.35 +		memcpy(lpos, lightlist[i].pos, sizeof lpos);
    1.36 +		lpos[3] = 1.0;
    1.37 +
    1.38 +		glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
    1.39 +		glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightlist[i].color);
    1.40 +	}
    1.41 +
    1.42  	glDisable(GL_TEXTURE_2D);
    1.43  	glEnable(GL_DEPTH_TEST);
    1.44  	glEnable(GL_LIGHTING);
    1.45  	glEnable(GL_LIGHT0);
    1.46 -	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
    1.47 -	glEnable(GL_COLOR_MATERIAL);
    1.48  
    1.49  	glMatrixMode(GL_PROJECTION);
    1.50  	glPushMatrix();
    1.51 @@ -154,22 +172,22 @@
    1.52  
    1.53  	Material *materials = scn->get_materials();
    1.54  
    1.55 -	glBegin(GL_TRIANGLES);
    1.56  	int num_faces = scn->get_num_faces();
    1.57 +	int cur_mat = -1;
    1.58 +
    1.59  	for(int i=0; i<num_faces; i++) {
    1.60 -		Material *mat = materials ? materials + faces[i].matid : 0;
    1.61 -
    1.62 -		if(mat) {
    1.63 -			glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
    1.64 -		} else {
    1.65 -			glColor3f(1, 1, 1);
    1.66 +		if(faces[i].matid != cur_mat) {
    1.67 +			if(cur_mat != -1) {
    1.68 +				glEnd();
    1.69 +			}
    1.70 +			dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
    1.71 +			cur_mat = faces[i].matid;
    1.72 +			glBegin(GL_TRIANGLES);
    1.73  		}
    1.74  
    1.75  		for(int j=0; j<3; j++) {
    1.76 -			float *pos = faces[i].v[j].pos;
    1.77 -			float *norm = faces[i].normal;
    1.78 -			glNormal3fv(norm);
    1.79 -			glVertex3fv(pos);
    1.80 +			glNormal3fv(faces[i].v[j].normal);
    1.81 +			glVertex3fv(faces[i].v[j].pos);
    1.82  		}
    1.83  	}
    1.84  	glEnd();