qvolray

view 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
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
5 #include <GL/glew.h>
6 #ifndef __APPLE__
7 #include <GL/glut.h>
8 #else
9 #include <GLUT/glut.h>
10 #endif
12 #include <vmath/vmath.h>
13 #include <imago2.h>
14 #include "sdr.h"
16 struct slice_file {
17 char *name;
18 struct slice_file *next;
19 };
21 int init(void);
22 void disp(void);
23 void reshape(int x, int y);
24 void keyb(unsigned char key, int x, int y);
25 void mouse(int bn, int state, int x, int y);
26 void motion(int x, int y);
27 int parse_args(int argc, char **argv);
29 unsigned int create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale);
30 static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg);
31 static int round_pow2(int x);
33 float cam_theta = 0, cam_phi = 0, cam_dist = 4.0;
34 float cam_x, cam_y, cam_z;
36 vec2_t tex_scale;
37 struct slice_file *flist;
38 int nslices;
39 unsigned int sdr, vol_tex, ray_tex;
40 int win_xsz, win_ysz;
42 int main(int argc, char **argv)
43 {
44 glutInit(&argc, argv);
45 glutInitWindowSize(1280, 720);
46 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
48 if(parse_args(argc, argv) == -1) {
49 return 1;
50 }
52 glutCreateWindow("Volume Raytracer");
54 glutDisplayFunc(disp);
55 glutReshapeFunc(reshape);
56 glutKeyboardFunc(keyb);
57 glutMouseFunc(mouse);
58 glutMotionFunc(motion);
60 glewInit();
62 if(init() == -1) {
63 return 1;
64 }
66 glutMainLoop();
67 return 0;
68 }
70 int init(void)
71 {
72 int i, vol_xsz, vol_ysz;
74 if(!(sdr = create_program_load("volray.v.glsl", "volray.p.glsl"))) {
75 return 1;
76 }
78 glGenTextures(1, &vol_tex);
79 glBindTexture(GL_TEXTURE_3D, vol_tex);
80 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
81 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
82 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
83 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
84 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
86 for(i=0; i<nslices; i++) {
87 int xsz, ysz;
88 void *pix;
89 struct slice_file *sfile = flist;
90 flist = flist->next;
92 if(!(pix = img_load_pixels(sfile->name, &xsz, &ysz, IMG_FMT_RGBA32))) {
93 fprintf(stderr, "failed to load image: %s\n", sfile->name);
94 return -1;
95 }
97 if(i == 0) {
98 /* allocate storage for the texture */
99 glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, xsz, ysz, nslices, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
101 vol_xsz = xsz;
102 vol_ysz = ysz;
104 } else {
105 if(xsz != vol_xsz || ysz != vol_ysz) {
106 fprintf(stderr, "%s: inconsistent slice size: %dx%d. expected: %dx%d\n",
107 sfile->name, xsz, ysz, vol_xsz, vol_ysz);
108 img_free_pixels(pix);
109 return -1;
110 }
111 }
112 free(sfile);
114 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, xsz, ysz, 1, GL_RGBA, GL_UNSIGNED_BYTE, pix);
115 img_free_pixels(pix);
116 }
118 return 0;
119 }
121 void disp(void)
122 {
123 glMatrixMode(GL_MODELVIEW);
124 glLoadIdentity();
125 glTranslatef(cam_x, cam_y, -cam_z);
126 glRotatef(cam_theta, 0, 1, 0);
127 glRotatef(cam_phi, 1, 0, 0);
128 glTranslatef(0, 0, -cam_dist);
130 glMatrixMode(GL_TEXTURE);
131 glPushMatrix();
132 glScalef(tex_scale.x, tex_scale.y, 1.0);
134 glActiveTexture(GL_TEXTURE0);
135 glEnable(GL_TEXTURE_3D);
136 glBindTexture(GL_TEXTURE_3D, vol_tex);
138 glActiveTexture(GL_TEXTURE1);
139 glEnable(GL_TEXTURE_2D);
140 glBindTexture(GL_TEXTURE_2D, ray_tex);
142 bind_program(sdr);
143 glBegin(GL_QUADS);
144 glColor3f(1, 1, 1);
145 glTexCoord2f(0, 1);
146 glVertex2f(-1, -1);
147 glTexCoord2f(1, 1);
148 glVertex2f(1, -1);
149 glTexCoord2f(1, 0);
150 glVertex2f(1, 1);
151 glTexCoord2f(0, 0);
152 glVertex2f(-1, 1);
153 glEnd();
154 bind_program(0);
156 glDisable(GL_TEXTURE_2D);
157 glActiveTexture(GL_TEXTURE0);
158 glDisable(GL_TEXTURE_3D);
160 glMatrixMode(GL_TEXTURE);
161 glPopMatrix();
163 glutSwapBuffers();
164 assert(glGetError() == GL_NO_ERROR);
165 }
167 void reshape(int x, int y)
168 {
169 glViewport(0, 0, x, y);
171 if(x != win_xsz || y != win_ysz) {
172 ray_tex = create_ray_texture(x, y, 50.0, &tex_scale);
173 win_xsz = x;
174 win_ysz = y;
175 }
176 }
178 void keyb(unsigned char key, int x, int y)
179 {
180 switch(key) {
181 case 27:
182 exit(0);
183 }
184 }
186 static int bnstate[32];
187 static int prev_x, prev_y;
189 void mouse(int bn, int state, int x, int y)
190 {
191 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
192 prev_x = x;
193 prev_y = y;
194 }
196 void motion(int x, int y)
197 {
198 int dx = x - prev_x;
199 int dy = y - prev_y;
201 prev_x = x;
202 prev_y = y;
204 if(bnstate[0]) {
205 cam_theta += dx * 0.5;
206 cam_phi += dy * 0.5;
208 if(cam_phi < -90) cam_phi = -90;
209 if(cam_phi > 90) cam_phi = 90;
211 glutPostRedisplay();
212 }
214 if(bnstate[1]) {
215 cam_y += dy * 0.1;
216 glutPostRedisplay();
217 }
219 if(bnstate[2]) {
220 cam_dist += dy * 0.1;
221 if(cam_dist < 0.0) cam_dist = 0.0;
222 glutPostRedisplay();
223 }
224 }
227 int parse_args(int argc, char **argv)
228 {
229 int i;
230 struct slice_file *tail;
232 for(i=1; i<argc; i++) {
233 struct slice_file *sfile;
235 if(!(sfile = malloc(sizeof *sfile))) {
236 perror("failed to allocate memory");
237 return -1;
238 }
239 sfile->name = argv[i];
240 sfile->next = 0;
242 if(!flist) {
243 flist = tail = sfile;
244 } else {
245 tail->next = sfile;
246 tail = sfile;
247 }
248 nslices++;
249 }
251 if(!nslices) {
252 fprintf(stderr, "pass the slice filenames\n");
253 return -1;
254 }
256 return 0;
257 }
260 unsigned int create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale)
261 {
262 int i, j;
263 unsigned int tex;
264 int tex_xsz = round_pow2(xsz);
265 int tex_ysz = round_pow2(ysz);
266 float *teximg, *dir;
268 if(!(teximg = malloc(3 * tex_xsz * tex_ysz * sizeof *teximg))) {
269 return 0;
270 }
271 dir = teximg;
273 for(i=0; i<tex_ysz; i++) {
274 for(j=0; j<tex_xsz; j++) {
275 if(j < xsz && i < ysz) {
276 vec3_t rdir = get_primary_ray_dir(j, i, xsz, ysz, vfov);
277 dir[0] = rdir.x;
278 dir[1] = rdir.y;
279 dir[2] = rdir.z;
280 } else {
281 dir[0] = dir[1] = 0.0f;
282 dir[2] = 1.0f;
283 }
285 dir += 3;
286 }
287 }
289 glGenTextures(1, &tex);
290 glBindTexture(GL_TEXTURE_2D, tex);
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
293 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
295 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, teximg);
296 free(teximg);
298 if(tex_scale) {
299 tex_scale->x = (float)xsz / (float)tex_xsz;
300 tex_scale->y = (float)ysz / (float)tex_ysz;
301 }
302 return tex;
303 }
305 static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg)
306 {
307 float vfov = M_PI * vfov_deg / 180.0;
308 float aspect = (float)w / (float)h;
310 float ysz = 2.0;
311 float xsz = aspect * ysz;
313 float px = ((float)x / (float)w) * xsz - xsz / 2.0;
314 float py = 1.0 - ((float)y / (float)h) * ysz;
315 float pz = 1.0 / tan(0.5 * vfov);
317 float mag = sqrt(px * px + py * py + pz * pz);
319 return v3_cons(px / mag, py / mag, pz / mag);
320 }
322 static int round_pow2(int x)
323 {
324 x--;
325 x = (x >> 1) | x;
326 x = (x >> 2) | x;
327 x = (x >> 4) | x;
328 x = (x >> 8) | x;
329 x = (x >> 16) | x;
330 return x + 1;
331 }