dbf-udg

view src/mballs.cc @ 5:e09cbb2e9d4f

metaballs
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 18 Feb 2013 03:46:52 +0200
parents
children f0a47f46ee45
line source
1 #include <vector>
2 #include "opengl.h"
3 #include "mballs.h"
4 #include "metasurf.h"
5 #include "vmath/vmath.h"
7 struct MetaBall {
8 Vector3 pos;
9 float orbit;
10 float energy;
11 float phase_offs;
12 };
14 #define VOL_SZ 2
15 #define MBALL_GRID_SZ 50
17 static void update();
18 static float calc_field(float x, float y, float z);
19 static float eval(float x, float y, float z);
20 static void vertex(float x, float y, float z);
21 static void normal(float x, float y, float z);
23 static float grid[MBALL_GRID_SZ][MBALL_GRID_SZ][MBALL_GRID_SZ];
24 static std::vector<MetaBall> balls;
25 static struct metasurface *msurf;
27 bool mball_init()
28 {
29 static const float bbmin = -VOL_SZ / 2.0;
30 static const float bbmax = VOL_SZ / 2.0;
32 if(!(msurf = msurf_create())) {
33 return false;
34 }
35 msurf_threshold(msurf, 10);
36 msurf_resolution(msurf, MBALL_GRID_SZ, MBALL_GRID_SZ, MBALL_GRID_SZ);
37 msurf_bounds(msurf, bbmin, bbmin, bbmin, bbmax, bbmax, bbmax);
38 msurf_eval_func(msurf, eval);
39 msurf_vertex_func(msurf, vertex);
40 //msurf_normal_func(msurf, normal);
42 for(int i=0; i<10; i++) {
43 MetaBall mb;
44 mb.orbit = 0.25 * rand() / (float)RAND_MAX + 0.35;
45 mb.energy = 0.2 * rand() / (float)RAND_MAX + 0.15;
46 mb.phase_offs = rand() / (float)RAND_MAX * M_PI * 2.0;
47 balls.push_back(mb);
48 }
50 return true;
51 }
53 void mball_render()
54 {
55 update();
57 glMatrixMode(GL_MODELVIEW);
58 glPushMatrix();
59 glScalef(4.0, 4.0, 4.0);
61 glBegin(GL_TRIANGLES);
62 msurf_polygonize(msurf);
63 glEnd();
65 glPopMatrix();
66 }
69 static void update()
70 {
71 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
72 float sec = msec / 1000.0;
74 for(size_t i=0; i<balls.size(); i++) {
75 float t = sec + balls[i].phase_offs;
76 balls[i].pos.x = cos(t * 1.8) * balls[i].orbit;
77 balls[i].pos.z = sin(t * 1.2) * balls[i].orbit;
78 balls[i].pos.y = (sin(t) + cos(t * 2.0) / 2.0 + sin(t * 3.0) / 3.0) * 0.45;
79 }
81 for(int i=0; i<MBALL_GRID_SZ; i++) {
82 float x = (float)i / (float)MBALL_GRID_SZ * 2.0 - 1.0;
83 for(int j=0; j<MBALL_GRID_SZ; j++) {
84 float y = (float)j / (float)MBALL_GRID_SZ * 2.0 - 1.0;
85 for(int k=0; k<MBALL_GRID_SZ; k++) {
86 float z = (float)k / (float)MBALL_GRID_SZ * 2.0 - 1.0;
87 grid[i][j][k] = calc_field(x, y, z);
88 }
89 }
90 }
91 }
93 static float calc_field(float x, float y, float z)
94 {
95 Vector3 pt(x, y, z);
97 float sum = 0.0f;
98 for(size_t i=0; i<balls.size(); i++) {
99 float dist_sq = (balls[i].pos - pt).length_sq();
100 if(dist_sq > 1e-6) {
101 sum += balls[i].energy / dist_sq;
102 } else {
103 sum += 1000.0;
104 }
105 }
106 return sum;
107 }
109 static inline int clamp(int x, int a, int b)
110 {
111 return x < a ? a : (x > b ? b : x);
112 }
114 static float eval(float x, float y, float z)
115 {
116 int cell_x = clamp((int)((x / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1);
117 int cell_y = clamp((int)((y / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1);
118 int cell_z = clamp((int)((z / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1);
120 return grid[cell_x][cell_y][cell_z];
121 }
123 static void vertex(float x, float y, float z)
124 {
125 float delta = (float)VOL_SZ / (float)MBALL_GRID_SZ;
127 float dfdx = calc_field(x - delta, y, z) - calc_field(x + delta, y, z);
128 float dfdy = calc_field(x, y - delta, z) - calc_field(x, y + delta, z);
129 float dfdz = calc_field(x, y, z - delta) - calc_field(x, y, z + delta);
131 float len = sqrt(dfdx * dfdx + dfdy * dfdy + dfdz * dfdz);
133 glNormal3f(dfdx / len, dfdy / len, dfdz / len);
134 glVertex3f(x, y, z);
135 }
137 static void normal(float x, float y, float z)
138 {
139 glNormal3f(x, y, z);
140 }