qvolray

annotate src/volray.cc @ 23:53aca4775514

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