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