clray
view src/clray.cc @ 43:f9eec11e5acc
shoehorned the kdtree into an opnecl image and improved performance slightly
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 28 Aug 2010 09:38:49 +0100 |
parents | 057b8575a1c1 |
children | b5eb404af481 |
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"
16 void cleanup();
17 void disp();
18 void reshape(int x, int y);
19 void keyb(unsigned char key, int x, int y);
20 void mouse(int bn, int status, int x, int y);
21 void motion(int x, int y);
22 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
24 static int xsz, ysz;
25 static bool need_update = true;
27 static float cam_theta, cam_phi = 25.0;
28 static float cam_dist = 10.0;
30 static bool dbg_glrender = false;
31 static bool dbg_show_kdtree = false;
32 static bool dbg_show_obj = true;
34 static Scene scn;
35 static unsigned int tex;
38 int main(int argc, char **argv)
39 {
40 glutInitWindowSize(800, 600);
41 glutInit(&argc, argv);
43 int loaded = 0;
44 for(int i=1; i<argc; i++) {
45 if(argv[i][0] == '-' && argv[i][2] == 0) {
46 switch(argv[i][1]) {
47 case 'i':
48 if(!argv[++i] || !isdigit(argv[i][0])) {
49 fprintf(stderr, "-i must be followed by the intersection cost\n");
50 return 1;
51 }
53 set_accel_param(ACCEL_PARAM_COST_INTERSECT, atoi(argv[i]));
54 break;
56 case 't':
57 if(!argv[++i] || !isdigit(argv[i][0])) {
58 fprintf(stderr, "-t must be followed by the traversal cost\n");
59 return 1;
60 }
62 set_accel_param(ACCEL_PARAM_COST_TRAVERSE, atoi(argv[i]));
63 break;
65 case 'c':
66 if(!argv[++i] || !isdigit(argv[i][0])) {
67 fprintf(stderr, "-c must be followed by the max number of items per leaf node\n");
68 return 1;
69 }
71 set_accel_param(ACCEL_PARAM_MAX_NODE_ITEMS, atoi(argv[i]));
72 break;
74 default:
75 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
76 return 1;
77 }
78 } else {
79 if(!scn.load(argv[i])) {
80 fprintf(stderr, "failed to load scene: %s\n", argv[i]);
81 return false;
82 }
83 loaded++;
84 }
85 }
87 if(!loaded) {
88 fprintf(stderr, "you must specify a scene file to load\n");
89 return false;
90 }
91 if(!scn.get_num_faces()) {
92 fprintf(stderr, "didn't load any polygons\n");
93 return false;
94 }
96 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
97 glutCreateWindow("OpenCL Raytracer");
99 xsz = glutGet(GLUT_WINDOW_WIDTH);
100 ysz = glutGet(GLUT_WINDOW_HEIGHT);
102 glutDisplayFunc(disp);
103 glutReshapeFunc(reshape);
104 glutKeyboardFunc(keyb);
105 glutMouseFunc(mouse);
106 glutMotionFunc(motion);
108 unsigned int *test_pattern = new unsigned int[xsz * ysz];
109 for(int i=0; i<ysz; i++) {
110 for(int j=0; j<xsz; j++) {
111 test_pattern[i * xsz + j] = ((i >> 4) & 1) == ((j >> 4) & 1) ? 0xff0000 : 0xff00;
112 }
113 }
115 glGenTextures(1, &tex);
116 glBindTexture(GL_TEXTURE_2D, tex);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
121 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, test_pattern);
122 delete [] test_pattern;
124 if(!init_opencl()) {
125 return 1;
126 }
128 if(!init_renderer(xsz, ysz, &scn, tex)) {
129 return 1;
130 }
131 atexit(cleanup);
133 glutMainLoop();
134 return 0;
135 }
137 void cleanup()
138 {
139 printf("destroying renderer ...\n");
140 destroy_renderer();
142 printf("shutting down OpenCL ...\n");
143 destroy_opencl();
145 printf("cleaning up OpenGL resources ...\n");
146 glDeleteTextures(1, &tex);
147 }
149 static Matrix4x4 mat, inv_mat, inv_trans;
151 void disp()
152 {
153 glMatrixMode(GL_MODELVIEW);
154 glLoadIdentity();
156 if(need_update) {
157 glPushMatrix();
158 glRotatef(-cam_theta, 0, 1, 0);
159 glRotatef(-cam_phi, 1, 0, 0);
160 glTranslatef(0, 0, cam_dist);
162 glGetFloatv(GL_MODELVIEW_MATRIX, mat.m);
164 inv_mat = mat;
165 inv_mat.invert();
167 /*inv_trans = inv_mat;
168 inv_trans.transpose();*/
169 inv_trans = mat;
170 inv_trans.m[3] = inv_trans.m[7] = inv_trans.m[11] = 0.0;
171 inv_trans.m[12] = inv_trans.m[13] = inv_trans.m[14] = 0.0;
172 inv_trans.m[15] = 1.0;
174 set_xform(mat.m, inv_trans.m);
175 glPopMatrix();
177 if(!dbg_glrender) {
178 if(!render()) {
179 exit(1);
180 }
181 need_update = false;
182 }
183 }
185 if(dbg_glrender) {
186 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
187 glLoadMatrixf(inv_mat.m);
188 dbg_render_gl(&scn, dbg_show_kdtree, dbg_show_obj);
189 } else {
190 glEnable(GL_TEXTURE_2D);
191 glDisable(GL_LIGHTING);
193 glBegin(GL_QUADS);
194 glColor3f(1, 1, 1);
195 glTexCoord2f(0, 1); glVertex2f(-1, -1);
196 glTexCoord2f(1, 1); glVertex2f(1, -1);
197 glTexCoord2f(1, 0); glVertex2f(1, 1);
198 glTexCoord2f(0, 0); glVertex2f(-1, 1);
199 glEnd();
201 glDisable(GL_TEXTURE_2D);
202 }
204 glutSwapBuffers();
205 }
207 void reshape(int x, int y)
208 {
209 glViewport(0, 0, x, y);
211 /* reallocate the framebuffer */
212 /*delete [] fb;
213 fb = new float[x * y * 4];
214 set_framebuffer(fb, x, y);*/
215 }
217 void keyb(unsigned char key, int x, int y)
218 {
219 switch(key) {
220 case 27:
221 exit(0);
223 case 'r':
224 need_update = true;
225 glutPostRedisplay();
226 break;
228 case 'd':
229 dbg_glrender = !dbg_glrender;
230 if(dbg_glrender) {
231 printf("Debug OpenGL rendering\n");
232 } else {
233 printf("Raytracing\n");
234 }
235 glutPostRedisplay();
236 break;
238 case 'k':
239 dbg_show_kdtree = !dbg_show_kdtree;
240 if(dbg_glrender) {
241 glutPostRedisplay();
242 }
243 break;
245 case 'o':
246 dbg_show_obj = !dbg_show_obj;
247 if(dbg_glrender) {
248 glutPostRedisplay();
249 }
250 break;
252 default:
253 break;
254 }
255 }
257 static bool bnstate[32];
258 static int prev_x, prev_y;
260 void mouse(int bn, int state, int x, int y)
261 {
262 if(state == GLUT_DOWN) {
263 prev_x = x;
264 prev_y = y;
265 bnstate[bn] = true;
266 } else {
267 bnstate[bn] = false;
268 }
269 }
271 #define ROT_SCALE 0.5
272 #define PAN_SCALE 0.1
274 void motion(int x, int y)
275 {
276 int dx = x - prev_x;
277 int dy = y - prev_y;
278 prev_x = x;
279 prev_y = y;
281 if(bnstate[0]) {
282 cam_theta += dx * ROT_SCALE;
283 cam_phi += dy * ROT_SCALE;
285 if(cam_phi < -89) cam_phi = -89;
286 if(cam_phi > 89) cam_phi = 89;
288 need_update = true;
289 glutPostRedisplay();
290 }
291 if(bnstate[2]) {
292 cam_dist += dy * PAN_SCALE;
293 if(cam_dist < 0) cam_dist = 0;
295 need_update = true;
296 glutPostRedisplay();
297 }
298 }
300 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
301 {
302 FILE *fp;
304 if(!(fp = fopen(fname, "wb"))) {
305 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
306 return false;
307 }
308 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
310 for(int i=0; i<xsz * ysz * 4; i++) {
311 if(i % 4 == 3) continue;
313 unsigned char c = (unsigned char)(fb[i] * 255.0);
314 fputc(c, fp);
315 }
316 fclose(fp);
317 return true;
318 }