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