dbf-udg

view src/mballs.cc @ 9:7056437a361b

added demosys
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 19 Feb 2013 18:17:17 +0200
parents f0a47f46ee45
children 5f99c4c7a9fe
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"
8 struct MetaBall {
9 Vector3 pos;
10 float orbit;
11 float energy;
12 float phase_offs;
13 };
15 #define VOL_SZ 2
16 #define MBALL_GRID_SZ 50
18 static void update(float sec);
19 static float calc_field(float x, float y, float z);
20 static float eval(float x, float y, float z);
21 static void vertex(float x, float y, float z);
22 static void normal(float x, float y, float z);
24 static float grid[MBALL_GRID_SZ][MBALL_GRID_SZ][MBALL_GRID_SZ];
25 static std::vector<MetaBall> balls;
26 static struct metasurface *msurf;
27 static float floor_height = -0.95;
29 bool mball_init()
30 {
31 static const float bbmin = -VOL_SZ / 2.0;
32 static const float bbmax = VOL_SZ / 2.0;
34 if(!(msurf = msurf_create())) {
35 return false;
36 }
37 msurf_threshold(msurf, 10);
38 msurf_resolution(msurf, MBALL_GRID_SZ, MBALL_GRID_SZ, MBALL_GRID_SZ);
39 msurf_bounds(msurf, bbmin, bbmin, bbmin, bbmax, bbmax, bbmax);
40 msurf_eval_func(msurf, eval);
41 msurf_vertex_func(msurf, vertex);
42 //msurf_normal_func(msurf, normal);
44 for(int i=0; i<10; i++) {
45 MetaBall mb;
46 mb.orbit = 0.25 * rand() / (float)RAND_MAX + 0.35;
47 mb.energy = 0.1 * rand() / (float)RAND_MAX + 0.15;
48 mb.phase_offs = rand() / (float)RAND_MAX * M_PI * 2.0;
49 balls.push_back(mb);
50 }
52 return true;
53 }
55 void mball_render(float sec)
56 {
57 update(sec);
59 const float blue[] = {0.4, 0.45, 1.0, 1};
60 const float dark_red[] = {0.6, 0.2, 0.1, 1};
61 const float white[] = {1, 1, 1, 1};
62 const float black[] = {0, 0, 0, 1};
64 glMatrixMode(GL_MODELVIEW);
65 glPushMatrix();
66 glScalef(4.0, 4.0, 4.0);
68 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
69 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
70 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
72 glBegin(GL_TRIANGLES);
73 msurf_polygonize(msurf);
74 glEnd();
76 // floor
77 glBegin(GL_QUADS);
78 glNormal3f(0, 1, 0);
79 glVertex3f(-5, -1, 5);
80 glVertex3f(5, -1, 5);
81 glVertex3f(5, -1, -5);
82 glVertex3f(-5, -1, -5);
83 glEnd();
85 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dark_red);
86 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
87 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
89 // box
90 glPushMatrix();
91 glTranslatef(0, -1.7, 1);
92 glScalef(1.05, 1, 0.05);
93 glutSolidCube(2.0);
94 glPopMatrix();
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(1, -1.7, 0);
104 glScalef(0.05, 1, 1);
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 glPopMatrix();
115 }
118 static void update(float sec)
119 {
120 for(size_t i=0; i<balls.size(); i++) {
121 float t = sec + balls[i].phase_offs;
122 balls[i].pos.x = cos(t * 1.8) * balls[i].orbit;
123 balls[i].pos.z = sin(t * 1.2) * balls[i].orbit;
124 balls[i].pos.y = (sin(t) + cos(t * 2.0) / 2.0 + sin(t * 3.0) / 3.0) * 0.45;
125 }
127 for(int i=0; i<MBALL_GRID_SZ; i++) {
128 float x = (float)i / (float)MBALL_GRID_SZ * 2.0 - 1.0;
129 for(int j=0; j<MBALL_GRID_SZ; j++) {
130 float y = (float)j / (float)MBALL_GRID_SZ * 2.0 - 1.0;
131 for(int k=0; k<MBALL_GRID_SZ; k++) {
132 float z = (float)k / (float)MBALL_GRID_SZ * 2.0 - 1.0;
133 grid[i][j][k] = calc_field(x, y, z);
134 }
135 }
136 }
137 }
139 static float calc_field(float x, float y, float z)
140 {
141 Vector3 pt(x, y, z);
143 float sum = 0.0f;
144 for(size_t i=0; i<balls.size(); i++) {
145 float dist_sq = (balls[i].pos - pt).length_sq();
146 if(dist_sq > 1e-6) {
147 sum += balls[i].energy / dist_sq;
148 } else {
149 sum += 1000.0;
150 }
151 }
153 // floor
154 float height = y - floor_height;
155 if(height > 1e-6) {
156 sum += 1.0 / height;
157 } else {
158 sum += 1000.0;
159 }
161 return sum;
162 }
164 static inline int clamp(int x, int a, int b)
165 {
166 return x < a ? a : (x > b ? b : x);
167 }
169 static float eval(float x, float y, float z)
170 {
171 int cell_x = clamp((int)((x / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1);
172 int cell_y = clamp((int)((y / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1);
173 int cell_z = clamp((int)((z / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1);
175 return grid[cell_x][cell_y][cell_z];
176 }
178 static void vertex(float x, float y, float z)
179 {
180 float delta = (float)VOL_SZ / (float)MBALL_GRID_SZ;
182 float dfdx = calc_field(x - delta, y, z) - calc_field(x + delta, y, z);
183 float dfdy = calc_field(x, y - delta, z) - calc_field(x, y + delta, z);
184 float dfdz = calc_field(x, y, z - delta) - calc_field(x, y, z + delta);
186 float len = sqrt(dfdx * dfdx + dfdy * dfdy + dfdz * dfdz);
188 glNormal3f(dfdx / len, dfdy / len, dfdz / len);
189 glVertex3f(x, y, z);
190 }
192 static void normal(float x, float y, float z)
193 {
194 glNormal3f(x, y, z);
195 }