metasurf

view examples/simple/simple.c @ 11:430d8dde62aa

random changes
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 23 Aug 2015 07:17:56 +0300
parents
children
line source
1 #include <stdlib.h>
3 #ifndef __APPLE__
4 #include <GL/glut.h>
5 #else
6 #include <GLUT/glut.h>
7 #endif
9 #include "metasurf.h"
11 float eval(float x, float y, float z);
12 void disp(void);
13 void reshape(int x, int y);
14 void keyb(unsigned char key, int x, int y);
16 struct metasurface *ms;
18 int main(int argc, char **argv)
19 {
20 float ldir[] = {-0.3, 0.3, 1, 0};
22 glutInitWindowSize(800, 600);
23 glutInit(&argc, argv);
24 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
25 glutCreateWindow("metasurf example: simple");
27 glutDisplayFunc(disp);
28 glutReshapeFunc(reshape);
29 glutKeyboardFunc(keyb);
31 glEnable(GL_DEPTH_TEST);
32 glEnable(GL_CULL_FACE);
34 glEnable(GL_LIGHTING);
35 glEnable(GL_LIGHT0);
36 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
38 glEnable(GL_NORMALIZE);
40 ms = msurf_create();
41 /* consider anything below the threshold (0 by default) to be inside */
42 msurf_inside(ms, MSURF_LESS);
43 /* set the evaluation callback */
44 msurf_eval_func(ms, eval);
45 /* pass any vertices and normals generated directly to OpenGL */
46 msurf_vertex_func(ms, glVertex3f);
47 msurf_normal_func(ms, glNormal3f);
48 /* slightly increase the bounds to avoid clipping the unit sphere */
49 msurf_bounds(ms, -1.1, -1.1, -1.1, 1.1, 1.1, 1.1);
51 glutMainLoop();
52 return 0;
53 }
55 /* the unit sphere is implicitly defined as the locus of points in R3 satisfying
56 * the equation: x^2 + y^2 + z^2 - 1 = 0
57 */
58 float eval(float x, float y, float z)
59 {
60 return (x * x + y * y + z * z) - 1.0;
61 }
63 void disp(void)
64 {
65 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
67 glMatrixMode(GL_MODELVIEW);
68 glLoadIdentity();
69 glTranslatef(0, 0, -3);
71 glBegin(GL_TRIANGLES);
72 msurf_polygonize(ms);
73 glEnd();
75 glutSwapBuffers();
76 }
78 void reshape(int x, int y)
79 {
80 glMatrixMode(GL_PROJECTION);
81 glLoadIdentity();
82 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
83 }
85 void keyb(unsigned char key, int x, int y)
86 {
87 switch(key) {
88 case 27:
89 exit(0);
91 case 'w':
92 {
93 static int wire;
94 wire = !wire;
95 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
96 glutPostRedisplay();
97 }
98 break;
100 default:
101 break;
102 }
103 }