metasurf

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