qvolray

annotate src/volray.c @ 1:57072295eb83

progress
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 02 Apr 2012 02:11:35 +0300
parents b050ce167ff1
children 0b73aa7317e1
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@0 239 struct slice_file *sfile;
nuclear@0 240
nuclear@0 241 if(!(sfile = malloc(sizeof *sfile))) {
nuclear@0 242 perror("failed to allocate memory");
nuclear@0 243 return -1;
nuclear@0 244 }
nuclear@0 245 sfile->name = argv[i];
nuclear@0 246 sfile->next = 0;
nuclear@0 247
nuclear@0 248 if(!flist) {
nuclear@0 249 flist = tail = sfile;
nuclear@0 250 } else {
nuclear@0 251 tail->next = sfile;
nuclear@0 252 tail = sfile;
nuclear@0 253 }
nuclear@0 254 nslices++;
nuclear@0 255 }
nuclear@0 256
nuclear@0 257 if(!nslices) {
nuclear@0 258 fprintf(stderr, "pass the slice filenames\n");
nuclear@0 259 return -1;
nuclear@0 260 }
nuclear@0 261
nuclear@0 262 return 0;
nuclear@0 263 }
nuclear@0 264
nuclear@0 265
nuclear@1 266 static void create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale)
nuclear@0 267 {
nuclear@0 268 int i, j;
nuclear@1 269 int cur_tex_xsz, cur_tex_ysz;
nuclear@0 270 int tex_xsz = round_pow2(xsz);
nuclear@0 271 int tex_ysz = round_pow2(ysz);
nuclear@0 272 float *teximg, *dir;
nuclear@0 273
nuclear@1 274 if(!(teximg = malloc(3 * xsz * ysz * sizeof *teximg))) {
nuclear@1 275 return;
nuclear@0 276 }
nuclear@0 277 dir = teximg;
nuclear@0 278
nuclear@1 279 for(i=0; i<ysz; i++) {
nuclear@1 280 for(j=0; j<xsz; j++) {
nuclear@1 281 vec3_t rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
nuclear@1 282 *dir++ = rdir.x;
nuclear@1 283 *dir++ = rdir.y;
nuclear@1 284 *dir++ = rdir.z;
nuclear@0 285 }
nuclear@0 286 }
nuclear@0 287
nuclear@1 288 if(!ray_tex) {
nuclear@1 289 glGenTextures(1, &ray_tex);
nuclear@1 290 }
nuclear@1 291
nuclear@1 292 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cur_tex_xsz);
nuclear@1 293 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cur_tex_ysz);
nuclear@1 294
nuclear@1 295 if(tex_xsz > cur_tex_xsz || tex_ysz > cur_tex_ysz) {
nuclear@1 296 glBindTexture(GL_TEXTURE_2D, ray_tex);
nuclear@1 297 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nuclear@1 298 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nuclear@1 299 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
nuclear@1 300 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
nuclear@1 301 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0);
nuclear@1 302 }
nuclear@1 303
nuclear@1 304 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg);
nuclear@0 305 free(teximg);
nuclear@0 306
nuclear@0 307 if(tex_scale) {
nuclear@0 308 tex_scale->x = (float)xsz / (float)tex_xsz;
nuclear@0 309 tex_scale->y = (float)ysz / (float)tex_ysz;
nuclear@0 310 }
nuclear@1 311 raytex_needs_recalc = 0;
nuclear@0 312 }
nuclear@0 313
nuclear@0 314 static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
nuclear@0 315 {
nuclear@0 316 float vfov = M_PI * vfov_deg / 180.0;
nuclear@0 317 float aspect = (float)w / (float)h;
nuclear@0 318
nuclear@0 319 float ysz = 2.0;
nuclear@0 320 float xsz = aspect * ysz;
nuclear@0 321
nuclear@0 322 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
nuclear@0 323 float py = 1.0 - ((float)y / (float)h) * ysz;
nuclear@0 324 float pz = 1.0 / tan(0.5 * vfov);
nuclear@0 325
nuclear@0 326 float mag = sqrt(px * px + py * py + pz * pz);
nuclear@0 327
nuclear@0 328 return v3_cons(px / mag, py / mag, pz / mag);
nuclear@0 329 }
nuclear@0 330
nuclear@0 331 static int round_pow2(int x)
nuclear@0 332 {
nuclear@0 333 x--;
nuclear@0 334 x = (x >> 1) | x;
nuclear@0 335 x = (x >> 2) | x;
nuclear@0 336 x = (x >> 4) | x;
nuclear@0 337 x = (x >> 8) | x;
nuclear@0 338 x = (x >> 16) | x;
nuclear@0 339 return x + 1;
nuclear@0 340 }
nuclear@0 341