metasurf
diff src/metasurf.c @ 4:2c575855f707
added simple example
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 25 Oct 2011 23:21:32 +0300 |
parents | 9ab057fba0c5 |
children | 430d8dde62aa |
line diff
1.1 --- a/src/metasurf.c Tue Oct 25 13:30:03 2011 +0300 1.2 +++ b/src/metasurf.c Tue Oct 25 23:21:32 2011 +0300 1.3 @@ -38,6 +38,9 @@ 1.4 msurf_vertex_func_t vertex; 1.5 msurf_normal_func_t normal; 1.6 1.7 + float dx, dy, dz; 1.8 + int flip; 1.9 + 1.10 vec3 vbuf[3]; 1.11 int nverts; 1.12 }; 1.13 @@ -81,9 +84,28 @@ 1.14 ms->res[0] = ms->res[1] = ms->res[2] = 40; 1.15 ms->nverts = 0; 1.16 1.17 + ms->dx = ms->dy = ms->dz = 0.001; 1.18 + ms->flip = 0; 1.19 + 1.20 return 0; 1.21 } 1.22 1.23 +void msurf_inside(struct metasurface *ms, int inside) 1.24 +{ 1.25 + switch(inside) { 1.26 + case MSURF_GREATER: 1.27 + ms->flip = 0; 1.28 + break; 1.29 + 1.30 + case MSURF_LESS: 1.31 + ms->flip = 1; 1.32 + break; 1.33 + 1.34 + default: 1.35 + fprintf(stderr, "msurf_inside expects MSURF_GREATER or MSURF_LESS\n"); 1.36 + } 1.37 +} 1.38 + 1.39 void msurf_eval_func(struct metasurface *ms, msurf_eval_func_t func) 1.40 { 1.41 ms->eval = func; 1.42 @@ -219,6 +241,10 @@ 1.43 vec3 vert[12]; 1.44 unsigned int code = mc_bitcode(val, ms->thres); 1.45 1.46 + if(ms->flip) { 1.47 + code = ~code & 0xff; 1.48 + } 1.49 + 1.50 if(mc_edge_table[code] == 0) { 1.51 return; 1.52 } 1.53 @@ -238,6 +264,21 @@ 1.54 for(i=0; mc_tri_table[code][i] != -1; i+=3) { 1.55 for(j=0; j<3; j++) { 1.56 float *v = vert[mc_tri_table[code][i + j]]; 1.57 + 1.58 + if(ms->normal) { 1.59 + float dfdx, dfdy, dfdz; 1.60 + dfdx = ms->eval(v[0] - ms->dx, v[1], v[2]) - ms->eval(v[0] + ms->dx, v[1], v[2]); 1.61 + dfdy = ms->eval(v[0], v[1] - ms->dy, v[2]) - ms->eval(v[0], v[1] + ms->dy, v[2]); 1.62 + dfdz = ms->eval(v[0], v[1], v[2] - ms->dz) - ms->eval(v[0], v[1], v[2] + ms->dz); 1.63 + 1.64 + if(ms->flip) { 1.65 + dfdx = -dfdx; 1.66 + dfdy = -dfdy; 1.67 + dfdz = -dfdz; 1.68 + } 1.69 + ms->normal(dfdx, dfdy, dfdz); 1.70 + } 1.71 + 1.72 ms->vertex(v[0], v[1], v[2]); 1.73 } 1.74 }