dbf-udg

annotate src/mballs.cc @ 8:f0a47f46ee45

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