volray

annotate src/volray.c @ 0:b050ce167ff1

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