added a non-interactive blobs exhibit
[laserbrain_demo] / src / blob_exhibit.cc
1 #include "blob_exhibit.h"
2 #include "blobs/metasurf.h"
3 #include "app.h"
4
5 struct Metaball {
6         Vec3 pos;
7         float energy;
8         Vec3 path_scale, path_offset;
9         float phase_offset, speed;
10 };
11 #define NUM_MBALLS      8
12
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}
22 };
23
24 struct BlobPriv {
25         metasurface *msurf;
26         Metaball mballs[NUM_MBALLS];
27         Texture *tex;
28 };
29
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);
32
33
34 BlobExhibit::BlobExhibit()
35 {
36         priv = new BlobPriv;
37         for(int i=0; i<NUM_MBALLS; i++) {
38                 priv->mballs[i] = def_mball_data[i];
39         }
40 }
41
42 BlobExhibit::~BlobExhibit()
43 {
44         delete priv;
45 }
46
47 bool BlobExhibit::init()
48 {
49         if(!(priv->msurf = msurf_create())) {
50                 return false;
51         }
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);
58
59         priv->tex = texman.get_texture("data/sphmap.jpg");
60         return true;
61 }
62
63 void BlobExhibit::destroy()
64 {
65         msurf_free(priv->msurf);
66         priv->msurf = 0;
67 }
68
69 void BlobExhibit::update(float dt)
70 {
71         double sec = time_msec / 1000.0;
72
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;
78         }
79 }
80
81 void BlobExhibit::draw() const
82 {
83         pre_draw();
84
85         glPushAttrib(GL_ENABLE_BIT);
86
87         glUseProgram(0);
88
89         glDisable(GL_LIGHTING);
90         glEnable(GL_TEXTURE_2D);
91         priv->tex->bind();
92
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);
97
98         glFrontFace(GL_CW);
99         glBegin(GL_TRIANGLES);
100         glColor3f(1, 1, 1);
101         msurf_polygonize(priv->msurf);
102         glEnd();
103         glFrontFace(GL_CCW);
104
105         glPopAttrib();
106
107         post_draw();
108 }
109
110 static void vertex(struct metasurface *ms, float x, float y, float z)
111 {
112         static const float delta = 0.01;
113
114         float val = eval(ms, x, y, z);
115         float dfdx = eval(ms, x + delta, y, z) - val;
116         float dfdy = eval(ms, x, y + delta, z) - val;
117         float dfdz = eval(ms, x, y, z + delta) - val;
118
119         glNormal3f(dfdx, dfdy, dfdz);
120         glVertex3f(x, y, z);
121 }
122
123 static float eval(struct metasurface *ms, float x, float y, float z)
124 {
125         float sum = 0.0f;
126         BlobPriv *priv = (BlobPriv*)msurf_get_user_data(ms);
127
128         for(int i=0; i<NUM_MBALLS; i++) {
129                 float dx = x - priv->mballs[i].pos.x;
130                 float dy = y - priv->mballs[i].pos.y;
131                 float dz = z - priv->mballs[i].pos.z;
132                 float dsq = dx * dx + dy * dy + dz * dz;
133
134                 sum += priv->mballs[i].energy / dsq;
135         }
136         return sum;
137 }