volray

annotate src/volray.c @ 5:0c3874aa717a

slice
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 04 Apr 2012 01:37:33 +0300
parents 3e53a16d4667
children f40e4edfee7e
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <assert.h>
nuclear@0 4
nuclear@0 5 #include <GL/glew.h>
nuclear@0 6 #ifndef __APPLE__
nuclear@0 7 #include <GL/glut.h>
nuclear@0 8 #else
nuclear@0 9 #include <GLUT/glut.h>
nuclear@0 10 #endif
nuclear@0 11
nuclear@0 12 #include <vmath/vmath.h>
nuclear@0 13 #include <imago2.h>
nuclear@0 14 #include "sdr.h"
nuclear@0 15
nuclear@3 16 #define XFER_MAP_SZ 512
nuclear@3 17
nuclear@0 18 struct slice_file {
nuclear@0 19 char *name;
nuclear@0 20 struct slice_file *next;
nuclear@0 21 };
nuclear@0 22
nuclear@5 23 enum {
nuclear@5 24 UIMODE_DEFAULT,
nuclear@5 25 UIMODE_XFER,
nuclear@5 26 UIMODE_CURSOR
nuclear@5 27 };
nuclear@5 28
nuclear@0 29 int init(void);
nuclear@0 30 void disp(void);
nuclear@4 31 void render_volume(void);
nuclear@5 32 void draw_slice(void);
nuclear@4 33 void draw_xfer_func(void);
nuclear@0 34 void reshape(int x, int y);
nuclear@0 35 void keyb(unsigned char key, int x, int y);
nuclear@4 36 void keyb_up(unsigned char key, int x, int y);
nuclear@0 37 void mouse(int bn, int state, int x, int y);
nuclear@0 38 void motion(int x, int y);
nuclear@0 39 int parse_args(int argc, char **argv);
nuclear@0 40
nuclear@1 41 static void create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale);
nuclear@0 42 static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
nuclear@0 43 static int round_pow2(int x);
nuclear@3 44 static void create_transfer_map(float mean, float sdev);
nuclear@0 45
nuclear@0 46 float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
nuclear@0 47 float cam_x, cam_y, cam_z;
nuclear@0 48
nuclear@0 49 vec2_t tex_scale;
nuclear@0 50 struct slice_file *flist;
nuclear@0 51 int nslices;
nuclear@5 52 unsigned int vol_sdr, slice_sdr, vol_tex, ray_tex;
nuclear@0 53 int win_xsz, win_ysz;
nuclear@1 54 int raytex_needs_recalc = 1;
nuclear@0 55
nuclear@3 56 unsigned int xfer_tex;
nuclear@3 57 float xfer_mean = 0.5, xfer_sdev = 1.0;
nuclear@3 58 int xfertex_needs_recalc = 1;
nuclear@3 59
nuclear@5 60 static int uimode;
nuclear@5 61 static float cur_z = 0.5;
nuclear@4 62
nuclear@0 63 int main(int argc, char **argv)
nuclear@0 64 {
nuclear@0 65 glutInit(&argc, argv);
nuclear@0 66 glutInitWindowSize(1280, 720);
nuclear@0 67 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
nuclear@0 68
nuclear@0 69 if(parse_args(argc, argv) == -1) {
nuclear@0 70 return 1;
nuclear@0 71 }
nuclear@0 72
nuclear@0 73 glutCreateWindow("Volume Raytracer");
nuclear@0 74
nuclear@0 75 glutDisplayFunc(disp);
nuclear@0 76 glutReshapeFunc(reshape);
nuclear@0 77 glutKeyboardFunc(keyb);
nuclear@4 78 glutKeyboardUpFunc(keyb_up);
nuclear@0 79 glutMouseFunc(mouse);
nuclear@0 80 glutMotionFunc(motion);
nuclear@0 81
nuclear@0 82 glewInit();
nuclear@0 83
nuclear@0 84 if(init() == -1) {
nuclear@0 85 return 1;
nuclear@0 86 }
nuclear@0 87
nuclear@0 88 glutMainLoop();
nuclear@0 89 return 0;
nuclear@0 90 }
nuclear@0 91
nuclear@0 92 int init(void)
nuclear@0 93 {
nuclear@0 94 int i, vol_xsz, vol_ysz;
nuclear@0 95
nuclear@5 96 if(!(vol_sdr = create_program_load("volray.v.glsl", "volray.p.glsl"))) {
nuclear@1 97 return -1;
nuclear@0 98 }
nuclear@5 99 set_uniform_int(vol_sdr, "volume", 0);
nuclear@5 100 set_uniform_int(vol_sdr, "ray_tex", 1);
nuclear@5 101 set_uniform_int(vol_sdr, "xfer_tex", 2);
nuclear@5 102
nuclear@5 103 if(!(slice_sdr = create_program_load(0, "slice.p.glsl"))) {
nuclear@5 104 return -1;
nuclear@5 105 }
nuclear@5 106 set_uniform_int(slice_sdr, "volume", 0);
nuclear@5 107 set_uniform_int(slice_sdr, "xfer_tex", 1);
nuclear@0 108
nuclear@0 109 glGenTextures(1, &vol_tex);
nuclear@0 110 glBindTexture(GL_TEXTURE_3D, vol_tex);
nuclear@0 111 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@0 112 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@4 113 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@4 114 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@4 115 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
nuclear@0 116
nuclear@0 117 for(i=0; i<nslices; i++) {
nuclear@0 118 int xsz, ysz;
nuclear@0 119 void *pix;
nuclear@0 120 struct slice_file *sfile = flist;
nuclear@0 121 flist = flist->next;
nuclear@0 122
nuclear@0 123 if(!(pix = img_load_pixels(sfile->name, &xsz, &ysz, IMG_FMT_RGBA32))) {
nuclear@0 124 fprintf(stderr, "failed to load image: %s\n", sfile->name);
nuclear@0 125 return -1;
nuclear@0 126 }
nuclear@0 127
nuclear@0 128 if(i == 0) {
nuclear@0 129 /* allocate storage for the texture */
nuclear@0 130 glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, xsz, ysz, nslices, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
nuclear@0 131
nuclear@0 132 vol_xsz = xsz;
nuclear@0 133 vol_ysz = ysz;
nuclear@0 134
nuclear@0 135 } else {
nuclear@0 136 if(xsz != vol_xsz || ysz != vol_ysz) {
nuclear@0 137 fprintf(stderr, "%s: inconsistent slice size: %dx%d. expected: %dx%d\n",
nuclear@0 138 sfile->name, xsz, ysz, vol_xsz, vol_ysz);
nuclear@0 139 img_free_pixels(pix);
nuclear@0 140 return -1;
nuclear@0 141 }
nuclear@0 142 }
nuclear@0 143 free(sfile);
nuclear@0 144
nuclear@0 145 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_UNSIGNED_BYTE, pix);
nuclear@0 146 img_free_pixels(pix);
nuclear@0 147 }
nuclear@0 148 return 0;
nuclear@0 149 }
nuclear@0 150
nuclear@0 151 void disp(void)
nuclear@0 152 {
nuclear@4 153 /* recalculate primary ray texture if needed */
nuclear@1 154 if(raytex_needs_recalc) {
nuclear@1 155 create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
nuclear@1 156 }
nuclear@4 157 /* recalculate transfer function texture if needed */
nuclear@3 158 if(xfertex_needs_recalc) {
nuclear@3 159 create_transfer_map(xfer_mean, xfer_sdev);
nuclear@3 160 }
nuclear@1 161
nuclear@4 162 render_volume();
nuclear@5 163 draw_slice();
nuclear@4 164 draw_xfer_func();
nuclear@4 165
nuclear@4 166 glutSwapBuffers();
nuclear@4 167 assert(glGetError() == GL_NO_ERROR);
nuclear@4 168 }
nuclear@4 169
nuclear@4 170 void render_volume(void)
nuclear@4 171 {
nuclear@4 172 /* set the camera transformation */
nuclear@0 173 glMatrixMode(GL_MODELVIEW);
nuclear@4 174 glPushMatrix();
nuclear@0 175 glLoadIdentity();
nuclear@1 176 glRotatef(-90, 1, 0, 0);
nuclear@0 177 glTranslatef(cam_x, cam_y, -cam_z);
nuclear@0 178 glRotatef(cam_theta, 0, 1, 0);
nuclear@0 179 glRotatef(cam_phi, 1, 0, 0);
nuclear@0 180 glTranslatef(0, 0, -cam_dist);
nuclear@0 181
nuclear@4 182 /* setup the texture matrix to map the useful part of the ray texture to [0,1] */
nuclear@0 183 glMatrixMode(GL_TEXTURE);
nuclear@4 184 glPushMatrix();
nuclear@1 185 glLoadIdentity();
nuclear@0 186 glScalef(tex_scale.x, tex_scale.y, 1.0);
nuclear@0 187
nuclear@4 188 /* tex unit0: volume data 3D texture */
nuclear@0 189 glActiveTexture(GL_TEXTURE0);
nuclear@1 190 glBindTexture(GL_TEXTURE_3D, vol_tex);
nuclear@0 191 glEnable(GL_TEXTURE_3D);
nuclear@0 192
nuclear@4 193 /* tex unit1: primary rays in view space */
nuclear@0 194 glActiveTexture(GL_TEXTURE1);
nuclear@1 195 glBindTexture(GL_TEXTURE_2D, ray_tex);
nuclear@0 196 glEnable(GL_TEXTURE_2D);
nuclear@0 197
nuclear@4 198 /* tex unit2: transfer function (1d) */
nuclear@3 199 glActiveTexture(GL_TEXTURE2);
nuclear@3 200 glBindTexture(GL_TEXTURE_1D, xfer_tex);
nuclear@3 201 glEnable(GL_TEXTURE_1D);
nuclear@3 202
nuclear@5 203 bind_program(vol_sdr);
nuclear@0 204 glBegin(GL_QUADS);
nuclear@0 205 glColor3f(1, 1, 1);
nuclear@1 206 glTexCoord2f(0, 1); glVertex2f(-1, -1);
nuclear@1 207 glTexCoord2f(1, 1); glVertex2f(1, -1);
nuclear@1 208 glTexCoord2f(1, 0); glVertex2f(1, 1);
nuclear@1 209 glTexCoord2f(0, 0); glVertex2f(-1, 1);
nuclear@0 210 glEnd();
nuclear@0 211 bind_program(0);
nuclear@0 212
nuclear@3 213 glActiveTexture(GL_TEXTURE2);
nuclear@3 214 glDisable(GL_TEXTURE_1D);
nuclear@1 215 glActiveTexture(GL_TEXTURE1);
nuclear@0 216 glDisable(GL_TEXTURE_2D);
nuclear@0 217 glActiveTexture(GL_TEXTURE0);
nuclear@0 218 glDisable(GL_TEXTURE_3D);
nuclear@0 219
nuclear@0 220 glMatrixMode(GL_TEXTURE);
nuclear@4 221 glPopMatrix();
nuclear@4 222 glMatrixMode(GL_MODELVIEW);
nuclear@4 223 glPopMatrix();
nuclear@4 224 }
nuclear@0 225
nuclear@5 226 void draw_slice(void)
nuclear@5 227 {
nuclear@5 228 glMatrixMode(GL_MODELVIEW);
nuclear@5 229 glPushMatrix();
nuclear@5 230 glTranslatef(0.9, 0.9, 0);
nuclear@5 231 glScalef(0.3, 0.3 * ((float)win_xsz / win_ysz), 1);
nuclear@5 232 glTranslatef(-1, -1, 0);
nuclear@5 233
nuclear@5 234 glActiveTexture(GL_TEXTURE0);
nuclear@5 235 glBindTexture(GL_TEXTURE_3D, vol_tex);
nuclear@5 236 glEnable(GL_TEXTURE_3D);
nuclear@5 237
nuclear@5 238 glActiveTexture(GL_TEXTURE1);
nuclear@5 239 glBindTexture(GL_TEXTURE_1D, xfer_tex);
nuclear@5 240 glEnable(GL_TEXTURE_1D);
nuclear@5 241
nuclear@5 242 bind_program(slice_sdr);
nuclear@5 243
nuclear@5 244 glBegin(GL_QUADS);
nuclear@5 245 glColor3f(1, 1, 1);
nuclear@5 246 glTexCoord3f(0, 1, cur_z); glVertex2f(-1, -1);
nuclear@5 247 glTexCoord3f(1, 1, cur_z); glVertex2f(1, -1);
nuclear@5 248 glTexCoord3f(1, 0, cur_z); glVertex2f(1, 1);
nuclear@5 249 glTexCoord3f(0, 0, cur_z); glVertex2f(-1, 1);
nuclear@5 250 glEnd();
nuclear@5 251
nuclear@5 252 bind_program(0);
nuclear@5 253
nuclear@5 254 glActiveTexture(GL_TEXTURE1);
nuclear@5 255 glDisable(GL_TEXTURE_1D);
nuclear@5 256 glActiveTexture(GL_TEXTURE0);
nuclear@5 257 glDisable(GL_TEXTURE_3D);
nuclear@5 258 glPopMatrix();
nuclear@5 259 }
nuclear@5 260
nuclear@4 261 void draw_xfer_func(void)
nuclear@4 262 {
nuclear@4 263 glMatrixMode(GL_MODELVIEW);
nuclear@4 264 glPushMatrix();
nuclear@4 265 glTranslatef(-0.9, -0.9, 0);
nuclear@4 266 glScalef(0.5, 0.1, 1);
nuclear@4 267
nuclear@4 268 glBindTexture(GL_TEXTURE_1D, xfer_tex);
nuclear@4 269 glEnable(GL_TEXTURE_1D);
nuclear@4 270
nuclear@4 271 glBegin(GL_QUADS);
nuclear@4 272 glColor3f(1, 1, 1);
nuclear@4 273 glTexCoord1f(1);
nuclear@4 274 glVertex2f(1, 0);
nuclear@4 275 glVertex2f(1, 1);
nuclear@4 276 glTexCoord1f(0);
nuclear@4 277 glVertex2f(0, 1);
nuclear@4 278 glVertex2f(0, 0);
nuclear@4 279 glEnd();
nuclear@4 280
nuclear@4 281 glDisable(GL_TEXTURE_1D);
nuclear@4 282
nuclear@4 283 glLineWidth(2.0);
nuclear@4 284 glBegin(GL_LINE_LOOP);
nuclear@5 285 if(uimode == UIMODE_XFER) {
nuclear@5 286 glColor3f(1, 0, 0);
nuclear@5 287 } else {
nuclear@5 288 glColor3f(0, 0, 1);
nuclear@5 289 }
nuclear@4 290 glVertex2f(0, 0);
nuclear@4 291 glVertex2f(1, 0);
nuclear@4 292 glVertex2f(1, 1);
nuclear@4 293 glVertex2f(0, 1);
nuclear@4 294 glEnd();
nuclear@4 295
nuclear@4 296 glPopMatrix();
nuclear@0 297 }
nuclear@0 298
nuclear@0 299 void reshape(int x, int y)
nuclear@0 300 {
nuclear@0 301 glViewport(0, 0, x, y);
nuclear@0 302
nuclear@0 303 if(x != win_xsz || y != win_ysz) {
nuclear@1 304 raytex_needs_recalc = 1;
nuclear@0 305 win_xsz = x;
nuclear@0 306 win_ysz = y;
nuclear@0 307 }
nuclear@0 308 }
nuclear@0 309
nuclear@0 310 void keyb(unsigned char key, int x, int y)
nuclear@0 311 {
nuclear@0 312 switch(key) {
nuclear@0 313 case 27:
nuclear@0 314 exit(0);
nuclear@4 315
nuclear@4 316 case 'x':
nuclear@5 317 uimode = UIMODE_XFER;
nuclear@4 318 glutPostRedisplay();
nuclear@4 319 break;
nuclear@5 320
nuclear@5 321 case 'c':
nuclear@5 322 uimode = UIMODE_CURSOR;
nuclear@5 323 glutPostRedisplay();
nuclear@5 324 break;
nuclear@5 325
nuclear@5 326 default:
nuclear@5 327 break;
nuclear@4 328 }
nuclear@4 329 }
nuclear@4 330
nuclear@4 331 void keyb_up(unsigned char key, int x, int y)
nuclear@4 332 {
nuclear@4 333 switch(key) {
nuclear@4 334 case 'x':
nuclear@5 335 if(uimode == UIMODE_XFER) {
nuclear@5 336 uimode = UIMODE_DEFAULT;
nuclear@5 337 glutPostRedisplay();
nuclear@5 338 }
nuclear@5 339 break;
nuclear@5 340
nuclear@5 341 case 'c':
nuclear@5 342 if(uimode == UIMODE_CURSOR) {
nuclear@5 343 uimode = UIMODE_DEFAULT;
nuclear@5 344 glutPostRedisplay();
nuclear@5 345 }
nuclear@5 346 break;
nuclear@5 347
nuclear@5 348 default:
nuclear@4 349 break;
nuclear@0 350 }
nuclear@0 351 }
nuclear@0 352
nuclear@0 353 static int bnstate[32];
nuclear@0 354 static int prev_x, prev_y;
nuclear@0 355
nuclear@0 356 void mouse(int bn, int state, int x, int y)
nuclear@0 357 {
nuclear@0 358 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
nuclear@0 359 prev_x = x;
nuclear@0 360 prev_y = y;
nuclear@0 361 }
nuclear@0 362
nuclear@0 363 void motion(int x, int y)
nuclear@0 364 {
nuclear@0 365 int dx = x - prev_x;
nuclear@0 366 int dy = y - prev_y;
nuclear@0 367 prev_x = x;
nuclear@0 368 prev_y = y;
nuclear@0 369
nuclear@5 370 switch(uimode) {
nuclear@5 371 case UIMODE_XFER:
nuclear@4 372 if(dx || dy) {
nuclear@4 373 xfer_mean += dx / (float)win_xsz;
nuclear@4 374 xfer_sdev += 0.5 * dy / (float)win_ysz;
nuclear@0 375
nuclear@4 376 xfer_mean = xfer_mean < 0.0 ? 0.0 : (xfer_mean > 1.0 ? 1.0 : xfer_mean);
nuclear@4 377 xfer_sdev = xfer_sdev < 0.0 ? 0.0 : (xfer_sdev > 1.0 ? 1.0 : xfer_sdev);
nuclear@0 378
nuclear@4 379 xfertex_needs_recalc = 1;
nuclear@4 380 glutPostRedisplay();
nuclear@4 381 }
nuclear@5 382 break;
nuclear@0 383
nuclear@5 384 case UIMODE_CURSOR:
nuclear@5 385 cur_z += 0.5 * dy / (float)win_ysz;
nuclear@5 386
nuclear@5 387 if(cur_z < 0.0)
nuclear@5 388 cur_z = 0.0;
nuclear@5 389 if(cur_z > 1.0)
nuclear@5 390 cur_z = 1.0;
nuclear@5 391 glutPostRedisplay();
nuclear@5 392 break;
nuclear@5 393
nuclear@5 394 default:
nuclear@5 395 /* view control */
nuclear@4 396 if(bnstate[0]) {
nuclear@4 397 cam_theta += dx * 0.5;
nuclear@4 398 cam_phi += dy * 0.5;
nuclear@0 399
nuclear@4 400 if(cam_phi <= -90) cam_phi = -89;
nuclear@4 401 if(cam_phi >= 90) cam_phi = 89;
nuclear@4 402
nuclear@4 403 glutPostRedisplay();
nuclear@4 404 }
nuclear@4 405
nuclear@4 406 if(bnstate[1]) {
nuclear@4 407 cam_x += dx * 0.025;
nuclear@4 408 cam_y += dy * 0.025;
nuclear@4 409 glutPostRedisplay();
nuclear@4 410 }
nuclear@4 411
nuclear@4 412 if(bnstate[2]) {
nuclear@4 413 cam_dist += dy * 0.025;
nuclear@4 414 if(cam_dist < 0.0) cam_dist = 0.0;
nuclear@4 415 glutPostRedisplay();
nuclear@4 416 }
nuclear@0 417 }
nuclear@0 418 }
nuclear@0 419
nuclear@0 420
nuclear@0 421 int parse_args(int argc, char **argv)
nuclear@0 422 {
nuclear@0 423 int i;
nuclear@0 424 struct slice_file *tail;
nuclear@3 425 char *endp;
nuclear@0 426
nuclear@0 427 for(i=1; i<argc; i++) {
nuclear@2 428 if(argv[i][0] == '-' && argv[i][2] == 0) {
nuclear@2 429 switch(argv[i][1]) {
nuclear@3 430 case 'm':
nuclear@3 431 xfer_mean = strtod(argv[++i], &endp);
nuclear@3 432 if(endp == argv[i]) {
nuclear@3 433 fprintf(stderr, "-m must be followed by the transfer function mean\n");
nuclear@3 434 return -1;
nuclear@3 435 }
nuclear@2 436 break;
nuclear@3 437
nuclear@4 438 case 'd':
nuclear@3 439 xfer_sdev = strtod(argv[++i], &endp);
nuclear@3 440 if(endp == argv[i]) {
nuclear@4 441 fprintf(stderr, "-d must be followed by the transfer function std.deviation\n");
nuclear@3 442 return -1;
nuclear@3 443 }
nuclear@3 444 break;
nuclear@3 445
nuclear@3 446 default:
nuclear@3 447 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
nuclear@3 448 return -1;
nuclear@2 449 }
nuclear@2 450 } else {
nuclear@2 451 struct slice_file *sfile;
nuclear@0 452
nuclear@2 453 if(!(sfile = malloc(sizeof *sfile))) {
nuclear@2 454 perror("failed to allocate memory");
nuclear@2 455 return -1;
nuclear@2 456 }
nuclear@2 457 sfile->name = argv[i];
nuclear@2 458 sfile->next = 0;
nuclear@2 459
nuclear@2 460 if(!flist) {
nuclear@2 461 flist = tail = sfile;
nuclear@2 462 } else {
nuclear@2 463 tail->next = sfile;
nuclear@2 464 tail = sfile;
nuclear@2 465 }
nuclear@2 466 nslices++;
nuclear@0 467 }
nuclear@0 468 }
nuclear@0 469
nuclear@0 470 if(!nslices) {
nuclear@0 471 fprintf(stderr, "pass the slice filenames\n");
nuclear@0 472 return -1;
nuclear@0 473 }
nuclear@0 474 return 0;
nuclear@0 475 }
nuclear@0 476
nuclear@0 477
nuclear@1 478 static void create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale)
nuclear@0 479 {
nuclear@0 480 int i, j;
nuclear@1 481 int cur_tex_xsz, cur_tex_ysz;
nuclear@0 482 int tex_xsz = round_pow2(xsz);
nuclear@0 483 int tex_ysz = round_pow2(ysz);
nuclear@0 484 float *teximg, *dir;
nuclear@0 485
nuclear@1 486 if(!(teximg = malloc(3 * xsz * ysz * sizeof *teximg))) {
nuclear@1 487 return;
nuclear@0 488 }
nuclear@0 489 dir = teximg;
nuclear@0 490
nuclear@1 491 for(i=0; i<ysz; i++) {
nuclear@1 492 for(j=0; j<xsz; j++) {
nuclear@1 493 vec3_t rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
nuclear@1 494 *dir++ = rdir.x;
nuclear@1 495 *dir++ = rdir.y;
nuclear@1 496 *dir++ = rdir.z;
nuclear@0 497 }
nuclear@0 498 }
nuclear@0 499
nuclear@1 500 if(!ray_tex) {
nuclear@1 501 glGenTextures(1, &ray_tex);
nuclear@1 502 }
nuclear@1 503
nuclear@1 504 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
nuclear@1 505 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
nuclear@1 506
nuclear@1 507 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
nuclear@1 508 glBindTexture(GL_TEXTURE_2D, ray_tex);
nuclear@1 509 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nuclear@1 510 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nuclear@1 511 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
nuclear@1 512 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
nuclear@1 513 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
nuclear@1 514 }
nuclear@1 515
nuclear@1 516 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
nuclear@0 517 free(teximg);
nuclear@0 518
nuclear@0 519 if(tex_scale) {
nuclear@0 520 tex_scale->x = (float)xsz / (float)tex_xsz;
nuclear@0 521 tex_scale->y = (float)ysz / (float)tex_ysz;
nuclear@0 522 }
nuclear@1 523 raytex_needs_recalc = 0;
nuclear@0 524 }
nuclear@0 525
nuclear@0 526 static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
nuclear@0 527 {
nuclear@0 528 float vfov = M_PI * vfov_deg / 180.0;
nuclear@0 529 float aspect = (float)w / (float)h;
nuclear@0 530
nuclear@0 531 float ysz = 2.0;
nuclear@0 532 float xsz = aspect * ysz;
nuclear@0 533
nuclear@0 534 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
nuclear@0 535 float py = 1.0 - ((float)y / (float)h) * ysz;
nuclear@0 536 float pz = 1.0 / tan(0.5 * vfov);
nuclear@0 537
nuclear@0 538 float mag = sqrt(px * px + py * py + pz * pz);
nuclear@0 539
nuclear@0 540 return v3_cons(px / mag, py / mag, pz / mag);
nuclear@0 541 }
nuclear@0 542
nuclear@0 543 static int round_pow2(int x)
nuclear@0 544 {
nuclear@0 545 x--;
nuclear@0 546 x = (x >> 1) | x;
nuclear@0 547 x = (x >> 2) | x;
nuclear@0 548 x = (x >> 4) | x;
nuclear@0 549 x = (x >> 8) | x;
nuclear@0 550 x = (x >> 16) | x;
nuclear@0 551 return x + 1;
nuclear@0 552 }
nuclear@0 553
nuclear@3 554 static void create_transfer_map(float mean, float sdev)
nuclear@3 555 {
nuclear@3 556 static float map[XFER_MAP_SZ];
nuclear@3 557 int i;
nuclear@3 558
nuclear@3 559 if(!xfer_tex) {
nuclear@3 560 glGenTextures(1, &xfer_tex);
nuclear@3 561 glBindTexture(GL_TEXTURE_1D, xfer_tex);
nuclear@3 562 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@3 563 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@3 564 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@3 565 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@3 566 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, XFER_MAP_SZ, 0, GL_LUMINANCE, GL_FLOAT, 0);
nuclear@3 567 }
nuclear@3 568
nuclear@3 569 for(i=0; i<XFER_MAP_SZ; i++) {
nuclear@4 570 float x = (float)i / (float)(XFER_MAP_SZ - 1);
nuclear@4 571 map[i] = gaussian(x, mean, sdev) - 1.0;
nuclear@3 572 }
nuclear@4 573 putchar('\n');
nuclear@3 574
nuclear@4 575 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, XFER_MAP_SZ, GL_LUMINANCE, GL_FLOAT, map);
nuclear@3 576 xfertex_needs_recalc = 0;
nuclear@3 577 }