# HG changeset patch # User John Tsiombikas # Date 1319518627 -10800 # Node ID dc0e882ec3f94fd04d2eca624a4869ea20510823 # Parent 7aa4627e492bb97d5aacc3f0c3b21a84a592c82f renamed example source file test.c -> metaballs.c diff -r 7aa4627e492b -r dc0e882ec3f9 README --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README Tue Oct 25 07:57:07 2011 +0300 @@ -0,0 +1,49 @@ +metasurf - a library for implicit surface polygonization + +1. Overview +----------- + +Metasurf is a library for implict surface polygonization. You only need to +set a callback that returns the scalar field value at any given point in +3-space, and another callback to accept isosurface vertices. Then at any point +just call msurf_polygonize, and the library handles everything else for you. + +2. Usage +-------- +The following snippet is sufficient to draw the surface of an implict sphere. + + struct metasurface *ms; + + /* initialization */ + ms = msurf_create(); + msurf_eval_func(ms, eval); + msurf_vertex_func(ms, glVertex3f); + + /* drawing */ + glBegin(GL_TRIANGLES); + msurf_polygonize(ms); + glEnd(); + + /* evaluator */ + float eval(float x, float y, float z) + { + return x * x + y * y + z * z; + } + +See the examples subdirectory for more examples. + +3. License +---------- +Copyright: John Tsiombikas + +Metasurf is free software, you may use, modify, and redistribute it freely under +the terms of the GNU Lesser General Public License (LGPL) v3 (or at your option, +any later version published by the Free Software Foundation). See COPYING and +COPYING.LESSER for more details. + + +4. Contributions +---------------- +If you'd like to fix the marching tetrahedra implementation or have any other +ideas for improving this library drop me an email at: nuclear@member.fsf.org. +Also feel free to submit patches for bugfixes. diff -r 7aa4627e492b -r dc0e882ec3f9 examples/metaballs/Makefile --- a/examples/metaballs/Makefile Tue Oct 25 07:34:31 2011 +0300 +++ b/examples/metaballs/Makefile Tue Oct 25 07:57:07 2011 +0300 @@ -1,11 +1,11 @@ src = $(wildcard src/*.c) obj = $(src:.c=.o) dep = $(obj:.o=.d) -bin = metasurf +bin = metaballs CC = gcc -CFLAGS = -pedantic -Wall -g -O3 -LDFLAGS = $(libgl) +CFLAGS = -pedantic -Wall -g -I../../src +LDFLAGS = -L../.. -lmetasurf $(libgl) ifeq ($(shell uname -s), Darwin) libgl = -framework OpenGL -framework GLUT -lGLEW diff -r 7aa4627e492b -r dc0e882ec3f9 examples/metaballs/src/metaballs.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/metaballs/src/metaballs.c Tue Oct 25 07:57:07 2011 +0300 @@ -0,0 +1,307 @@ +#include +#include +#include +#include + +#include +#ifndef __APPLE__ +#include +#else +#include +#endif + +#include "cam.h" +#include "sdr.h" +#include "metasurf.h" + +#define RES 38 + +struct metaball { + float energy; + float x, y, z; +} mball[] = { + {1.0, 0, 0, 0}, + {0.25, 0.45, 0, 0.25}, + {0.15, -0.3, 0.2, 0.1} +}; + +int num_mballs = sizeof mball / sizeof *mball; + +float eval(float x, float y, float z); +float eval_cached(float x, float y, float z); +void vertex(float x, float y, float z); +void render(void); +void disp(void); +void reshape(int x, int y); +void keyb(unsigned char key, int x, int y); +void mouse(int bn, int state, int x, int y); +void motion(int x, int y); +void sball_button(int bn, int state); +void sball_motion(int x, int y, int z); +int parse_args(int argc, char **argv); + +int stereo; +struct metasurface *msurf; +float threshold = 12; +unsigned int sdr; +int bidx = 1; + +int main(int argc, char **argv) +{ + float amb[] = {0, 0, 0, 0}; + float lpos[] = {-0.2, 0.2, 1, 0}; + + glutInitWindowSize(1280, 720); + glutInit(&argc, argv); + + if(parse_args(argc, argv) == -1) { + return 1; + } + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (stereo ? GLUT_STEREO : 0)); + glutCreateWindow("metasurf"); + + glutDisplayFunc(disp); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); + glutMouseFunc(mouse); + glutMotionFunc(motion); + glutSpaceballButtonFunc(sball_button); + glutSpaceballMotionFunc(sball_motion); + + glewInit(); + + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb); + + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT0, GL_POSITION, lpos); + + glEnable(GL_NORMALIZE); + + cam_focus_dist(2.0); + cam_clip(0.1, 200.0); + cam_rotate(0, 0); + cam_dolly(2); + + msurf = msurf_create(); + msurf_eval_func(msurf, eval); + msurf_vertex_func(msurf, vertex); + msurf_threshold(msurf, threshold); + msurf_resolution(msurf, RES, RES, RES); + msurf_bounds(msurf, -1, -1, -1, 1, 1, 1); + + glClearColor(0.8, 0.8, 0.8, 1.0); + + if(!(sdr = create_program_load("sdr/vert.glsl", "sdr/frag.glsl"))) { + return 1; + } + + glutMainLoop(); + return 0; +} + +float eval(float x, float y, float z) +{ + int i; + float val = 0.0f; + + for(i=0; i -#include -#include -#include - -#include -#ifndef __APPLE__ -#include -#else -#include -#endif - -#include "cam.h" -#include "sdr.h" -#include "metasurf.h" - -#define RES 38 - -struct metaball { - float energy; - float x, y, z; -} mball[] = { - {1.0, 0, 0, 0}, - {0.25, 0.45, 0, 0.25}, - {0.15, -0.3, 0.2, 0.1} -}; - -int num_mballs = sizeof mball / sizeof *mball; - -float eval(float x, float y, float z); -float eval_cached(float x, float y, float z); -void vertex(float x, float y, float z); -void render(void); -void disp(void); -void reshape(int x, int y); -void keyb(unsigned char key, int x, int y); -void mouse(int bn, int state, int x, int y); -void motion(int x, int y); -void sball_button(int bn, int state); -void sball_motion(int x, int y, int z); -int parse_args(int argc, char **argv); - -int stereo; -struct metasurface *msurf; -float threshold = 12; -unsigned int sdr; -int bidx = 1; - -int main(int argc, char **argv) -{ - float amb[] = {0, 0, 0, 0}; - float lpos[] = {-0.2, 0.2, 1, 0}; - - glutInitWindowSize(1280, 720); - glutInit(&argc, argv); - - if(parse_args(argc, argv) == -1) { - return 1; - } - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (stereo ? GLUT_STEREO : 0)); - glutCreateWindow("metasurf"); - - glutDisplayFunc(disp); - glutReshapeFunc(reshape); - glutKeyboardFunc(keyb); - glutMouseFunc(mouse); - glutMotionFunc(motion); - glutSpaceballButtonFunc(sball_button); - glutSpaceballMotionFunc(sball_motion); - - glewInit(); - - glEnable(GL_CULL_FACE); - glEnable(GL_DEPTH_TEST); - - glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb); - - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - glLightfv(GL_LIGHT0, GL_POSITION, lpos); - - glEnable(GL_NORMALIZE); - - cam_focus_dist(2.0); - cam_clip(0.1, 200.0); - cam_rotate(0, 0); - cam_dolly(2); - - msurf = msurf_create(); - msurf_eval_func(msurf, eval); - msurf_vertex_func(msurf, vertex); - msurf_threshold(msurf, threshold); - msurf_resolution(msurf, RES, RES, RES); - msurf_bounds(msurf, -1, -1, -1, 1, 1, 1); - - glClearColor(0.8, 0.8, 0.8, 1.0); - - if(!(sdr = create_program_load("sdr/vert.glsl", "sdr/frag.glsl"))) { - return 1; - } - - glutMainLoop(); - return 0; -} - -float eval(float x, float y, float z) -{ - int i; - float val = 0.0f; - - for(i=0; i