metasurf

view examples/volume/src/volume.c @ 3:52664d3451ad

added volume rendering example
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Oct 2011 13:30:03 +0300
parents
children c1a60ab45bf7
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <assert.h>
6 #ifndef NO_SHADERS
7 #include <GL/glew.h>
8 #include "sdr.h"
9 #endif
11 #ifndef __APPLE__
12 #include <GL/glut.h>
13 #else
14 #include <GLUT/glut.h>
15 #endif
17 #include <imago2.h>
19 #include "cam.h"
20 #include "metasurf.h"
22 float eval(float x, float y, float z);
23 void vertex(float x, float y, float z);
24 void render(void);
25 void disp(void);
26 void reshape(int x, int y);
27 void keyb(unsigned char key, int x, int y);
28 void mouse(int bn, int state, int x, int y);
29 void motion(int x, int y);
30 int parse_args(int argc, char **argv);
32 int stereo, fullscreen;
33 int orig_xsz, orig_ysz;
35 struct metasurface *msurf;
36 float threshold = 0.5;
37 #ifndef NO_SHADERS
38 unsigned int sdr;
39 #endif
41 struct img_pixmap *volume;
42 int xres, yres, num_slices;
44 int dlist, need_update = 1;
46 int main(int argc, char **argv)
47 {
48 float amb[] = {0, 0, 0, 0};
49 float lpos[] = {-0.2, 0.2, 1, 0};
51 glutInitWindowSize(1280, 720);
52 glutInit(&argc, argv);
54 if(parse_args(argc, argv) == -1) {
55 return 1;
56 }
57 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (stereo ? GLUT_STEREO : 0));
58 glutCreateWindow("metasurf - volume rendering");
60 orig_xsz = glutGet(GLUT_WINDOW_WIDTH);
61 orig_ysz = glutGet(GLUT_WINDOW_HEIGHT);
63 if(fullscreen) {
64 glutFullScreen();
65 }
67 glutDisplayFunc(disp);
68 glutReshapeFunc(reshape);
69 glutKeyboardFunc(keyb);
70 glutMouseFunc(mouse);
71 glutMotionFunc(motion);
73 #ifndef NO_SHADERS
74 glewInit();
75 if(!(sdr = create_program_load("sdr/vert.glsl", "sdr/frag.glsl"))) {
76 return 1;
77 }
78 #endif
80 glEnable(GL_CULL_FACE);
81 glEnable(GL_DEPTH_TEST);
83 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
85 glEnable(GL_LIGHTING);
86 glEnable(GL_LIGHT0);
87 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
89 glEnable(GL_NORMALIZE);
91 cam_focus_dist(3.0);
92 cam_clip(0.1, 200.0);
93 cam_rotate(0, 0);
94 cam_dolly(2);
96 msurf = msurf_create();
97 msurf_eval_func(msurf, eval);
98 msurf_vertex_func(msurf, vertex);
99 msurf_threshold(msurf, threshold);
100 msurf_resolution(msurf, xres, yres, num_slices);
101 msurf_bounds(msurf, -1, -1, -1, 1, 1, 1);
103 glClearColor(0.6, 0.6, 0.6, 1.0);
105 dlist = glGenLists(1);
107 glutMainLoop();
108 return 0;
109 }
111 float eval(float x, float y, float z)
112 {
113 int px, py, slice;
114 struct img_pixmap *img;
116 px = round((x * 0.5 + 0.5) * xres);
117 py = round((y * 0.5 + 0.5) * yres);
118 slice = round((z * 0.5 + 0.5) * num_slices);
120 if(px < 0) px = 0;
121 if(px >= xres) px = xres - 1;
123 if(py < 0) py = 0;
124 if(py >= yres) py = yres - 1;
126 if(slice < 0) slice = 0;
127 if(slice >= num_slices) slice = num_slices - 1;
129 img = volume + slice;
130 return *((unsigned char*)img->pixels + py * img->width + px) / 255.0;
131 }
133 void vertex(float x, float y, float z)
134 {
135 float dx = 1.0 / xres;
136 float dy = 1.0 / yres;
137 float dz = 1.0 / num_slices;
138 float dfdx = eval(x - dx, y, z) - eval(x + dx, y, z);
139 float dfdy = eval(x, y - dy, z) - eval(x, y + dy, z);
140 float dfdz = eval(x, y, z - dz) - eval(x, y, z + dz);
142 glNormal3f(dfdx, dfdy, dfdz);
143 glVertex3f(x, y, z);
144 }
146 void render(void)
147 {
148 float kd[] = {0.87, 0.82, 0.74, 1.0};
149 float ks[] = {0.9, 0.9, 0.9, 1.0};
151 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, kd);
152 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ks);
153 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
155 #ifndef NO_SHADERS
156 bind_program(sdr);
157 #endif
159 glMatrixMode(GL_MODELVIEW);
160 glPushMatrix();
161 glRotatef(90, 1, 0, 0);
163 if(need_update) {
164 glNewList(dlist, GL_COMPILE);
165 glBegin(GL_TRIANGLES);
166 printf("generating mesh... ");
167 fflush(stdout);
168 msurf_polygonize(msurf);
169 glEnd();
170 glEndList();
171 need_update = 0;
172 printf("done\n");
173 }
174 glCallList(dlist);
176 glPopMatrix();
178 assert(glGetError() == GL_NO_ERROR);
179 }
181 void disp(void)
182 {
183 if(stereo) {
184 glDrawBuffer(GL_BACK_LEFT);
185 }
187 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
189 glMatrixMode(GL_PROJECTION);
190 glLoadIdentity();
191 cam_stereo_proj_matrix(stereo ? CAM_LEFT : CAM_CENTER);
193 glMatrixMode(GL_MODELVIEW);
194 glLoadIdentity();
195 cam_stereo_view_matrix(stereo ? CAM_LEFT : CAM_CENTER);
197 render();
199 if(stereo) {
200 glDrawBuffer(GL_BACK_RIGHT);
201 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
203 glMatrixMode(GL_PROJECTION);
204 glLoadIdentity();
205 cam_stereo_proj_matrix(CAM_RIGHT);
207 glMatrixMode(GL_MODELVIEW);
208 glLoadIdentity();
209 cam_stereo_view_matrix(CAM_RIGHT);
211 render();
212 }
213 glutSwapBuffers();
214 }
216 void reshape(int x, int y)
217 {
218 glViewport(0, 0, x, y);
219 cam_aspect((float)x / (float)y);
220 }
222 void keyb(unsigned char key, int x, int y)
223 {
224 static int wire;
226 switch(key) {
227 case 27:
228 exit(0);
230 case 'f':
231 fullscreen = !fullscreen;
232 if(fullscreen) {
233 glutFullScreen();
234 } else {
235 glutReshapeWindow(orig_xsz, orig_ysz);
236 }
237 break;
239 case 's':
240 stereo = !stereo;
241 glutPostRedisplay();
242 break;
244 case 'w':
245 wire = !wire;
246 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
247 glutPostRedisplay();
248 break;
250 case '=':
251 threshold += 0.05;
252 msurf_threshold(msurf, threshold);
253 printf("threshold: %f\n", threshold);
254 glutPostRedisplay();
255 need_update = 1;
256 break;
258 case '-':
259 threshold -= 0.05;
260 msurf_threshold(msurf, threshold);
261 printf("threshold: %f\n", threshold);
262 glutPostRedisplay();
263 need_update = 1;
264 break;
266 default:
267 break;
268 }
269 }
271 int bnstate[32];
272 int prev_x, prev_y;
274 void mouse(int bn, int state, int x, int y)
275 {
276 bnstate[bn] = state == GLUT_DOWN;
277 prev_x = x;
278 prev_y = y;
279 }
281 void motion(int x, int y)
282 {
283 int dx, dy;
285 dx = x - prev_x;
286 dy = y - prev_y;
287 prev_x = x;
288 prev_y = y;
290 if(bnstate[GLUT_LEFT_BUTTON]) {
291 cam_inp_rotate(dx, dy);
292 }
293 if(bnstate[GLUT_RIGHT_BUTTON]) {
294 cam_inp_zoom(dy);
295 }
296 glutPostRedisplay();
297 }
299 struct list_node {
300 struct img_pixmap img;
301 struct list_node *next;
302 };
304 int parse_args(int argc, char **argv)
305 {
306 int i;
307 struct list_node *head = 0, *tail = 0;
309 for(i=1; i<argc; i++) {
310 if(argv[i][0] == '-' && argv[i][2] == 0) {
311 switch(argv[i][1]) {
312 case 'f':
313 fullscreen = !fullscreen;
314 break;
316 case 's':
317 stereo = !stereo;
318 break;
320 case 'h':
321 printf("usage: %s [opt]\n", argv[0]);
322 printf("options:\n");
323 printf(" -f start in fullscreen\n");
324 printf(" -s enable stereoscopic rendering\n");
325 printf(" -h print usage and exit\n");
326 exit(0);
328 default:
329 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
330 return -1;
331 }
332 } else {
333 struct list_node *slice;
335 if(!(slice = malloc(sizeof *slice))) {
336 fprintf(stderr, "failed to allocate volume slice: %d\n", num_slices);
337 return -1;
338 }
339 slice->next = 0;
341 img_init(&slice->img);
342 if(img_load(&slice->img, argv[i]) == -1) {
343 fprintf(stderr, "failed to load volume slice %d: %s\n", num_slices, argv[i]);
344 free(slice);
345 return -1;
346 }
347 img_convert(&slice->img, IMG_FMT_GREY8);
349 if(num_slices > 0 && (xres != slice->img.width || yres != slice->img.height)) {
350 fprintf(stderr, "error: slice %d (%s) is %dx%d, up to now we had %dx%d images\n", num_slices, argv[i],
351 slice->img.width, slice->img.height, xres, yres);
352 img_destroy(&slice->img);
353 free(slice);
354 return -1;
355 }
356 xres = slice->img.width;
357 yres = slice->img.height;
359 if(head) {
360 tail->next = slice;
361 tail = slice;
362 } else {
363 head = tail = slice;
364 }
365 printf("loaded volume slice %d: %s\n", num_slices++, argv[i]);
366 }
367 }
369 if(!head) {
370 fprintf(stderr, "you must specify a list of images for the volume data slices\n");
371 return -1;
372 }
374 if(!(volume = malloc(num_slices * sizeof *volume))) {
375 fprintf(stderr, "failed to allocate volume data (%d slices)\n", num_slices);
376 return -1;
377 }
379 for(i=0; i<num_slices; i++) {
380 void *tmp;
382 assert(head);
383 volume[i] = head->img;
385 tmp = head;
386 head = head->next;
387 free(tmp);
388 }
390 return 0;
391 }