metasurf

annotate examples/metaballs/src/metaballs.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
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <math.h>
nuclear@0 4 #include <assert.h>
nuclear@0 5
nuclear@2 6 #ifndef NO_SHADERS
nuclear@0 7 #include <GL/glew.h>
nuclear@2 8 #include "sdr.h"
nuclear@2 9 #endif
nuclear@2 10
nuclear@0 11 #ifndef __APPLE__
nuclear@0 12 #include <GL/glut.h>
nuclear@0 13 #else
nuclear@0 14 #include <GLUT/glut.h>
nuclear@0 15 #endif
nuclear@0 16
nuclear@0 17 #include "cam.h"
nuclear@0 18 #include "metasurf.h"
nuclear@0 19
nuclear@0 20 #define RES 38
nuclear@0 21
nuclear@0 22 struct metaball {
nuclear@0 23 float energy;
nuclear@0 24 float x, y, z;
nuclear@0 25 } mball[] = {
nuclear@0 26 {1.0, 0, 0, 0},
nuclear@0 27 {0.25, 0.45, 0, 0.25},
nuclear@0 28 {0.15, -0.3, 0.2, 0.1}
nuclear@0 29 };
nuclear@0 30
nuclear@0 31 int num_mballs = sizeof mball / sizeof *mball;
nuclear@0 32
nuclear@0 33 float eval(float x, float y, float z);
nuclear@0 34 void vertex(float x, float y, float z);
nuclear@0 35 void render(void);
nuclear@0 36 void disp(void);
nuclear@0 37 void reshape(int x, int y);
nuclear@0 38 void keyb(unsigned char key, int x, int y);
nuclear@0 39 void mouse(int bn, int state, int x, int y);
nuclear@0 40 void motion(int x, int y);
nuclear@0 41 void sball_button(int bn, int state);
nuclear@0 42 void sball_motion(int x, int y, int z);
nuclear@0 43 int parse_args(int argc, char **argv);
nuclear@0 44
nuclear@2 45 int stereo, fullscreen;
nuclear@2 46 int orig_xsz, orig_ysz;
nuclear@2 47
nuclear@0 48 struct metasurface *msurf;
nuclear@0 49 float threshold = 12;
nuclear@2 50 #ifndef NO_SHADERS
nuclear@0 51 unsigned int sdr;
nuclear@2 52 #endif
nuclear@0 53 int bidx = 1;
nuclear@0 54
nuclear@0 55 int main(int argc, char **argv)
nuclear@0 56 {
nuclear@0 57 float amb[] = {0, 0, 0, 0};
nuclear@0 58 float lpos[] = {-0.2, 0.2, 1, 0};
nuclear@0 59
nuclear@0 60 glutInitWindowSize(1280, 720);
nuclear@0 61 glutInit(&argc, argv);
nuclear@0 62
nuclear@0 63 if(parse_args(argc, argv) == -1) {
nuclear@0 64 return 1;
nuclear@0 65 }
nuclear@0 66 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (stereo ? GLUT_STEREO : 0));
nuclear@0 67 glutCreateWindow("metasurf");
nuclear@0 68
nuclear@2 69 orig_xsz = glutGet(GLUT_WINDOW_WIDTH);
nuclear@2 70 orig_ysz = glutGet(GLUT_WINDOW_HEIGHT);
nuclear@2 71
nuclear@2 72 if(fullscreen) {
nuclear@2 73 glutFullScreen();
nuclear@2 74 }
nuclear@2 75
nuclear@0 76 glutDisplayFunc(disp);
nuclear@0 77 glutReshapeFunc(reshape);
nuclear@0 78 glutKeyboardFunc(keyb);
nuclear@0 79 glutMouseFunc(mouse);
nuclear@0 80 glutMotionFunc(motion);
nuclear@0 81 glutSpaceballButtonFunc(sball_button);
nuclear@0 82 glutSpaceballMotionFunc(sball_motion);
nuclear@0 83
nuclear@2 84 #ifndef NO_SHADERS
nuclear@0 85 glewInit();
nuclear@2 86 if(!(sdr = create_program_load("sdr/vert.glsl", "sdr/frag.glsl"))) {
nuclear@2 87 return 1;
nuclear@2 88 }
nuclear@2 89 #endif
nuclear@0 90
nuclear@0 91 glEnable(GL_CULL_FACE);
nuclear@0 92 glEnable(GL_DEPTH_TEST);
nuclear@0 93
nuclear@0 94 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
nuclear@0 95
nuclear@0 96 glEnable(GL_LIGHTING);
nuclear@0 97 glEnable(GL_LIGHT0);
nuclear@0 98 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@0 99
nuclear@0 100 glEnable(GL_NORMALIZE);
nuclear@0 101
nuclear@0 102 cam_focus_dist(2.0);
nuclear@0 103 cam_clip(0.1, 200.0);
nuclear@0 104 cam_rotate(0, 0);
nuclear@0 105 cam_dolly(2);
nuclear@0 106
nuclear@0 107 msurf = msurf_create();
nuclear@0 108 msurf_eval_func(msurf, eval);
nuclear@0 109 msurf_vertex_func(msurf, vertex);
nuclear@0 110 msurf_threshold(msurf, threshold);
nuclear@0 111 msurf_resolution(msurf, RES, RES, RES);
nuclear@0 112 msurf_bounds(msurf, -1, -1, -1, 1, 1, 1);
nuclear@0 113
nuclear@0 114 glClearColor(0.8, 0.8, 0.8, 1.0);
nuclear@0 115
nuclear@0 116 glutMainLoop();
nuclear@0 117 return 0;
nuclear@0 118 }
nuclear@0 119
nuclear@0 120 float eval(float x, float y, float z)
nuclear@0 121 {
nuclear@0 122 int i;
nuclear@0 123 float val = 0.0f;
nuclear@0 124
nuclear@0 125 for(i=0; i<num_mballs; i++) {
nuclear@0 126 float dx = mball[i].x - x;
nuclear@0 127 float dy = mball[i].y - y;
nuclear@0 128 float dz = mball[i].z - z;
nuclear@0 129 float dist_sq = dx * dx + dy * dy + dz * dz;
nuclear@0 130
nuclear@0 131 if(dist_sq < 1e-6) {
nuclear@0 132 val += 100.0;
nuclear@0 133 } else {
nuclear@0 134 val += mball[i].energy / dist_sq;
nuclear@0 135 }
nuclear@0 136 }
nuclear@0 137 return val;
nuclear@0 138 }
nuclear@0 139
nuclear@0 140 void vertex(float x, float y, float z)
nuclear@0 141 {
nuclear@0 142 const float dt = 0.001;
nuclear@0 143 float dfdx = eval(x - dt, y, z) - eval(x + dt, y, z);
nuclear@0 144 float dfdy = eval(x, y - dt, z) - eval(x, y + dt, z);
nuclear@0 145 float dfdz = eval(x, y, z - dt) - eval(x, y, z + dt);
nuclear@0 146
nuclear@0 147 glNormal3f(dfdx, dfdy, dfdz);
nuclear@0 148 glVertex3f(x, y, z);
nuclear@0 149 }
nuclear@0 150
nuclear@0 151 void render(void)
nuclear@0 152 {
nuclear@0 153 float kd[] = {0.7, 0.28, 0.2, 1.0};
nuclear@0 154 float ks[] = {0.9, 0.9, 0.9, 1.0};
nuclear@0 155
nuclear@0 156 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, kd);
nuclear@0 157 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ks);
nuclear@0 158 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
nuclear@0 159
nuclear@2 160 #ifndef NO_SHADERS
nuclear@0 161 bind_program(sdr);
nuclear@2 162 #endif
nuclear@0 163
nuclear@0 164 glBegin(GL_TRIANGLES);
nuclear@0 165 msurf_polygonize(msurf);
nuclear@0 166 glEnd();
nuclear@0 167
nuclear@0 168 assert(glGetError() == GL_NO_ERROR);
nuclear@0 169 }
nuclear@0 170
nuclear@0 171 void disp(void)
nuclear@0 172 {
nuclear@0 173 if(stereo) {
nuclear@0 174 glDrawBuffer(GL_BACK_LEFT);
nuclear@0 175 }
nuclear@0 176
nuclear@0 177 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 178
nuclear@0 179 glMatrixMode(GL_PROJECTION);
nuclear@0 180 glLoadIdentity();
nuclear@0 181 cam_stereo_proj_matrix(stereo ? CAM_LEFT : CAM_CENTER);
nuclear@0 182
nuclear@0 183 glMatrixMode(GL_MODELVIEW);
nuclear@0 184 glLoadIdentity();
nuclear@0 185 cam_stereo_view_matrix(stereo ? CAM_LEFT : CAM_CENTER);
nuclear@0 186
nuclear@0 187 render();
nuclear@0 188
nuclear@0 189 if(stereo) {
nuclear@0 190 glDrawBuffer(GL_BACK_RIGHT);
nuclear@0 191 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 192
nuclear@0 193 glMatrixMode(GL_PROJECTION);
nuclear@0 194 glLoadIdentity();
nuclear@0 195 cam_stereo_proj_matrix(CAM_RIGHT);
nuclear@0 196
nuclear@0 197 glMatrixMode(GL_MODELVIEW);
nuclear@0 198 glLoadIdentity();
nuclear@0 199 cam_stereo_view_matrix(CAM_RIGHT);
nuclear@0 200
nuclear@0 201 render();
nuclear@0 202 }
nuclear@0 203 glutSwapBuffers();
nuclear@0 204 }
nuclear@0 205
nuclear@0 206 void reshape(int x, int y)
nuclear@0 207 {
nuclear@0 208 glViewport(0, 0, x, y);
nuclear@0 209 cam_aspect((float)x / (float)y);
nuclear@0 210 }
nuclear@0 211
nuclear@0 212 void keyb(unsigned char key, int x, int y)
nuclear@0 213 {
nuclear@0 214 static int wire;
nuclear@0 215
nuclear@0 216 switch(key) {
nuclear@0 217 case 27:
nuclear@0 218 exit(0);
nuclear@0 219
nuclear@2 220 case 'f':
nuclear@2 221 fullscreen = !fullscreen;
nuclear@2 222 if(fullscreen) {
nuclear@2 223 glutFullScreen();
nuclear@2 224 } else {
nuclear@2 225 glutReshapeWindow(orig_xsz, orig_ysz);
nuclear@2 226 }
nuclear@2 227 break;
nuclear@2 228
nuclear@0 229 case 's':
nuclear@0 230 stereo = !stereo;
nuclear@0 231 glutPostRedisplay();
nuclear@0 232 break;
nuclear@0 233
nuclear@0 234 case 'w':
nuclear@0 235 wire = !wire;
nuclear@0 236 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
nuclear@0 237 glutPostRedisplay();
nuclear@0 238 break;
nuclear@0 239
nuclear@0 240 case '=':
nuclear@0 241 threshold += 0.05;
nuclear@0 242 msurf_threshold(msurf, threshold);
nuclear@0 243 printf("threshold: %f\n", threshold);
nuclear@0 244 glutPostRedisplay();
nuclear@0 245 break;
nuclear@0 246
nuclear@0 247 case '-':
nuclear@0 248 threshold -= 0.05;
nuclear@0 249 msurf_threshold(msurf, threshold);
nuclear@0 250 printf("threshold: %f\n", threshold);
nuclear@0 251 glutPostRedisplay();
nuclear@0 252 break;
nuclear@0 253
nuclear@0 254 case ' ':
nuclear@0 255 bidx = (bidx + 1) % num_mballs;
nuclear@0 256 break;
nuclear@0 257
nuclear@0 258 default:
nuclear@0 259 break;
nuclear@0 260 }
nuclear@0 261 }
nuclear@0 262
nuclear@0 263 int bnstate[32];
nuclear@0 264 int prev_x, prev_y;
nuclear@5 265 int mod;
nuclear@0 266
nuclear@0 267 void mouse(int bn, int state, int x, int y)
nuclear@0 268 {
nuclear@0 269 bnstate[bn] = state == GLUT_DOWN;
nuclear@0 270 prev_x = x;
nuclear@0 271 prev_y = y;
nuclear@5 272 mod = glutGetModifiers();
nuclear@0 273 }
nuclear@0 274
nuclear@0 275 void motion(int x, int y)
nuclear@0 276 {
nuclear@0 277 int dx, dy;
nuclear@0 278
nuclear@0 279 dx = x - prev_x;
nuclear@0 280 dy = y - prev_y;
nuclear@0 281 prev_x = x;
nuclear@0 282 prev_y = y;
nuclear@0 283
nuclear@5 284 if(mod) {
nuclear@2 285 if(bnstate[GLUT_LEFT_BUTTON]) {
nuclear@2 286 cam_inp_rotate(dx, dy);
nuclear@2 287 }
nuclear@2 288 if(bnstate[GLUT_RIGHT_BUTTON]) {
nuclear@2 289 cam_inp_zoom(dy);
nuclear@2 290 }
nuclear@2 291 } else {
nuclear@2 292 mball[bidx].x += (float)dx * 0.005;
nuclear@2 293 mball[bidx].y -= (float)dy * 0.005;
nuclear@0 294 }
nuclear@2 295 glutPostRedisplay();
nuclear@0 296 }
nuclear@0 297
nuclear@0 298 void sball_button(int bn, int state)
nuclear@0 299 {
nuclear@0 300 if(state) return;
nuclear@0 301
nuclear@0 302 if(bn < num_mballs) {
nuclear@0 303 bidx = bn;
nuclear@0 304 } else {
nuclear@0 305 bidx = (bidx + 1) % num_mballs;
nuclear@0 306 }
nuclear@0 307 }
nuclear@0 308
nuclear@0 309 void sball_motion(int x, int y, int z)
nuclear@0 310 {
nuclear@2 311 mball[bidx].x += (float)x / 16384.0;
nuclear@2 312 mball[bidx].y += (float)y / 16384.0;
nuclear@2 313 mball[bidx].z -= (float)z / 16384.0;
nuclear@0 314 glutPostRedisplay();
nuclear@0 315 }
nuclear@0 316
nuclear@0 317 int parse_args(int argc, char **argv)
nuclear@0 318 {
nuclear@0 319 int i;
nuclear@0 320
nuclear@0 321 for(i=1; i<argc; i++) {
nuclear@0 322 if(argv[i][0] == '-' && argv[i][2] == 0) {
nuclear@0 323 switch(argv[i][1]) {
nuclear@2 324 case 'f':
nuclear@2 325 fullscreen = !fullscreen;
nuclear@2 326 break;
nuclear@2 327
nuclear@0 328 case 's':
nuclear@0 329 stereo = !stereo;
nuclear@0 330 break;
nuclear@0 331
nuclear@2 332 case 'h':
nuclear@2 333 printf("usage: %s [opt]\n", argv[0]);
nuclear@2 334 printf("options:\n");
nuclear@2 335 printf(" -f start in fullscreen\n");
nuclear@2 336 printf(" -s enable stereoscopic rendering\n");
nuclear@2 337 printf(" -h print usage and exit\n");
nuclear@2 338 exit(0);
nuclear@2 339
nuclear@0 340 default:
nuclear@0 341 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
nuclear@0 342 return -1;
nuclear@0 343 }
nuclear@0 344 } else {
nuclear@0 345 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
nuclear@0 346 return -1;
nuclear@0 347 }
nuclear@0 348 }
nuclear@0 349 return 0;
nuclear@0 350 }