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