12 int res[3], newres[3];
20 int varr_size, varr_alloc_size;
24 static int msurf_init(struct metasurface *ms);
25 static void process_cell(struct metasurface *ms, int xcell, int ycell, int zcell, vec3 pos, vec3 sz);
28 struct metasurface *msurf_create(void)
30 struct metasurface *ms;
32 if(!(ms = malloc(sizeof *ms))) {
35 if(msurf_init(ms) == -1) {
41 void msurf_free(struct metasurface *ms)
51 static int msurf_init(struct metasurface *ms)
55 ms->min[0] = ms->min[1] = ms->min[2] = -1.0;
56 ms->max[0] = ms->max[1] = ms->max[2] = 1.0;
57 ms->res[0] = ms->res[1] = ms->res[2] = 0;
58 ms->newres[0] = ms->newres[1] = ms->newres[2] = 40;
60 ms->varr_alloc_size = ms->varr_size = 0;
61 ms->varr = ms->narr = 0;
63 ms->dx = ms->dy = ms->dz = 0.001;
69 void msurf_enable(struct metasurface *ms, unsigned int opt)
74 void msurf_disable(struct metasurface *ms, unsigned int opt)
79 int msurf_is_enabled(struct metasurface *ms, unsigned int opt)
81 return ms->flags & opt;
84 void msurf_set_inside(struct metasurface *ms, int inside)
88 msurf_enable(ms, MSURF_FLIP);
92 msurf_disable(ms, MSURF_FLIP);
96 fprintf(stderr, "msurf_inside expects MSURF_GREATER or MSURF_LESS\n");
100 int msurf_get_inside(struct metasurface *ms)
102 return msurf_is_enabled(ms, MSURF_FLIP) ? MSURF_LESS : MSURF_GREATER;
105 void msurf_set_bounds(struct metasurface *ms, float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
115 void msurf_get_bounds(struct metasurface *ms, float *xmin, float *ymin, float *zmin, float *xmax, float *ymax, float *zmax)
125 void msurf_set_resolution(struct metasurface *ms, int xres, int yres, int zres)
127 ms->newres[0] = xres;
128 ms->newres[1] = yres;
129 ms->newres[2] = zres;
132 void msurf_get_resolution(struct metasurface *ms, int *xres, int *yres, int *zres)
139 void msurf_set_threshold(struct metasurface *ms, float thres)
144 float msurf_get_threshold(struct metasurface *ms)
150 float *msurf_voxels(struct metasurface *ms)
152 if(ms->res[0] != ms->newres[0] || ms->res[1] != ms->newres[1] || ms->res[2] != ms->newres[2]) {
154 ms->res[0] = ms->newres[0];
155 ms->res[1] = ms->newres[1];
156 ms->res[2] = ms->newres[2];
157 count = ms->res[0] * ms->res[1] * ms->res[2];
159 if(!(ms->voxels = malloc(count * sizeof *ms->voxels))) {
166 float *msurf_slice(struct metasurface *ms, int idx)
168 float *vox = msurf_voxels(ms);
171 return vox + ms->res[0] * ms->res[1] * idx;
174 int msurf_polygonize(struct metasurface *ms)
179 if(!ms->voxels) return -1;
184 delta[i] = (ms->max[i] - ms->min[i]) / (float)ms->res[i];
187 for(i=0; i<ms->res[0] - 1; i++) {
188 for(j=0; j<ms->res[1] - 1; j++) {
189 for(k=0; k<ms->res[2] - 1; k++) {
191 pos[0] = ms->min[0] + i * delta[0];
192 pos[1] = ms->min[1] + j * delta[1];
193 pos[2] = ms->min[2] + k * delta[2];
195 process_cell(ms, i, j, k, pos, delta);
202 int msurf_vertex_count(struct metasurface *ms)
204 return ms->varr_size / 3;
207 float *msurf_vertices(struct metasurface *ms)
212 float *msurf_normals(struct metasurface *ms)
217 static unsigned int mc_bitcode(float *val, float thres);
219 static void process_cell(struct metasurface *ms, int xcell, int ycell, int zcell, vec3 cellpos, vec3 cellsz)
221 int i, j, k, slice_size;
223 float dfdx[8], dfdy[8], dfdz[8];
224 vec3 vert[12], norm[12];
229 static const int offs[][3] = {
240 static const int pidx[12][2] = {
241 {0, 1}, {1, 2}, {2, 3}, {3, 0}, {4, 5}, {5, 6},
242 {6, 7}, {7, 4}, {0, 4}, {1, 5}, {2, 6}, {3, 7}
245 slice_size = ms->res[0] * ms->res[1];
246 cellptr = ms->voxels + slice_size * zcell + ms->res[0] * ycell + xcell;
248 #define GRIDOFFS(x, y, z) ((z) * slice_size + (y) * ms->res[0] + (x))
251 val[i] = cellptr[GRIDOFFS(offs[i][0], offs[i][1], offs[i][2])];
254 code = mc_bitcode(val, ms->thres);
255 if(ms->flags & MSURF_FLIP) {
258 if(mc_edge_table[code] == 0) {
262 /* calculate normals at the 8 corners */
264 float *ptr = cellptr + GRIDOFFS(offs[i][0], offs[i][1], offs[i][2]);
266 if(xcell < ms->res[0] - 1) {
267 dfdx[i] = ptr[GRIDOFFS(1, 0, 0)] - *ptr;
269 dfdx[i] = *ptr - ptr[GRIDOFFS(-1, 0, 0)];
271 if(ycell < ms->res[1] - 1) {
272 dfdy[i] = ptr[GRIDOFFS(0, 1, 0)] - *ptr;
274 dfdy[i] = *ptr - ptr[GRIDOFFS(0, -1, 0)];
276 if(zcell < ms->res[2] - 1) {
277 dfdz[i] = ptr[GRIDOFFS(0, 0, 1)] - *ptr;
279 dfdz[i] = *ptr - ptr[GRIDOFFS(0, 0, -1)];
283 /* calculate the world-space position of each corner */
285 pos[i][0] = cellpos[0] + cellsz[0] * offs[i][0];
286 pos[i][1] = cellpos[1] + cellsz[1] * offs[i][1];
287 pos[i][2] = cellpos[2] + cellsz[2] * offs[i][2];
290 /* generate up to a max of 12 vertices per cube. interpolate positions and normals for each one */
291 for(i=0; i<12; i++) {
292 if(mc_edge_table[code] & (1 << i)) {
297 float t = (ms->thres - val[p0]) / (val[p1] - val[p0]);
298 vert[i][0] = pos[p0][0] + (pos[p1][0] - pos[p0][0]) * t;
299 vert[i][1] = pos[p0][1] + (pos[p1][1] - pos[p0][1]) * t;
300 vert[i][2] = pos[p0][2] + (pos[p1][2] - pos[p0][2]) * t;
302 nx = dfdx[p0] + (dfdx[p1] - dfdx[p0]) * t;
303 ny = dfdy[p0] + (dfdy[p1] - dfdy[p0]) * t;
304 nz = dfdz[p0] + (dfdz[p1] - dfdz[p0]) * t;
306 if(ms->flags & MSURF_FLIP) {
312 if(ms->flags & MSURF_NORMALIZE) {
313 float len = sqrt(nx * nx + ny * ny + nz * nz);
315 float s = 1.0f / len;
328 /* for each triangle of the cube add the appropriate vertices to the vertex buffer */
329 for(i=0; mc_tri_table[code][i] != -1; i+=3) {
331 int idx = mc_tri_table[code][i + j];
332 float *v = vert[idx];
333 float *n = norm[idx];
335 /* TODO multithreadied polygon emit */
336 if(ms->varr_size + 3 > ms->varr_alloc_size) {
337 int newsz = ms->varr_alloc_size ? ms->varr_alloc_size * 2 : 1024;
338 float *new_varr, *new_narr;
340 if(!(new_varr = realloc(ms->varr, newsz * sizeof *new_varr)) ||
341 !(new_narr = realloc(ms->narr, newsz * sizeof *new_narr))) {
343 error_log("msurf_polygonize: failed to grow vertex buffers to %d elements\n", newsz);
348 ms->varr_alloc_size = newsz;
352 ms->varr[ms->varr_size] = v[k];
353 ms->narr[ms->varr_size] = n[k];
360 static unsigned int mc_bitcode(float *val, float thres)
362 unsigned int i, res = 0;