clray
view src/clray.cc @ 51:9c0867942612
changed the binary target to clray
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 02 Sep 2010 01:41:51 +0100 |
parents | 1ae68d46cfda |
children | 55b30d8b6805 |
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();
215 }
217 void reshape(int x, int y)
218 {
219 glViewport(0, 0, x, y);
221 /* reallocate the framebuffer */
222 /*delete [] fb;
223 fb = new float[x * y * 4];
224 set_framebuffer(fb, x, y);*/
225 }
227 void idle()
228 {
229 need_update = true;
230 glutPostRedisplay();
231 }
233 void keyb(unsigned char key, int x, int y)
234 {
235 switch(key) {
236 case 27:
237 exit(0);
239 case '\b':
240 {
241 static bool busyloop;
243 busyloop = !busyloop;
244 printf("%s busy-looping\n", busyloop ? "WARNING: enabling" : "disabling");
245 glutIdleFunc(busyloop ? idle : 0);
246 }
247 break;
249 case 'd':
250 dbg_glrender = !dbg_glrender;
251 if(dbg_glrender) {
252 printf("Debug OpenGL rendering\n");
253 } else {
254 printf("Raytracing\n");
255 }
256 glutPostRedisplay();
257 break;
259 case 'k':
260 dbg_show_kdtree = !dbg_show_kdtree;
261 if(dbg_glrender) {
262 glutPostRedisplay();
263 }
264 break;
266 case 'o':
267 dbg_show_obj = !dbg_show_obj;
268 if(dbg_glrender) {
269 glutPostRedisplay();
270 }
271 break;
273 case 's':
274 {
275 bool shadows = get_render_option_bool(ROPT_SHAD);
276 shadows = !shadows;
277 printf("%s shadows\n", shadows ? "enabling" : "disabling");
278 set_render_option(ROPT_SHAD, shadows);
279 need_update = true;
280 glutPostRedisplay();
281 }
282 break;
284 case 'r':
285 {
286 bool refl = get_render_option_bool(ROPT_REFL);
287 refl = !refl;
288 printf("%s reflections\n", refl ? "enabling" : "disabling");
289 set_render_option(ROPT_REFL, refl);
290 need_update = true;
291 glutPostRedisplay();
292 }
293 break;
295 case ']':
296 {
297 int iter = get_render_option_int(ROPT_ITER);
298 printf("setting max iterations: %d\n", iter + 1);
299 set_render_option(ROPT_ITER, iter + 1);
300 need_update = true;
301 glutPostRedisplay();
302 }
303 break;
305 case '[':
306 {
307 int iter = get_render_option_int(ROPT_ITER);
308 if(iter-- > 0) {
309 printf("setting max iterations: %d\n", iter);
310 set_render_option(ROPT_ITER, iter);
311 need_update = true;
312 glutPostRedisplay();
313 }
314 }
315 break;
317 case '`':
318 capture("shot%03d.ppm");
319 break;
321 case 't':
322 dbg_frame_time = !dbg_frame_time;
323 break;
325 default:
326 break;
327 }
328 }
330 static bool bnstate[32];
331 static int prev_x, prev_y;
333 void mouse(int bn, int state, int x, int y)
334 {
335 if(state == GLUT_DOWN) {
336 prev_x = x;
337 prev_y = y;
338 bnstate[bn] = true;
339 } else {
340 bnstate[bn] = false;
341 }
342 }
344 #define ROT_SCALE 0.5
345 #define PAN_SCALE 0.1
347 void motion(int x, int y)
348 {
349 int dx = x - prev_x;
350 int dy = y - prev_y;
351 prev_x = x;
352 prev_y = y;
354 if(bnstate[0]) {
355 cam_theta += dx * ROT_SCALE;
356 cam_phi += dy * ROT_SCALE;
358 if(cam_phi < -89) cam_phi = -89;
359 if(cam_phi > 89) cam_phi = 89;
361 need_update = true;
362 glutPostRedisplay();
363 }
364 if(bnstate[2]) {
365 cam_dist += dy * PAN_SCALE;
366 if(cam_dist < 0) cam_dist = 0;
368 need_update = true;
369 glutPostRedisplay();
370 }
371 }
373 bool capture(const char *namefmt)
374 {
375 static int num;
376 char fname[256];
378 num++;
379 snprintf(fname, sizeof fname, namefmt, num);
380 printf("saving image %s\n", fname);
382 float *pixels = new float[4 * xsz * ysz];
383 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels);
385 bool res = write_ppm("shot.ppm", pixels, xsz, ysz);
386 if(!res) {
387 num--;
388 }
389 delete [] pixels;
390 return res;
391 }
393 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
394 {
395 FILE *fp;
397 if(!(fp = fopen(fname, "wb"))) {
398 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
399 return false;
400 }
401 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
403 for(int i=0; i<xsz * ysz * 4; i++) {
404 if(i % 4 == 3) continue;
406 unsigned char c = (unsigned char)(fb[i] * 255.0);
407 fputc(c, fp);
408 }
409 fclose(fp);
410 return true;
411 }