clray

diff src/dbggl.cc @ 54:6a30f27fa1e6

separated the OpenGL visualization and added a CPU raytracing mode
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 10 Sep 2010 16:47:00 +0100
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/dbggl.cc	Fri Sep 10 16:47:00 2010 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +#include <string.h>
     1.5 +#include <assert.h>
     1.6 +#include "rt.h"
     1.7 +#include "ogl.h"
     1.8 +
     1.9 +#define MIN(a, b)	((a) < (b) ? (a) : (b))
    1.10 +static void dbg_set_gl_material(Material *mat)
    1.11 +{
    1.12 +	static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
    1.13 +
    1.14 +	if(!mat) mat = &def_mat;
    1.15 +
    1.16 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
    1.17 +	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
    1.18 +	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MIN(mat->spow, 128.0f));
    1.19 +}
    1.20 +
    1.21 +void dbg_render_gl(Scene *scn, bool show_tree, bool show_obj)
    1.22 +{
    1.23 +	const RendInfo *rinf = get_render_info();
    1.24 +	const Face *faces = scn->get_face_buffer();
    1.25 +
    1.26 +	glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
    1.27 +
    1.28 +	for(int i=0; i<scn->get_num_lights(); i++) {
    1.29 +		float lpos[4];
    1.30 +
    1.31 +		memcpy(lpos, scn->lights[i].pos, sizeof lpos);
    1.32 +		lpos[3] = 1.0;
    1.33 +
    1.34 +		glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
    1.35 +		glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, scn->lights[i].color);
    1.36 +		glEnable(GL_LIGHT0 + i);
    1.37 +	}
    1.38 +
    1.39 +	glDisable(GL_TEXTURE_2D);
    1.40 +	glEnable(GL_DEPTH_TEST);
    1.41 +	glEnable(GL_LIGHTING);
    1.42 +
    1.43 +	glMatrixMode(GL_PROJECTION);
    1.44 +	glPushMatrix();
    1.45 +	glLoadIdentity();
    1.46 +	gluPerspective(45.0, (float)rinf->xsz / (float)rinf->ysz, 0.5, 1000.0);
    1.47 +
    1.48 +	if(show_obj) {
    1.49 +		Material *materials = scn->get_materials();
    1.50 +
    1.51 +		int num_faces = scn->get_num_faces();
    1.52 +		int cur_mat = -1;
    1.53 +
    1.54 +		for(int i=0; i<num_faces; i++) {
    1.55 +			if(faces[i].matid != cur_mat) {
    1.56 +				if(cur_mat != -1) {
    1.57 +					glEnd();
    1.58 +				}
    1.59 +				dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
    1.60 +				cur_mat = faces[i].matid;
    1.61 +				glBegin(GL_TRIANGLES);
    1.62 +			}
    1.63 +
    1.64 +			for(int j=0; j<3; j++) {
    1.65 +				glNormal3fv(faces[i].v[j].normal);
    1.66 +				glVertex3fv(faces[i].v[j].pos);
    1.67 +			}
    1.68 +		}
    1.69 +		glEnd();
    1.70 +	}
    1.71 +
    1.72 +	if(show_tree) {
    1.73 +		scn->draw_kdtree();
    1.74 +	}
    1.75 +
    1.76 +	glPopMatrix();
    1.77 +	glPopAttrib();
    1.78 +
    1.79 +	assert(glGetError() == GL_NO_ERROR);
    1.80 +}