X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=ld42_outofspace;a=blobdiff_plain;f=src%2Fgamescr.cc;h=d51c16a9471d6c28099bd53d6e1f01cb76bdfb81;hp=26afe4224d8ef1453fa1a79c7ee2f395e810b7cc;hb=89df915930177f3bc5f71095562c6e15074be220;hpb=b8200afc389ccca3e86463eb48c0563c0e093552 diff --git a/src/gamescr.cc b/src/gamescr.cc index 26afe42..d51c16a 100644 --- a/src/gamescr.cc +++ b/src/gamescr.cc @@ -1,9 +1,11 @@ +#include #include #include #include "game.h" #include "screen.h" #include "opengl.h" #include "texture.h" +#include "sdr.h" /* NOTES: * - whistle hhgg music @@ -12,6 +14,7 @@ */ struct Particle { + float radius; float mass; Vec2 pos; Vec2 vel; @@ -24,61 +27,181 @@ struct Emitter { float angle, spread; }; +struct Rect { + int x, y; + int width, height; +}; #define SIM_DT 0.016 -#define GRID_SIZE 4096 +#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 emitters; +static std::vector emitters; -static Texture *grid_tex; +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; - if(!grid_tex->load("data/purple_grid.png")) { + 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; } - grid_tex->set_anisotropy(glcaps.max_aniso); + 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 grid_tex; + 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; imass); + + float *gptr = grid + cbox.y * GRID_SIZE + cbox.x; + Vec2 startpos = grid_to_pos(cbox.x, cbox.y); + + for(int y=0; ypos; + 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() { - static float interval; + 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 @@ -115,7 +238,10 @@ void GameScreen::draw() glDisable(GL_CULL_FACE); glEnable(GL_TEXTURE_2D); - bind_texture(grid_tex); + 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; @@ -123,16 +249,26 @@ void GameScreen::draw() glBegin(GL_QUADS); glColor3f(1, 1, 1); - glTexCoord2f(0, 0); + + glMultiTexCoord2f(0, 0, 0); + glMultiTexCoord2f(1, 0, 0); glVertex3f(-hsz, 0, -hsz); - glTexCoord2f(maxu, 0); + + glMultiTexCoord2f(0, maxu, 0); + glMultiTexCoord2f(1, 1, 0); glVertex3f(hsz, 0, -hsz); - glTexCoord2f(maxu, maxv); + + glMultiTexCoord2f(0, maxu, maxv); + glMultiTexCoord2f(1, 1, 1); glVertex3f(hsz, 0, hsz); - glTexCoord2f(0, maxv); + + glMultiTexCoord2f(0, 0, maxv); + glMultiTexCoord2f(1, 0, 1); glVertex3f(-hsz, 0, hsz); glEnd(); + glUseProgram(0); + glPopAttrib(); } @@ -182,3 +318,25 @@ void GameScreen::mwheel(int dir, int x, int y) 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); +}