clray

view src/clray.cc @ 57:14c8ebe8f122

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 11 Sep 2010 03:01:20 +0100
parents df239a52a091 e3b4457dc4d2
children 3d13924b22e6
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();
235 /* We need to make sure OpenGL has finished with the texture
236 * before allowing the OpenCL kernel to run again.
237 */
238 glFinish();
239 }
241 void reshape(int x, int y)
242 {
243 glViewport(0, 0, x, y);
245 /* reallocate the framebuffer */
246 /*delete [] fb;
247 fb = new float[x * y * 4];
248 set_framebuffer(fb, x, y);*/
249 }
251 void idle()
252 {
253 need_update = true;
254 glutPostRedisplay();
255 }
257 void keyb(unsigned char key, int x, int y)
258 {
259 switch(key) {
260 case 27:
261 exit(0);
263 case '\b':
264 {
265 static bool busyloop;
267 busyloop = !busyloop;
268 printf("%s busy-looping\n", busyloop ? "WARNING: enabling" : "disabling");
269 glutIdleFunc(busyloop ? idle : 0);
270 }
271 break;
273 case 'd':
274 dbg_glrender = !dbg_glrender;
275 if(dbg_glrender) {
276 printf("Debug OpenGL rendering\n");
277 } else {
278 printf("Raytracing\n");
279 }
280 glutPostRedisplay();
281 break;
283 case 'k':
284 dbg_show_kdtree = !dbg_show_kdtree;
285 if(dbg_glrender) {
286 glutPostRedisplay();
287 }
288 break;
290 case 'o':
291 dbg_show_obj = !dbg_show_obj;
292 if(dbg_glrender) {
293 glutPostRedisplay();
294 }
295 break;
297 case 's':
298 {
299 bool shadows = get_render_option_bool(ROPT_SHAD);
300 shadows = !shadows;
301 printf("%s shadows\n", shadows ? "enabling" : "disabling");
302 set_render_option(ROPT_SHAD, shadows);
303 need_update = true;
304 glutPostRedisplay();
305 }
306 break;
308 case 'r':
309 {
310 bool refl = get_render_option_bool(ROPT_REFL);
311 refl = !refl;
312 printf("%s reflections\n", refl ? "enabling" : "disabling");
313 set_render_option(ROPT_REFL, refl);
314 need_update = true;
315 glutPostRedisplay();
316 }
317 break;
319 case ']':
320 {
321 int iter = get_render_option_int(ROPT_ITER);
322 printf("setting max iterations: %d\n", iter + 1);
323 set_render_option(ROPT_ITER, iter + 1);
324 need_update = true;
325 glutPostRedisplay();
326 }
327 break;
329 case '[':
330 {
331 int iter = get_render_option_int(ROPT_ITER);
332 if(iter-- > 0) {
333 printf("setting max iterations: %d\n", iter);
334 set_render_option(ROPT_ITER, iter);
335 need_update = true;
336 glutPostRedisplay();
337 }
338 }
339 break;
341 case '`':
342 capture("shot%03d.ppm");
343 break;
345 case 't':
346 dbg_frame_time = !dbg_frame_time;
347 break;
349 case 'n':
350 dbg_nocl = !dbg_nocl;
351 printf("switching to %s rendering\n", dbg_nocl ? "debug CPU" : "OpenCL");
352 need_update = true;
353 glutPostRedisplay();
354 break;
356 default:
357 break;
358 }
359 }
361 static bool bnstate[32];
362 static int prev_x, prev_y;
364 void mouse(int bn, int state, int x, int y)
365 {
366 if(state == GLUT_DOWN) {
367 prev_x = x;
368 prev_y = y;
369 bnstate[bn] = true;
370 } else {
371 bnstate[bn] = false;
372 }
373 }
375 #define ROT_SCALE 0.5
376 #define PAN_SCALE 0.1
378 void motion(int x, int y)
379 {
380 int dx = x - prev_x;
381 int dy = y - prev_y;
382 prev_x = x;
383 prev_y = y;
385 if(bnstate[0]) {
386 cam_theta += dx * ROT_SCALE;
387 cam_phi += dy * ROT_SCALE;
389 if(cam_phi < -89) cam_phi = -89;
390 if(cam_phi > 89) cam_phi = 89;
392 need_update = true;
393 glutPostRedisplay();
394 }
395 if(bnstate[2]) {
396 cam_dist += dy * PAN_SCALE;
397 if(cam_dist < 0) cam_dist = 0;
399 need_update = true;
400 glutPostRedisplay();
401 }
402 }
404 bool capture(const char *namefmt)
405 {
406 static int num;
407 char fname[256];
409 num++;
410 snprintf(fname, sizeof fname, namefmt, num);
411 printf("saving image %s\n", fname);
413 float *pixels = new float[4 * xsz * ysz];
414 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels);
416 bool res = write_ppm(fname, pixels, xsz, ysz);
417 if(!res) {
418 num--;
419 }
420 delete [] pixels;
421 return res;
422 }
424 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
425 {
426 FILE *fp;
428 if(!(fp = fopen(fname, "wb"))) {
429 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
430 return false;
431 }
432 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
434 for(int i=0; i<xsz * ysz * 4; i++) {
435 if(i % 4 == 3) continue;
437 unsigned char c = (unsigned char)(fb[i] * 255.0);
438 fputc(c, fp);
439 }
440 fclose(fp);
441 return true;
442 }