qvolray

annotate src/volray.c @ 9:a6765984e057

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