1 #include "blob_exhibit.h"
2 #include "blobs/metasurf.h"
8 Vec3 path_scale, path_offset;
9 float phase_offset, speed;
13 static Metaball def_mball_data[] = {
14 {Vec3(0, 0, 0), 2.18038, Vec3(1.09157, 1.69766, 1), Vec3(0.622818, 0.905624, 0), 1.24125, 0.835223},
15 {Vec3(0, 0, 0), 2.03646, Vec3(0.916662, 1.2161, 1), Vec3(0.118734, 0.283516, 0), 2.29201, 1.0134},
16 {Vec3(0, 0, 0), 2.40446, Vec3(1.87429, 1.57595, 1), Vec3(0.298566, -0.788474, 0), 3.8137, 0.516301},
17 {Vec3(0, 0, 0), 0.985774, Vec3(0.705847, 0.735019, 1), Vec3(0.669189, -0.217922, 0), 0.815497, 0.608809},
18 {Vec3(0, 0, 0), 2.49785, Vec3(0.827385, 1.75867, 1), Vec3(0.0284513, 0.247808, 0), 1.86002, 1.13755},
19 {Vec3(0, 0, 0), 1.54857, Vec3(1.24037, 0.938775, 1), Vec3(1.04011, 0.596987, 0), 3.30964, 1.26991},
20 {Vec3(0, 0, 0), 1.30046, Vec3(1.83729, 1.02869, 1), Vec3(-0.476708, 0.676994, 0), 5.77441, 0.569755},
21 {Vec3(0, 0, 0), 2.39865, Vec3(1.28899, 0.788321, 1), Vec3(-0.910677, 0.359099, 0), 5.5935, 0.848893}
26 Metaball mballs[NUM_MBALLS];
30 static void vertex(struct metasurface *ms, float x, float y, float z);
31 static float eval(struct metasurface *ms, float x, float y, float z);
34 BlobExhibit::BlobExhibit()
37 for(int i=0; i<NUM_MBALLS; i++) {
38 priv->mballs[i] = def_mball_data[i];
42 BlobExhibit::~BlobExhibit()
48 bool BlobExhibit::init()
50 if(!(priv->msurf = msurf_create())) {
53 msurf_set_user_data(priv->msurf, priv);
54 msurf_set_threshold(priv->msurf, 8);
55 msurf_set_inside(priv->msurf, MSURF_GREATER);
56 msurf_set_bounds(priv->msurf, -3.5, 3.5, -3.5, 3.5, -3.5, 3.5);
57 msurf_eval_func(priv->msurf, eval);
58 msurf_vertex_func(priv->msurf, vertex);
60 priv->tex = texman.get_texture("data/sphmap.jpg");
64 void BlobExhibit::destroy()
67 msurf_free(priv->msurf);
72 void BlobExhibit::update(float dt)
74 double sec = time_msec / 1000.0;
76 for(int i=0; i<NUM_MBALLS; i++) {
77 float t = fmod(sec * priv->mballs[i].speed + priv->mballs[i].phase_offset, M_PI * 2.0);
78 priv->mballs[i].pos.x = cos(t) * priv->mballs[i].path_scale.x + priv->mballs[i].path_offset.x;
79 priv->mballs[i].pos.y = sin(t) * priv->mballs[i].path_scale.y + priv->mballs[i].path_offset.y;
80 priv->mballs[i].pos.z = -cos(t) * priv->mballs[i].path_scale.z + priv->mballs[i].path_offset.z;
84 void BlobExhibit::draw() const
88 glPushAttrib(GL_ENABLE_BIT);
92 glDisable(GL_LIGHTING);
93 glEnable(GL_TEXTURE_2D);
96 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
97 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
98 glEnable(GL_TEXTURE_GEN_S);
99 glEnable(GL_TEXTURE_GEN_T);
101 glMatrixMode(GL_TEXTURE);
106 glBegin(GL_TRIANGLES);
108 msurf_polygonize(priv->msurf);
113 glMatrixMode(GL_MODELVIEW);
120 static void vertex(struct metasurface *ms, float x, float y, float z)
122 static const float delta = 0.01;
124 float val = eval(ms, x, y, z);
125 float dfdx = eval(ms, x + delta, y, z) - val;
126 float dfdy = eval(ms, x, y + delta, z) - val;
127 float dfdz = eval(ms, x, y, z + delta) - val;
129 glNormal3f(dfdx, dfdy, dfdz);
133 static float eval(struct metasurface *ms, float x, float y, float z)
136 BlobPriv *priv = (BlobPriv*)msurf_get_user_data(ms);
138 for(int i=0; i<NUM_MBALLS; i++) {
139 float dx = x - priv->mballs[i].pos.x;
140 float dy = y - priv->mballs[i].pos.y;
141 float dz = z - priv->mballs[i].pos.z;
142 float dsq = dx * dx + dy * dy + dz * dz;
144 sum += priv->mballs[i].energy / dsq;