initial commit
[ld37_one_room] / 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         glMatrixMode(GL_TEXTURE);
99         glLoadIdentity();
100         glScalef(1, -1, 1);
101
102         glFrontFace(GL_CW);
103         glBegin(GL_TRIANGLES);
104         glColor3f(1, 1, 1);
105         msurf_polygonize(priv->msurf);
106         glEnd();
107         glFrontFace(GL_CCW);
108
109         glLoadIdentity();
110         glMatrixMode(GL_MODELVIEW);
111
112         glPopAttrib();
113
114         post_draw();
115 }
116
117 static void vertex(struct metasurface *ms, float x, float y, float z)
118 {
119         static const float delta = 0.01;
120
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;
125
126         glNormal3f(dfdx, dfdy, dfdz);
127         glVertex3f(x, y, z);
128 }
129
130 static float eval(struct metasurface *ms, float x, float y, float z)
131 {
132         float sum = 0.0f;
133         BlobPriv *priv = (BlobPriv*)msurf_get_user_data(ms);
134
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;
140
141                 sum += priv->mballs[i].energy / dsq;
142         }
143         return sum;
144 }