clray

annotate src/clray.cc @ 61:0f174fd60f19

added readme file and license
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Dec 2015 10:27:23 +0200
parents 14c8ebe8f122
children
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"
John@49 15 #include "ogl.h"
John@49 16
John@49 17 #ifdef _MSC_VER
John@49 18 #define snprintf _snprintf
John@49 19 #endif
nuclear@0 20
nuclear@3 21 void cleanup();
nuclear@3 22 void disp();
nuclear@3 23 void reshape(int x, int y);
nuclear@3 24 void keyb(unsigned char key, int x, int y);
nuclear@3 25 void mouse(int bn, int status, int x, int y);
nuclear@3 26 void motion(int x, int y);
nuclear@47 27 bool capture(const char *namefmt);
nuclear@2 28 bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
nuclear@2 29
nuclear@3 30 static int xsz, ysz;
nuclear@3 31 static bool need_update = true;
nuclear@3 32
nuclear@8 33 static float cam_theta, cam_phi = 25.0;
nuclear@8 34 static float cam_dist = 10.0;
nuclear@8 35
nuclear@54 36 static bool dbg_glrender;
nuclear@54 37 static bool dbg_nocl;
nuclear@54 38 static bool dbg_show_kdtree;
nuclear@27 39 static bool dbg_show_obj = true;
John@50 40 bool dbg_frame_time = true;
nuclear@13 41
nuclear@13 42 static Scene scn;
nuclear@39 43 static unsigned int tex;
nuclear@12 44
nuclear@54 45 static Light lightlist[] = {
nuclear@54 46 {{-8, 15, 18, 0}, {1, 1, 1, 1}}
nuclear@54 47 };
nuclear@54 48
nuclear@54 49
nuclear@54 50
nuclear@3 51 int main(int argc, char **argv)
nuclear@0 52 {
nuclear@3 53 glutInitWindowSize(800, 600);
nuclear@3 54 glutInit(&argc, argv);
nuclear@13 55
nuclear@13 56 int loaded = 0;
nuclear@13 57 for(int i=1; i<argc; i++) {
nuclear@27 58 if(argv[i][0] == '-' && argv[i][2] == 0) {
nuclear@27 59 switch(argv[i][1]) {
nuclear@27 60 case 'i':
nuclear@27 61 if(!argv[++i] || !isdigit(argv[i][0])) {
nuclear@27 62 fprintf(stderr, "-i must be followed by the intersection cost\n");
nuclear@27 63 return 1;
nuclear@27 64 }
nuclear@27 65
nuclear@27 66 set_accel_param(ACCEL_PARAM_COST_INTERSECT, atoi(argv[i]));
nuclear@27 67 break;
nuclear@27 68
nuclear@27 69 case 't':
nuclear@27 70 if(!argv[++i] || !isdigit(argv[i][0])) {
nuclear@27 71 fprintf(stderr, "-t must be followed by the traversal cost\n");
nuclear@27 72 return 1;
nuclear@27 73 }
nuclear@27 74
nuclear@27 75 set_accel_param(ACCEL_PARAM_COST_TRAVERSE, atoi(argv[i]));
nuclear@27 76 break;
nuclear@27 77
nuclear@27 78 case 'c':
nuclear@27 79 if(!argv[++i] || !isdigit(argv[i][0])) {
nuclear@27 80 fprintf(stderr, "-c must be followed by the max number of items per leaf node\n");
nuclear@27 81 return 1;
nuclear@27 82 }
nuclear@27 83
nuclear@27 84 set_accel_param(ACCEL_PARAM_MAX_NODE_ITEMS, atoi(argv[i]));
nuclear@27 85 break;
nuclear@27 86
nuclear@46 87 case 'd':
nuclear@46 88 dbg_glrender = true;
nuclear@46 89 break;
nuclear@46 90
nuclear@54 91 case 'n':
nuclear@54 92 dbg_nocl = true;
nuclear@54 93 break;
nuclear@54 94
nuclear@27 95 default:
nuclear@27 96 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
nuclear@27 97 return 1;
nuclear@27 98 }
nuclear@27 99 } else {
nuclear@27 100 if(!scn.load(argv[i])) {
nuclear@27 101 fprintf(stderr, "failed to load scene: %s\n", argv[i]);
nuclear@27 102 return false;
nuclear@27 103 }
nuclear@27 104 loaded++;
nuclear@13 105 }
nuclear@13 106 }
nuclear@13 107
nuclear@13 108 if(!loaded) {
nuclear@13 109 fprintf(stderr, "you must specify a scene file to load\n");
nuclear@13 110 return false;
nuclear@13 111 }
nuclear@13 112 if(!scn.get_num_faces()) {
nuclear@13 113 fprintf(stderr, "didn't load any polygons\n");
nuclear@13 114 return false;
nuclear@13 115 }
nuclear@13 116
nuclear@54 117 int num_lights = sizeof lightlist / sizeof *lightlist;
nuclear@54 118 for(int i=0; i<num_lights; i++) {
nuclear@54 119 scn.add_light(lightlist[i]);
nuclear@54 120 }
nuclear@54 121
nuclear@18 122 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@3 123 glutCreateWindow("OpenCL Raytracer");
nuclear@0 124
nuclear@3 125 xsz = glutGet(GLUT_WINDOW_WIDTH);
nuclear@3 126 ysz = glutGet(GLUT_WINDOW_HEIGHT);
nuclear@2 127
nuclear@3 128 glutDisplayFunc(disp);
nuclear@3 129 glutReshapeFunc(reshape);
nuclear@3 130 glutKeyboardFunc(keyb);
nuclear@3 131 glutMouseFunc(mouse);
nuclear@3 132 glutMotionFunc(motion);
nuclear@0 133
nuclear@32 134 unsigned int *test_pattern = new unsigned int[xsz * ysz];
nuclear@32 135 for(int i=0; i<ysz; i++) {
nuclear@32 136 for(int j=0; j<xsz; j++) {
nuclear@32 137 test_pattern[i * xsz + j] = ((i >> 4) & 1) == ((j >> 4) & 1) ? 0xff0000 : 0xff00;
nuclear@32 138 }
nuclear@32 139 }
nuclear@27 140
nuclear@39 141 glGenTextures(1, &tex);
nuclear@39 142 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@3 143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
nuclear@3 144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
nuclear@3 145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@3 146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@39 147 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, test_pattern);
nuclear@32 148 delete [] test_pattern;
nuclear@2 149
nuclear@39 150 if(!init_opencl()) {
nuclear@39 151 return 1;
nuclear@39 152 }
nuclear@39 153
nuclear@39 154 if(!init_renderer(xsz, ysz, &scn, tex)) {
nuclear@39 155 return 1;
nuclear@39 156 }
nuclear@39 157 atexit(cleanup);
nuclear@39 158
nuclear@3 159 glutMainLoop();
nuclear@0 160 return 0;
nuclear@0 161 }
nuclear@2 162
nuclear@3 163 void cleanup()
nuclear@2 164 {
nuclear@41 165 printf("destroying renderer ...\n");
nuclear@3 166 destroy_renderer();
nuclear@40 167
nuclear@41 168 printf("shutting down OpenCL ...\n");
nuclear@40 169 destroy_opencl();
nuclear@40 170
nuclear@41 171 printf("cleaning up OpenGL resources ...\n");
nuclear@41 172 glDeleteTextures(1, &tex);
nuclear@3 173 }
nuclear@2 174
nuclear@12 175 static Matrix4x4 mat, inv_mat, inv_trans;
nuclear@12 176
nuclear@3 177 void disp()
nuclear@3 178 {
nuclear@8 179 glMatrixMode(GL_MODELVIEW);
nuclear@8 180 glLoadIdentity();
nuclear@8 181
nuclear@3 182 if(need_update) {
nuclear@12 183 glPushMatrix();
nuclear@12 184 glRotatef(-cam_theta, 0, 1, 0);
nuclear@12 185 glRotatef(-cam_phi, 1, 0, 0);
nuclear@12 186 glTranslatef(0, 0, cam_dist);
nuclear@8 187
nuclear@12 188 glGetFloatv(GL_MODELVIEW_MATRIX, mat.m);
nuclear@8 189
nuclear@12 190 inv_trans = mat;
nuclear@12 191 inv_trans.m[3] = inv_trans.m[7] = inv_trans.m[11] = 0.0;
nuclear@12 192 inv_trans.m[12] = inv_trans.m[13] = inv_trans.m[14] = 0.0;
nuclear@12 193 inv_trans.m[15] = 1.0;
nuclear@12 194
nuclear@12 195 set_xform(mat.m, inv_trans.m);
nuclear@8 196 glPopMatrix();
nuclear@8 197
nuclear@13 198 if(!dbg_glrender) {
nuclear@54 199 if(dbg_nocl) {
nuclear@54 200 dbg_render(mat.m, inv_trans.m);
nuclear@55 201 print_render_stats();
nuclear@54 202 } else {
nuclear@54 203 if(!render()) {
nuclear@54 204 exit(1);
nuclear@54 205 }
nuclear@58 206
nuclear@58 207 if(dbg_frame_time) {
nuclear@58 208 const RenderStats *rstat = get_render_stats();
nuclear@58 209 printf("render time (msec): %lu\n", rstat->render_time);
nuclear@58 210 }
nuclear@13 211 }
nuclear@13 212 need_update = false;
nuclear@12 213 }
nuclear@3 214 }
nuclear@2 215
nuclear@12 216 if(dbg_glrender) {
nuclear@54 217 inv_mat = mat;
nuclear@54 218 inv_mat.invert();
nuclear@54 219
nuclear@12 220 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@12 221 glLoadMatrixf(inv_mat.m);
nuclear@27 222 dbg_render_gl(&scn, dbg_show_kdtree, dbg_show_obj);
nuclear@12 223 } else {
nuclear@12 224 glEnable(GL_TEXTURE_2D);
nuclear@22 225 glDisable(GL_LIGHTING);
nuclear@2 226
nuclear@12 227 glBegin(GL_QUADS);
nuclear@12 228 glColor3f(1, 1, 1);
nuclear@12 229 glTexCoord2f(0, 1); glVertex2f(-1, -1);
nuclear@12 230 glTexCoord2f(1, 1); glVertex2f(1, -1);
nuclear@12 231 glTexCoord2f(1, 0); glVertex2f(1, 1);
nuclear@12 232 glTexCoord2f(0, 0); glVertex2f(-1, 1);
nuclear@12 233 glEnd();
nuclear@2 234
nuclear@12 235 glDisable(GL_TEXTURE_2D);
nuclear@12 236 }
nuclear@3 237
nuclear@3 238 glutSwapBuffers();
nuclear@56 239
nuclear@56 240 /* We need to make sure OpenGL has finished with the texture
nuclear@56 241 * before allowing the OpenCL kernel to run again.
nuclear@56 242 */
nuclear@56 243 glFinish();
nuclear@3 244 }
nuclear@3 245
nuclear@3 246 void reshape(int x, int y)
nuclear@3 247 {
nuclear@3 248 glViewport(0, 0, x, y);
nuclear@3 249
nuclear@58 250 /* TODO reallocate the framebuffer to fit the window */
nuclear@3 251 }
nuclear@3 252
nuclear@47 253 void idle()
nuclear@47 254 {
nuclear@47 255 need_update = true;
nuclear@47 256 glutPostRedisplay();
nuclear@47 257 }
nuclear@47 258
nuclear@3 259 void keyb(unsigned char key, int x, int y)
nuclear@3 260 {
nuclear@3 261 switch(key) {
nuclear@3 262 case 27:
nuclear@3 263 exit(0);
nuclear@3 264
nuclear@47 265 case '\b':
nuclear@47 266 {
nuclear@47 267 static bool busyloop;
nuclear@47 268
nuclear@47 269 busyloop = !busyloop;
nuclear@47 270 printf("%s busy-looping\n", busyloop ? "WARNING: enabling" : "disabling");
nuclear@47 271 glutIdleFunc(busyloop ? idle : 0);
nuclear@47 272 }
nuclear@3 273 break;
nuclear@3 274
nuclear@12 275 case 'd':
nuclear@12 276 dbg_glrender = !dbg_glrender;
nuclear@12 277 if(dbg_glrender) {
nuclear@21 278 printf("Debug OpenGL rendering\n");
nuclear@21 279 } else {
nuclear@21 280 printf("Raytracing\n");
nuclear@12 281 }
nuclear@3 282 glutPostRedisplay();
nuclear@3 283 break;
nuclear@3 284
nuclear@27 285 case 'k':
nuclear@27 286 dbg_show_kdtree = !dbg_show_kdtree;
nuclear@27 287 if(dbg_glrender) {
nuclear@27 288 glutPostRedisplay();
nuclear@27 289 }
nuclear@27 290 break;
nuclear@27 291
nuclear@27 292 case 'o':
nuclear@27 293 dbg_show_obj = !dbg_show_obj;
nuclear@27 294 if(dbg_glrender) {
nuclear@27 295 glutPostRedisplay();
nuclear@27 296 }
nuclear@27 297 break;
nuclear@27 298
nuclear@47 299 case 's':
nuclear@47 300 {
nuclear@47 301 bool shadows = get_render_option_bool(ROPT_SHAD);
nuclear@47 302 shadows = !shadows;
nuclear@47 303 printf("%s shadows\n", shadows ? "enabling" : "disabling");
nuclear@47 304 set_render_option(ROPT_SHAD, shadows);
nuclear@47 305 need_update = true;
nuclear@47 306 glutPostRedisplay();
nuclear@47 307 }
nuclear@47 308 break;
nuclear@47 309
nuclear@47 310 case 'r':
nuclear@47 311 {
nuclear@47 312 bool refl = get_render_option_bool(ROPT_REFL);
nuclear@47 313 refl = !refl;
nuclear@47 314 printf("%s reflections\n", refl ? "enabling" : "disabling");
nuclear@47 315 set_render_option(ROPT_REFL, refl);
nuclear@47 316 need_update = true;
nuclear@47 317 glutPostRedisplay();
nuclear@47 318 }
nuclear@47 319 break;
nuclear@47 320
nuclear@47 321 case ']':
nuclear@47 322 {
nuclear@47 323 int iter = get_render_option_int(ROPT_ITER);
nuclear@47 324 printf("setting max iterations: %d\n", iter + 1);
nuclear@47 325 set_render_option(ROPT_ITER, iter + 1);
nuclear@47 326 need_update = true;
nuclear@47 327 glutPostRedisplay();
nuclear@47 328 }
nuclear@47 329 break;
nuclear@47 330
nuclear@47 331 case '[':
nuclear@47 332 {
nuclear@47 333 int iter = get_render_option_int(ROPT_ITER);
nuclear@47 334 if(iter-- > 0) {
nuclear@47 335 printf("setting max iterations: %d\n", iter);
nuclear@47 336 set_render_option(ROPT_ITER, iter);
nuclear@47 337 need_update = true;
nuclear@47 338 glutPostRedisplay();
nuclear@47 339 }
nuclear@47 340 }
nuclear@47 341 break;
nuclear@47 342
nuclear@47 343 case '`':
nuclear@47 344 capture("shot%03d.ppm");
nuclear@47 345 break;
nuclear@47 346
John@50 347 case 't':
John@50 348 dbg_frame_time = !dbg_frame_time;
John@50 349 break;
John@50 350
nuclear@54 351 case 'n':
nuclear@54 352 dbg_nocl = !dbg_nocl;
nuclear@54 353 printf("switching to %s rendering\n", dbg_nocl ? "debug CPU" : "OpenCL");
nuclear@54 354 need_update = true;
nuclear@54 355 glutPostRedisplay();
nuclear@54 356 break;
nuclear@54 357
nuclear@3 358 default:
nuclear@3 359 break;
nuclear@3 360 }
nuclear@3 361 }
nuclear@3 362
nuclear@8 363 static bool bnstate[32];
nuclear@8 364 static int prev_x, prev_y;
nuclear@8 365
nuclear@3 366 void mouse(int bn, int state, int x, int y)
nuclear@3 367 {
nuclear@8 368 if(state == GLUT_DOWN) {
nuclear@8 369 prev_x = x;
nuclear@8 370 prev_y = y;
nuclear@8 371 bnstate[bn] = true;
nuclear@8 372 } else {
nuclear@8 373 bnstate[bn] = false;
nuclear@8 374 }
nuclear@3 375 }
nuclear@3 376
nuclear@8 377 #define ROT_SCALE 0.5
nuclear@8 378 #define PAN_SCALE 0.1
nuclear@8 379
nuclear@3 380 void motion(int x, int y)
nuclear@3 381 {
nuclear@8 382 int dx = x - prev_x;
nuclear@8 383 int dy = y - prev_y;
nuclear@8 384 prev_x = x;
nuclear@8 385 prev_y = y;
nuclear@8 386
nuclear@8 387 if(bnstate[0]) {
nuclear@8 388 cam_theta += dx * ROT_SCALE;
nuclear@8 389 cam_phi += dy * ROT_SCALE;
nuclear@8 390
nuclear@12 391 if(cam_phi < -89) cam_phi = -89;
nuclear@8 392 if(cam_phi > 89) cam_phi = 89;
nuclear@8 393
nuclear@8 394 need_update = true;
nuclear@8 395 glutPostRedisplay();
nuclear@8 396 }
nuclear@8 397 if(bnstate[2]) {
nuclear@8 398 cam_dist += dy * PAN_SCALE;
nuclear@8 399 if(cam_dist < 0) cam_dist = 0;
nuclear@8 400
nuclear@8 401 need_update = true;
nuclear@8 402 glutPostRedisplay();
nuclear@8 403 }
nuclear@2 404 }
nuclear@2 405
nuclear@47 406 bool capture(const char *namefmt)
nuclear@47 407 {
nuclear@47 408 static int num;
nuclear@47 409 char fname[256];
nuclear@47 410
nuclear@47 411 num++;
nuclear@47 412 snprintf(fname, sizeof fname, namefmt, num);
nuclear@47 413 printf("saving image %s\n", fname);
nuclear@47 414
nuclear@47 415 float *pixels = new float[4 * xsz * ysz];
nuclear@47 416 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels);
nuclear@47 417
nuclear@52 418 bool res = write_ppm(fname, pixels, xsz, ysz);
nuclear@47 419 if(!res) {
nuclear@47 420 num--;
nuclear@47 421 }
nuclear@47 422 delete [] pixels;
nuclear@47 423 return res;
nuclear@47 424 }
nuclear@47 425
nuclear@2 426 bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
nuclear@2 427 {
nuclear@2 428 FILE *fp;
nuclear@2 429
nuclear@2 430 if(!(fp = fopen(fname, "wb"))) {
nuclear@2 431 fprintf(stderr, "write_ppm: failed to open file %s for writing: %s\n", fname, strerror(errno));
nuclear@2 432 return false;
nuclear@2 433 }
nuclear@2 434 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
nuclear@2 435
nuclear@2 436 for(int i=0; i<xsz * ysz * 4; i++) {
nuclear@2 437 if(i % 4 == 3) continue;
nuclear@2 438
nuclear@2 439 unsigned char c = (unsigned char)(fb[i] * 255.0);
nuclear@2 440 fputc(c, fp);
nuclear@2 441 }
nuclear@2 442 fclose(fp);
nuclear@2 443 return true;
nuclear@2 444 }