16 struct g3d_vertex *varr;
26 static int init(void);
27 static void destroy(void);
28 static void start(long trans_time);
29 static void draw(void);
30 static void draw_mesh(struct mesh *mesh);
31 static void zsort(struct mesh *m);
33 static void calc_voxel_field(void);
34 static float eval(struct metasurface *ms, float x, float y, float z);
35 static void emit_vertex(struct metasurface *ms, float x, float y, float z);
37 static struct screen scr = {
45 static float cam_theta, cam_phi = 25;
46 static float cam_dist = 3;
47 static struct mesh mmesh;
49 static struct metasurface *msurf;
52 #define VOL_SCALE 10.0f
53 #define VOL_HALF_SCALE (VOL_SCALE * 0.5f)
55 #define VOXEL(x, y, z) (volume[(z) * VOL_SZ * VOL_SZ + (y) * VOL_SZ + (x)])
58 static struct metaball mball[NUM_MBALLS];
61 struct screen *metaballs_screen(void)
68 if(!(volume = malloc(VOL_SZ * VOL_SZ * VOL_SZ * sizeof *volume))) {
69 fprintf(stderr, "failed to allocate %dx%dx%d voxel field\n", VOL_SZ, VOL_SZ, VOL_SZ);
73 mball[0].energy = 1.2;
74 mball[1].energy = 0.8;
75 mball[2].energy = 1.0;
77 if(!(msurf = msurf_create())) {
78 fprintf(stderr, "failed to initialize metasurf\n");
81 msurf_set_resolution(msurf, VOL_SZ, VOL_SZ, VOL_SZ);
82 msurf_set_bounds(msurf, 0, 0, 0, VOL_SCALE, VOL_SCALE, VOL_SCALE);
83 msurf_eval_func(msurf, eval);
84 msurf_set_threshold(msurf, 0.5);
85 msurf_set_inside(msurf, MSURF_GREATER);
86 msurf_vertex_func(msurf, emit_vertex);
88 mmesh.prim = G3D_TRIANGLES;
91 mmesh.vcount = mmesh.icount = 0;
96 static void destroy(void)
98 dynarr_free(mmesh.varr);
101 static void start(long trans_time)
103 g3d_matrix_mode(G3D_PROJECTION);
105 g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
107 g3d_enable(G3D_CULL_FACE);
108 g3d_enable(G3D_LIGHTING);
109 g3d_enable(G3D_LIGHT0);
111 g3d_polygon_mode(G3D_WIRE);
114 static void update(void)
116 static int prev_mx, prev_my;
117 static unsigned int prev_bmask;
120 if((mouse_bmask ^ prev_bmask) == 0) {
121 int dx = mouse_x - prev_mx;
122 int dy = mouse_y - prev_my;
125 if(mouse_bmask & 1) {
126 cam_theta += dx * 1.0;
129 if(cam_phi < -90) cam_phi = -90;
130 if(cam_phi > 90) cam_phi = 90;
132 if(mouse_bmask & 4) {
133 cam_dist += dy * 0.5;
135 if(cam_dist < 0) cam_dist = 0;
142 prev_bmask = mouse_bmask;
146 float tsec = time_msec / 1000.0f;
147 static float phase[] = {0.0, M_PI / 3.0, M_PI * 0.8};
148 static float speed[] = {0.8, 1.4, 1.0};
149 static float scale[][3] = {{1, 2, 0.8}, {0.5, 1.6, 0.6}, {1.5, 0.7, 0.5}};
150 static float offset[][3] = {{0, 0, 0}, {0.25, 0, 0}, {-0.2, 0.15, 0.2}};
152 for(i=0; i<NUM_MBALLS; i++) {
153 float t = (tsec + phase[i]) * speed[i];
156 float x = sin(t + j * M_PI / 2.0);
157 mball[i].pos[j] = offset[i][j] + x * scale[i][j];
164 dynarr_free(mmesh.varr);
165 mmesh.varr = dynarr_alloc(0, sizeof *mmesh.varr);
166 msurf_polygonize(msurf);
169 static void draw(void)
173 memset(fb_pixels, 0, fb_width * fb_height * 2);
175 g3d_matrix_mode(G3D_MODELVIEW);
177 g3d_translate(0, 0, -cam_dist);
178 g3d_rotate(cam_phi, 1, 0, 0);
179 g3d_rotate(cam_theta, 0, 1, 0);
181 /*g3d_light_pos(0, -10, 10, 20);*/
183 /*zsort(&metamesh);*/
185 /*g3d_mtl_diffuse(0.6, 0.6, 0.6);*/
189 swap_buffers(fb_pixels);
192 static void draw_mesh(struct mesh *mesh)
195 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
197 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
202 struct g3d_vertex *varr;
206 static int zsort_cmp(const void *aptr, const void *bptr)
208 const int16_t *a = (const int16_t*)aptr;
209 const int16_t *b = (const int16_t*)bptr;
211 const float *m = zsort_cls.xform;
213 const struct g3d_vertex *va = zsort_cls.varr + a[0];
214 const struct g3d_vertex *vb = zsort_cls.varr + b[0];
216 float za = m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
217 float zb = m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
219 va = zsort_cls.varr + a[2];
220 vb = zsort_cls.varr + b[2];
222 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
223 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
228 static void zsort(struct mesh *m)
230 int nfaces = m->icount / m->prim;
232 zsort_cls.varr = m->varr;
233 zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
235 qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_cmp);
238 static void calc_voxel_field(void)
241 float *voxptr = volume;
243 for(i=0; i<VOL_SZ; i++) {
244 float z = VOL_SCALE * ((float)i / (float)VOL_SZ - 0.5);
246 for(j=0; j<VOL_SZ; j++) {
247 float y = VOL_SCALE * ((float)j / (float)VOL_SZ - 0.5);
249 for(k=0; k<VOL_SZ; k++) {
250 float x = VOL_SCALE * ((float)k / (float)VOL_SZ - 0.5);
253 for(b=0; b<NUM_MBALLS; b++) {
254 float dx = mball[b].pos[0] - x;
255 float dy = mball[b].pos[1] - y;
256 float dz = mball[b].pos[2] - z;
258 float lensq = dx * dx + dy * dy + dz * dz;
260 val += lensq == 0.0f ? 1024.0f : mball[b].energy / lensq;
269 static float eval(struct metasurface *ms, float x, float y, float z)
271 int xidx = cround64(x);
272 int yidx = cround64(y);
273 int zidx = cround64(z);
275 assert(xidx >= 0 && xidx < VOL_SZ);
276 assert(yidx >= 0 && yidx < VOL_SZ);
277 assert(zidx >= 0 && zidx < VOL_SZ);
279 return VOXEL(xidx, yidx, zidx);
282 static void emit_vertex(struct metasurface *ms, float x, float y, float z)
286 v.x = x - VOL_HALF_SCALE;
287 v.y = y - VOL_HALF_SCALE;
288 v.z = z - VOL_HALF_SCALE;
289 v.r = cround64(255.0 * x / VOL_SCALE);
290 v.g = cround64(255.0 * y / VOL_SCALE);
291 v.b = cround64(255.0 * z / VOL_SCALE);
293 mmesh.varr = dynarr_push(mmesh.varr, &v);