volray

annotate src/volray.c @ 2:0b73aa7317e1

lalal
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 02 Apr 2012 14:00:28 +0300
parents 57072295eb83
children 6f275934717b
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@0 16 struct slice_file {
nuclear@0 17 char *name;
nuclear@0 18 struct slice_file *next;
nuclear@0 19 };
nuclear@0 20
nuclear@0 21 int init(void);
nuclear@0 22 void disp(void);
nuclear@0 23 void reshape(int x, int y);
nuclear@0 24 void keyb(unsigned char key, int x, int y);
nuclear@0 25 void mouse(int bn, int state, int x, int y);
nuclear@0 26 void motion(int x, int y);
nuclear@0 27 int parse_args(int argc, char **argv);
nuclear@0 28
nuclear@1 29 static void create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale);
nuclear@0 30 static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
nuclear@0 31 static int round_pow2(int x);
nuclear@0 32
nuclear@0 33 float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
nuclear@0 34 float cam_x, cam_y, cam_z;
nuclear@0 35
nuclear@0 36 vec2_t tex_scale;
nuclear@0 37 struct slice_file *flist;
nuclear@0 38 int nslices;
nuclear@0 39 unsigned int sdr, vol_tex, ray_tex;
nuclear@0 40 int win_xsz, win_ysz;
nuclear@1 41 int raytex_needs_recalc = 1;
nuclear@0 42
nuclear@0 43 int main(int argc, char **argv)
nuclear@0 44 {
nuclear@0 45 glutInit(&argc, argv);
nuclear@0 46 glutInitWindowSize(1280, 720);
nuclear@0 47 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
nuclear@0 48
nuclear@0 49 if(parse_args(argc, argv) == -1) {
nuclear@0 50 return 1;
nuclear@0 51 }
nuclear@0 52
nuclear@0 53 glutCreateWindow("Volume Raytracer");
nuclear@0 54
nuclear@0 55 glutDisplayFunc(disp);
nuclear@0 56 glutReshapeFunc(reshape);
nuclear@0 57 glutKeyboardFunc(keyb);
nuclear@0 58 glutMouseFunc(mouse);
nuclear@0 59 glutMotionFunc(motion);
nuclear@0 60
nuclear@0 61 glewInit();
nuclear@0 62
nuclear@0 63 if(init() == -1) {
nuclear@0 64 return 1;
nuclear@0 65 }
nuclear@0 66
nuclear@0 67 glutMainLoop();
nuclear@0 68 return 0;
nuclear@0 69 }
nuclear@0 70
nuclear@0 71 int init(void)
nuclear@0 72 {
nuclear@0 73 int i, vol_xsz, vol_ysz;
nuclear@0 74
nuclear@0 75 if(!(sdr = create_program_load("volray.v.glsl", "volray.p.glsl"))) {
nuclear@1 76 return -1;
nuclear@0 77 }
nuclear@1 78 set_uniform_int(sdr, "volume", 0);
nuclear@1 79 set_uniform_int(sdr, "ray_tex", 1);
nuclear@0 80
nuclear@0 81 glGenTextures(1, &vol_tex);
nuclear@0 82 glBindTexture(GL_TEXTURE_3D, vol_tex);
nuclear@0 83 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@0 84 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@1 85 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
nuclear@1 86 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
nuclear@1 87 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
nuclear@0 88
nuclear@0 89 for(i=0; i<nslices; i++) {
nuclear@0 90 int xsz, ysz;
nuclear@0 91 void *pix;
nuclear@0 92 struct slice_file *sfile = flist;
nuclear@0 93 flist = flist->next;
nuclear@0 94
nuclear@0 95 if(!(pix = img_load_pixels(sfile->name, &xsz, &ysz, IMG_FMT_RGBA32))) {
nuclear@0 96 fprintf(stderr, "failed to load image: %s\n", sfile->name);
nuclear@0 97 return -1;
nuclear@0 98 }
nuclear@0 99
nuclear@0 100 if(i == 0) {
nuclear@0 101 /* allocate storage for the texture */
nuclear@0 102 glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, xsz, ysz, nslices, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
nuclear@0 103
nuclear@0 104 vol_xsz = xsz;
nuclear@0 105 vol_ysz = ysz;
nuclear@0 106
nuclear@0 107 } else {
nuclear@0 108 if(xsz != vol_xsz || ysz != vol_ysz) {
nuclear@0 109 fprintf(stderr, "%s: inconsistent slice size: %dx%d. expected: %dx%d\n",
nuclear@0 110 sfile->name, xsz, ysz, vol_xsz, vol_ysz);
nuclear@0 111 img_free_pixels(pix);
nuclear@0 112 return -1;
nuclear@0 113 }
nuclear@0 114 }
nuclear@0 115 free(sfile);
nuclear@0 116
nuclear@0 117 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_UNSIGNED_BYTE, pix);
nuclear@0 118 img_free_pixels(pix);
nuclear@0 119 }
nuclear@0 120 return 0;
nuclear@0 121 }
nuclear@0 122
nuclear@0 123 void disp(void)
nuclear@0 124 {
nuclear@1 125 if(raytex_needs_recalc) {
nuclear@1 126 create_ray_texture(win_xsz, win_ysz, 50.0, &tex_scale);
nuclear@1 127 }
nuclear@1 128
nuclear@0 129 glMatrixMode(GL_MODELVIEW);
nuclear@0 130 glLoadIdentity();
nuclear@1 131 glRotatef(-90, 1, 0, 0);
nuclear@0 132 glTranslatef(cam_x, cam_y, -cam_z);
nuclear@0 133 glRotatef(cam_theta, 0, 1, 0);
nuclear@0 134 glRotatef(cam_phi, 1, 0, 0);
nuclear@0 135 glTranslatef(0, 0, -cam_dist);
nuclear@0 136
nuclear@0 137 glMatrixMode(GL_TEXTURE);
nuclear@1 138 glLoadIdentity();
nuclear@0 139 glScalef(tex_scale.x, tex_scale.y, 1.0);
nuclear@0 140
nuclear@0 141 glActiveTexture(GL_TEXTURE0);
nuclear@1 142 glBindTexture(GL_TEXTURE_3D, vol_tex);
nuclear@0 143 glEnable(GL_TEXTURE_3D);
nuclear@0 144
nuclear@0 145 glActiveTexture(GL_TEXTURE1);
nuclear@1 146 glBindTexture(GL_TEXTURE_2D, ray_tex);
nuclear@0 147 glEnable(GL_TEXTURE_2D);
nuclear@0 148
nuclear@0 149 bind_program(sdr);
nuclear@0 150 glBegin(GL_QUADS);
nuclear@0 151 glColor3f(1, 1, 1);
nuclear@1 152 glTexCoord2f(0, 1); glVertex2f(-1, -1);
nuclear@1 153 glTexCoord2f(1, 1); glVertex2f(1, -1);
nuclear@1 154 glTexCoord2f(1, 0); glVertex2f(1, 1);
nuclear@1 155 glTexCoord2f(0, 0); glVertex2f(-1, 1);
nuclear@0 156 glEnd();
nuclear@0 157 bind_program(0);
nuclear@0 158
nuclear@1 159 glActiveTexture(GL_TEXTURE1);
nuclear@0 160 glDisable(GL_TEXTURE_2D);
nuclear@0 161 glActiveTexture(GL_TEXTURE0);
nuclear@0 162 glDisable(GL_TEXTURE_3D);
nuclear@0 163
nuclear@0 164 glMatrixMode(GL_TEXTURE);
nuclear@1 165 glLoadIdentity();
nuclear@0 166
nuclear@0 167 glutSwapBuffers();
nuclear@0 168 assert(glGetError() == GL_NO_ERROR);
nuclear@0 169 }
nuclear@0 170
nuclear@0 171 void reshape(int x, int y)
nuclear@0 172 {
nuclear@1 173 printf("reshape: %dx%d\n", x, y);
nuclear@0 174 glViewport(0, 0, x, y);
nuclear@0 175
nuclear@0 176 if(x != win_xsz || y != win_ysz) {
nuclear@1 177 raytex_needs_recalc = 1;
nuclear@0 178 win_xsz = x;
nuclear@0 179 win_ysz = y;
nuclear@0 180 }
nuclear@0 181 }
nuclear@0 182
nuclear@0 183 void keyb(unsigned char key, int x, int y)
nuclear@0 184 {
nuclear@0 185 switch(key) {
nuclear@0 186 case 27:
nuclear@0 187 exit(0);
nuclear@0 188 }
nuclear@0 189 }
nuclear@0 190
nuclear@0 191 static int bnstate[32];
nuclear@0 192 static int prev_x, prev_y;
nuclear@0 193
nuclear@0 194 void mouse(int bn, int state, int x, int y)
nuclear@0 195 {
nuclear@0 196 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
nuclear@0 197 prev_x = x;
nuclear@0 198 prev_y = y;
nuclear@0 199 }
nuclear@0 200
nuclear@0 201 void motion(int x, int y)
nuclear@0 202 {
nuclear@0 203 int dx = x - prev_x;
nuclear@0 204 int dy = y - prev_y;
nuclear@0 205
nuclear@0 206 prev_x = x;
nuclear@0 207 prev_y = y;
nuclear@0 208
nuclear@0 209 if(bnstate[0]) {
nuclear@0 210 cam_theta += dx * 0.5;
nuclear@0 211 cam_phi += dy * 0.5;
nuclear@0 212
nuclear@1 213 if(cam_phi <= -90) cam_phi = -89;
nuclear@1 214 if(cam_phi >= 90) cam_phi = 89;
nuclear@0 215
nuclear@0 216 glutPostRedisplay();
nuclear@0 217 }
nuclear@0 218
nuclear@0 219 if(bnstate[1]) {
nuclear@1 220 cam_x += dx * 0.025;
nuclear@1 221 cam_y += dy * 0.025;
nuclear@0 222 glutPostRedisplay();
nuclear@0 223 }
nuclear@0 224
nuclear@0 225 if(bnstate[2]) {
nuclear@1 226 cam_dist += dy * 0.025;
nuclear@0 227 if(cam_dist < 0.0) cam_dist = 0.0;
nuclear@0 228 glutPostRedisplay();
nuclear@0 229 }
nuclear@0 230 }
nuclear@0 231
nuclear@0 232
nuclear@0 233 int parse_args(int argc, char **argv)
nuclear@0 234 {
nuclear@0 235 int i;
nuclear@0 236 struct slice_file *tail;
nuclear@0 237
nuclear@0 238 for(i=1; i<argc; i++) {
nuclear@2 239 if(argv[i][0] == '-' && argv[i][2] == 0) {
nuclear@2 240 switch(argv[i][1]) {
nuclear@2 241 case 'r':
nuclear@2 242 break;
nuclear@2 243 }
nuclear@2 244 } else {
nuclear@2 245 struct slice_file *sfile;
nuclear@0 246
nuclear@2 247 if(!(sfile = malloc(sizeof *sfile))) {
nuclear@2 248 perror("failed to allocate memory");
nuclear@2 249 return -1;
nuclear@2 250 }
nuclear@2 251 sfile->name = argv[i];
nuclear@2 252 sfile->next = 0;
nuclear@2 253
nuclear@2 254 if(!flist) {
nuclear@2 255 flist = tail = sfile;
nuclear@2 256 } else {
nuclear@2 257 tail->next = sfile;
nuclear@2 258 tail = sfile;
nuclear@2 259 }
nuclear@2 260 nslices++;
nuclear@0 261 }
nuclear@0 262 }
nuclear@0 263
nuclear@0 264 if(!nslices) {
nuclear@0 265 fprintf(stderr, "pass the slice filenames\n");
nuclear@0 266 return -1;
nuclear@0 267 }
nuclear@0 268
nuclear@0 269 return 0;
nuclear@0 270 }
nuclear@0 271
nuclear@0 272
nuclear@1 273 static void create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale)
nuclear@0 274 {
nuclear@0 275 int i, j;
nuclear@1 276 int cur_tex_xsz, cur_tex_ysz;
nuclear@0 277 int tex_xsz = round_pow2(xsz);
nuclear@0 278 int tex_ysz = round_pow2(ysz);
nuclear@0 279 float *teximg, *dir;
nuclear@0 280
nuclear@1 281 if(!(teximg = malloc(3 * xsz * ysz * sizeof *teximg))) {
nuclear@1 282 return;
nuclear@0 283 }
nuclear@0 284 dir = teximg;
nuclear@0 285
nuclear@1 286 for(i=0; i<ysz; i++) {
nuclear@1 287 for(j=0; j<xsz; j++) {
nuclear@1 288 vec3_t rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
nuclear@1 289 *dir++ = rdir.x;
nuclear@1 290 *dir++ = rdir.y;
nuclear@1 291 *dir++ = rdir.z;
nuclear@0 292 }
nuclear@0 293 }
nuclear@0 294
nuclear@1 295 if(!ray_tex) {
nuclear@1 296 glGenTextures(1, &ray_tex);
nuclear@1 297 }
nuclear@1 298
nuclear@1 299 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
nuclear@1 300 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
nuclear@1 301
nuclear@1 302 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
nuclear@1 303 glBindTexture(GL_TEXTURE_2D, ray_tex);
nuclear@1 304 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nuclear@1 305 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nuclear@1 306 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
nuclear@1 307 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
nuclear@1 308 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
nuclear@1 309 }
nuclear@1 310
nuclear@1 311 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
nuclear@0 312 free(teximg);
nuclear@0 313
nuclear@0 314 if(tex_scale) {
nuclear@0 315 tex_scale->x = (float)xsz / (float)tex_xsz;
nuclear@0 316 tex_scale->y = (float)ysz / (float)tex_ysz;
nuclear@0 317 }
nuclear@1 318 raytex_needs_recalc = 0;
nuclear@0 319 }
nuclear@0 320
nuclear@0 321 static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
nuclear@0 322 {
nuclear@0 323 float vfov = M_PI * vfov_deg / 180.0;
nuclear@0 324 float aspect = (float)w / (float)h;
nuclear@0 325
nuclear@0 326 float ysz = 2.0;
nuclear@0 327 float xsz = aspect * ysz;
nuclear@0 328
nuclear@0 329 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
nuclear@0 330 float py = 1.0 - ((float)y / (float)h) * ysz;
nuclear@0 331 float pz = 1.0 / tan(0.5 * vfov);
nuclear@0 332
nuclear@0 333 float mag = sqrt(px * px + py * py + pz * pz);
nuclear@0 334
nuclear@0 335 return v3_cons(px / mag, py / mag, pz / mag);
nuclear@0 336 }
nuclear@0 337
nuclear@0 338 static int round_pow2(int x)
nuclear@0 339 {
nuclear@0 340 x--;
nuclear@0 341 x = (x >> 1) | x;
nuclear@0 342 x = (x >> 2) | x;
nuclear@0 343 x = (x >> 4) | x;
nuclear@0 344 x = (x >> 8) | x;
nuclear@0 345 x = (x >> 16) | x;
nuclear@0 346 return x + 1;
nuclear@0 347 }
nuclear@0 348