clray

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