metasurf

changeset 4:2c575855f707

added simple example
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Oct 2011 23:21:32 +0300
parents 52664d3451ad
children dedd153d2ceb
files .hgignore Makefile.in examples/simple/Makefile examples/simple/simple.c src/metasurf.c src/metasurf.h
diffstat 6 files changed, 172 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Tue Oct 25 13:30:03 2011 +0300
     1.2 +++ b/.hgignore	Tue Oct 25 23:21:32 2011 +0300
     1.3 @@ -5,3 +5,4 @@
     1.4  ^libmetasurf\.a$
     1.5  ^metasurf\.dylib
     1.6  ^Makefile$
     1.7 +\.png$
     2.1 --- a/Makefile.in	Tue Oct 25 13:30:03 2011 +0300
     2.2 +++ b/Makefile.in	Tue Oct 25 23:21:32 2011 +0300
     2.3 @@ -52,6 +52,7 @@
     2.4  	cp src/$(hdr) $(PREFIX)/include/$(hdr)
     2.5  	cp $(lib_so) $(PREFIX)/lib/$(lib_so)
     2.6  	[ -n "$(soname)" ] \
     2.7 +		&& rm -f $(PREFIX)/lib/$(soname) $(PREFIX)/lib/$(devlink) \
     2.8  		&& ln -s $(PREFIX)/lib/$(lib_so) $(PREFIX)/lib/$(soname) \
     2.9  		&& ln -s $(PREFIX)/lib/$(soname) $(PREFIX)/lib/$(devlink) \
    2.10  		|| true
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/examples/simple/Makefile	Tue Oct 25 23:21:32 2011 +0300
     3.3 @@ -0,0 +1,20 @@
     3.4 +src = $(wildcard *.c)
     3.5 +obj = $(src:.c=.o)
     3.6 +bin = simple
     3.7 +
     3.8 +CC = gcc
     3.9 +CFLAGS = -pedantic -Wall -g -I../../src
    3.10 +LDFLAGS = -L../.. -lmetasurf $(libgl)
    3.11 +
    3.12 +ifeq ($(shell uname -s), Darwin)
    3.13 +	libgl = -framework OpenGL -framework GLUT
    3.14 +else
    3.15 +	libgl = -lGL -lGLU -lglut
    3.16 +endif
    3.17 +
    3.18 +$(bin): $(obj)
    3.19 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    3.20 +
    3.21 +.PHONY: clean
    3.22 +clean:
    3.23 +	rm -f $(obj) $(bin)
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/examples/simple/simple.c	Tue Oct 25 23:21:32 2011 +0300
     4.3 @@ -0,0 +1,103 @@
     4.4 +#include <stdlib.h>
     4.5 +
     4.6 +#ifndef __APPLE__
     4.7 +#include <GL/glut.h>
     4.8 +#else
     4.9 +#include <GLUT/glut.h>
    4.10 +#endif
    4.11 +
    4.12 +#include "metasurf.h"
    4.13 +
    4.14 +float eval(float x, float y, float z);
    4.15 +void disp(void);
    4.16 +void reshape(int x, int y);
    4.17 +void keyb(unsigned char key, int x, int y);
    4.18 +
    4.19 +struct metasurface *ms;
    4.20 +
    4.21 +int main(int argc, char **argv)
    4.22 +{
    4.23 +	float ldir[] = {-0.3, 0.3, 1, 0};
    4.24 +
    4.25 +	glutInitWindowSize(800, 600);
    4.26 +	glutInit(&argc, argv);
    4.27 +	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    4.28 +	glutCreateWindow("metasurf example: simple");
    4.29 +
    4.30 +	glutDisplayFunc(disp);
    4.31 +	glutReshapeFunc(reshape);
    4.32 +	glutKeyboardFunc(keyb);
    4.33 +
    4.34 +	glEnable(GL_DEPTH_TEST);
    4.35 +	glEnable(GL_CULL_FACE);
    4.36 +
    4.37 +	glEnable(GL_LIGHTING);
    4.38 +	glEnable(GL_LIGHT0);
    4.39 +	glLightfv(GL_LIGHT0, GL_POSITION, ldir);
    4.40 +
    4.41 +	glEnable(GL_NORMALIZE);
    4.42 +
    4.43 +	ms = msurf_create();
    4.44 +	/* consider anything below the threshold (0 by default) to be inside */
    4.45 +	msurf_inside(ms, MSURF_LESS);
    4.46 +	/* set the evaluation callback */
    4.47 +	msurf_eval_func(ms, eval);
    4.48 +	/* pass any vertices and normals generated directly to OpenGL */
    4.49 +	msurf_vertex_func(ms, glVertex3f);
    4.50 +	msurf_normal_func(ms, glNormal3f);
    4.51 +	/* slightly increase the bounds to avoid clipping the unit sphere */
    4.52 +	msurf_bounds(ms, -1.1, -1.1, -1.1, 1.1, 1.1, 1.1);
    4.53 +
    4.54 +	glutMainLoop();
    4.55 +	return 0;
    4.56 +}
    4.57 +
    4.58 +/* the unit sphere is implicitly defined as the locus of points in R3 satisfying
    4.59 + * the equation: x^2 + y^2 + z^2 - 1 = 0
    4.60 + */
    4.61 +float eval(float x, float y, float z)
    4.62 +{
    4.63 +	return (x * x + y * y + z * z) - 1.0;
    4.64 +}
    4.65 +
    4.66 +void disp(void)
    4.67 +{
    4.68 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    4.69 +
    4.70 +	glMatrixMode(GL_MODELVIEW);
    4.71 +	glLoadIdentity();
    4.72 +	glTranslatef(0, 0, -3);
    4.73 +
    4.74 +	glBegin(GL_TRIANGLES);
    4.75 +	msurf_polygonize(ms);
    4.76 +	glEnd();
    4.77 +
    4.78 +	glutSwapBuffers();
    4.79 +}
    4.80 +
    4.81 +void reshape(int x, int y)
    4.82 +{
    4.83 +	glMatrixMode(GL_PROJECTION);
    4.84 +	glLoadIdentity();
    4.85 +	gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
    4.86 +}
    4.87 +
    4.88 +void keyb(unsigned char key, int x, int y)
    4.89 +{
    4.90 +	switch(key) {
    4.91 +	case 27:
    4.92 +		exit(0);
    4.93 +
    4.94 +	case 'w':
    4.95 +		{
    4.96 +			static int wire;
    4.97 +			wire = !wire;
    4.98 +			glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
    4.99 +			glutPostRedisplay();
   4.100 +		}
   4.101 +		break;
   4.102 +
   4.103 +	default:
   4.104 +		break;
   4.105 +	}
   4.106 +}
     5.1 --- a/src/metasurf.c	Tue Oct 25 13:30:03 2011 +0300
     5.2 +++ b/src/metasurf.c	Tue Oct 25 23:21:32 2011 +0300
     5.3 @@ -38,6 +38,9 @@
     5.4  	msurf_vertex_func_t vertex;
     5.5  	msurf_normal_func_t normal;
     5.6  
     5.7 +	float dx, dy, dz;
     5.8 +	int flip;
     5.9 +
    5.10  	vec3 vbuf[3];
    5.11  	int nverts;
    5.12  };
    5.13 @@ -81,9 +84,28 @@
    5.14  	ms->res[0] = ms->res[1] = ms->res[2] = 40;
    5.15  	ms->nverts = 0;
    5.16  
    5.17 +	ms->dx = ms->dy = ms->dz = 0.001;
    5.18 +	ms->flip = 0;
    5.19 +
    5.20  	return 0;
    5.21  }
    5.22  
    5.23 +void msurf_inside(struct metasurface *ms, int inside)
    5.24 +{
    5.25 +	switch(inside) {
    5.26 +	case MSURF_GREATER:
    5.27 +		ms->flip = 0;
    5.28 +		break;
    5.29 +
    5.30 +	case MSURF_LESS:
    5.31 +		ms->flip = 1;
    5.32 +		break;
    5.33 +
    5.34 +	default:
    5.35 +		fprintf(stderr, "msurf_inside expects MSURF_GREATER or MSURF_LESS\n");
    5.36 +	}
    5.37 +}
    5.38 +
    5.39  void msurf_eval_func(struct metasurface *ms, msurf_eval_func_t func)
    5.40  {
    5.41  	ms->eval = func;
    5.42 @@ -219,6 +241,10 @@
    5.43  	vec3 vert[12];
    5.44  	unsigned int code = mc_bitcode(val, ms->thres);
    5.45  
    5.46 +	if(ms->flip) {
    5.47 +		code = ~code & 0xff;
    5.48 +	}
    5.49 +
    5.50  	if(mc_edge_table[code] == 0) {
    5.51  		return;
    5.52  	}
    5.53 @@ -238,6 +264,21 @@
    5.54  	for(i=0; mc_tri_table[code][i] != -1; i+=3) {
    5.55  		for(j=0; j<3; j++) {
    5.56  			float *v = vert[mc_tri_table[code][i + j]];
    5.57 +
    5.58 +			if(ms->normal) {
    5.59 +				float dfdx, dfdy, dfdz;
    5.60 +				dfdx = ms->eval(v[0] - ms->dx, v[1], v[2]) - ms->eval(v[0] + ms->dx, v[1], v[2]);
    5.61 +				dfdy = ms->eval(v[0], v[1] - ms->dy, v[2]) - ms->eval(v[0], v[1] + ms->dy, v[2]);
    5.62 +				dfdz = ms->eval(v[0], v[1], v[2] - ms->dz) - ms->eval(v[0], v[1], v[2] + ms->dz);
    5.63 +
    5.64 +				if(ms->flip) {
    5.65 +					dfdx = -dfdx;
    5.66 +					dfdy = -dfdy;
    5.67 +					dfdz = -dfdz;
    5.68 +				}
    5.69 +				ms->normal(dfdx, dfdy, dfdz);
    5.70 +			}
    5.71 +
    5.72  			ms->vertex(v[0], v[1], v[2]);
    5.73  		}
    5.74  	}
     6.1 --- a/src/metasurf.h	Tue Oct 25 13:30:03 2011 +0300
     6.2 +++ b/src/metasurf.h	Tue Oct 25 23:21:32 2011 +0300
     6.3 @@ -19,6 +19,9 @@
     6.4  #ifndef METASURF_H_
     6.5  #define METASURF_H_
     6.6  
     6.7 +#define MSURF_GREATER	1
     6.8 +#define MSURF_LESS		0
     6.9 +
    6.10  struct metasurface;
    6.11  
    6.12  typedef float (*msurf_eval_func_t)(float, float, float);
    6.13 @@ -32,6 +35,9 @@
    6.14  struct metasurface *msurf_create(void);
    6.15  void msurf_free(struct metasurface *ms);
    6.16  
    6.17 +/* which is inside above or below the threshold */
    6.18 +void msurf_inside(struct metasurface *ms, int inside);
    6.19 +
    6.20  /* set a scalar field evaluator function */
    6.21  void msurf_eval_func(struct metasurface *ms, msurf_eval_func_t func);
    6.22