metasurf

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