58522d9303997fa74eab8e3ad94d1ea305b62644
[laserbrain_demo] / src / blob_exhibit.cc
1 #include "blob_exhibit.h"
2 #include "blobs/metasurf.h"
3 #include "app.h"
4 #include <imago2.h>
5
6 struct Metaball {
7         Vec3 pos;
8         float energy;
9         Vec3 path_scale, path_offset;
10         float phase_offset, speed;
11 };
12 #define NUM_MBALLS      8
13
14 static Metaball def_mball_data[] = {
15         {Vec3(0, 0, 0), 2.18038,  Vec3(1.09157, 1.69766, 1),   Vec3(0.622818, 0.905624, 0), 1.24125, 0.835223},
16         {Vec3(0, 0, 0), 2.03646,  Vec3(0.916662, 1.2161, 1),   Vec3(0.118734, 0.283516, 0), 2.29201, 1.0134},
17         {Vec3(0, 0, 0), 2.40446,  Vec3(1.87429, 1.57595, 1),   Vec3(0.298566, -0.788474, 0), 3.8137, 0.516301},
18         {Vec3(0, 0, 0), 0.985774, Vec3(0.705847, 0.735019, 1), Vec3(0.669189, -0.217922, 0), 0.815497, 0.608809},
19         {Vec3(0, 0, 0), 2.49785,  Vec3(0.827385, 1.75867, 1),  Vec3(0.0284513, 0.247808, 0), 1.86002, 1.13755},
20         {Vec3(0, 0, 0), 1.54857,  Vec3(1.24037, 0.938775, 1),  Vec3(1.04011, 0.596987, 0), 3.30964, 1.26991},
21         {Vec3(0, 0, 0), 1.30046,  Vec3(1.83729, 1.02869, 1),   Vec3(-0.476708, 0.676994, 0), 5.77441, 0.569755},
22         {Vec3(0, 0, 0), 2.39865,  Vec3(1.28899, 0.788321, 1),  Vec3(-0.910677, 0.359099, 0), 5.5935, 0.848893}
23 };
24
25 struct BlobPriv {
26         metasurface *msurf;
27         Metaball mballs[NUM_MBALLS];
28         Texture *tex;
29 };
30
31 BlobExhibit::BlobExhibit()
32 {
33         priv = new BlobPriv;
34         for(int i=0; i<NUM_MBALLS; i++) {
35                 priv->mballs[i] = def_mball_data[i];
36         }
37 }
38
39 BlobExhibit::~BlobExhibit()
40 {
41         destroy();
42         delete priv;
43 }
44
45 bool BlobExhibit::init()
46 {
47         if(!(priv->msurf = msurf_create())) {
48                 return false;
49         }
50         msurf_set_threshold(priv->msurf, 8);
51         msurf_set_inside(priv->msurf, MSURF_GREATER);
52         msurf_set_bounds(priv->msurf, -3.5, -3.5, -3.5, 3.5, 3.5, 3.5);
53         msurf_enable(priv->msurf, MSURF_NORMALIZE);
54
55         priv->tex = texman.get_texture("data/sphmap.jpg");
56
57         return true;
58 }
59
60 void BlobExhibit::destroy()
61 {
62         if(priv->msurf) {
63                 msurf_free(priv->msurf);
64                 priv->msurf = 0;
65         }
66 }
67
68 void BlobExhibit::update(float dt)
69 {
70         double sec = time_msec / 1000.0;
71
72         float xmin, xmax, ymin, ymax, zmin, zmax;
73         int xres, yres, zres;
74
75         msurf_get_bounds(priv->msurf, &xmin, &ymin, &zmin, &xmax, &ymax, &zmax);
76         msurf_get_resolution(priv->msurf, &xres, &yres, &zres);
77
78         float xstep = (xmax - xmin) / xres;
79         float ystep = (ymax - ymin) / yres;
80         float zstep = (zmax - zmin) / zres;
81
82         for(int i=0; i<NUM_MBALLS; i++) {
83                 float t = fmod(sec * priv->mballs[i].speed + priv->mballs[i].phase_offset, M_PI * 2.0);
84                 priv->mballs[i].pos.x = cos(t) * priv->mballs[i].path_scale.x + priv->mballs[i].path_offset.x;
85                 priv->mballs[i].pos.y = sin(t) * priv->mballs[i].path_scale.y + priv->mballs[i].path_offset.y;
86                 priv->mballs[i].pos.z = -cos(t) * priv->mballs[i].path_scale.z + priv->mballs[i].path_offset.z;
87         }
88
89         if(!msurf_voxels(priv->msurf)) {
90                 return;
91         }
92
93         float max_energy = 0.0f;
94
95 #pragma omp parallel for
96         for(int i=0; i<zres; i++) {
97                 float z = zmin + i * zstep;
98                 float *voxptr = msurf_slice(priv->msurf, i);
99
100                 for(int j=0; j<yres; j++) {
101                         float y = ymin + j * ystep;
102
103                         for(int k=0; k<xres; k++) {
104                                 float x = xmin + k * xstep;
105
106                                 float sum = 0.0f;
107                                 for(int n=0; n<NUM_MBALLS; n++) {
108                                         float dx = x - priv->mballs[n].pos.x;
109                                         float dy = y - priv->mballs[n].pos.y;
110                                         float dz = z - priv->mballs[n].pos.z;
111                                         float dsq = dx * dx + dy * dy + dz * dz;
112
113                                         sum += priv->mballs[n].energy / dsq;
114                                 }
115                                 *voxptr++ = sum;
116                                 if(sum > max_energy) max_energy = sum;
117                         }
118                 }
119         }
120
121         msurf_polygonize(priv->msurf);
122 }
123
124 void BlobExhibit::draw() const
125 {
126         glPushAttrib(GL_ENABLE_BIT);
127
128         glUseProgram(0);
129
130         glDisable(GL_LIGHTING);
131         glEnable(GL_TEXTURE_2D);
132         glEnable(GL_NORMALIZE);
133         priv->tex->bind();
134
135         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
136         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
137         glEnable(GL_TEXTURE_GEN_S);
138         glEnable(GL_TEXTURE_GEN_T);
139
140         glMatrixMode(GL_TEXTURE);
141         glLoadIdentity();
142         glScalef(1, -1, 1);
143
144         int nverts = msurf_vertex_count(priv->msurf);
145         float *varr = msurf_vertices(priv->msurf);
146         float *narr = msurf_normals(priv->msurf);
147
148         glColor3f(1, 1, 1);
149
150         glBindBuffer(GL_ARRAY_BUFFER, 0);
151         glEnableClientState(GL_VERTEX_ARRAY);
152         glEnableClientState(GL_NORMAL_ARRAY);
153         glVertexPointer(3, GL_FLOAT, 0, varr);
154         glNormalPointer(GL_FLOAT, 0, narr);
155
156         glDrawArrays(GL_TRIANGLES, 0, nverts);
157
158         glDisableClientState(GL_VERTEX_ARRAY);
159         glDisableClientState(GL_NORMAL_ARRAY);
160
161         /*
162         glDisable(GL_TEXTURE_2D);
163         glDisable(GL_LIGHTING);
164
165         varr = msurf_vertices(priv->msurf);
166         narr = msurf_normals(priv->msurf);
167
168         glBegin(GL_LINES);
169         glColor3f(0, 1, 0);
170
171         float nscale = 0.2;
172         for(int i=0; i<nverts; i++) {
173                 glVertex3f(varr[0], varr[1], varr[2]);
174                 glVertex3f(varr[0] + narr[0] * nscale, varr[1] + narr[1] * nscale, varr[2] + narr[2] * nscale);
175                 varr += 3;
176                 narr += 3;
177         }
178         glEnd();
179         */
180
181         glLoadIdentity();
182         glMatrixMode(GL_MODELVIEW);
183
184         glPopAttrib();
185 }