clray
view src/clray.cc @ 28:97cfd9675310
trying to pass the kdtree to the kernel
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 21 Aug 2010 03:42:49 +0100 |
parents | 6c44e4b1726d |
children | 353d80127627 |
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"
15 void cleanup();
16 void disp();
17 void reshape(int x, int y);
18 void keyb(unsigned char key, int x, int y);
19 void mouse(int bn, int status, int x, int y);
20 void motion(int x, int y);
21 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
23 static int xsz, ysz;
24 static bool need_update = true;
26 static float cam_theta, cam_phi = 25.0;
27 static float cam_dist = 10.0;
29 static bool dbg_glrender = true;
30 static bool dbg_show_kdtree = false;
31 static bool dbg_show_obj = true;
33 static Scene scn;
35 int main(int argc, char **argv)
36 {
37 glutInitWindowSize(800, 600);
38 glutInit(&argc, argv);
40 int loaded = 0;
41 for(int i=1; i<argc; i++) {
42 if(argv[i][0] == '-' && argv[i][2] == 0) {
43 switch(argv[i][1]) {
44 case 'i':
45 if(!argv[++i] || !isdigit(argv[i][0])) {
46 fprintf(stderr, "-i must be followed by the intersection cost\n");
47 return 1;
48 }
50 set_accel_param(ACCEL_PARAM_COST_INTERSECT, atoi(argv[i]));
51 break;
53 case 't':
54 if(!argv[++i] || !isdigit(argv[i][0])) {
55 fprintf(stderr, "-t must be followed by the traversal cost\n");
56 return 1;
57 }
59 set_accel_param(ACCEL_PARAM_COST_TRAVERSE, atoi(argv[i]));
60 break;
62 case 'c':
63 if(!argv[++i] || !isdigit(argv[i][0])) {
64 fprintf(stderr, "-c must be followed by the max number of items per leaf node\n");
65 return 1;
66 }
68 set_accel_param(ACCEL_PARAM_MAX_NODE_ITEMS, atoi(argv[i]));
69 break;
71 default:
72 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
73 return 1;
74 }
75 } else {
76 if(!scn.load(argv[i])) {
77 fprintf(stderr, "failed to load scene: %s\n", argv[i]);
78 return false;
79 }
80 loaded++;
81 }
82 }
84 if(!loaded) {
85 fprintf(stderr, "you must specify a scene file to load\n");
86 return false;
87 }
88 if(!scn.get_num_faces()) {
89 fprintf(stderr, "didn't load any polygons\n");
90 return false;
91 }
93 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
94 glutCreateWindow("OpenCL Raytracer");
96 xsz = glutGet(GLUT_WINDOW_WIDTH);
97 ysz = glutGet(GLUT_WINDOW_HEIGHT);
99 glutDisplayFunc(disp);
100 glutReshapeFunc(reshape);
101 glutKeyboardFunc(keyb);
102 glutMouseFunc(mouse);
103 glutMotionFunc(motion);
105 if(!init_renderer(xsz, ysz, &scn)) {
106 return 1;
107 }
108 atexit(cleanup);
110 if(!scn.build_kdtree()) {
111 return 1;
112 }
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, 4, xsz, ysz, 0, GL_RGBA, GL_FLOAT, 0);
123 glutMainLoop();
124 return 0;
125 }
127 void cleanup()
128 {
129 destroy_renderer();
130 }
132 static Matrix4x4 mat, inv_mat, inv_trans;
134 void disp()
135 {
136 glMatrixMode(GL_MODELVIEW);
137 glLoadIdentity();
139 if(need_update) {
140 glPushMatrix();
141 glRotatef(-cam_theta, 0, 1, 0);
142 glRotatef(-cam_phi, 1, 0, 0);
143 glTranslatef(0, 0, cam_dist);
145 glGetFloatv(GL_MODELVIEW_MATRIX, mat.m);
147 inv_mat = mat;
148 inv_mat.invert();
150 /*inv_trans = inv_mat;
151 inv_trans.transpose();*/
152 inv_trans = mat;
153 inv_trans.m[3] = inv_trans.m[7] = inv_trans.m[11] = 0.0;
154 inv_trans.m[12] = inv_trans.m[13] = inv_trans.m[14] = 0.0;
155 inv_trans.m[15] = 1.0;
157 set_xform(mat.m, inv_trans.m);
158 glPopMatrix();
160 if(!dbg_glrender) {
161 if(!render()) {
162 exit(1);
163 }
164 need_update = false;
165 }
166 }
168 if(dbg_glrender) {
169 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
170 glLoadMatrixf(inv_mat.m);
171 dbg_render_gl(&scn, dbg_show_kdtree, dbg_show_obj);
172 } else {
173 glEnable(GL_TEXTURE_2D);
174 glDisable(GL_LIGHTING);
176 glBegin(GL_QUADS);
177 glColor3f(1, 1, 1);
178 glTexCoord2f(0, 1); glVertex2f(-1, -1);
179 glTexCoord2f(1, 1); glVertex2f(1, -1);
180 glTexCoord2f(1, 0); glVertex2f(1, 1);
181 glTexCoord2f(0, 0); glVertex2f(-1, 1);
182 glEnd();
184 glDisable(GL_TEXTURE_2D);
185 }
187 glutSwapBuffers();
188 }
190 void reshape(int x, int y)
191 {
192 glViewport(0, 0, x, y);
194 /* reallocate the framebuffer */
195 /*delete [] fb;
196 fb = new float[x * y * 4];
197 set_framebuffer(fb, x, y);*/
198 }
200 void keyb(unsigned char key, int x, int y)
201 {
202 switch(key) {
203 case 27:
204 exit(0);
206 case 'r':
207 need_update = true;
208 glutPostRedisplay();
209 break;
211 case 'd':
212 dbg_glrender = !dbg_glrender;
213 if(dbg_glrender) {
214 printf("Debug OpenGL rendering\n");
215 } else {
216 printf("Raytracing\n");
217 }
218 glutPostRedisplay();
219 break;
221 case 'k':
222 dbg_show_kdtree = !dbg_show_kdtree;
223 if(dbg_glrender) {
224 glutPostRedisplay();
225 }
226 break;
228 case 'o':
229 dbg_show_obj = !dbg_show_obj;
230 if(dbg_glrender) {
231 glutPostRedisplay();
232 }
233 break;
235 default:
236 break;
237 }
238 }
240 static bool bnstate[32];
241 static int prev_x, prev_y;
243 void mouse(int bn, int state, int x, int y)
244 {
245 if(state == GLUT_DOWN) {
246 prev_x = x;
247 prev_y = y;
248 bnstate[bn] = true;
249 } else {
250 bnstate[bn] = false;
251 }
252 }
254 #define ROT_SCALE 0.5
255 #define PAN_SCALE 0.1
257 void motion(int x, int y)
258 {
259 int dx = x - prev_x;
260 int dy = y - prev_y;
261 prev_x = x;
262 prev_y = y;
264 if(bnstate[0]) {
265 cam_theta += dx * ROT_SCALE;
266 cam_phi += dy * ROT_SCALE;
268 if(cam_phi < -89) cam_phi = -89;
269 if(cam_phi > 89) cam_phi = 89;
271 need_update = true;
272 glutPostRedisplay();
273 }
274 if(bnstate[2]) {
275 cam_dist += dy * PAN_SCALE;
276 if(cam_dist < 0) cam_dist = 0;
278 need_update = true;
279 glutPostRedisplay();
280 }
281 }
283 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
284 {
285 FILE *fp;
287 if(!(fp = fopen(fname, "wb"))) {
288 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
289 return false;
290 }
291 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
293 for(int i=0; i<xsz * ysz * 4; i++) {
294 if(i % 4 == 3) continue;
296 unsigned char c = (unsigned char)(fb[i] * 255.0);
297 fputc(c, fp);
298 }
299 fclose(fp);
300 return true;
301 }