clray
view src/clray.cc @ 40:1bcbb53b3505
segfault on exit?
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 27 Aug 2010 19:00:14 +0100 |
parents | 980bc07be868 |
children | 057b8575a1c1 |
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;
37 int main(int argc, char **argv)
38 {
39 glutInitWindowSize(800, 600);
40 glutInit(&argc, argv);
42 int loaded = 0;
43 for(int i=1; i<argc; i++) {
44 if(argv[i][0] == '-' && argv[i][2] == 0) {
45 switch(argv[i][1]) {
46 case 'i':
47 if(!argv[++i] || !isdigit(argv[i][0])) {
48 fprintf(stderr, "-i must be followed by the intersection cost\n");
49 return 1;
50 }
52 set_accel_param(ACCEL_PARAM_COST_INTERSECT, atoi(argv[i]));
53 break;
55 case 't':
56 if(!argv[++i] || !isdigit(argv[i][0])) {
57 fprintf(stderr, "-t must be followed by the traversal cost\n");
58 return 1;
59 }
61 set_accel_param(ACCEL_PARAM_COST_TRAVERSE, atoi(argv[i]));
62 break;
64 case 'c':
65 if(!argv[++i] || !isdigit(argv[i][0])) {
66 fprintf(stderr, "-c must be followed by the max number of items per leaf node\n");
67 return 1;
68 }
70 set_accel_param(ACCEL_PARAM_MAX_NODE_ITEMS, atoi(argv[i]));
71 break;
73 default:
74 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
75 return 1;
76 }
77 } else {
78 if(!scn.load(argv[i])) {
79 fprintf(stderr, "failed to load scene: %s\n", argv[i]);
80 return false;
81 }
82 loaded++;
83 }
84 }
86 if(!loaded) {
87 fprintf(stderr, "you must specify a scene file to load\n");
88 return false;
89 }
90 if(!scn.get_num_faces()) {
91 fprintf(stderr, "didn't load any polygons\n");
92 return false;
93 }
95 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
96 glutCreateWindow("OpenCL Raytracer");
98 xsz = glutGet(GLUT_WINDOW_WIDTH);
99 ysz = glutGet(GLUT_WINDOW_HEIGHT);
101 glutDisplayFunc(disp);
102 glutReshapeFunc(reshape);
103 glutKeyboardFunc(keyb);
104 glutMouseFunc(mouse);
105 glutMotionFunc(motion);
107 unsigned int *test_pattern = new unsigned int[xsz * ysz];
108 for(int i=0; i<ysz; i++) {
109 for(int j=0; j<xsz; j++) {
110 test_pattern[i * xsz + j] = ((i >> 4) & 1) == ((j >> 4) & 1) ? 0xff0000 : 0xff00;
111 }
112 }
114 glGenTextures(1, &tex);
115 glBindTexture(GL_TEXTURE_2D, tex);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
120 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, test_pattern);
121 delete [] test_pattern;
123 if(!init_opencl()) {
124 return 1;
125 }
127 if(!init_renderer(xsz, ysz, &scn, tex)) {
128 return 1;
129 }
130 atexit(cleanup);
132 glutMainLoop();
133 return 0;
134 }
136 void cleanup()
137 {
138 printf("destroying renderer...\n");
139 destroy_renderer();
141 printf("shutting down OpenCL...\n");
142 destroy_opencl();
144 printf("done cleaning up\n");
145 }
147 static Matrix4x4 mat, inv_mat, inv_trans;
149 void disp()
150 {
151 glMatrixMode(GL_MODELVIEW);
152 glLoadIdentity();
154 if(need_update) {
155 glPushMatrix();
156 glRotatef(-cam_theta, 0, 1, 0);
157 glRotatef(-cam_phi, 1, 0, 0);
158 glTranslatef(0, 0, cam_dist);
160 glGetFloatv(GL_MODELVIEW_MATRIX, mat.m);
162 inv_mat = mat;
163 inv_mat.invert();
165 /*inv_trans = inv_mat;
166 inv_trans.transpose();*/
167 inv_trans = mat;
168 inv_trans.m[3] = inv_trans.m[7] = inv_trans.m[11] = 0.0;
169 inv_trans.m[12] = inv_trans.m[13] = inv_trans.m[14] = 0.0;
170 inv_trans.m[15] = 1.0;
172 set_xform(mat.m, inv_trans.m);
173 glPopMatrix();
175 if(!dbg_glrender) {
176 if(!render()) {
177 exit(1);
178 }
179 need_update = false;
180 }
181 }
183 if(dbg_glrender) {
184 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
185 glLoadMatrixf(inv_mat.m);
186 dbg_render_gl(&scn, dbg_show_kdtree, dbg_show_obj);
187 } else {
188 glEnable(GL_TEXTURE_2D);
189 glDisable(GL_LIGHTING);
191 glBegin(GL_QUADS);
192 glColor3f(1, 1, 1);
193 glTexCoord2f(0, 1); glVertex2f(-1, -1);
194 glTexCoord2f(1, 1); glVertex2f(1, -1);
195 glTexCoord2f(1, 0); glVertex2f(1, 1);
196 glTexCoord2f(0, 0); glVertex2f(-1, 1);
197 glEnd();
199 glDisable(GL_TEXTURE_2D);
200 }
202 glutSwapBuffers();
203 }
205 void reshape(int x, int y)
206 {
207 glViewport(0, 0, x, y);
209 /* reallocate the framebuffer */
210 /*delete [] fb;
211 fb = new float[x * y * 4];
212 set_framebuffer(fb, x, y);*/
213 }
215 void keyb(unsigned char key, int x, int y)
216 {
217 switch(key) {
218 case 27:
219 exit(0);
221 case 'r':
222 need_update = true;
223 glutPostRedisplay();
224 break;
226 case 'd':
227 dbg_glrender = !dbg_glrender;
228 if(dbg_glrender) {
229 printf("Debug OpenGL rendering\n");
230 } else {
231 printf("Raytracing\n");
232 }
233 glutPostRedisplay();
234 break;
236 case 'k':
237 dbg_show_kdtree = !dbg_show_kdtree;
238 if(dbg_glrender) {
239 glutPostRedisplay();
240 }
241 break;
243 case 'o':
244 dbg_show_obj = !dbg_show_obj;
245 if(dbg_glrender) {
246 glutPostRedisplay();
247 }
248 break;
250 default:
251 break;
252 }
253 }
255 static bool bnstate[32];
256 static int prev_x, prev_y;
258 void mouse(int bn, int state, int x, int y)
259 {
260 if(state == GLUT_DOWN) {
261 prev_x = x;
262 prev_y = y;
263 bnstate[bn] = true;
264 } else {
265 bnstate[bn] = false;
266 }
267 }
269 #define ROT_SCALE 0.5
270 #define PAN_SCALE 0.1
272 void motion(int x, int y)
273 {
274 int dx = x - prev_x;
275 int dy = y - prev_y;
276 prev_x = x;
277 prev_y = y;
279 if(bnstate[0]) {
280 cam_theta += dx * ROT_SCALE;
281 cam_phi += dy * ROT_SCALE;
283 if(cam_phi < -89) cam_phi = -89;
284 if(cam_phi > 89) cam_phi = 89;
286 need_update = true;
287 glutPostRedisplay();
288 }
289 if(bnstate[2]) {
290 cam_dist += dy * PAN_SCALE;
291 if(cam_dist < 0) cam_dist = 0;
293 need_update = true;
294 glutPostRedisplay();
295 }
296 }
298 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
299 {
300 FILE *fp;
302 if(!(fp = fopen(fname, "wb"))) {
303 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
304 return false;
305 }
306 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
308 for(int i=0; i<xsz * ysz * 4; i++) {
309 if(i % 4 == 3) continue;
311 unsigned char c = (unsigned char)(fb[i] * 255.0);
312 fputc(c, fp);
313 }
314 fclose(fp);
315 return true;
316 }