clray
view src/clray.cc @ 41:057b8575a1c1
- changed the membuffer into an imagebuffer for the non-GL/CL-interop case
- fixed the segfault
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 27 Aug 2010 20:39:55 +0100 |
parents | 1bcbb53b3505 |
children | f9eec11e5acc |
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("cleaning up OpenGL resources ...\n");
145 glDeleteTextures(1, &tex);
146 }
148 static Matrix4x4 mat, inv_mat, inv_trans;
150 void disp()
151 {
152 glMatrixMode(GL_MODELVIEW);
153 glLoadIdentity();
155 if(need_update) {
156 glPushMatrix();
157 glRotatef(-cam_theta, 0, 1, 0);
158 glRotatef(-cam_phi, 1, 0, 0);
159 glTranslatef(0, 0, cam_dist);
161 glGetFloatv(GL_MODELVIEW_MATRIX, mat.m);
163 inv_mat = mat;
164 inv_mat.invert();
166 /*inv_trans = inv_mat;
167 inv_trans.transpose();*/
168 inv_trans = mat;
169 inv_trans.m[3] = inv_trans.m[7] = inv_trans.m[11] = 0.0;
170 inv_trans.m[12] = inv_trans.m[13] = inv_trans.m[14] = 0.0;
171 inv_trans.m[15] = 1.0;
173 set_xform(mat.m, inv_trans.m);
174 glPopMatrix();
176 if(!dbg_glrender) {
177 if(!render()) {
178 exit(1);
179 }
180 need_update = false;
181 }
182 }
184 if(dbg_glrender) {
185 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
186 glLoadMatrixf(inv_mat.m);
187 dbg_render_gl(&scn, dbg_show_kdtree, dbg_show_obj);
188 } else {
189 glEnable(GL_TEXTURE_2D);
190 glDisable(GL_LIGHTING);
192 glBegin(GL_QUADS);
193 glColor3f(1, 1, 1);
194 glTexCoord2f(0, 1); glVertex2f(-1, -1);
195 glTexCoord2f(1, 1); glVertex2f(1, -1);
196 glTexCoord2f(1, 0); glVertex2f(1, 1);
197 glTexCoord2f(0, 0); glVertex2f(-1, 1);
198 glEnd();
200 glDisable(GL_TEXTURE_2D);
201 }
203 glutSwapBuffers();
204 }
206 void reshape(int x, int y)
207 {
208 glViewport(0, 0, x, y);
210 /* reallocate the framebuffer */
211 /*delete [] fb;
212 fb = new float[x * y * 4];
213 set_framebuffer(fb, x, y);*/
214 }
216 void keyb(unsigned char key, int x, int y)
217 {
218 switch(key) {
219 case 27:
220 exit(0);
222 case 'r':
223 need_update = true;
224 glutPostRedisplay();
225 break;
227 case 'd':
228 dbg_glrender = !dbg_glrender;
229 if(dbg_glrender) {
230 printf("Debug OpenGL rendering\n");
231 } else {
232 printf("Raytracing\n");
233 }
234 glutPostRedisplay();
235 break;
237 case 'k':
238 dbg_show_kdtree = !dbg_show_kdtree;
239 if(dbg_glrender) {
240 glutPostRedisplay();
241 }
242 break;
244 case 'o':
245 dbg_show_obj = !dbg_show_obj;
246 if(dbg_glrender) {
247 glutPostRedisplay();
248 }
249 break;
251 default:
252 break;
253 }
254 }
256 static bool bnstate[32];
257 static int prev_x, prev_y;
259 void mouse(int bn, int state, int x, int y)
260 {
261 if(state == GLUT_DOWN) {
262 prev_x = x;
263 prev_y = y;
264 bnstate[bn] = true;
265 } else {
266 bnstate[bn] = false;
267 }
268 }
270 #define ROT_SCALE 0.5
271 #define PAN_SCALE 0.1
273 void motion(int x, int y)
274 {
275 int dx = x - prev_x;
276 int dy = y - prev_y;
277 prev_x = x;
278 prev_y = y;
280 if(bnstate[0]) {
281 cam_theta += dx * ROT_SCALE;
282 cam_phi += dy * ROT_SCALE;
284 if(cam_phi < -89) cam_phi = -89;
285 if(cam_phi > 89) cam_phi = 89;
287 need_update = true;
288 glutPostRedisplay();
289 }
290 if(bnstate[2]) {
291 cam_dist += dy * PAN_SCALE;
292 if(cam_dist < 0) cam_dist = 0;
294 need_update = true;
295 glutPostRedisplay();
296 }
297 }
299 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
300 {
301 FILE *fp;
303 if(!(fp = fopen(fname, "wb"))) {
304 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
305 return false;
306 }
307 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
309 for(int i=0; i<xsz * ysz * 4; i++) {
310 if(i % 4 == 3) continue;
312 unsigned char c = (unsigned char)(fb[i] * 255.0);
313 fputc(c, fp);
314 }
315 fclose(fp);
316 return true;
317 }