going to sleep
[ld42_outofspace] / src / gamescr.cc
index 0ae1672..d51c16a 100644 (file)
+#include <assert.h>
+#include <vector>
+#include <gmath/gmath.h>
 #include "game.h"
 #include "screen.h"
 #include "opengl.h"
+#include "texture.h"
+#include "sdr.h"
+
+/* NOTES:
+ * - whistle hhgg music
+ * - colliding particles merge
+ * - select objects and center camera on them
+ */
+
+struct Particle {
+       float radius;
+       float mass;
+       Vec2 pos;
+       Vec2 vel;
+};
+
+struct Emitter {
+       Vec2 pos;
+       float mass;
+       float rate, chunk;
+       float angle, spread;
+};
+
+struct Rect {
+       int x, y;
+       int width, height;
+};
+
+#define SIM_DT         0.016
+
+#define GRID_SIZE      2048
+#define GRID_BITS      12
+#define GRID_X(idx)    (((idx) >> GRID_BITS) & (GRID_SIZE - 1))
+#define GRID_Y(idx)    ((idx) & (GRID_SIZE - 1))
+#define GRID_DELTA     ((float)FIELD_SIZE / (float)GRID_SIZE)
+
+#define FIELD_SIZE     2048
+#define MIN_CAM_DIST   1.0f
+#define MAX_CAM_DIST   350.0f
+
+#define MASS_TO_RAD(m) log((m) + 1.0)
+
+#define CONTRIB_THRES  0.005
+#define CONTRIB_RANGE(m) sqrt((m) / CONTRIB_THRES)
+
+static int pos_to_grid(float x, float y);
+static Vec2 grid_to_pos(int gx, int gy);
+
+static float grid[GRID_SIZE * GRID_SIZE];
+static Particle grid_part[GRID_SIZE * GRID_SIZE];
+static Texture *grid_tex;
+
+static std::vector<Emitter*> emitters;
+
+static Texture *gvis_tex;      // texture tile for visualizing a grid
+static unsigned int field_sdr;
+
+static float cam_theta;
+static float cam_dist = 100.0f;
+static Vec2 *targ_pos;
+static Mat4 view_matrix, proj_matrix;
+
+// emitter placement data (filled by event handlers, completed in update)
+static bool emit_place_pending;
+static Vec2 emit_place_pos;
+
 
 bool GameScreen::init()
 {
+       grid_tex = new Texture;
+       grid_tex->create(GRID_SIZE, GRID_SIZE, TEX_2D, GL_LUMINANCE32F_ARB);
+       grid_tex->set_anisotropy(glcaps.max_aniso);
+
+       gvis_tex = new Texture;
+       if(!gvis_tex->load("data/purple_grid.png")) {
+               return false;
+       }
+       gvis_tex->set_anisotropy(glcaps.max_aniso);
+
+       unsigned int vsdr, tcsdr, tesdr, psdr;
+
+       if(!(vsdr = load_vertex_shader("sdr/field.v.glsl")) ||
+                       !(tcsdr = load_tessctl_shader("sdr/field.tc.glsl")) ||
+                       !(tesdr = load_tesseval_shader("sdr/field.te.glsl")) ||
+                       !(psdr = load_pixel_shader("sdr/field.p.glsl"))) {
+               return false;
+       }
+
+       if(!(field_sdr = create_program_link(vsdr, tcsdr, tesdr, psdr, 0))) {
+               return false;
+       }
+       set_uniform_int(field_sdr, "gvis_tex", 0);
+       set_uniform_int(field_sdr, "field_tex", 1);
+       set_uniform_int(field_sdr, "tess_level", 2);
+
+       // XXX DBG
+       emit_place_pos = Vec2(0, 0);
+       emit_place_pending = true;
+
        return true;
 }
 
 void GameScreen::destroy()
 {
+       delete gvis_tex;
 }
 
+static void calc_contrib_bounds(const Emitter *em, Rect *rect)
+{
+       int gidx = pos_to_grid(em->pos.x, em->pos.y);
+       int gx = GRID_X(gidx);
+       int gy = GRID_Y(gidx);
+       int maxrange = (int)ceil(CONTRIB_RANGE(em->mass));
+
+       int sx = gx - maxrange;
+       int sy = gy - maxrange;
+       int ex = gx + maxrange;
+       int ey = gy + maxrange;
+
+       if(ex > GRID_SIZE) ex = GRID_SIZE;
+       if(ey > GRID_SIZE) ey = GRID_SIZE;
+
+       rect->x = sx < 0 ? 0 : sx;
+       rect->y = sy < 0 ? 0 : sy;
+       rect->width = ex - sx;
+       rect->height = ey - sy;
+}
+
+static void simstep()
+{
+       // calculate gravitational field - assume field within radius constant: m / r^2
+
+       // first clear the field, and then add contributions
+       memset(grid, 0, sizeof grid);
+
+       // contribution of emitters
+       int num_emitters = emitters.size();
+       for(int i=0; i<num_emitters; i++) {
+               Emitter *em = emitters[i];
+               Rect cbox;
+               calc_contrib_bounds(em, &cbox);
+               float emradius = MASS_TO_RAD(em->mass);
+
+               float *gptr = grid + cbox.y * GRID_SIZE + cbox.x;
+               Vec2 startpos = grid_to_pos(cbox.x, cbox.y);
+
+               for(int y=0; y<cbox.height; y++) {
+                       for(int x=0; x<cbox.width; x++) {
+                               Vec2 cellpos = Vec2(startpos.x + (float)x * GRID_DELTA, startpos.y);
+
+                               Vec2 dir = cellpos - em->pos;
+                               float dsq = dot(dir, dir);
+                               float radsq = emradius * emradius;
+                               if(dsq < radsq) {
+                                       dsq = radsq;
+                               }
+
+                               gptr[x] -= em->mass / dsq;
+                       }
+
+                       startpos.y += GRID_DELTA;
+                       gptr += GRID_SIZE;
+               }
+       }
+
+       // update texture
+       assert(glGetError() == GL_NO_ERROR);
+       grid_tex->bind();
+       glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GRID_SIZE, GRID_SIZE, GL_LUMINANCE,
+                       GL_FLOAT, grid);
+       assert(glGetError() == GL_NO_ERROR);
+}
+
+static void update()
+{
+       if(emit_place_pending) {
+               emit_place_pending = false;
+               Emitter *em = new Emitter;
+               em->pos = emit_place_pos;
+               em->mass = 100;
+               em->rate = 1;
+               em->chunk = 0.05;
+               em->angle = -1;
+               em->spread = 0;
+               emitters.push_back(em);
+
+               Rect cbox;
+               calc_contrib_bounds(em, &cbox);
+               printf("bounds: %d,%d %dx%d\n", cbox.x, cbox.y, cbox.width, cbox.height);
+       }
+
+       // update simulation
+       static float interval;
+       interval += frame_dt;
+       if(interval >= SIM_DT) {
+               interval -= SIM_DT;
+               simstep();
+               assert(glGetError() == GL_NO_ERROR);
+       }
+
+       // update projection matrix
+       proj_matrix.perspective(deg_to_rad(60.0f), win_aspect, 0.5, 5000.0);
+
+       // update view matrix
+       Vec3 targ;
+       if(targ_pos) {
+               targ.x = targ_pos->x;
+               targ.z = targ_pos->y;
+       }
+
+       float theta = -deg_to_rad(cam_theta);
+       Vec3 camdir = Vec3(sin(theta) * cam_dist, pow(cam_dist * 0.1, 2.0) + 0.5, cos(theta) * cam_dist);
+       Vec3 campos = targ + camdir;
+
+       view_matrix.inv_lookat(campos, targ, Vec3(0, 1, 0));
+}
 
 void GameScreen::draw()
 {
-       glClearColor(1, 0, 0, 1);
+       update();
+
+       glClearColor(0.01, 0.01, 0.01, 1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+       glMatrixMode(GL_PROJECTION);
+       glLoadMatrixf(proj_matrix[0]);
+       glMatrixMode(GL_MODELVIEW);
+       glLoadMatrixf(view_matrix[0]);
+
+       glPushAttrib(GL_ENABLE_BIT);
+       glDisable(GL_LIGHTING);
+       glDisable(GL_CULL_FACE);
+
+       glEnable(GL_TEXTURE_2D);
+       bind_texture(gvis_tex, 0);
+       bind_texture(grid_tex, 1);
+
+       glUseProgram(field_sdr);
+
+       float maxu = FIELD_SIZE / 32.0f;
+       float maxv = FIELD_SIZE / 32.0f;
+       float hsz = FIELD_SIZE * 0.5f;
+
+       glBegin(GL_QUADS);
+       glColor3f(1, 1, 1);
+
+       glMultiTexCoord2f(0, 0, 0);
+       glMultiTexCoord2f(1, 0, 0);
+       glVertex3f(-hsz, 0, -hsz);
+
+       glMultiTexCoord2f(0, maxu, 0);
+       glMultiTexCoord2f(1, 1, 0);
+       glVertex3f(hsz, 0, -hsz);
+
+       glMultiTexCoord2f(0, maxu, maxv);
+       glMultiTexCoord2f(1, 1, 1);
+       glVertex3f(hsz, 0, hsz);
+
+       glMultiTexCoord2f(0, 0, maxv);
+       glMultiTexCoord2f(1, 0, 1);
+       glVertex3f(-hsz, 0, hsz);
+       glEnd();
+
+       glUseProgram(0);
+
+       glPopAttrib();
 }
 
+void GameScreen::reshape(int x, int y)
+{
+}
+
+
 
 void GameScreen::keyboard(int key, bool pressed)
 {
@@ -33,14 +292,51 @@ void GameScreen::keyboard(int key, bool pressed)
        }
 }
 
+static int prev_x, prev_y;
+
 void GameScreen::mbutton(int bn, bool pressed, int x, int y)
 {
+       prev_x = x;
+       prev_y = y;
 }
 
 void GameScreen::mmotion(int x, int y)
 {
+       int dx = x - prev_x;
+       int dy = y - prev_y;
+       prev_x = x;
+       prev_y = y;
+
+       if(game_bnstate(2)) {
+               cam_theta += dx * 0.5;
+       }
 }
 
 void GameScreen::mwheel(int dir, int x, int y)
 {
+       cam_dist -= dir * cam_dist * 0.05f;
+       if(cam_dist <= MIN_CAM_DIST) cam_dist = MIN_CAM_DIST;
+       if(cam_dist > MAX_CAM_DIST) cam_dist = MAX_CAM_DIST;
+}
+
+
+static int pos_to_grid(float x, float y)
+{
+       int gx = ((x / (float)FIELD_SIZE) + 0.5f) * (float)GRID_SIZE;
+       int gy = ((y / (float)FIELD_SIZE) + 0.5f) * (float)GRID_SIZE;
+
+       if(gx < 0) gx = 0;
+       if(gx >= GRID_SIZE) gx = GRID_SIZE - 1;
+       if(gy < 0) gy = 0;
+       if(gy >= GRID_SIZE) gy = GRID_SIZE - 1;
+
+       return (gx << GRID_BITS) | gy;
+}
+
+static Vec2 grid_to_pos(int gx, int gy)
+{
+       float x = (((float)gx / (float)GRID_SIZE) - 0.5f) * (float)FIELD_SIZE;
+       float y = (((float)gy / (float)GRID_SIZE) - 0.5f) * (float)FIELD_SIZE;
+
+       return Vec2(x, y);
 }