dbf-udg

view src/mballs.cc @ 11:5f99c4c7a9fe

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