clray
diff src/clray.cc @ 12:85fd61f374d9
fixed the bloody intersection bug
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 03 Aug 2010 13:06:59 +0100 |
parents | deaf85acf6af |
children | 407935b73af3 |
line diff
1.1 --- a/src/clray.cc Sat Jul 31 22:23:57 2010 +0100 1.2 +++ b/src/clray.cc Tue Aug 03 13:06:59 2010 +0100 1.3 @@ -8,6 +8,7 @@ 1.4 #include <GLUT/glut.h> 1.5 #endif 1.6 #include "rt.h" 1.7 +#include "matrix.h" 1.8 1.9 void cleanup(); 1.10 void disp(); 1.11 @@ -17,13 +18,14 @@ 1.12 void motion(int x, int y); 1.13 bool write_ppm(const char *fname, float *fb, int xsz, int ysz); 1.14 1.15 -static float *fb; 1.16 static int xsz, ysz; 1.17 static bool need_update = true; 1.18 1.19 static float cam_theta, cam_phi = 25.0; 1.20 static float cam_dist = 10.0; 1.21 1.22 +static bool dbg_glrender; 1.23 + 1.24 int main(int argc, char **argv) 1.25 { 1.26 glutInitWindowSize(800, 600); 1.27 @@ -40,8 +42,7 @@ 1.28 glutMouseFunc(mouse); 1.29 glutMotionFunc(motion); 1.30 1.31 - fb = new float[xsz * ysz * 4]; 1.32 - if(!init_renderer(xsz, ysz, fb)) { 1.33 + if(!init_renderer(xsz, ysz)) { 1.34 return 1; 1.35 } 1.36 atexit(cleanup); 1.37 @@ -60,42 +61,60 @@ 1.38 1.39 void cleanup() 1.40 { 1.41 - delete [] fb; 1.42 destroy_renderer(); 1.43 } 1.44 1.45 +static Matrix4x4 mat, inv_mat, inv_trans; 1.46 + 1.47 void disp() 1.48 { 1.49 glMatrixMode(GL_MODELVIEW); 1.50 glLoadIdentity(); 1.51 1.52 if(need_update) { 1.53 - float mat[16]; 1.54 + glPushMatrix(); 1.55 + glRotatef(-cam_theta, 0, 1, 0); 1.56 + glRotatef(-cam_phi, 1, 0, 0); 1.57 + glTranslatef(0, 0, cam_dist); 1.58 1.59 - glPushMatrix(); 1.60 - glRotatef(cam_theta, 0, 1, 0); 1.61 - glRotatef(cam_phi, 1, 0, 0); 1.62 - glTranslatef(0, 0, -cam_dist); 1.63 + glGetFloatv(GL_MODELVIEW_MATRIX, mat.m); 1.64 1.65 - glGetFloatv(GL_MODELVIEW_MATRIX, mat); 1.66 - set_xform(mat); 1.67 + inv_mat = mat; 1.68 + inv_mat.invert(); 1.69 + 1.70 + /*inv_trans = inv_mat; 1.71 + inv_trans.transpose();*/ 1.72 + inv_trans = mat; 1.73 + inv_trans.m[3] = inv_trans.m[7] = inv_trans.m[11] = 0.0; 1.74 + inv_trans.m[12] = inv_trans.m[13] = inv_trans.m[14] = 0.0; 1.75 + inv_trans.m[15] = 1.0; 1.76 + 1.77 + set_xform(mat.m, inv_trans.m); 1.78 glPopMatrix(); 1.79 1.80 - render(); 1.81 - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGBA, GL_FLOAT, fb); 1.82 + if(!render()) { 1.83 + exit(1); 1.84 + } 1.85 need_update = false; 1.86 } 1.87 1.88 - glEnable(GL_TEXTURE_2D); 1.89 + if(dbg_glrender) { 1.90 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.91 + glLoadMatrixf(inv_mat.m); 1.92 + dbg_render_gl(); 1.93 + } else { 1.94 + glEnable(GL_TEXTURE_2D); 1.95 1.96 - glBegin(GL_QUADS); 1.97 - glTexCoord2f(0, 1); glVertex2f(-1, -1); 1.98 - glTexCoord2f(1, 1); glVertex2f(1, -1); 1.99 - glTexCoord2f(1, 0); glVertex2f(1, 1); 1.100 - glTexCoord2f(0, 0); glVertex2f(-1, 1); 1.101 - glEnd(); 1.102 + glBegin(GL_QUADS); 1.103 + glColor3f(1, 1, 1); 1.104 + glTexCoord2f(0, 1); glVertex2f(-1, -1); 1.105 + glTexCoord2f(1, 1); glVertex2f(1, -1); 1.106 + glTexCoord2f(1, 0); glVertex2f(1, 1); 1.107 + glTexCoord2f(0, 0); glVertex2f(-1, 1); 1.108 + glEnd(); 1.109 1.110 - glDisable(GL_TEXTURE_2D); 1.111 + glDisable(GL_TEXTURE_2D); 1.112 + } 1.113 1.114 glutSwapBuffers(); 1.115 } 1.116 @@ -116,14 +135,16 @@ 1.117 case 27: 1.118 exit(0); 1.119 1.120 - case 's': 1.121 - if(write_ppm("shot.ppm", fb, xsz, ysz)) { 1.122 - printf("captured screenshot shot.ppm\n"); 1.123 - } 1.124 + case 'r': 1.125 + need_update = true; 1.126 + glutPostRedisplay(); 1.127 break; 1.128 1.129 - case 'r': 1.130 - need_update = true; 1.131 + case 'd': 1.132 + dbg_glrender = !dbg_glrender; 1.133 + if(dbg_glrender) { 1.134 + printf("DEBUG GL RENDER\n"); 1.135 + } 1.136 glutPostRedisplay(); 1.137 break; 1.138 1.139 @@ -160,7 +181,7 @@ 1.140 cam_theta += dx * ROT_SCALE; 1.141 cam_phi += dy * ROT_SCALE; 1.142 1.143 - if(cam_phi < -89) cam_phi = 89; 1.144 + if(cam_phi < -89) cam_phi = -89; 1.145 if(cam_phi > 89) cam_phi = 89; 1.146 1.147 need_update = true;