nuclear@5: #include nuclear@5: #include "opengl.h" nuclear@5: #include "mballs.h" nuclear@5: #include "metasurf.h" nuclear@5: #include "vmath/vmath.h" nuclear@5: nuclear@5: struct MetaBall { nuclear@5: Vector3 pos; nuclear@5: float orbit; nuclear@5: float energy; nuclear@5: float phase_offs; nuclear@5: }; nuclear@5: nuclear@5: #define VOL_SZ 2 nuclear@5: #define MBALL_GRID_SZ 50 nuclear@5: nuclear@5: static void update(); nuclear@5: static float calc_field(float x, float y, float z); nuclear@5: static float eval(float x, float y, float z); nuclear@5: static void vertex(float x, float y, float z); nuclear@5: static void normal(float x, float y, float z); nuclear@5: nuclear@5: static float grid[MBALL_GRID_SZ][MBALL_GRID_SZ][MBALL_GRID_SZ]; nuclear@5: static std::vector balls; nuclear@5: static struct metasurface *msurf; nuclear@5: nuclear@5: bool mball_init() nuclear@5: { nuclear@5: static const float bbmin = -VOL_SZ / 2.0; nuclear@5: static const float bbmax = VOL_SZ / 2.0; nuclear@5: nuclear@5: if(!(msurf = msurf_create())) { nuclear@5: return false; nuclear@5: } nuclear@5: msurf_threshold(msurf, 10); nuclear@5: msurf_resolution(msurf, MBALL_GRID_SZ, MBALL_GRID_SZ, MBALL_GRID_SZ); nuclear@5: msurf_bounds(msurf, bbmin, bbmin, bbmin, bbmax, bbmax, bbmax); nuclear@5: msurf_eval_func(msurf, eval); nuclear@5: msurf_vertex_func(msurf, vertex); nuclear@5: //msurf_normal_func(msurf, normal); nuclear@5: nuclear@5: for(int i=0; i<10; i++) { nuclear@5: MetaBall mb; nuclear@5: mb.orbit = 0.25 * rand() / (float)RAND_MAX + 0.35; nuclear@5: mb.energy = 0.2 * rand() / (float)RAND_MAX + 0.15; nuclear@5: mb.phase_offs = rand() / (float)RAND_MAX * M_PI * 2.0; nuclear@5: balls.push_back(mb); nuclear@5: } nuclear@5: nuclear@5: return true; nuclear@5: } nuclear@5: nuclear@5: void mball_render() nuclear@5: { nuclear@5: update(); nuclear@5: nuclear@5: glMatrixMode(GL_MODELVIEW); nuclear@5: glPushMatrix(); nuclear@5: glScalef(4.0, 4.0, 4.0); nuclear@5: nuclear@5: glBegin(GL_TRIANGLES); nuclear@5: msurf_polygonize(msurf); nuclear@5: glEnd(); nuclear@5: nuclear@5: glPopMatrix(); nuclear@5: } nuclear@5: nuclear@5: nuclear@5: static void update() nuclear@5: { nuclear@5: unsigned int msec = glutGet(GLUT_ELAPSED_TIME); nuclear@5: float sec = msec / 1000.0; nuclear@5: nuclear@5: for(size_t i=0; i 1e-6) { nuclear@5: sum += balls[i].energy / dist_sq; nuclear@5: } else { nuclear@5: sum += 1000.0; nuclear@5: } nuclear@5: } nuclear@5: return sum; nuclear@5: } nuclear@5: nuclear@5: static inline int clamp(int x, int a, int b) nuclear@5: { nuclear@5: return x < a ? a : (x > b ? b : x); nuclear@5: } nuclear@5: nuclear@5: static float eval(float x, float y, float z) nuclear@5: { nuclear@5: int cell_x = clamp((int)((x / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1); nuclear@5: int cell_y = clamp((int)((y / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1); nuclear@5: int cell_z = clamp((int)((z / VOL_SZ + 0.5) * MBALL_GRID_SZ), 0, MBALL_GRID_SZ - 1); nuclear@5: nuclear@5: return grid[cell_x][cell_y][cell_z]; nuclear@5: } nuclear@5: nuclear@5: static void vertex(float x, float y, float z) nuclear@5: { nuclear@5: float delta = (float)VOL_SZ / (float)MBALL_GRID_SZ; nuclear@5: nuclear@5: float dfdx = calc_field(x - delta, y, z) - calc_field(x + delta, y, z); nuclear@5: float dfdy = calc_field(x, y - delta, z) - calc_field(x, y + delta, z); nuclear@5: float dfdz = calc_field(x, y, z - delta) - calc_field(x, y, z + delta); nuclear@5: nuclear@5: float len = sqrt(dfdx * dfdx + dfdy * dfdy + dfdz * dfdz); nuclear@5: nuclear@5: glNormal3f(dfdx / len, dfdy / len, dfdz / len); nuclear@5: glVertex3f(x, y, z); nuclear@5: } nuclear@5: nuclear@5: static void normal(float x, float y, float z) nuclear@5: { nuclear@5: glNormal3f(x, y, z); nuclear@5: }