metasurf

view examples/volume/src/volume.c @ 9:1fd80c90d566

added website in the readme file
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Apr 2014 13:46:04 +0300
parents 52664d3451ad
children 430d8dde62aa
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 char *endp;
308 struct list_node *head = 0, *tail = 0;
310 for(i=1; i<argc; i++) {
311 if(argv[i][0] == '-' && argv[i][2] == 0) {
312 switch(argv[i][1]) {
313 case 'f':
314 fullscreen = !fullscreen;
315 break;
317 case 's':
318 stereo = !stereo;
319 break;
321 case 't':
322 threshold = strtod(argv[++i], &endp);
323 if(endp == argv[i]) {
324 fprintf(stderr, "-t must be followed by a number\n");
325 return -1;
326 }
327 break;
329 case 'h':
330 printf("usage: %s [opt]\n", argv[0]);
331 printf("options:\n");
332 printf(" -f start in fullscreen\n");
333 printf(" -s enable stereoscopic rendering\n");
334 printf(" -h print usage and exit\n");
335 exit(0);
337 default:
338 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
339 return -1;
340 }
341 } else {
342 struct list_node *slice;
344 if(!(slice = malloc(sizeof *slice))) {
345 fprintf(stderr, "failed to allocate volume slice: %d\n", num_slices);
346 return -1;
347 }
348 slice->next = 0;
350 img_init(&slice->img);
351 if(img_load(&slice->img, argv[i]) == -1) {
352 fprintf(stderr, "failed to load volume slice %d: %s\n", num_slices, argv[i]);
353 free(slice);
354 return -1;
355 }
356 img_convert(&slice->img, IMG_FMT_GREY8);
358 if(num_slices > 0 && (xres != slice->img.width || yres != slice->img.height)) {
359 fprintf(stderr, "error: slice %d (%s) is %dx%d, up to now we had %dx%d images\n", num_slices, argv[i],
360 slice->img.width, slice->img.height, xres, yres);
361 img_destroy(&slice->img);
362 free(slice);
363 return -1;
364 }
365 xres = slice->img.width;
366 yres = slice->img.height;
368 if(head) {
369 tail->next = slice;
370 tail = slice;
371 } else {
372 head = tail = slice;
373 }
374 printf("loaded volume slice %d: %s\n", num_slices++, argv[i]);
375 }
376 }
378 if(!head) {
379 fprintf(stderr, "you must specify a list of images for the volume data slices\n");
380 return -1;
381 }
383 if(!(volume = malloc(num_slices * sizeof *volume))) {
384 fprintf(stderr, "failed to allocate volume data (%d slices)\n", num_slices);
385 return -1;
386 }
388 for(i=0; i<num_slices; i++) {
389 void *tmp;
391 assert(head);
392 volume[i] = head->img;
394 tmp = head;
395 head = head->next;
396 free(tmp);
397 }
399 return 0;
400 }