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