clray

view src/dbggl.cc @ 55:df239a52a091

extensive render stats for the CPU raytracer
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 11 Sep 2010 03:00:21 +0100
parents
children
line source
1 #include <string.h>
2 #include <assert.h>
3 #include "rt.h"
4 #include "ogl.h"
6 #define MIN(a, b) ((a) < (b) ? (a) : (b))
7 static void dbg_set_gl_material(Material *mat)
8 {
9 static Material def_mat = {{0.7, 0.7, 0.7, 1}, {0, 0, 0, 0}, 0, 0, 0};
11 if(!mat) mat = &def_mat;
13 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat->kd);
14 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->ks);
15 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, MIN(mat->spow, 128.0f));
16 }
18 void dbg_render_gl(Scene *scn, bool show_tree, bool show_obj)
19 {
20 const RendInfo *rinf = get_render_info();
21 const Face *faces = scn->get_face_buffer();
23 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_LIGHTING_BIT);
25 for(int i=0; i<scn->get_num_lights(); i++) {
26 float lpos[4];
28 memcpy(lpos, scn->lights[i].pos, sizeof lpos);
29 lpos[3] = 1.0;
31 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos);
32 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, scn->lights[i].color);
33 glEnable(GL_LIGHT0 + i);
34 }
36 glDisable(GL_TEXTURE_2D);
37 glEnable(GL_DEPTH_TEST);
38 glEnable(GL_LIGHTING);
40 glMatrixMode(GL_PROJECTION);
41 glPushMatrix();
42 glLoadIdentity();
43 gluPerspective(45.0, (float)rinf->xsz / (float)rinf->ysz, 0.5, 1000.0);
45 if(show_obj) {
46 Material *materials = scn->get_materials();
48 int num_faces = scn->get_num_faces();
49 int cur_mat = -1;
51 for(int i=0; i<num_faces; i++) {
52 if(faces[i].matid != cur_mat) {
53 if(cur_mat != -1) {
54 glEnd();
55 }
56 dbg_set_gl_material(materials ? materials + faces[i].matid : 0);
57 cur_mat = faces[i].matid;
58 glBegin(GL_TRIANGLES);
59 }
61 for(int j=0; j<3; j++) {
62 glNormal3fv(faces[i].v[j].normal);
63 glVertex3fv(faces[i].v[j].pos);
64 }
65 }
66 glEnd();
67 }
69 if(show_tree) {
70 scn->draw_kdtree();
71 }
73 glPopMatrix();
74 glPopAttrib();
76 assert(glGetError() == GL_NO_ERROR);
77 }