clray

view src/clray.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 55b30d8b6805
children df239a52a091
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"
14 #include "ocl.h"
15 #include "ogl.h"
17 #ifdef _MSC_VER
18 #define snprintf _snprintf
19 #endif
21 void cleanup();
22 void disp();
23 void reshape(int x, int y);
24 void keyb(unsigned char key, int x, int y);
25 void mouse(int bn, int status, int x, int y);
26 void motion(int x, int y);
27 bool capture(const char *namefmt);
28 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
30 static int xsz, ysz;
31 static bool need_update = true;
33 static float cam_theta, cam_phi = 25.0;
34 static float cam_dist = 10.0;
36 static bool dbg_glrender;
37 static bool dbg_nocl;
38 static bool dbg_show_kdtree;
39 static bool dbg_show_obj = true;
40 bool dbg_frame_time = true;
42 static Scene scn;
43 static unsigned int tex;
45 static Light lightlist[] = {
46 {{-8, 15, 18, 0}, {1, 1, 1, 1}}
47 };
51 int main(int argc, char **argv)
52 {
53 glutInitWindowSize(800, 600);
54 glutInit(&argc, argv);
56 int loaded = 0;
57 for(int i=1; i<argc; i++) {
58 if(argv[i][0] == '-' && argv[i][2] == 0) {
59 switch(argv[i][1]) {
60 case 'i':
61 if(!argv[++i] || !isdigit(argv[i][0])) {
62 fprintf(stderr, "-i must be followed by the intersection cost\n");
63 return 1;
64 }
66 set_accel_param(ACCEL_PARAM_COST_INTERSECT, atoi(argv[i]));
67 break;
69 case 't':
70 if(!argv[++i] || !isdigit(argv[i][0])) {
71 fprintf(stderr, "-t must be followed by the traversal cost\n");
72 return 1;
73 }
75 set_accel_param(ACCEL_PARAM_COST_TRAVERSE, atoi(argv[i]));
76 break;
78 case 'c':
79 if(!argv[++i] || !isdigit(argv[i][0])) {
80 fprintf(stderr, "-c must be followed by the max number of items per leaf node\n");
81 return 1;
82 }
84 set_accel_param(ACCEL_PARAM_MAX_NODE_ITEMS, atoi(argv[i]));
85 break;
87 case 'd':
88 dbg_glrender = true;
89 break;
91 case 'n':
92 dbg_nocl = true;
93 break;
95 default:
96 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
97 return 1;
98 }
99 } else {
100 if(!scn.load(argv[i])) {
101 fprintf(stderr, "failed to load scene: %s\n", argv[i]);
102 return false;
103 }
104 loaded++;
105 }
106 }
108 if(!loaded) {
109 fprintf(stderr, "you must specify a scene file to load\n");
110 return false;
111 }
112 if(!scn.get_num_faces()) {
113 fprintf(stderr, "didn't load any polygons\n");
114 return false;
115 }
117 int num_lights = sizeof lightlist / sizeof *lightlist;
118 for(int i=0; i<num_lights; i++) {
119 scn.add_light(lightlist[i]);
120 }
122 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
123 glutCreateWindow("OpenCL Raytracer");
125 xsz = glutGet(GLUT_WINDOW_WIDTH);
126 ysz = glutGet(GLUT_WINDOW_HEIGHT);
128 glutDisplayFunc(disp);
129 glutReshapeFunc(reshape);
130 glutKeyboardFunc(keyb);
131 glutMouseFunc(mouse);
132 glutMotionFunc(motion);
134 unsigned int *test_pattern = new unsigned int[xsz * ysz];
135 for(int i=0; i<ysz; i++) {
136 for(int j=0; j<xsz; j++) {
137 test_pattern[i * xsz + j] = ((i >> 4) & 1) == ((j >> 4) & 1) ? 0xff0000 : 0xff00;
138 }
139 }
141 glGenTextures(1, &tex);
142 glBindTexture(GL_TEXTURE_2D, tex);
143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
147 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, test_pattern);
148 delete [] test_pattern;
150 if(!init_opencl()) {
151 return 1;
152 }
154 if(!init_renderer(xsz, ysz, &scn, tex)) {
155 return 1;
156 }
157 atexit(cleanup);
159 glutMainLoop();
160 return 0;
161 }
163 void cleanup()
164 {
165 printf("destroying renderer ...\n");
166 destroy_renderer();
168 printf("shutting down OpenCL ...\n");
169 destroy_opencl();
171 printf("cleaning up OpenGL resources ...\n");
172 glDeleteTextures(1, &tex);
173 }
175 static Matrix4x4 mat, inv_mat, inv_trans;
177 void disp()
178 {
179 glMatrixMode(GL_MODELVIEW);
180 glLoadIdentity();
182 if(need_update) {
183 glPushMatrix();
184 glRotatef(-cam_theta, 0, 1, 0);
185 glRotatef(-cam_phi, 1, 0, 0);
186 glTranslatef(0, 0, cam_dist);
188 glGetFloatv(GL_MODELVIEW_MATRIX, mat.m);
190 inv_trans = mat;
191 inv_trans.m[3] = inv_trans.m[7] = inv_trans.m[11] = 0.0;
192 inv_trans.m[12] = inv_trans.m[13] = inv_trans.m[14] = 0.0;
193 inv_trans.m[15] = 1.0;
195 set_xform(mat.m, inv_trans.m);
196 glPopMatrix();
198 if(!dbg_glrender) {
199 if(dbg_nocl) {
200 dbg_render(mat.m, inv_trans.m);
201 } else {
202 if(!render()) {
203 exit(1);
204 }
205 }
206 need_update = false;
207 }
208 }
210 if(dbg_glrender) {
211 inv_mat = mat;
212 inv_mat.invert();
214 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
215 glLoadMatrixf(inv_mat.m);
216 dbg_render_gl(&scn, dbg_show_kdtree, dbg_show_obj);
217 } else {
218 glEnable(GL_TEXTURE_2D);
219 glDisable(GL_LIGHTING);
221 glBegin(GL_QUADS);
222 glColor3f(1, 1, 1);
223 glTexCoord2f(0, 1); glVertex2f(-1, -1);
224 glTexCoord2f(1, 1); glVertex2f(1, -1);
225 glTexCoord2f(1, 0); glVertex2f(1, 1);
226 glTexCoord2f(0, 0); glVertex2f(-1, 1);
227 glEnd();
229 glDisable(GL_TEXTURE_2D);
230 }
232 glutSwapBuffers();
233 }
235 void reshape(int x, int y)
236 {
237 glViewport(0, 0, x, y);
239 /* reallocate the framebuffer */
240 /*delete [] fb;
241 fb = new float[x * y * 4];
242 set_framebuffer(fb, x, y);*/
243 }
245 void idle()
246 {
247 need_update = true;
248 glutPostRedisplay();
249 }
251 void keyb(unsigned char key, int x, int y)
252 {
253 switch(key) {
254 case 27:
255 exit(0);
257 case '\b':
258 {
259 static bool busyloop;
261 busyloop = !busyloop;
262 printf("%s busy-looping\n", busyloop ? "WARNING: enabling" : "disabling");
263 glutIdleFunc(busyloop ? idle : 0);
264 }
265 break;
267 case 'd':
268 dbg_glrender = !dbg_glrender;
269 if(dbg_glrender) {
270 printf("Debug OpenGL rendering\n");
271 } else {
272 printf("Raytracing\n");
273 }
274 glutPostRedisplay();
275 break;
277 case 'k':
278 dbg_show_kdtree = !dbg_show_kdtree;
279 if(dbg_glrender) {
280 glutPostRedisplay();
281 }
282 break;
284 case 'o':
285 dbg_show_obj = !dbg_show_obj;
286 if(dbg_glrender) {
287 glutPostRedisplay();
288 }
289 break;
291 case 's':
292 {
293 bool shadows = get_render_option_bool(ROPT_SHAD);
294 shadows = !shadows;
295 printf("%s shadows\n", shadows ? "enabling" : "disabling");
296 set_render_option(ROPT_SHAD, shadows);
297 need_update = true;
298 glutPostRedisplay();
299 }
300 break;
302 case 'r':
303 {
304 bool refl = get_render_option_bool(ROPT_REFL);
305 refl = !refl;
306 printf("%s reflections\n", refl ? "enabling" : "disabling");
307 set_render_option(ROPT_REFL, refl);
308 need_update = true;
309 glutPostRedisplay();
310 }
311 break;
313 case ']':
314 {
315 int iter = get_render_option_int(ROPT_ITER);
316 printf("setting max iterations: %d\n", iter + 1);
317 set_render_option(ROPT_ITER, iter + 1);
318 need_update = true;
319 glutPostRedisplay();
320 }
321 break;
323 case '[':
324 {
325 int iter = get_render_option_int(ROPT_ITER);
326 if(iter-- > 0) {
327 printf("setting max iterations: %d\n", iter);
328 set_render_option(ROPT_ITER, iter);
329 need_update = true;
330 glutPostRedisplay();
331 }
332 }
333 break;
335 case '`':
336 capture("shot%03d.ppm");
337 break;
339 case 't':
340 dbg_frame_time = !dbg_frame_time;
341 break;
343 case 'n':
344 dbg_nocl = !dbg_nocl;
345 printf("switching to %s rendering\n", dbg_nocl ? "debug CPU" : "OpenCL");
346 need_update = true;
347 glutPostRedisplay();
348 break;
350 default:
351 break;
352 }
353 }
355 static bool bnstate[32];
356 static int prev_x, prev_y;
358 void mouse(int bn, int state, int x, int y)
359 {
360 if(state == GLUT_DOWN) {
361 prev_x = x;
362 prev_y = y;
363 bnstate[bn] = true;
364 } else {
365 bnstate[bn] = false;
366 }
367 }
369 #define ROT_SCALE 0.5
370 #define PAN_SCALE 0.1
372 void motion(int x, int y)
373 {
374 int dx = x - prev_x;
375 int dy = y - prev_y;
376 prev_x = x;
377 prev_y = y;
379 if(bnstate[0]) {
380 cam_theta += dx * ROT_SCALE;
381 cam_phi += dy * ROT_SCALE;
383 if(cam_phi < -89) cam_phi = -89;
384 if(cam_phi > 89) cam_phi = 89;
386 need_update = true;
387 glutPostRedisplay();
388 }
389 if(bnstate[2]) {
390 cam_dist += dy * PAN_SCALE;
391 if(cam_dist < 0) cam_dist = 0;
393 need_update = true;
394 glutPostRedisplay();
395 }
396 }
398 bool capture(const char *namefmt)
399 {
400 static int num;
401 char fname[256];
403 num++;
404 snprintf(fname, sizeof fname, namefmt, num);
405 printf("saving image %s\n", fname);
407 float *pixels = new float[4 * xsz * ysz];
408 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels);
410 bool res = write_ppm(fname, pixels, xsz, ysz);
411 if(!res) {
412 num--;
413 }
414 delete [] pixels;
415 return res;
416 }
418 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
419 {
420 FILE *fp;
422 if(!(fp = fopen(fname, "wb"))) {
423 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
424 return false;
425 }
426 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
428 for(int i=0; i<xsz * ysz * 4; i++) {
429 if(i % 4 == 3) continue;
431 unsigned char c = (unsigned char)(fb[i] * 255.0);
432 fputc(c, fp);
433 }
434 fclose(fp);
435 return true;
436 }