metasurf

annotate examples/simple/simple.c @ 4:2c575855f707

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