volray

annotate src/volray.c @ 7:f40e4edfee7e

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