qvolray

annotate src/volray.cc @ 35:6ca076bf5084

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