clray

view src/clray.cc @ 56:e3b4457dc4d2

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