11 #define BBOX_SIZE 10.0f
12 #define BBOX_HSZ (BBOX_SIZE / 2.0f)
14 #define VOX_STEP (BBOX_SIZE / (float)VOX_RES)
16 #define VBUF_MAX_TRIS 256
17 #define VBUF_SIZE (VBUF_MAX_TRIS * 3)
24 static struct g3d_mesh mesh;
25 static struct g3d_vertex *vbuf;
26 static struct metasurface *msurf;
27 static struct mball *balls;
30 static void update(float tsec);
31 static void draw_metaballs(void);
40 g3d_framebuffer(320, 200, framebuf);
41 g3d_viewport(0, 0, 320, 200);
43 g3d_clear_color(0, 0, 0);
45 g3d_matrix_mode(G3D_PROJECTION);
47 g3d_perspective(60.0f, 1.33333, 0.5, 500.0);
49 g3d_enable(G3D_CULL_FACE);
50 g3d_enable(G3D_DEPTH_TEST);
51 g3d_enable(G3D_LIGHTING);
52 g3d_enable(G3D_LIGHT0);
54 g3d_polygon_mode(G3D_GOURAUD);
56 gen_torus_mesh(&mesh, 2.0, 0.7, 24, 12);
58 if(!(msurf = msurf_create())) {
61 msurf_set_threshold(msurf, 8);
62 msurf_set_inside(msurf, MSURF_GREATER);
63 msurf_set_bounds(msurf, -BBOX_HSZ, -BBOX_HSZ, -BBOX_HSZ, BBOX_HSZ, BBOX_HSZ, BBOX_HSZ);
64 msurf_set_resolution(msurf, VOX_RES, VOX_RES, VOX_RES);
65 msurf_enable(msurf, MSURF_NORMALIZE);
67 vbuf = malloc_nf(VBUF_SIZE * sizeof *vbuf);
70 balls = calloc_nf(num_balls, sizeof *balls);
72 for(i=0; i<num_balls; i++) {
78 void game_shutdown(void)
82 static void update(float tsec)
85 float x, y, z, dx, dy, dz, dsq, energy;
86 float *vox = msurf_voxels(msurf);
88 for(i=0; i<num_balls; i++) {
89 balls[i].y = sin(tsec) * 5.0f;
92 for(i=0; i<VOX_RES; i++) {
93 z = -BBOX_HSZ + i * VOX_STEP;
94 for(j=0; j<VOX_RES; j++) {
95 y = -BBOX_HSZ + j * VOX_STEP;
96 for(k=0; k<VOX_RES; k++) {
97 x = -BBOX_HSZ + k * VOX_STEP;
99 /* initialize with the vertical distance for the pool */
100 energy = 5.0 / (y + BBOX_HSZ * 0.98);
102 /* add the contribution of the balls */
103 for(n=0; n<num_balls; n++) {
107 dsq = dx * dx + dy * dy + dz * dz;
109 energy += balls[n].energy / dsq;
116 msurf_polygonize(msurf);
121 unsigned long msec = game_getmsec();
122 float tsec = (float)msec / 1000.0f;
126 g3d_clear(G3D_COLOR_BUFFER_BIT | G3D_DEPTH_BUFFER_BIT);
128 g3d_matrix_mode(G3D_MODELVIEW);
130 g3d_translate(0, 0, -15);
131 /*g3d_rotate(tsec * 50.0f, 1, 0, 0);
132 g3d_rotate(tsec * 30.0f, 0, 0, 1);
140 static void draw_metaballs(void)
142 int i, nverts, vbuf_count;
144 struct g3d_vertex *vbptr;
147 nverts = msurf_vertex_count(msurf);
148 varr = msurf_vertices(msurf);
149 narr = msurf_normals(msurf);
152 for(i=0; i<nverts; i++) {
153 vbuf_count = vbptr - vbuf;
154 if(vbuf_count >= VBUF_SIZE) {
155 g3d_draw(G3D_TRIANGLES, vbuf, vbuf_count);
173 g3d_draw(G3D_TRIANGLES, vbuf, vbptr - vbuf);
179 void game_keyboard(int key, int press)
181 if(key == 27) game_quit();
184 void game_mouse(int bn, int press, int x, int y)
188 void game_motion(int x, int y)