10 #include "cgmath/cgmath.h"
18 #define BBOX_HXSZ (BBOX_XSZ / 2.0f)
19 #define BBOX_HYSZ (BBOX_YSZ / 2.0f)
20 #define BBOX_HZSZ (BBOX_ZSZ / 2.0f)
21 #define VOX_XRES (VOX_RES * BBOX_XSZ / BBOX_ZSZ)
22 #define VOX_YRES (VOX_RES * BBOX_YSZ / BBOX_ZSZ)
23 #define VOX_ZRES VOX_RES
24 #define VOX_XSTEP (BBOX_XSZ / (float)VOX_XRES)
25 #define VOX_YSTEP (BBOX_YSZ / (float)VOX_YRES)
26 #define VOX_ZSTEP (BBOX_ZSZ / (float)VOX_ZRES)
28 #define VBUF_MAX_TRIS 256
29 #define VBUF_SIZE (VBUF_MAX_TRIS * 3)
31 static struct g3d_vertex *vbuf;
32 static struct metasurface *msurf;
33 static struct mobject **mobj;
36 static int num_mobj, cur_obj;
39 static int mousebn[3];
40 static int mousex, mousey;
41 static float cam_theta, cam_phi;
43 extern unsigned char textures_img[];
44 extern unsigned char textures_cmap[];
45 extern unsigned char textures_slut[];
48 static void update(float tsec);
49 static void draw_metaballs(void);
55 load_colormap(0, 256, textures_cmap, textures_slut);
58 g3d_framebuffer(FB_WIDTH, FB_HEIGHT, framebuf);
59 g3d_viewport(0, 0, FB_WIDTH, FB_HEIGHT);
63 g3d_matrix_mode(G3D_PROJECTION);
65 g3d_perspective(60.0f, 1.33333, 0.5, 500.0);
67 g3d_enable(G3D_CULL_FACE);
68 g3d_enable(G3D_DEPTH_TEST);
69 g3d_enable(G3D_LIGHTING);
70 g3d_enable(G3D_LIGHT0);
71 g3d_light_ambient(0.2);
73 g3d_polygon_mode(G3D_GOURAUD);
75 if(!(msurf = msurf_create())) {
78 msurf_set_threshold(msurf, 8);
79 msurf_set_inside(msurf, MSURF_GREATER);
80 msurf_set_bounds(msurf, -BBOX_HXSZ, -BBOX_HYSZ, -BBOX_HZSZ, BBOX_HXSZ, BBOX_HYSZ, BBOX_HZSZ);
81 msurf_set_resolution(msurf, VOX_XRES, VOX_YRES, VOX_ZRES);
82 msurf_enable(msurf, MSURF_NORMALIZE);
84 vbuf = malloc_nf(VBUF_SIZE * sizeof *vbuf);
87 mobj = malloc(num_mobj * sizeof *mobj);
88 mobj[0] = metaobj_sgi();
89 mobj[1] = metaobj_sflake();
94 void game_shutdown(void)
98 static void update(float tsec)
103 float *vox = msurf_voxels(msurf);
105 mobj[cur_obj]->update(mobj[cur_obj], tsec);
107 for(i=0; i<VOX_ZRES; i++) {
108 pos.z = -BBOX_HZSZ + i * VOX_ZSTEP;
109 for(j=0; j<VOX_YRES; j++) {
110 pos.y = -BBOX_HYSZ + j * VOX_YSTEP;
111 for(k=0; k<VOX_XRES; k++) {
112 pos.x = -BBOX_HXSZ + k * VOX_XSTEP;
114 /* initialize with the vertical distance for the pool */
115 energy = 5.0 / (pos.y + BBOX_HYSZ);
116 /*energy += 5.0 / (pos.x + BBOX_HXSZ);
117 energy += 5.0 / (BBOX_HXSZ - pos.x);*/
119 energy += mobj[cur_obj]->eval(mobj[cur_obj], &pos);
126 msurf_polygonize(msurf);
131 unsigned long msec = game_getmsec();
132 float tsec = (float)msec / 1000.0f;
136 g3d_clear(G3D_COLOR_BUFFER_BIT | G3D_DEPTH_BUFFER_BIT);
138 g3d_matrix_mode(G3D_MODELVIEW);
140 g3d_translate(0, 1, -14);
141 g3d_rotate(cam_phi, 1, 0, 0);
142 g3d_rotate(cam_theta, 0, 1, 0);
144 g3d_disable(G3D_LIGHTING);
145 g3d_enable(G3D_TEXTURE_2D);
146 g3d_enable(G3D_TEXTURE_GEN);
147 g3d_set_texture(32, 32, textures_img);
149 g3d_disable(G3D_TEXTURE_GEN);
150 g3d_enable(G3D_LIGHTING);
155 static void draw_metaballs(void)
157 int i, nverts, vbuf_count;
159 struct g3d_vertex *vbptr;
162 nverts = msurf_vertex_count(msurf);
163 varr = msurf_vertices(msurf);
164 narr = msurf_normals(msurf);
167 for(i=0; i<nverts; i++) {
168 vbuf_count = vbptr - vbuf;
169 if(vbuf_count >= VBUF_SIZE) {
170 g3d_draw(G3D_TRIANGLES, vbuf, vbuf_count);
188 g3d_draw(G3D_TRIANGLES, vbuf, vbptr - vbuf);
194 void game_keyboard(int key, int press)
196 if(key == 27) game_quit();
199 void game_mouse(int bn, int press, int x, int y)
206 if(press && !grabbed) {
208 } else if(!press && grabbed) {
214 void game_motion(int x, int y)
221 if((dx | dy) == 0) return;
225 mobj[cur_obj]->pos.x += dx * 0.1;
226 mobj[cur_obj]->pos.y -= dy * 0.1;
230 cam_theta += (float)dx * (0.6f * 1.333333333f);
231 cam_phi += (float)dy * 0.6f;
232 if(cam_phi < -90) cam_phi = -90;
233 if(cam_phi > 90) cam_phi = 90;