# HG changeset patch # User John Tsiombikas # Date 1361152012 -7200 # Node ID e09cbb2e9d4f8c46bb488bca00838d9c54c804c8 # Parent 5fb21401b7c8d76412fd6b7d92ff61dec683d515 metaballs diff -r 5fb21401b7c8 -r e09cbb2e9d4f Makefile --- a/Makefile Thu Feb 07 18:52:28 2013 +0200 +++ b/Makefile Mon Feb 18 03:46:52 2013 +0200 @@ -1,12 +1,13 @@ -csrc = $(wildcard src/*.c) +csrc = $(wildcard src/*.c) \ + $(wildcard libs/metasurf/*.c) ccsrc = $(wildcard src/*.cc) obj = $(csrc:.c=.o) $(ccsrc:.cc=.o) dep = $(obj:.o=.d) bin = udg -CFLAGS = -pedantic -Wall -g +CFLAGS = -pedantic -Wall -g -O3 -Ilibs/metasurf CXXFLAGS = -std=c++11 $(CFLAGS) -LDFLAGS = $(libgl) -limago +LDFLAGS = $(libgl) -limago -lvmath ifeq ($(shell uname -s), Darwin) libgl = -framework OpenGL -framework GLUT -lGLEW diff -r 5fb21401b7c8 -r e09cbb2e9d4f libs/metasurf/mcubes.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libs/metasurf/mcubes.h Mon Feb 18 03:46:52 2013 +0200 @@ -0,0 +1,294 @@ +static int mc_edge_table[256] = { + 0x0 , 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c, + 0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00, + 0x190, 0x99 , 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c, + 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90, + 0x230, 0x339, 0x33 , 0x13a, 0x636, 0x73f, 0x435, 0x53c, + 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30, + 0x3a0, 0x2a9, 0x1a3, 0xaa , 0x7a6, 0x6af, 0x5a5, 0x4ac, + 0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0, + 0x460, 0x569, 0x663, 0x76a, 0x66 , 0x16f, 0x265, 0x36c, + 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60, + 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff , 0x3f5, 0x2fc, + 0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0, + 0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55 , 0x15c, + 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950, + 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc , + 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0, + 0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc, + 0xcc , 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0, + 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c, + 0x15c, 0x55 , 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650, + 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc, + 0x2fc, 0x3f5, 0xff , 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0, + 0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c, + 0x36c, 0x265, 0x16f, 0x66 , 0x76a, 0x663, 0x569, 0x460, + 0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac, + 0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa , 0x1a3, 0x2a9, 0x3a0, + 0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c, + 0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33 , 0x339, 0x230, + 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c, + 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190, + 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c, + 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0 +}; + + +static int mc_tri_table[256][16] = { + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {1, 8, 3, 9, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 8, 3, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {9, 2, 10, 0, 2, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {2, 8, 3, 2, 10, 8, 10, 9, 8, -1, -1, -1, -1, -1, -1, -1}, + {3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 11, 2, 8, 11, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {1, 9, 0, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {1, 11, 2, 1, 9, 11, 9, 8, 11, -1, -1, -1, -1, -1, -1, -1}, + {3, 10, 1, 11, 10, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 10, 1, 0, 8, 10, 8, 11, 10, -1, -1, -1, -1, -1, -1, -1}, + {3, 9, 0, 3, 11, 9, 11, 10, 9, -1, -1, -1, -1, -1, -1, -1}, + {9, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {4, 3, 0, 7, 3, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 1, 9, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {4, 1, 9, 4, 7, 1, 7, 3, 1, -1, -1, -1, -1, -1, -1, -1}, + {1, 2, 10, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {3, 4, 7, 3, 0, 4, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1}, + {9, 2, 10, 9, 0, 2, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1}, + {2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, -1, -1, -1, -1}, + {8, 4, 7, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {11, 4, 7, 11, 2, 4, 2, 0, 4, -1, -1, -1, -1, -1, -1, -1}, + {9, 0, 1, 8, 4, 7, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1}, + {4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, -1, -1, -1, -1}, + {3, 10, 1, 3, 11, 10, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1}, + {1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, -1, -1, -1, -1}, + {4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, -1, -1, -1, -1}, + {4, 7, 11, 4, 11, 9, 9, 11, 10, -1, -1, -1, -1, -1, -1, -1}, + {9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {9, 5, 4, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 5, 4, 1, 5, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {8, 5, 4, 8, 3, 5, 3, 1, 5, -1, -1, -1, -1, -1, -1, -1}, + {1, 2, 10, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {3, 0, 8, 1, 2, 10, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1}, + {5, 2, 10, 5, 4, 2, 4, 0, 2, -1, -1, -1, -1, -1, -1, -1}, + {2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, -1, -1, -1, -1}, + {9, 5, 4, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 11, 2, 0, 8, 11, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1}, + {0, 5, 4, 0, 1, 5, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1}, + {2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, -1, -1, -1, -1}, + {10, 3, 11, 10, 1, 3, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1}, + {4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, -1, -1, -1, -1}, + {5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, -1, -1, -1, -1}, + {5, 4, 8, 5, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1}, + {9, 7, 8, 5, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {9, 3, 0, 9, 5, 3, 5, 7, 3, -1, -1, -1, -1, -1, -1, -1}, + {0, 7, 8, 0, 1, 7, 1, 5, 7, -1, -1, -1, -1, -1, -1, -1}, + {1, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {9, 7, 8, 9, 5, 7, 10, 1, 2, -1, -1, -1, -1, -1, -1, -1}, + {10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, -1, -1, -1, -1}, + {8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, -1, -1, -1, -1}, + {2, 10, 5, 2, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1}, + {7, 9, 5, 7, 8, 9, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1}, + {9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, -1, -1, -1, -1}, + {2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, -1, -1, -1, -1}, + {11, 2, 1, 11, 1, 7, 7, 1, 5, -1, -1, -1, -1, -1, -1, -1}, + {9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, -1, -1, -1, -1}, + {5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, -1}, + {11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, -1}, + {11, 10, 5, 7, 11, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 8, 3, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {9, 0, 1, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {1, 8, 3, 1, 9, 8, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1}, + {1, 6, 5, 2, 6, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {1, 6, 5, 1, 2, 6, 3, 0, 8, -1, -1, -1, -1, -1, -1, -1}, + {9, 6, 5, 9, 0, 6, 0, 2, 6, -1, -1, -1, -1, -1, -1, -1}, + {5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, -1, -1, -1, -1}, + {2, 3, 11, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {11, 0, 8, 11, 2, 0, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1}, + {0, 1, 9, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1}, + {5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, -1, -1, -1, -1}, + {6, 3, 11, 6, 5, 3, 5, 1, 3, -1, -1, -1, -1, -1, -1, -1}, + {0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, -1, -1, -1, -1}, + {3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, -1, -1, -1, -1}, + {6, 5, 9, 6, 9, 11, 11, 9, 8, -1, -1, -1, -1, -1, -1, -1}, + {5, 10, 6, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {4, 3, 0, 4, 7, 3, 6, 5, 10, -1, -1, -1, -1, -1, -1, -1}, + {1, 9, 0, 5, 10, 6, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1}, + {10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, -1, -1, -1, -1}, + {6, 1, 2, 6, 5, 1, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1}, + {1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, -1, -1, -1, -1}, + {8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, -1, -1, -1, -1}, + {7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, -1}, + {3, 11, 2, 7, 8, 4, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1}, + {5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, -1, -1, -1, -1}, + {0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1}, + {9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, -1}, + {8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, -1, -1, -1, -1}, + {5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, -1}, + {0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, -1}, + {6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, -1, -1, -1, -1}, + {10, 4, 9, 6, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {4, 10, 6, 4, 9, 10, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1}, + {10, 0, 1, 10, 6, 0, 6, 4, 0, -1, -1, -1, -1, -1, -1, -1}, + {8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, -1, -1, -1, -1}, + {1, 4, 9, 1, 2, 4, 2, 6, 4, -1, -1, -1, -1, -1, -1, -1}, + {3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, -1, -1, -1, -1}, + {0, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {8, 3, 2, 8, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1}, + {10, 4, 9, 10, 6, 4, 11, 2, 3, -1, -1, -1, -1, -1, -1, -1}, + {0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, -1, -1, -1, -1}, + {3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, -1, -1, -1, -1}, + {6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, -1}, + {9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, -1, -1, -1, -1}, + {8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, -1}, + {3, 11, 6, 3, 6, 0, 0, 6, 4, -1, -1, -1, -1, -1, -1, -1}, + {6, 4, 8, 11, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {7, 10, 6, 7, 8, 10, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1}, + {0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, -1, -1, -1, -1}, + {10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, -1, -1, -1, -1}, + {10, 6, 7, 10, 7, 1, 1, 7, 3, -1, -1, -1, -1, -1, -1, -1}, + {1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, -1, -1, -1, -1}, + {2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, -1}, + {7, 8, 0, 7, 0, 6, 6, 0, 2, -1, -1, -1, -1, -1, -1, -1}, + {7, 3, 2, 6, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, -1, -1, -1, -1}, + {2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, -1}, + {1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, -1}, + {11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, -1, -1, -1, -1}, + {8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, -1}, + {0, 9, 1, 11, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, -1, -1, -1, -1}, + {7, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {3, 0, 8, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 1, 9, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {8, 1, 9, 8, 3, 1, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1}, + {10, 1, 2, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {1, 2, 10, 3, 0, 8, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1}, + {2, 9, 0, 2, 10, 9, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1}, + {6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, -1, -1, -1, -1}, + {7, 2, 3, 6, 2, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {7, 0, 8, 7, 6, 0, 6, 2, 0, -1, -1, -1, -1, -1, -1, -1}, + {2, 7, 6, 2, 3, 7, 0, 1, 9, -1, -1, -1, -1, -1, -1, -1}, + {1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, -1, -1, -1, -1}, + {10, 7, 6, 10, 1, 7, 1, 3, 7, -1, -1, -1, -1, -1, -1, -1}, + {10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, -1, -1, -1, -1}, + {0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, -1, -1, -1, -1}, + {7, 6, 10, 7, 10, 8, 8, 10, 9, -1, -1, -1, -1, -1, -1, -1}, + {6, 8, 4, 11, 8, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {3, 6, 11, 3, 0, 6, 0, 4, 6, -1, -1, -1, -1, -1, -1, -1}, + {8, 6, 11, 8, 4, 6, 9, 0, 1, -1, -1, -1, -1, -1, -1, -1}, + {9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, -1, -1, -1, -1}, + {6, 8, 4, 6, 11, 8, 2, 10, 1, -1, -1, -1, -1, -1, -1, -1}, + {1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, -1, -1, -1, -1}, + {4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, -1, -1, -1, -1}, + {10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, -1}, + {8, 2, 3, 8, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1}, + {0, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, -1, -1, -1, -1}, + {1, 9, 4, 1, 4, 2, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1}, + {8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, -1, -1, -1, -1}, + {10, 1, 0, 10, 0, 6, 6, 0, 4, -1, -1, -1, -1, -1, -1, -1}, + {4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, -1}, + {10, 9, 4, 6, 10, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {4, 9, 5, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 8, 3, 4, 9, 5, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1}, + {5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1}, + {11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, -1, -1, -1, -1}, + {9, 5, 4, 10, 1, 2, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1}, + {6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, -1, -1, -1, -1}, + {7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, -1, -1, -1, -1}, + {3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, -1}, + {7, 2, 3, 7, 6, 2, 5, 4, 9, -1, -1, -1, -1, -1, -1, -1}, + {9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, -1, -1, -1, -1}, + {3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, -1, -1, -1, -1}, + {6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, -1}, + {9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, -1, -1, -1, -1}, + {1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, -1}, + {4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, -1}, + {7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, -1, -1, -1, -1}, + {6, 9, 5, 6, 11, 9, 11, 8, 9, -1, -1, -1, -1, -1, -1, -1}, + {3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, -1, -1, -1, -1}, + {0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, -1, -1, -1, -1}, + {6, 11, 3, 6, 3, 5, 5, 3, 1, -1, -1, -1, -1, -1, -1, -1}, + {1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, -1, -1, -1, -1}, + {0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, -1}, + {11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, -1}, + {6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, -1, -1, -1, -1}, + {5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, -1, -1, -1, -1}, + {9, 5, 6, 9, 6, 0, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1}, + {1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, -1}, + {1, 5, 6, 2, 1, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, -1}, + {10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, -1, -1, -1, -1}, + {0, 3, 8, 5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {10, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {11, 5, 10, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {11, 5, 10, 11, 7, 5, 8, 3, 0, -1, -1, -1, -1, -1, -1, -1}, + {5, 11, 7, 5, 10, 11, 1, 9, 0, -1, -1, -1, -1, -1, -1, -1}, + {10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, -1, -1, -1, -1}, + {11, 1, 2, 11, 7, 1, 7, 5, 1, -1, -1, -1, -1, -1, -1, -1}, + {0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, -1, -1, -1, -1}, + {9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, -1, -1, -1, -1}, + {7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, -1}, + {2, 5, 10, 2, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1}, + {8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, -1, -1, -1, -1}, + {9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, -1, -1, -1, -1}, + {9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, -1}, + {1, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 8, 7, 0, 7, 1, 1, 7, 5, -1, -1, -1, -1, -1, -1, -1}, + {9, 0, 3, 9, 3, 5, 5, 3, 7, -1, -1, -1, -1, -1, -1, -1}, + {9, 8, 7, 5, 9, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {5, 8, 4, 5, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1}, + {5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, -1, -1, -1, -1}, + {0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, -1, -1, -1, -1}, + {10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, -1}, + {2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, -1, -1, -1, -1}, + {0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, -1}, + {0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, -1}, + {9, 4, 5, 2, 11, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, -1, -1, -1, -1}, + {5, 10, 2, 5, 2, 4, 4, 2, 0, -1, -1, -1, -1, -1, -1, -1}, + {3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, -1}, + {5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, -1, -1, -1, -1}, + {8, 4, 5, 8, 5, 3, 3, 5, 1, -1, -1, -1, -1, -1, -1, -1}, + {0, 4, 5, 1, 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, -1, -1, -1, -1}, + {9, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {4, 11, 7, 4, 9, 11, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1}, + {0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, -1, -1, -1, -1}, + {1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, -1, -1, -1, -1}, + {3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, -1}, + {4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, -1, -1, -1, -1}, + {9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, -1}, + {11, 7, 4, 11, 4, 2, 2, 4, 0, -1, -1, -1, -1, -1, -1, -1}, + {11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, -1, -1, -1, -1}, + {2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, -1, -1, -1, -1}, + {9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, -1}, + {3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, -1}, + {1, 10, 2, 8, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {4, 9, 1, 4, 1, 7, 7, 1, 3, -1, -1, -1, -1, -1, -1, -1}, + {4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, -1, -1, -1, -1}, + {4, 0, 3, 7, 4, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {4, 8, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {9, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {3, 0, 9, 3, 9, 11, 11, 9, 10, -1, -1, -1, -1, -1, -1, -1}, + {0, 1, 10, 0, 10, 8, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1}, + {3, 1, 10, 11, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {1, 2, 11, 1, 11, 9, 9, 11, 8, -1, -1, -1, -1, -1, -1, -1}, + {3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, -1, -1, -1, -1}, + {0, 2, 11, 8, 0, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {3, 2, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {2, 3, 8, 2, 8, 10, 10, 8, 9, -1, -1, -1, -1, -1, -1, -1}, + {9, 10, 2, 0, 9, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, -1, -1, -1, -1}, + {1, 10, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} +}; diff -r 5fb21401b7c8 -r e09cbb2e9d4f libs/metasurf/metasurf.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libs/metasurf/metasurf.c Mon Feb 18 03:46:52 2013 +0200 @@ -0,0 +1,413 @@ +/* +metasurf - a library for implicit surface polygonization +Copyright (C) 2011 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program. If not, see . +*/ +#include +#include +#include "metasurf.h" +#include "mcubes.h" + +#undef USE_MTETRA +#define USE_MCUBES + +#if (defined(USE_MTETRA) && defined(USE_MCUBES)) || (!defined(USE_MTETRA) && !defined(USE_MCUBES)) +#error "pick either USE_MTETRA or USE_MCUBES, not both..." +#endif + +typedef float vec3[3]; + +struct metasurface { + vec3 min, max; + int res[3]; + float thres; + + msurf_eval_func_t eval; + msurf_vertex_func_t vertex; + msurf_normal_func_t normal; + + float dx, dy, dz; + int flip; + + vec3 vbuf[3]; + int nverts; +}; + +static int msurf_init(struct metasurface *ms); +static void process_cell(struct metasurface *ms, vec3 pos, vec3 sz); +#ifdef USE_MTETRA +static void process_tetra(struct metasurface *ms, int *idx, vec3 *pos, float *val); +#endif +#ifdef USE_MCUBES +static void process_cube(struct metasurface *ms, vec3 *pos, float *val); +#endif + + +struct metasurface *msurf_create(void) +{ + struct metasurface *ms; + + if(!(ms = malloc(sizeof *ms))) { + return 0; + } + if(msurf_init(ms) == -1) { + free(ms); + } + return ms; +} + +void msurf_free(struct metasurface *ms) +{ + free(ms); +} + +static int msurf_init(struct metasurface *ms) +{ + ms->thres = 0.0; + ms->eval = 0; + ms->vertex = 0; + ms->normal = 0; + ms->min[0] = ms->min[1] = ms->min[2] = -1.0; + ms->max[0] = ms->max[1] = ms->max[2] = 1.0; + ms->res[0] = ms->res[1] = ms->res[2] = 40; + ms->nverts = 0; + + ms->dx = ms->dy = ms->dz = 0.001; + ms->flip = 0; + + return 0; +} + +void msurf_inside(struct metasurface *ms, int inside) +{ + switch(inside) { + case MSURF_GREATER: + ms->flip = 0; + break; + + case MSURF_LESS: + ms->flip = 1; + break; + + default: + fprintf(stderr, "msurf_inside expects MSURF_GREATER or MSURF_LESS\n"); + } +} + +void msurf_eval_func(struct metasurface *ms, msurf_eval_func_t func) +{ + ms->eval = func; +} + +void msurf_vertex_func(struct metasurface *ms, msurf_vertex_func_t func) +{ + ms->vertex = func; +} + +void msurf_normal_func(struct metasurface *ms, msurf_normal_func_t func) +{ + ms->normal = func; +} + +void msurf_bounds(struct metasurface *ms, float xmin, float ymin, float zmin, float xmax, float ymax, float zmax) +{ + ms->min[0] = xmin; + ms->min[1] = ymin; + ms->min[2] = zmin; + ms->max[0] = xmax; + ms->max[1] = ymax; + ms->max[2] = zmax; +} + +void msurf_resolution(struct metasurface *ms, int xres, int yres, int zres) +{ + ms->res[0] = xres; + ms->res[1] = yres; + ms->res[2] = zres; +} + +void msurf_threshold(struct metasurface *ms, float thres) +{ + ms->thres = thres; +} + + +int msurf_polygonize(struct metasurface *ms) +{ + int i, j, k; + vec3 pos, delta; + + if(!ms->eval || !ms->vertex) { + fprintf(stderr, "you need to set eval and vertex callbacks before calling msurf_polygonize\n"); + return -1; + } + + for(i=0; i<3; i++) { + delta[i] = (ms->max[i] - ms->min[i]) / (float)ms->res[i]; + } + + pos[0] = ms->min[0]; + for(i=0; ires[0] - 1; i++) { + + pos[1] = ms->min[1]; + for(j=0; jres[1] - 1; j++) { + + pos[2] = ms->min[2]; + for(k=0; kres[2] - 1; k++) { + + process_cell(ms, pos, delta); + + pos[2] += delta[2]; + } + pos[1] += delta[1]; + } + pos[0] += delta[0]; + } + return 0; +} + + +static void process_cell(struct metasurface *ms, vec3 pos, vec3 sz) +{ + int i; + vec3 p[8]; + float val[8]; + +#ifdef USE_MTETRA + static int tetra[][4] = { + {0, 2, 3, 7}, + {0, 2, 6, 7}, + {0, 4, 6, 7}, + {0, 6, 1, 2}, + {0, 6, 1, 4}, + {5, 6, 1, 4} + }; +#endif + + static const float offs[][3] = { + {0.0f, 0.0f, 0.0f}, + {1.0f, 0.0f, 0.0f}, + {1.0f, 1.0f, 0.0f}, + {0.0f, 1.0f, 0.0f}, + {0.0f, 0.0f, 1.0f}, + {1.0f, 0.0f, 1.0f}, + {1.0f, 1.0f, 1.0f}, + {0.0f, 1.0f, 1.0f} + }; + + for(i=0; i<8; i++) { + p[i][0] = pos[0] + sz[0] * offs[i][2]; + p[i][1] = pos[1] + sz[1] * offs[i][1]; + p[i][2] = pos[2] + sz[2] * offs[i][0]; + + val[i] = ms->eval(p[i][0], p[i][1], p[i][2]); + } + +#ifdef USE_MTETRA + for(i=0; i<6; i++) { + process_tetra(ms, tetra[i], p, val); + } +#endif +#ifdef USE_MCUBES + process_cube(ms, p, val); +#endif +} + + +/* ---- marching cubes implementation ---- */ +#ifdef USE_MCUBES + +static unsigned int mc_bitcode(float *val, float thres); + +static void process_cube(struct metasurface *ms, vec3 *pos, float *val) +{ + static const int pidx[12][2] = { + {0, 1}, {1, 2}, {2, 3}, {3, 0}, {4, 5}, {5, 6}, + {6, 7}, {7, 4}, {0, 4}, {1, 5}, {2, 6}, {3, 7} + }; + int i, j; + vec3 vert[12]; + unsigned int code = mc_bitcode(val, ms->thres); + + if(ms->flip) { + code = ~code & 0xff; + } + + if(mc_edge_table[code] == 0) { + return; + } + + for(i=0; i<12; i++) { + if(mc_edge_table[code] & (1 << i)) { + int p0 = pidx[i][0]; + int p1 = pidx[i][1]; + + float t = (ms->thres - val[p0]) / (val[p1] - val[p0]); + vert[i][0] = pos[p0][0] + (pos[p1][0] - pos[p0][0]) * t; + vert[i][1] = pos[p0][1] + (pos[p1][1] - pos[p0][1]) * t; + vert[i][2] = pos[p0][2] + (pos[p1][2] - pos[p0][2]) * t; + } + } + + for(i=0; mc_tri_table[code][i] != -1; i+=3) { + for(j=0; j<3; j++) { + float *v = vert[mc_tri_table[code][i + j]]; + + if(ms->normal) { + float dfdx, dfdy, dfdz; + dfdx = ms->eval(v[0] - ms->dx, v[1], v[2]) - ms->eval(v[0] + ms->dx, v[1], v[2]); + dfdy = ms->eval(v[0], v[1] - ms->dy, v[2]) - ms->eval(v[0], v[1] + ms->dy, v[2]); + dfdz = ms->eval(v[0], v[1], v[2] - ms->dz) - ms->eval(v[0], v[1], v[2] + ms->dz); + + if(ms->flip) { + dfdx = -dfdx; + dfdy = -dfdy; + dfdz = -dfdz; + } + ms->normal(dfdx, dfdy, dfdz); + } + + ms->vertex(v[0], v[1], v[2]); + } + } +} + +static unsigned int mc_bitcode(float *val, float thres) +{ + unsigned int i, res = 0; + + for(i=0; i<8; i++) { + if(val[i] > thres) { + res |= 1 << i; + } + } + return res; +} +#endif /* USE_MCUBES */ + + +/* ---- marching tetrahedra implementation (incomplete) ---- */ +#ifdef USE_MTETRA + +static unsigned int mt_bitcode(float v0, float v1, float v2, float v3, float thres); +static void emmit(struct metasurface *ms, float v0, float v1, vec3 p0, vec3 p1, int rev) + + +#define REVBIT(x) ((x) & 8) +#define INV(x) (~(x) & 0xf) +#define EDGE(a, b) emmit(ms, val[idx[a]], val[idx[b]], pos[idx[a]], pos[idx[b]], REVBIT(code)) +static void process_tetra(struct metasurface *ms, int *idx, vec3 *pos, float *val) +{ + unsigned int code = mt_bitcode(val[idx[0]], val[idx[1]], val[idx[2]], val[idx[3]], ms->thres); + + switch(code) { + case 1: + case INV(1): + EDGE(0, 1); + EDGE(0, 2); + EDGE(0, 3); + break; + + case 2: + case INV(2): + EDGE(1, 0); + EDGE(1, 3); + EDGE(1, 2); + break; + + case 3: + case INV(3): + EDGE(0, 3); + EDGE(0, 2); + EDGE(1, 3); + + EDGE(1, 3); + EDGE(1, 2); + EDGE(0, 2); + break; + + case 4: + case INV(4): + EDGE(2, 0); + EDGE(2, 1); + EDGE(2, 3); + break; + + case 5: + case INV(5): + EDGE(0, 1); + EDGE(2, 3); + EDGE(0, 3); + + EDGE(0, 1); + EDGE(1, 2); + EDGE(2, 3); + break; + + case 6: + case INV(6): + EDGE(0, 1); + EDGE(1, 3); + EDGE(2, 3); + + EDGE(0, 1); + EDGE(0, 2); + EDGE(2, 3); + break; + + case 7: + case INV(7): + EDGE(3, 0); + EDGE(3, 2); + EDGE(3, 1); + break; + + default: + break; /* cases 0 and 15 */ + } +} + +#define BIT(i) ((v##i > thres) ? (1 << i) : 0) +static unsigned int mt_bitcode(float v0, float v1, float v2, float v3, float thres) +{ + return BIT(0) | BIT(1) | BIT(2) | BIT(3); +} + +static void emmit(struct metasurface *ms, float v0, float v1, vec3 p0, vec3 p1, int rev) +{ + int i; + float t = (ms->thres - v0) / (v1 - v0); + + vec3 p; + for(i=0; i<3; i++) { + p[i] = p0[i] + (p1[i] - p0[i]) * t; + } + ms->vertex(p[0], p[1], p[2]); + + /*for(i=0; i<3; i++) { + ms->vbuf[ms->nverts][i] = p0[i] + (p1[i] - p0[i]) * t; + } + + if(++ms->nverts >= 3) { + ms->nverts = 0; + + for(i=0; i<3; i++) { + int idx = rev ? (2 - i) : i; + ms->vertex(ms->vbuf[idx][0], ms->vbuf[idx][1], ms->vbuf[idx][2]); + } + }*/ +} + +#endif /* USE_MTETRA */ diff -r 5fb21401b7c8 -r e09cbb2e9d4f libs/metasurf/metasurf.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libs/metasurf/metasurf.h Mon Feb 18 03:46:52 2013 +0200 @@ -0,0 +1,72 @@ +/* +metasurf - a library for implicit surface polygonization +Copyright (C) 2011 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program. If not, see . +*/ + +#ifndef METASURF_H_ +#define METASURF_H_ + +#define MSURF_GREATER 1 +#define MSURF_LESS 0 + +struct metasurface; + +typedef float (*msurf_eval_func_t)(float, float, float); +typedef void (*msurf_vertex_func_t)(float, float, float); +typedef void (*msurf_normal_func_t)(float, float, float); + +#ifdef __cplusplus +extern "C" { +#endif + +struct metasurface *msurf_create(void); +void msurf_free(struct metasurface *ms); + +/* which is inside above or below the threshold */ +void msurf_inside(struct metasurface *ms, int inside); + +/* set a scalar field evaluator function */ +void msurf_eval_func(struct metasurface *ms, msurf_eval_func_t func); + +/* set a generated vertex callback function */ +void msurf_vertex_func(struct metasurface *ms, msurf_vertex_func_t func); + +/* set a generated surface normal callback function (unused yet) */ +void msurf_normal_func(struct metasurface *ms, msurf_normal_func_t func); + +/* set the bounding box (default: -1, -1, -1, 1, 1, 1) + * keep this as tight as possible to avoid wasting grid resolution + */ +void msurf_bounds(struct metasurface *ms, float xmin, float ymin, float zmin, float xmax, float ymax, float zmax); + +/* resolution of the 3D evaluation grid, the bigger, the better, the slower + * (default: 40, 40, 40) + */ +void msurf_resolution(struct metasurface *ms, int xres, int yres, int zres); + +/* isosurface threshold value (default: 0) */ +void msurf_threshold(struct metasurface *ms, float thres); + + +/* finally call this to perform the polygonization */ +int msurf_polygonize(struct metasurface *ms); + + +#ifdef __cplusplus +} +#endif + +#endif /* METASURF_H_ */ diff -r 5fb21401b7c8 -r e09cbb2e9d4f src/mballs.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mballs.cc Mon Feb 18 03:46:52 2013 +0200 @@ -0,0 +1,140 @@ +#include +#include "opengl.h" +#include "mballs.h" +#include "metasurf.h" +#include "vmath/vmath.h" + +struct MetaBall { + Vector3 pos; + float orbit; + float energy; + float phase_offs; +}; + +#define VOL_SZ 2 +#define MBALL_GRID_SZ 50 + +static void update(); +static float calc_field(float x, float y, float z); +static float eval(float x, float y, float z); +static void vertex(float x, float y, float z); +static void normal(float x, float y, float z); + +static float grid[MBALL_GRID_SZ][MBALL_GRID_SZ][MBALL_GRID_SZ]; +static std::vector balls; +static struct metasurface *msurf; + +bool mball_init() +{ + static const float bbmin = -VOL_SZ / 2.0; + static const float bbmax = VOL_SZ / 2.0; + + if(!(msurf = msurf_create())) { + return false; + } + msurf_threshold(msurf, 10); + msurf_resolution(msurf, MBALL_GRID_SZ, MBALL_GRID_SZ, MBALL_GRID_SZ); + msurf_bounds(msurf, bbmin, bbmin, bbmin, bbmax, bbmax, bbmax); + msurf_eval_func(msurf, eval); + msurf_vertex_func(msurf, vertex); + //msurf_normal_func(msurf, normal); + + for(int i=0; i<10; i++) { + MetaBall mb; + mb.orbit = 0.25 * rand() / (float)RAND_MAX + 0.35; + mb.energy = 0.2 * rand() / (float)RAND_MAX + 0.15; + mb.phase_offs = rand() / (float)RAND_MAX * M_PI * 2.0; + balls.push_back(mb); + } + + return true; +} + +void mball_render() +{ + update(); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glScalef(4.0, 4.0, 4.0); + + glBegin(GL_TRIANGLES); + msurf_polygonize(msurf); + glEnd(); + + glPopMatrix(); +} + + +static void update() +{ + unsigned int msec = glutGet(GLUT_ELAPSED_TIME); + float sec = msec / 1000.0; + + for(size_t i=0; i 1e-6) { + sum += balls[i].energy / dist_sq; + } else { + sum += 1000.0; + } + } + return sum; +} + +static inline int clamp(int x, int a, int b) +{ + return x < a ? a : (x > b ? b : x); +} + +static float eval(float x, float y, float z) +{ + int cell_x = clamp((int)((x / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1); + int cell_y = clamp((int)((y / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1); + int cell_z = clamp((int)((z / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1); + + return grid[cell_x][cell_y][cell_z]; +} + +static void vertex(float x, float y, float z) +{ + float delta = (float)VOL_SZ / (float)MBALL_GRID_SZ; + + float dfdx = calc_field(x - delta, y, z) - calc_field(x + delta, y, z); + float dfdy = calc_field(x, y - delta, z) - calc_field(x, y + delta, z); + float dfdz = calc_field(x, y, z - delta) - calc_field(x, y, z + delta); + + float len = sqrt(dfdx * dfdx + dfdy * dfdy + dfdz * dfdz); + + glNormal3f(dfdx / len, dfdy / len, dfdz / len); + glVertex3f(x, y, z); +} + +static void normal(float x, float y, float z) +{ + glNormal3f(x, y, z); +} diff -r 5fb21401b7c8 -r e09cbb2e9d4f src/mballs.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mballs.h Mon Feb 18 03:46:52 2013 +0200 @@ -0,0 +1,7 @@ +#ifndef MBALLS_H_ +#define MBALLS_H_ + +bool mball_init(); +void mball_render(); + +#endif /* MBALLS_H_ */ diff -r 5fb21401b7c8 -r e09cbb2e9d4f src/udg.cc --- a/src/udg.cc Thu Feb 07 18:52:28 2013 +0200 +++ b/src/udg.cc Mon Feb 18 03:46:52 2013 +0200 @@ -6,6 +6,7 @@ #include "sdr.h" #include "dither_matrix.h" #include "scroller.h" +#include "mballs.h" #define DITHER_SZ 8 #define DITHER_LEVELS 16 @@ -35,11 +36,14 @@ void destroy_rtarg(struct render_target *rt); int xsz, ysz; -float cam_theta, cam_phi = 25, cam_dist = 8; +float cam_theta, cam_phi = 25, cam_dist = 9; unsigned int dither_tex; struct render_target *rtarg; unsigned int prog; +int opt_highres, opt_regular_render; + + int main(int argc, char **argv) { glutInit(&argc, argv); @@ -96,10 +100,15 @@ return false; } + if(!mball_init()) { + return false; + } + glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); + glEnable(GL_NORMALIZE); return true; } @@ -137,11 +146,15 @@ void disp() { float ldir[] = {-1, 1, 2, 0}; + int xres, yres; - int xres = xsz / DITHER_SZ; - int yres = ysz / DITHER_SZ; - /*int xres = xsz; - int yres = ysz;*/ + if(opt_highres) { + xres = xsz; + yres = ysz; + } else { + xres = xsz / DITHER_SZ; + yres = ysz / DITHER_SZ; + } if(!rtarg) { printf("(re)creating render target (%dx%d)\n", xres, yres); @@ -150,7 +163,9 @@ } } - glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo); + if(!opt_regular_render) { + glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo); + } glViewport(0, 0, xres, yres); glClearColor(1, 1, 1, 1); @@ -161,72 +176,72 @@ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - glLightfv(GL_LIGHT0, GL_POSITION, ldir); - glTranslatef(0, 0, -cam_dist); glRotatef(cam_phi, 1, 0, 0); glRotatef(cam_theta, 0, 1, 0); + glLightfv(GL_LIGHT0, GL_POSITION, ldir); + const float blue[] = {0.4, 0.45, 1.0, 1}; const float white[] = {1, 1, 1, 1}; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0); - glFrontFace(GL_CW); - glutSolidTeapot(1.0); - glFrontFace(GL_CCW); + mball_render(); - glBindFramebufferEXT(GL_FRAMEBUFFER, 0); - glViewport(0, 0, xsz, ysz); + if(!opt_regular_render) { + glBindFramebufferEXT(GL_FRAMEBUFFER, 0); + glViewport(0, 0, xsz, ysz); - glClear(GL_COLOR_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT); - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glPushMatrix(); + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glPushMatrix(); - glPushAttrib(GL_ENABLE_BIT); - glDisable(GL_DEPTH_TEST); + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_DEPTH_TEST); - bind_program(prog); - set_uniform_int(prog, "framebuf", 0); - set_uniform_int(prog, "dither_tex", 1); - set_uniform_int(prog, "dither_levels", DITHER_LEVELS); - set_uniform_int(prog, "dither_size", DITHER_SZ); + bind_program(prog); + set_uniform_int(prog, "framebuf", 0); + set_uniform_int(prog, "dither_tex", 1); + set_uniform_int(prog, "dither_levels", DITHER_LEVELS); + set_uniform_int(prog, "dither_size", DITHER_SZ); - glActiveTextureARB(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, rtarg->color_tex); - glEnable(GL_TEXTURE_2D); - glActiveTextureARB(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, dither_tex); - glEnable(GL_TEXTURE_2D); + glActiveTextureARB(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, rtarg->color_tex); + glEnable(GL_TEXTURE_2D); + glActiveTextureARB(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, dither_tex); + glEnable(GL_TEXTURE_2D); - glBegin(GL_QUADS); - glColor3f(0, 1, 0); - glTexCoord2f(0, 0); glVertex2f(-1, -1); - glTexCoord2f(1, 0); glVertex2f(1, -1); - glTexCoord2f(1, 1); glVertex2f(1, 1); - glTexCoord2f(0, 1); glVertex2f(-1, 1); - glEnd(); + glBegin(GL_QUADS); + glColor3f(0, 1, 0); + glTexCoord2f(0, 0); glVertex2f(-1, -1); + glTexCoord2f(1, 0); glVertex2f(1, -1); + glTexCoord2f(1, 1); glVertex2f(1, 1); + glTexCoord2f(0, 1); glVertex2f(-1, 1); + glEnd(); - glActiveTextureARB(GL_TEXTURE1); - glDisable(GL_TEXTURE_2D); - glActiveTextureARB(GL_TEXTURE0); - glDisable(GL_TEXTURE_2D); + glActiveTextureARB(GL_TEXTURE1); + glDisable(GL_TEXTURE_2D); + glActiveTextureARB(GL_TEXTURE0); + glDisable(GL_TEXTURE_2D); - bind_program(0); + bind_program(0); - glPopAttrib(); + glPopAttrib(); - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - glPopMatrix(); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + } glutSwapBuffers(); assert(glGetError() == GL_NO_ERROR); @@ -274,6 +289,18 @@ } } break; + + case 'r': + opt_regular_render = !opt_regular_render; + break; + + case 'h': + opt_highres = !opt_highres; + if(rtarg) { + destroy_rtarg(rtarg); + rtarg = 0; + } + break; } }