clray

view src/clray.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 6a30f27fa1e6
children 14c8ebe8f122
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 print_render_stats();
202 } else {
203 if(!render()) {
204 exit(1);
205 }
206 }
207 need_update = false;
208 }
209 }
211 if(dbg_glrender) {
212 inv_mat = mat;
213 inv_mat.invert();
215 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
216 glLoadMatrixf(inv_mat.m);
217 dbg_render_gl(&scn, dbg_show_kdtree, dbg_show_obj);
218 } else {
219 glEnable(GL_TEXTURE_2D);
220 glDisable(GL_LIGHTING);
222 glBegin(GL_QUADS);
223 glColor3f(1, 1, 1);
224 glTexCoord2f(0, 1); glVertex2f(-1, -1);
225 glTexCoord2f(1, 1); glVertex2f(1, -1);
226 glTexCoord2f(1, 0); glVertex2f(1, 1);
227 glTexCoord2f(0, 0); glVertex2f(-1, 1);
228 glEnd();
230 glDisable(GL_TEXTURE_2D);
231 }
233 glutSwapBuffers();
234 }
236 void reshape(int x, int y)
237 {
238 glViewport(0, 0, x, y);
240 /* reallocate the framebuffer */
241 /*delete [] fb;
242 fb = new float[x * y * 4];
243 set_framebuffer(fb, x, y);*/
244 }
246 void idle()
247 {
248 need_update = true;
249 glutPostRedisplay();
250 }
252 void keyb(unsigned char key, int x, int y)
253 {
254 switch(key) {
255 case 27:
256 exit(0);
258 case '\b':
259 {
260 static bool busyloop;
262 busyloop = !busyloop;
263 printf("%s busy-looping\n", busyloop ? "WARNING: enabling" : "disabling");
264 glutIdleFunc(busyloop ? idle : 0);
265 }
266 break;
268 case 'd':
269 dbg_glrender = !dbg_glrender;
270 if(dbg_glrender) {
271 printf("Debug OpenGL rendering\n");
272 } else {
273 printf("Raytracing\n");
274 }
275 glutPostRedisplay();
276 break;
278 case 'k':
279 dbg_show_kdtree = !dbg_show_kdtree;
280 if(dbg_glrender) {
281 glutPostRedisplay();
282 }
283 break;
285 case 'o':
286 dbg_show_obj = !dbg_show_obj;
287 if(dbg_glrender) {
288 glutPostRedisplay();
289 }
290 break;
292 case 's':
293 {
294 bool shadows = get_render_option_bool(ROPT_SHAD);
295 shadows = !shadows;
296 printf("%s shadows\n", shadows ? "enabling" : "disabling");
297 set_render_option(ROPT_SHAD, shadows);
298 need_update = true;
299 glutPostRedisplay();
300 }
301 break;
303 case 'r':
304 {
305 bool refl = get_render_option_bool(ROPT_REFL);
306 refl = !refl;
307 printf("%s reflections\n", refl ? "enabling" : "disabling");
308 set_render_option(ROPT_REFL, refl);
309 need_update = true;
310 glutPostRedisplay();
311 }
312 break;
314 case ']':
315 {
316 int iter = get_render_option_int(ROPT_ITER);
317 printf("setting max iterations: %d\n", iter + 1);
318 set_render_option(ROPT_ITER, iter + 1);
319 need_update = true;
320 glutPostRedisplay();
321 }
322 break;
324 case '[':
325 {
326 int iter = get_render_option_int(ROPT_ITER);
327 if(iter-- > 0) {
328 printf("setting max iterations: %d\n", iter);
329 set_render_option(ROPT_ITER, iter);
330 need_update = true;
331 glutPostRedisplay();
332 }
333 }
334 break;
336 case '`':
337 capture("shot%03d.ppm");
338 break;
340 case 't':
341 dbg_frame_time = !dbg_frame_time;
342 break;
344 case 'n':
345 dbg_nocl = !dbg_nocl;
346 printf("switching to %s rendering\n", dbg_nocl ? "debug CPU" : "OpenCL");
347 need_update = true;
348 glutPostRedisplay();
349 break;
351 default:
352 break;
353 }
354 }
356 static bool bnstate[32];
357 static int prev_x, prev_y;
359 void mouse(int bn, int state, int x, int y)
360 {
361 if(state == GLUT_DOWN) {
362 prev_x = x;
363 prev_y = y;
364 bnstate[bn] = true;
365 } else {
366 bnstate[bn] = false;
367 }
368 }
370 #define ROT_SCALE 0.5
371 #define PAN_SCALE 0.1
373 void motion(int x, int y)
374 {
375 int dx = x - prev_x;
376 int dy = y - prev_y;
377 prev_x = x;
378 prev_y = y;
380 if(bnstate[0]) {
381 cam_theta += dx * ROT_SCALE;
382 cam_phi += dy * ROT_SCALE;
384 if(cam_phi < -89) cam_phi = -89;
385 if(cam_phi > 89) cam_phi = 89;
387 need_update = true;
388 glutPostRedisplay();
389 }
390 if(bnstate[2]) {
391 cam_dist += dy * PAN_SCALE;
392 if(cam_dist < 0) cam_dist = 0;
394 need_update = true;
395 glutPostRedisplay();
396 }
397 }
399 bool capture(const char *namefmt)
400 {
401 static int num;
402 char fname[256];
404 num++;
405 snprintf(fname, sizeof fname, namefmt, num);
406 printf("saving image %s\n", fname);
408 float *pixels = new float[4 * xsz * ysz];
409 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels);
411 bool res = write_ppm(fname, pixels, xsz, ysz);
412 if(!res) {
413 num--;
414 }
415 delete [] pixels;
416 return res;
417 }
419 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
420 {
421 FILE *fp;
423 if(!(fp = fopen(fname, "wb"))) {
424 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
425 return false;
426 }
427 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
429 for(int i=0; i<xsz * ysz * 4; i++) {
430 if(i % 4 == 3) continue;
432 unsigned char c = (unsigned char)(fb[i] * 255.0);
433 fputc(c, fp);
434 }
435 fclose(fp);
436 return true;
437 }