metasurf

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/examples/simple/simple.c	Tue Oct 25 23:21:32 2011 +0300
     1.3 @@ -0,0 +1,103 @@
     1.4 +#include <stdlib.h>
     1.5 +
     1.6 +#ifndef __APPLE__
     1.7 +#include <GL/glut.h>
     1.8 +#else
     1.9 +#include <GLUT/glut.h>
    1.10 +#endif
    1.11 +
    1.12 +#include "metasurf.h"
    1.13 +
    1.14 +float eval(float x, float y, float z);
    1.15 +void disp(void);
    1.16 +void reshape(int x, int y);
    1.17 +void keyb(unsigned char key, int x, int y);
    1.18 +
    1.19 +struct metasurface *ms;
    1.20 +
    1.21 +int main(int argc, char **argv)
    1.22 +{
    1.23 +	float ldir[] = {-0.3, 0.3, 1, 0};
    1.24 +
    1.25 +	glutInitWindowSize(800, 600);
    1.26 +	glutInit(&argc, argv);
    1.27 +	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    1.28 +	glutCreateWindow("metasurf example: simple");
    1.29 +
    1.30 +	glutDisplayFunc(disp);
    1.31 +	glutReshapeFunc(reshape);
    1.32 +	glutKeyboardFunc(keyb);
    1.33 +
    1.34 +	glEnable(GL_DEPTH_TEST);
    1.35 +	glEnable(GL_CULL_FACE);
    1.36 +
    1.37 +	glEnable(GL_LIGHTING);
    1.38 +	glEnable(GL_LIGHT0);
    1.39 +	glLightfv(GL_LIGHT0, GL_POSITION, ldir);
    1.40 +
    1.41 +	glEnable(GL_NORMALIZE);
    1.42 +
    1.43 +	ms = msurf_create();
    1.44 +	/* consider anything below the threshold (0 by default) to be inside */
    1.45 +	msurf_inside(ms, MSURF_LESS);
    1.46 +	/* set the evaluation callback */
    1.47 +	msurf_eval_func(ms, eval);
    1.48 +	/* pass any vertices and normals generated directly to OpenGL */
    1.49 +	msurf_vertex_func(ms, glVertex3f);
    1.50 +	msurf_normal_func(ms, glNormal3f);
    1.51 +	/* slightly increase the bounds to avoid clipping the unit sphere */
    1.52 +	msurf_bounds(ms, -1.1, -1.1, -1.1, 1.1, 1.1, 1.1);
    1.53 +
    1.54 +	glutMainLoop();
    1.55 +	return 0;
    1.56 +}
    1.57 +
    1.58 +/* the unit sphere is implicitly defined as the locus of points in R3 satisfying
    1.59 + * the equation: x^2 + y^2 + z^2 - 1 = 0
    1.60 + */
    1.61 +float eval(float x, float y, float z)
    1.62 +{
    1.63 +	return (x * x + y * y + z * z) - 1.0;
    1.64 +}
    1.65 +
    1.66 +void disp(void)
    1.67 +{
    1.68 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.69 +
    1.70 +	glMatrixMode(GL_MODELVIEW);
    1.71 +	glLoadIdentity();
    1.72 +	glTranslatef(0, 0, -3);
    1.73 +
    1.74 +	glBegin(GL_TRIANGLES);
    1.75 +	msurf_polygonize(ms);
    1.76 +	glEnd();
    1.77 +
    1.78 +	glutSwapBuffers();
    1.79 +}
    1.80 +
    1.81 +void reshape(int x, int y)
    1.82 +{
    1.83 +	glMatrixMode(GL_PROJECTION);
    1.84 +	glLoadIdentity();
    1.85 +	gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
    1.86 +}
    1.87 +
    1.88 +void keyb(unsigned char key, int x, int y)
    1.89 +{
    1.90 +	switch(key) {
    1.91 +	case 27:
    1.92 +		exit(0);
    1.93 +
    1.94 +	case 'w':
    1.95 +		{
    1.96 +			static int wire;
    1.97 +			wire = !wire;
    1.98 +			glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
    1.99 +			glutPostRedisplay();
   1.100 +		}
   1.101 +		break;
   1.102 +
   1.103 +	default:
   1.104 +		break;
   1.105 +	}
   1.106 +}