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