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