clray
view src/clray.cc @ 30:04803c702014
debugging ...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 21 Aug 2010 23:57:19 +0100 |
parents | 8b2f2ad14ae7 |
children | 4cf4919c3812 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <errno.h>
6 #ifndef __APPLE__
7 #include <GL/glut.h>
8 #else
9 #include <GLUT/glut.h>
10 #endif
11 #include "rt.h"
12 #include "matrix.h"
13 #include "scene.h"
15 void cleanup();
16 void disp();
17 void reshape(int x, int y);
18 void keyb(unsigned char key, int x, int y);
19 void mouse(int bn, int status, int x, int y);
20 void motion(int x, int y);
21 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
23 static int xsz, ysz;
24 static bool need_update = true;
26 static float cam_theta, cam_phi = 25.0;
27 static float cam_dist = 10.0;
29 static bool dbg_glrender = true;
30 static bool dbg_show_kdtree = false;
31 static bool dbg_show_obj = true;
33 static Scene scn;
35 int main(int argc, char **argv)
36 {
37 glutInitWindowSize(800, 600);
38 glutInit(&argc, argv);
40 int loaded = 0;
41 for(int i=1; i<argc; i++) {
42 if(argv[i][0] == '-' && argv[i][2] == 0) {
43 switch(argv[i][1]) {
44 case 'i':
45 if(!argv[++i] || !isdigit(argv[i][0])) {
46 fprintf(stderr, "-i must be followed by the intersection cost\n");
47 return 1;
48 }
50 set_accel_param(ACCEL_PARAM_COST_INTERSECT, atoi(argv[i]));
51 break;
53 case 't':
54 if(!argv[++i] || !isdigit(argv[i][0])) {
55 fprintf(stderr, "-t must be followed by the traversal cost\n");
56 return 1;
57 }
59 set_accel_param(ACCEL_PARAM_COST_TRAVERSE, atoi(argv[i]));
60 break;
62 case 'c':
63 if(!argv[++i] || !isdigit(argv[i][0])) {
64 fprintf(stderr, "-c must be followed by the max number of items per leaf node\n");
65 return 1;
66 }
68 set_accel_param(ACCEL_PARAM_MAX_NODE_ITEMS, atoi(argv[i]));
69 break;
71 default:
72 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
73 return 1;
74 }
75 } else {
76 if(!scn.load(argv[i])) {
77 fprintf(stderr, "failed to load scene: %s\n", argv[i]);
78 return false;
79 }
80 loaded++;
81 }
82 }
84 if(!loaded) {
85 fprintf(stderr, "you must specify a scene file to load\n");
86 return false;
87 }
88 if(!scn.get_num_faces()) {
89 fprintf(stderr, "didn't load any polygons\n");
90 return false;
91 }
93 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
94 glutCreateWindow("OpenCL Raytracer");
96 xsz = glutGet(GLUT_WINDOW_WIDTH);
97 ysz = glutGet(GLUT_WINDOW_HEIGHT);
99 glutDisplayFunc(disp);
100 glutReshapeFunc(reshape);
101 glutKeyboardFunc(keyb);
102 glutMouseFunc(mouse);
103 glutMotionFunc(motion);
105 if(!init_renderer(xsz, ysz, &scn)) {
106 return 1;
107 }
108 atexit(cleanup);
111 /*glGenTextures(1, &tex);
112 glBindTexture(GL_TEXTURE_2D, tex);*/
113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
117 glTexImage2D(GL_TEXTURE_2D, 0, 4, xsz, ysz, 0, GL_RGBA, GL_FLOAT, 0);
119 glutMainLoop();
120 return 0;
121 }
123 void cleanup()
124 {
125 destroy_renderer();
126 }
128 static Matrix4x4 mat, inv_mat, inv_trans;
130 void disp()
131 {
132 glMatrixMode(GL_MODELVIEW);
133 glLoadIdentity();
135 if(need_update) {
136 glPushMatrix();
137 glRotatef(-cam_theta, 0, 1, 0);
138 glRotatef(-cam_phi, 1, 0, 0);
139 glTranslatef(0, 0, cam_dist);
141 glGetFloatv(GL_MODELVIEW_MATRIX, mat.m);
143 inv_mat = mat;
144 inv_mat.invert();
146 /*inv_trans = inv_mat;
147 inv_trans.transpose();*/
148 inv_trans = mat;
149 inv_trans.m[3] = inv_trans.m[7] = inv_trans.m[11] = 0.0;
150 inv_trans.m[12] = inv_trans.m[13] = inv_trans.m[14] = 0.0;
151 inv_trans.m[15] = 1.0;
153 set_xform(mat.m, inv_trans.m);
154 glPopMatrix();
156 if(!dbg_glrender) {
157 if(!render()) {
158 exit(1);
159 }
160 need_update = false;
161 }
162 }
164 if(dbg_glrender) {
165 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
166 glLoadMatrixf(inv_mat.m);
167 dbg_render_gl(&scn, dbg_show_kdtree, dbg_show_obj);
168 } else {
169 glEnable(GL_TEXTURE_2D);
170 glDisable(GL_LIGHTING);
172 glBegin(GL_QUADS);
173 glColor3f(1, 1, 1);
174 glTexCoord2f(0, 1); glVertex2f(-1, -1);
175 glTexCoord2f(1, 1); glVertex2f(1, -1);
176 glTexCoord2f(1, 0); glVertex2f(1, 1);
177 glTexCoord2f(0, 0); glVertex2f(-1, 1);
178 glEnd();
180 glDisable(GL_TEXTURE_2D);
181 }
183 glutSwapBuffers();
184 }
186 void reshape(int x, int y)
187 {
188 glViewport(0, 0, x, y);
190 /* reallocate the framebuffer */
191 /*delete [] fb;
192 fb = new float[x * y * 4];
193 set_framebuffer(fb, x, y);*/
194 }
196 void keyb(unsigned char key, int x, int y)
197 {
198 switch(key) {
199 case 27:
200 exit(0);
202 case 'r':
203 need_update = true;
204 glutPostRedisplay();
205 break;
207 case 'd':
208 dbg_glrender = !dbg_glrender;
209 if(dbg_glrender) {
210 printf("Debug OpenGL rendering\n");
211 } else {
212 printf("Raytracing\n");
213 }
214 glutPostRedisplay();
215 break;
217 case 'k':
218 dbg_show_kdtree = !dbg_show_kdtree;
219 if(dbg_glrender) {
220 glutPostRedisplay();
221 }
222 break;
224 case 'o':
225 dbg_show_obj = !dbg_show_obj;
226 if(dbg_glrender) {
227 glutPostRedisplay();
228 }
229 break;
231 default:
232 break;
233 }
234 }
236 static bool bnstate[32];
237 static int prev_x, prev_y;
239 void mouse(int bn, int state, int x, int y)
240 {
241 if(state == GLUT_DOWN) {
242 prev_x = x;
243 prev_y = y;
244 bnstate[bn] = true;
245 } else {
246 bnstate[bn] = false;
247 }
248 }
250 #define ROT_SCALE 0.5
251 #define PAN_SCALE 0.1
253 void motion(int x, int y)
254 {
255 int dx = x - prev_x;
256 int dy = y - prev_y;
257 prev_x = x;
258 prev_y = y;
260 if(bnstate[0]) {
261 cam_theta += dx * ROT_SCALE;
262 cam_phi += dy * ROT_SCALE;
264 if(cam_phi < -89) cam_phi = -89;
265 if(cam_phi > 89) cam_phi = 89;
267 need_update = true;
268 glutPostRedisplay();
269 }
270 if(bnstate[2]) {
271 cam_dist += dy * PAN_SCALE;
272 if(cam_dist < 0) cam_dist = 0;
274 need_update = true;
275 glutPostRedisplay();
276 }
277 }
279 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
280 {
281 FILE *fp;
283 if(!(fp = fopen(fname, "wb"))) {
284 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
285 return false;
286 }
287 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
289 for(int i=0; i<xsz * ysz * 4; i++) {
290 if(i % 4 == 3) continue;
292 unsigned char c = (unsigned char)(fb[i] * 255.0);
293 fputc(c, fp);
294 }
295 fclose(fp);
296 return true;
297 }