clray
view src/clray.cc @ 39:980bc07be868
Implemented OpenGL/OpenCL interop, and removed the texture copy
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 27 Aug 2010 18:30:09 +0100 |
parents | a218551293ad |
children | 1bcbb53b3505 |
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 destroy_renderer();
139 }
141 static Matrix4x4 mat, inv_mat, inv_trans;
143 void disp()
144 {
145 glMatrixMode(GL_MODELVIEW);
146 glLoadIdentity();
148 if(need_update) {
149 glPushMatrix();
150 glRotatef(-cam_theta, 0, 1, 0);
151 glRotatef(-cam_phi, 1, 0, 0);
152 glTranslatef(0, 0, cam_dist);
154 glGetFloatv(GL_MODELVIEW_MATRIX, mat.m);
156 inv_mat = mat;
157 inv_mat.invert();
159 /*inv_trans = inv_mat;
160 inv_trans.transpose();*/
161 inv_trans = mat;
162 inv_trans.m[3] = inv_trans.m[7] = inv_trans.m[11] = 0.0;
163 inv_trans.m[12] = inv_trans.m[13] = inv_trans.m[14] = 0.0;
164 inv_trans.m[15] = 1.0;
166 set_xform(mat.m, inv_trans.m);
167 glPopMatrix();
169 if(!dbg_glrender) {
170 if(!render()) {
171 exit(1);
172 }
173 need_update = false;
174 }
175 }
177 if(dbg_glrender) {
178 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
179 glLoadMatrixf(inv_mat.m);
180 dbg_render_gl(&scn, dbg_show_kdtree, dbg_show_obj);
181 } else {
182 glEnable(GL_TEXTURE_2D);
183 glDisable(GL_LIGHTING);
185 glBegin(GL_QUADS);
186 glColor3f(1, 1, 1);
187 glTexCoord2f(0, 1); glVertex2f(-1, -1);
188 glTexCoord2f(1, 1); glVertex2f(1, -1);
189 glTexCoord2f(1, 0); glVertex2f(1, 1);
190 glTexCoord2f(0, 0); glVertex2f(-1, 1);
191 glEnd();
193 glDisable(GL_TEXTURE_2D);
194 }
196 glutSwapBuffers();
197 }
199 void reshape(int x, int y)
200 {
201 glViewport(0, 0, x, y);
203 /* reallocate the framebuffer */
204 /*delete [] fb;
205 fb = new float[x * y * 4];
206 set_framebuffer(fb, x, y);*/
207 }
209 void keyb(unsigned char key, int x, int y)
210 {
211 switch(key) {
212 case 27:
213 exit(0);
215 case 'r':
216 need_update = true;
217 glutPostRedisplay();
218 break;
220 case 'd':
221 dbg_glrender = !dbg_glrender;
222 if(dbg_glrender) {
223 printf("Debug OpenGL rendering\n");
224 } else {
225 printf("Raytracing\n");
226 }
227 glutPostRedisplay();
228 break;
230 case 'k':
231 dbg_show_kdtree = !dbg_show_kdtree;
232 if(dbg_glrender) {
233 glutPostRedisplay();
234 }
235 break;
237 case 'o':
238 dbg_show_obj = !dbg_show_obj;
239 if(dbg_glrender) {
240 glutPostRedisplay();
241 }
242 break;
244 default:
245 break;
246 }
247 }
249 static bool bnstate[32];
250 static int prev_x, prev_y;
252 void mouse(int bn, int state, int x, int y)
253 {
254 if(state == GLUT_DOWN) {
255 prev_x = x;
256 prev_y = y;
257 bnstate[bn] = true;
258 } else {
259 bnstate[bn] = false;
260 }
261 }
263 #define ROT_SCALE 0.5
264 #define PAN_SCALE 0.1
266 void motion(int x, int y)
267 {
268 int dx = x - prev_x;
269 int dy = y - prev_y;
270 prev_x = x;
271 prev_y = y;
273 if(bnstate[0]) {
274 cam_theta += dx * ROT_SCALE;
275 cam_phi += dy * ROT_SCALE;
277 if(cam_phi < -89) cam_phi = -89;
278 if(cam_phi > 89) cam_phi = 89;
280 need_update = true;
281 glutPostRedisplay();
282 }
283 if(bnstate[2]) {
284 cam_dist += dy * PAN_SCALE;
285 if(cam_dist < 0) cam_dist = 0;
287 need_update = true;
288 glutPostRedisplay();
289 }
290 }
292 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
293 {
294 FILE *fp;
296 if(!(fp = fopen(fname, "wb"))) {
297 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
298 return false;
299 }
300 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
302 for(int i=0; i<xsz * ysz * 4; i++) {
303 if(i % 4 == 3) continue;
305 unsigned char c = (unsigned char)(fb[i] * 255.0);
306 fputc(c, fp);
307 }
308 fclose(fp);
309 return true;
310 }