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()
47 bool BlobExhibit::init()
49 if(!(priv->msurf = msurf_create())) {
52 msurf_set_user_data(priv->msurf, priv);
53 msurf_set_threshold(priv->msurf, 8);
54 msurf_set_inside(priv->msurf, MSURF_GREATER);
55 msurf_set_bounds(priv->msurf, -3.5, 3.5, -3.5, 3.5, -3.5, 3.5);
56 msurf_eval_func(priv->msurf, eval);
57 msurf_vertex_func(priv->msurf, vertex);
59 priv->tex = texman.get_texture("data/sphmap.jpg");
63 void BlobExhibit::destroy()
65 msurf_free(priv->msurf);
69 void BlobExhibit::update(float dt)
71 double sec = time_msec / 1000.0;
73 for(int i=0; i<NUM_MBALLS; i++) {
74 float t = fmod(sec * priv->mballs[i].speed + priv->mballs[i].phase_offset, M_PI * 2.0);
75 priv->mballs[i].pos.x = cos(t) * priv->mballs[i].path_scale.x + priv->mballs[i].path_offset.x;
76 priv->mballs[i].pos.y = sin(t) * priv->mballs[i].path_scale.y + priv->mballs[i].path_offset.y;
77 priv->mballs[i].pos.z = -cos(t) * priv->mballs[i].path_scale.z + priv->mballs[i].path_offset.z;
81 void BlobExhibit::draw() const
85 glPushAttrib(GL_ENABLE_BIT);
89 glDisable(GL_LIGHTING);
90 glEnable(GL_TEXTURE_2D);
93 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
94 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
95 glEnable(GL_TEXTURE_GEN_S);
96 glEnable(GL_TEXTURE_GEN_T);
98 glMatrixMode(GL_TEXTURE);
103 glBegin(GL_TRIANGLES);
105 msurf_polygonize(priv->msurf);
110 glMatrixMode(GL_MODELVIEW);
117 static void vertex(struct metasurface *ms, float x, float y, float z)
119 static const float delta = 0.01;
121 float val = eval(ms, x, y, z);
122 float dfdx = eval(ms, x + delta, y, z) - val;
123 float dfdy = eval(ms, x, y + delta, z) - val;
124 float dfdz = eval(ms, x, y, z + delta) - val;
126 glNormal3f(dfdx, dfdy, dfdz);
130 static float eval(struct metasurface *ms, float x, float y, float z)
133 BlobPriv *priv = (BlobPriv*)msurf_get_user_data(ms);
135 for(int i=0; i<NUM_MBALLS; i++) {
136 float dx = x - priv->mballs[i].pos.x;
137 float dy = y - priv->mballs[i].pos.y;
138 float dz = z - priv->mballs[i].pos.z;
139 float dsq = dx * dx + dy * dy + dz * dz;
141 sum += priv->mballs[i].energy / dsq;