rev |
line source |
nuclear@5
|
1 #include <vector>
|
nuclear@5
|
2 #include "opengl.h"
|
nuclear@5
|
3 #include "mballs.h"
|
nuclear@5
|
4 #include "metasurf.h"
|
nuclear@5
|
5 #include "vmath/vmath.h"
|
nuclear@9
|
6 #include "dsys.h"
|
nuclear@5
|
7
|
nuclear@5
|
8 struct MetaBall {
|
nuclear@5
|
9 Vector3 pos;
|
nuclear@5
|
10 float orbit;
|
nuclear@5
|
11 float energy;
|
nuclear@5
|
12 float phase_offs;
|
nuclear@5
|
13 };
|
nuclear@5
|
14
|
nuclear@5
|
15 #define VOL_SZ 2
|
nuclear@5
|
16 #define MBALL_GRID_SZ 50
|
nuclear@5
|
17
|
nuclear@9
|
18 static void update(float sec);
|
nuclear@5
|
19 static float calc_field(float x, float y, float z);
|
nuclear@5
|
20 static float eval(float x, float y, float z);
|
nuclear@5
|
21 static void vertex(float x, float y, float z);
|
nuclear@5
|
22 static void normal(float x, float y, float z);
|
nuclear@5
|
23
|
nuclear@5
|
24 static float grid[MBALL_GRID_SZ][MBALL_GRID_SZ][MBALL_GRID_SZ];
|
nuclear@5
|
25 static std::vector<MetaBall> balls;
|
nuclear@5
|
26 static struct metasurface *msurf;
|
nuclear@8
|
27 static float floor_height = -0.95;
|
nuclear@5
|
28
|
nuclear@5
|
29 bool mball_init()
|
nuclear@5
|
30 {
|
nuclear@5
|
31 static const float bbmin = -VOL_SZ / 2.0;
|
nuclear@5
|
32 static const float bbmax = VOL_SZ / 2.0;
|
nuclear@5
|
33
|
nuclear@5
|
34 if(!(msurf = msurf_create())) {
|
nuclear@5
|
35 return false;
|
nuclear@5
|
36 }
|
nuclear@5
|
37 msurf_threshold(msurf, 10);
|
nuclear@5
|
38 msurf_resolution(msurf, MBALL_GRID_SZ, MBALL_GRID_SZ, MBALL_GRID_SZ);
|
nuclear@5
|
39 msurf_bounds(msurf, bbmin, bbmin, bbmin, bbmax, bbmax, bbmax);
|
nuclear@5
|
40 msurf_eval_func(msurf, eval);
|
nuclear@5
|
41 msurf_vertex_func(msurf, vertex);
|
nuclear@5
|
42 //msurf_normal_func(msurf, normal);
|
nuclear@5
|
43
|
nuclear@5
|
44 for(int i=0; i<10; i++) {
|
nuclear@5
|
45 MetaBall mb;
|
nuclear@5
|
46 mb.orbit = 0.25 * rand() / (float)RAND_MAX + 0.35;
|
nuclear@9
|
47 mb.energy = 0.1 * rand() / (float)RAND_MAX + 0.15;
|
nuclear@5
|
48 mb.phase_offs = rand() / (float)RAND_MAX * M_PI * 2.0;
|
nuclear@5
|
49 balls.push_back(mb);
|
nuclear@5
|
50 }
|
nuclear@5
|
51
|
nuclear@5
|
52 return true;
|
nuclear@5
|
53 }
|
nuclear@5
|
54
|
nuclear@9
|
55 void mball_render(float sec)
|
nuclear@5
|
56 {
|
nuclear@9
|
57 update(sec);
|
nuclear@5
|
58
|
nuclear@8
|
59 const float blue[] = {0.4, 0.45, 1.0, 1};
|
nuclear@8
|
60 const float dark_red[] = {0.6, 0.2, 0.1, 1};
|
nuclear@8
|
61 const float white[] = {1, 1, 1, 1};
|
nuclear@8
|
62 const float black[] = {0, 0, 0, 1};
|
nuclear@8
|
63
|
nuclear@5
|
64 glMatrixMode(GL_MODELVIEW);
|
nuclear@5
|
65 glPushMatrix();
|
nuclear@5
|
66 glScalef(4.0, 4.0, 4.0);
|
nuclear@5
|
67
|
nuclear@8
|
68 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
|
nuclear@8
|
69 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
|
nuclear@8
|
70 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
|
nuclear@8
|
71
|
nuclear@5
|
72 glBegin(GL_TRIANGLES);
|
nuclear@5
|
73 msurf_polygonize(msurf);
|
nuclear@5
|
74 glEnd();
|
nuclear@5
|
75
|
nuclear@8
|
76 // floor
|
nuclear@8
|
77 glBegin(GL_QUADS);
|
nuclear@8
|
78 glNormal3f(0, 1, 0);
|
nuclear@8
|
79 glVertex3f(-5, -1, 5);
|
nuclear@8
|
80 glVertex3f(5, -1, 5);
|
nuclear@8
|
81 glVertex3f(5, -1, -5);
|
nuclear@8
|
82 glVertex3f(-5, -1, -5);
|
nuclear@8
|
83 glEnd();
|
nuclear@8
|
84
|
nuclear@8
|
85 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dark_red);
|
nuclear@8
|
86 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
|
nuclear@8
|
87 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
|
nuclear@8
|
88
|
nuclear@8
|
89 // box
|
nuclear@8
|
90 glPushMatrix();
|
nuclear@8
|
91 glTranslatef(0, -1.7, 1);
|
nuclear@8
|
92 glScalef(1.05, 1, 0.05);
|
nuclear@8
|
93 glutSolidCube(2.0);
|
nuclear@8
|
94 glPopMatrix();
|
nuclear@8
|
95
|
nuclear@8
|
96 glPushMatrix();
|
nuclear@8
|
97 glTranslatef(0, -1.7, -1);
|
nuclear@8
|
98 glScalef(1.05, 1, 0.05);
|
nuclear@8
|
99 glutSolidCube(2.0);
|
nuclear@8
|
100 glPopMatrix();
|
nuclear@8
|
101
|
nuclear@8
|
102 glPushMatrix();
|
nuclear@8
|
103 glTranslatef(1, -1.7, 0);
|
nuclear@8
|
104 glScalef(0.05, 1, 1);
|
nuclear@8
|
105 glutSolidCube(2.0);
|
nuclear@8
|
106 glPopMatrix();
|
nuclear@8
|
107
|
nuclear@8
|
108 glPushMatrix();
|
nuclear@8
|
109 glTranslatef(-1, -1.7, 0);
|
nuclear@8
|
110 glScalef(0.05, 1, 1);
|
nuclear@8
|
111 glutSolidCube(2.0);
|
nuclear@8
|
112 glPopMatrix();
|
nuclear@8
|
113
|
nuclear@5
|
114 glPopMatrix();
|
nuclear@5
|
115 }
|
nuclear@5
|
116
|
nuclear@5
|
117
|
nuclear@9
|
118 static void update(float sec)
|
nuclear@5
|
119 {
|
nuclear@5
|
120 for(size_t i=0; i<balls.size(); i++) {
|
nuclear@5
|
121 float t = sec + balls[i].phase_offs;
|
nuclear@5
|
122 balls[i].pos.x = cos(t * 1.8) * balls[i].orbit;
|
nuclear@5
|
123 balls[i].pos.z = sin(t * 1.2) * balls[i].orbit;
|
nuclear@5
|
124 balls[i].pos.y = (sin(t) + cos(t * 2.0) / 2.0 + sin(t * 3.0) / 3.0) * 0.45;
|
nuclear@5
|
125 }
|
nuclear@5
|
126
|
nuclear@5
|
127 for(int i=0; i<MBALL_GRID_SZ; i++) {
|
nuclear@5
|
128 float x = (float)i / (float)MBALL_GRID_SZ * 2.0 - 1.0;
|
nuclear@5
|
129 for(int j=0; j<MBALL_GRID_SZ; j++) {
|
nuclear@5
|
130 float y = (float)j / (float)MBALL_GRID_SZ * 2.0 - 1.0;
|
nuclear@5
|
131 for(int k=0; k<MBALL_GRID_SZ; k++) {
|
nuclear@5
|
132 float z = (float)k / (float)MBALL_GRID_SZ * 2.0 - 1.0;
|
nuclear@5
|
133 grid[i][j][k] = calc_field(x, y, z);
|
nuclear@5
|
134 }
|
nuclear@5
|
135 }
|
nuclear@5
|
136 }
|
nuclear@5
|
137 }
|
nuclear@5
|
138
|
nuclear@5
|
139 static float calc_field(float x, float y, float z)
|
nuclear@5
|
140 {
|
nuclear@5
|
141 Vector3 pt(x, y, z);
|
nuclear@5
|
142
|
nuclear@5
|
143 float sum = 0.0f;
|
nuclear@5
|
144 for(size_t i=0; i<balls.size(); i++) {
|
nuclear@5
|
145 float dist_sq = (balls[i].pos - pt).length_sq();
|
nuclear@5
|
146 if(dist_sq > 1e-6) {
|
nuclear@5
|
147 sum += balls[i].energy / dist_sq;
|
nuclear@5
|
148 } else {
|
nuclear@5
|
149 sum += 1000.0;
|
nuclear@5
|
150 }
|
nuclear@5
|
151 }
|
nuclear@8
|
152
|
nuclear@8
|
153 // floor
|
nuclear@8
|
154 float height = y - floor_height;
|
nuclear@8
|
155 if(height > 1e-6) {
|
nuclear@8
|
156 sum += 1.0 / height;
|
nuclear@8
|
157 } else {
|
nuclear@8
|
158 sum += 1000.0;
|
nuclear@8
|
159 }
|
nuclear@8
|
160
|
nuclear@5
|
161 return sum;
|
nuclear@5
|
162 }
|
nuclear@5
|
163
|
nuclear@5
|
164 static inline int clamp(int x, int a, int b)
|
nuclear@5
|
165 {
|
nuclear@5
|
166 return x < a ? a : (x > b ? b : x);
|
nuclear@5
|
167 }
|
nuclear@5
|
168
|
nuclear@5
|
169 static float eval(float x, float y, float z)
|
nuclear@5
|
170 {
|
nuclear@5
|
171 int cell_x = clamp((int)((x / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1);
|
nuclear@5
|
172 int cell_y = clamp((int)((y / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1);
|
nuclear@5
|
173 int cell_z = clamp((int)((z / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1);
|
nuclear@5
|
174
|
nuclear@5
|
175 return grid[cell_x][cell_y][cell_z];
|
nuclear@5
|
176 }
|
nuclear@5
|
177
|
nuclear@5
|
178 static void vertex(float x, float y, float z)
|
nuclear@5
|
179 {
|
nuclear@5
|
180 float delta = (float)VOL_SZ / (float)MBALL_GRID_SZ;
|
nuclear@5
|
181
|
nuclear@5
|
182 float dfdx = calc_field(x - delta, y, z) - calc_field(x + delta, y, z);
|
nuclear@5
|
183 float dfdy = calc_field(x, y - delta, z) - calc_field(x, y + delta, z);
|
nuclear@5
|
184 float dfdz = calc_field(x, y, z - delta) - calc_field(x, y, z + delta);
|
nuclear@5
|
185
|
nuclear@5
|
186 float len = sqrt(dfdx * dfdx + dfdy * dfdy + dfdz * dfdz);
|
nuclear@5
|
187
|
nuclear@5
|
188 glNormal3f(dfdx / len, dfdy / len, dfdz / len);
|
nuclear@5
|
189 glVertex3f(x, y, z);
|
nuclear@5
|
190 }
|
nuclear@5
|
191
|
nuclear@5
|
192 static void normal(float x, float y, float z)
|
nuclear@5
|
193 {
|
nuclear@5
|
194 glNormal3f(x, y, z);
|
nuclear@5
|
195 }
|