clray

view src/clray.cc @ 61:0f174fd60f19

added readme file and license
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Dec 2015 10:27:23 +0200
parents 14c8ebe8f122
children
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 }
207 if(dbg_frame_time) {
208 const RenderStats *rstat = get_render_stats();
209 printf("render time (msec): %lu\n", rstat->render_time);
210 }
211 }
212 need_update = false;
213 }
214 }
216 if(dbg_glrender) {
217 inv_mat = mat;
218 inv_mat.invert();
220 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
221 glLoadMatrixf(inv_mat.m);
222 dbg_render_gl(&scn, dbg_show_kdtree, dbg_show_obj);
223 } else {
224 glEnable(GL_TEXTURE_2D);
225 glDisable(GL_LIGHTING);
227 glBegin(GL_QUADS);
228 glColor3f(1, 1, 1);
229 glTexCoord2f(0, 1); glVertex2f(-1, -1);
230 glTexCoord2f(1, 1); glVertex2f(1, -1);
231 glTexCoord2f(1, 0); glVertex2f(1, 1);
232 glTexCoord2f(0, 0); glVertex2f(-1, 1);
233 glEnd();
235 glDisable(GL_TEXTURE_2D);
236 }
238 glutSwapBuffers();
240 /* We need to make sure OpenGL has finished with the texture
241 * before allowing the OpenCL kernel to run again.
242 */
243 glFinish();
244 }
246 void reshape(int x, int y)
247 {
248 glViewport(0, 0, x, y);
250 /* TODO reallocate the framebuffer to fit the window */
251 }
253 void idle()
254 {
255 need_update = true;
256 glutPostRedisplay();
257 }
259 void keyb(unsigned char key, int x, int y)
260 {
261 switch(key) {
262 case 27:
263 exit(0);
265 case '\b':
266 {
267 static bool busyloop;
269 busyloop = !busyloop;
270 printf("%s busy-looping\n", busyloop ? "WARNING: enabling" : "disabling");
271 glutIdleFunc(busyloop ? idle : 0);
272 }
273 break;
275 case 'd':
276 dbg_glrender = !dbg_glrender;
277 if(dbg_glrender) {
278 printf("Debug OpenGL rendering\n");
279 } else {
280 printf("Raytracing\n");
281 }
282 glutPostRedisplay();
283 break;
285 case 'k':
286 dbg_show_kdtree = !dbg_show_kdtree;
287 if(dbg_glrender) {
288 glutPostRedisplay();
289 }
290 break;
292 case 'o':
293 dbg_show_obj = !dbg_show_obj;
294 if(dbg_glrender) {
295 glutPostRedisplay();
296 }
297 break;
299 case 's':
300 {
301 bool shadows = get_render_option_bool(ROPT_SHAD);
302 shadows = !shadows;
303 printf("%s shadows\n", shadows ? "enabling" : "disabling");
304 set_render_option(ROPT_SHAD, shadows);
305 need_update = true;
306 glutPostRedisplay();
307 }
308 break;
310 case 'r':
311 {
312 bool refl = get_render_option_bool(ROPT_REFL);
313 refl = !refl;
314 printf("%s reflections\n", refl ? "enabling" : "disabling");
315 set_render_option(ROPT_REFL, refl);
316 need_update = true;
317 glutPostRedisplay();
318 }
319 break;
321 case ']':
322 {
323 int iter = get_render_option_int(ROPT_ITER);
324 printf("setting max iterations: %d\n", iter + 1);
325 set_render_option(ROPT_ITER, iter + 1);
326 need_update = true;
327 glutPostRedisplay();
328 }
329 break;
331 case '[':
332 {
333 int iter = get_render_option_int(ROPT_ITER);
334 if(iter-- > 0) {
335 printf("setting max iterations: %d\n", iter);
336 set_render_option(ROPT_ITER, iter);
337 need_update = true;
338 glutPostRedisplay();
339 }
340 }
341 break;
343 case '`':
344 capture("shot%03d.ppm");
345 break;
347 case 't':
348 dbg_frame_time = !dbg_frame_time;
349 break;
351 case 'n':
352 dbg_nocl = !dbg_nocl;
353 printf("switching to %s rendering\n", dbg_nocl ? "debug CPU" : "OpenCL");
354 need_update = true;
355 glutPostRedisplay();
356 break;
358 default:
359 break;
360 }
361 }
363 static bool bnstate[32];
364 static int prev_x, prev_y;
366 void mouse(int bn, int state, int x, int y)
367 {
368 if(state == GLUT_DOWN) {
369 prev_x = x;
370 prev_y = y;
371 bnstate[bn] = true;
372 } else {
373 bnstate[bn] = false;
374 }
375 }
377 #define ROT_SCALE 0.5
378 #define PAN_SCALE 0.1
380 void motion(int x, int y)
381 {
382 int dx = x - prev_x;
383 int dy = y - prev_y;
384 prev_x = x;
385 prev_y = y;
387 if(bnstate[0]) {
388 cam_theta += dx * ROT_SCALE;
389 cam_phi += dy * ROT_SCALE;
391 if(cam_phi < -89) cam_phi = -89;
392 if(cam_phi > 89) cam_phi = 89;
394 need_update = true;
395 glutPostRedisplay();
396 }
397 if(bnstate[2]) {
398 cam_dist += dy * PAN_SCALE;
399 if(cam_dist < 0) cam_dist = 0;
401 need_update = true;
402 glutPostRedisplay();
403 }
404 }
406 bool capture(const char *namefmt)
407 {
408 static int num;
409 char fname[256];
411 num++;
412 snprintf(fname, sizeof fname, namefmt, num);
413 printf("saving image %s\n", fname);
415 float *pixels = new float[4 * xsz * ysz];
416 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels);
418 bool res = write_ppm(fname, pixels, xsz, ysz);
419 if(!res) {
420 num--;
421 }
422 delete [] pixels;
423 return res;
424 }
426 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
427 {
428 FILE *fp;
430 if(!(fp = fopen(fname, "wb"))) {
431 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
432 return false;
433 }
434 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
436 for(int i=0; i<xsz * ysz * 4; i++) {
437 if(i % 4 == 3) continue;
439 unsigned char c = (unsigned char)(fb[i] * 255.0);
440 fputc(c, fp);
441 }
442 fclose(fp);
443 return true;
444 }