crappy emitters, half-assed sim update
authorJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 13 Aug 2018 05:36:07 +0000 (08:36 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 13 Aug 2018 05:36:07 +0000 (08:36 +0300)
sdr/field.p.glsl
sdr/sph.p.glsl [new file with mode: 0644]
sdr/sph.v.glsl [new file with mode: 0644]
src/gamescr.cc

index 1969702..e14ba6f 100644 (file)
@@ -6,7 +6,7 @@ uniform float gvis_scale;
 void main()
 {
        vec2 uv = gl_TexCoord[0].st * vec2(gvis_scale, gvis_scale);
 void main()
 {
        vec2 uv = gl_TexCoord[0].st * vec2(gvis_scale, gvis_scale);
-       vec4 gridcol = texture2D(gvis_tex, uv);
+       vec3 gridcol = texture2D(gvis_tex, uv).rgb;
 
        gl_FragColor.rgb = gridcol.rgb;
        gl_FragColor.a = 1.0;
 
        gl_FragColor.rgb = gridcol.rgb;
        gl_FragColor.a = 1.0;
diff --git a/sdr/sph.p.glsl b/sdr/sph.p.glsl
new file mode 100644 (file)
index 0000000..2ed95b5
--- /dev/null
@@ -0,0 +1,5 @@
+void main()
+{
+       gl_FragColor.rgb = vec3(1.0, 0.0, 0.0);
+       gl_FragColor.a = 1.0;
+}
diff --git a/sdr/sph.v.glsl b/sdr/sph.v.glsl
new file mode 100644 (file)
index 0000000..2612ec3
--- /dev/null
@@ -0,0 +1,4 @@
+void main()
+{
+       gl_Position = ftransform();
+}
index 032b3b9..d0fae08 100644 (file)
@@ -6,11 +6,14 @@
 #include "opengl.h"
 #include "texture.h"
 #include "sdr.h"
 #include "opengl.h"
 #include "texture.h"
 #include "sdr.h"
+#include "mesh.h"
+#include "meshgen.h"
 
 /* NOTES:
  * - whistle hhgg music
  * - colliding particles merge
  * - select objects and center camera on them
 
 /* NOTES:
  * - whistle hhgg music
  * - colliding particles merge
  * - select objects and center camera on them
+ * - tesselate only where necessary
  */
 
 struct Particle {
  */
 
 struct Particle {
@@ -18,6 +21,8 @@ struct Particle {
        float mass;
        Vec2 pos;
        Vec2 vel;
        float mass;
        Vec2 pos;
        Vec2 vel;
+
+       Particle *next, *prev;
 };
 
 struct Emitter {
 };
 
 struct Emitter {
@@ -25,6 +30,8 @@ struct Emitter {
        float mass;
        float rate, chunk;
        float angle, spread;
        float mass;
        float rate, chunk;
        float angle, spread;
+
+       float spawn_pending;
 };
 
 struct Rect {
 };
 
 struct Rect {
@@ -45,7 +52,7 @@ struct QuadMesh {
 #define SIM_DT         0.016
 
 #define GRID_SIZE      2048
 #define SIM_DT         0.016
 
 #define GRID_SIZE      2048
-#define GRID_BITS      12
+#define GRID_BITS      11
 #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 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)
@@ -54,7 +61,7 @@ struct QuadMesh {
 #define MIN_CAM_DIST   1.0f
 #define MAX_CAM_DIST   350.0f
 
 #define MIN_CAM_DIST   1.0f
 #define MAX_CAM_DIST   350.0f
 
-#define MASS_TO_RAD(m) log((m) + 1.0)
+#define MASS_TO_RADIUS(m)      log((m) + 1.0)
 
 #define CONTRIB_THRES  0.005
 #define CONTRIB_RANGE(m) sqrt((m) / CONTRIB_THRES)
 
 #define CONTRIB_THRES  0.005
 #define CONTRIB_RANGE(m) sqrt((m) / CONTRIB_THRES)
@@ -62,14 +69,27 @@ struct QuadMesh {
 static int pos_to_grid(float x, float y);
 static Vec2 grid_to_pos(int gx, int gy);
 
 static int pos_to_grid(float x, float y);
 static Vec2 grid_to_pos(int gx, int gy);
 
+static void calc_contrib_bounds(const Vec2 &pos, float mass, Rect *rect);
+static void add_influence(const Vec2 &pos, float mass, float radius, const Rect &cbox);
+
 static void destroy_quadmesh(QuadMesh *m);
 static void draw_quadmesh(const QuadMesh *m, unsigned int prim = GL_QUADS);
 static void gen_quad_plane(QuadMesh *mesh, float width, float height, int usub, int vsub);
 
 static void destroy_quadmesh(QuadMesh *m);
 static void draw_quadmesh(const QuadMesh *m, unsigned int prim = GL_QUADS);
 static void gen_quad_plane(QuadMesh *mesh, float width, float height, int usub, int vsub);
 
+static void spawn_particle(const Vec2 &pos, const Vec2 &vel, float mass);
+static void add_particle(Particle *p, int grid_idx);
+static void remove_particle(Particle *p, int grid_idx);
+static Particle *alloc_particle();
+void free_particle(Particle *p);
+
 static float grid[GRID_SIZE * GRID_SIZE];
 static float grid[GRID_SIZE * GRID_SIZE];
-static Particle grid_part[GRID_SIZE * GRID_SIZE];
+static Particle *grid_part[GRID_SIZE * GRID_SIZE];
 static Texture *grid_tex;
 
 static Texture *grid_tex;
 
+#define NUM_PARTICLE_LISTS     16
+static Particle *plist[NUM_PARTICLE_LISTS];
+#define GRID_IDX_PLIST(idx)    ((idx) * NUM_PARTICLE_LISTS / (GRID_SIZE * GRID_SIZE))
+
 static std::vector<Emitter*> emitters;
 
 static Texture *gvis_tex;      // texture tile for visualizing a grid
 static std::vector<Emitter*> emitters;
 
 static Texture *gvis_tex;      // texture tile for visualizing a grid
@@ -79,6 +99,9 @@ static float field_scale = 16.0f;
 
 static QuadMesh field_mesh;
 
 
 static QuadMesh field_mesh;
 
+static Mesh *pmesh;
+static unsigned int particle_sdr;
+
 static float cam_theta;
 static float cam_dist = 100.0f;
 static Vec2 *targ_pos;
 static float cam_theta;
 static float cam_dist = 100.0f;
 static Vec2 *targ_pos;
@@ -121,6 +144,13 @@ bool GameScreen::init()
 
        gen_quad_plane(&field_mesh, FIELD_SIZE, FIELD_SIZE, 32, 32);
 
 
        gen_quad_plane(&field_mesh, FIELD_SIZE, FIELD_SIZE, 32, 32);
 
+       pmesh = new Mesh;
+       gen_geosphere(pmesh, 1.0, 2);
+
+       if(!(particle_sdr = create_program_load("sdr/sph.v.glsl", "sdr/sph.p.glsl"))) {
+               return false;
+       }
+
        // XXX DBG
        emit_place_pos = Vec2(0, 0);
        emit_place_pending = true;
        // XXX DBG
        emit_place_pos = Vec2(0, 0);
        emit_place_pending = true;
@@ -135,63 +165,68 @@ void GameScreen::destroy()
        delete gvis_tex;
        delete grid_tex;
        destroy_quadmesh(&field_mesh);
        delete gvis_tex;
        delete grid_tex;
        destroy_quadmesh(&field_mesh);
+       delete pmesh;
 }
 
 }
 
-static void calc_contrib_bounds(const Emitter *em, Rect *rect)
+static void simstep()
 {
 {
-       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));
+       // spawn particles
+       int num_emitters = emitters.size();
+       for(int i=0; i<num_emitters; i++) {
+               Emitter *em = emitters[i];
+               em->spawn_pending += em->rate * SIM_DT;
+               while(em->spawn_pending >= 1.0f && em->mass > 0.0f) {
+                       Vec2 pvel;      // XXX calc eject velocity
 
 
-       int sx = gx - maxrange;
-       int sy = gy - maxrange;
-       int ex = gx + maxrange;
-       int ey = gy + maxrange;
+                       float angle = (float)rand() / (float)RAND_MAX * (M_PI * 2.0);
+                       float emradius = MASS_TO_RADIUS(em->mass);
+                       Vec2 ppos = em->pos + Vec2(cos(angle), sin(angle)) * emradius * 1.00001;
 
 
-       if(ex > GRID_SIZE) ex = GRID_SIZE;
-       if(ey > GRID_SIZE) ey = GRID_SIZE;
+                       float pmass = em->chunk > em->mass ? em->mass : em->chunk;
+                       spawn_particle(ppos, pvel, pmass);
+                       em->mass -= pmass;
 
 
-       rect->x = sx < 0 ? 0 : sx;
-       rect->y = sy < 0 ? 0 : sy;
-       rect->width = ex - sx;
-       rect->height = ey - sy;
-}
+                       em->spawn_pending -= 1.0f;
+               }
+       }
+
+       // remove dead emitters
+       std::vector<Emitter*>::iterator it = emitters.begin();
+       while(it != emitters.end()) {
+               Emitter *em = *it;
+
+               if(em->mass <= 0.0f) {
+                       printf("emitter depleted\n");
+                       it = emitters.erase(it);
+                       delete em;
+               } else {
+                       it++;
+               }
+       }
 
 
-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
        // 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;
        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;
-                               }
+               calc_contrib_bounds(em->pos, em->mass, &cbox);
+               float emradius = MASS_TO_RADIUS(em->mass);
 
 
-                               gptr[x] -= em->mass / dsq;
-                       }
+               add_influence(em->pos, -em->mass, emradius, cbox);
+       }
 
 
-                       startpos.y += GRID_DELTA;
-                       gptr += GRID_SIZE;
+       // contribution of particles
+       for(int i=0; i<NUM_PARTICLE_LISTS; i++) {
+               Particle *p = plist[i];
+               while(p) {
+                       Rect cbox;
+                       calc_contrib_bounds(p->pos, p->mass, &cbox);
+                       add_influence(p->pos, p->mass, p->radius, cbox);
+                       p = p->next;
                }
        }
 
                }
        }
 
@@ -211,13 +246,14 @@ static void update()
                em->pos = emit_place_pos;
                em->mass = 100;
                em->rate = 1;
                em->pos = emit_place_pos;
                em->mass = 100;
                em->rate = 1;
-               em->chunk = 0.05;
+               em->chunk = 0.05 * em->mass;
                em->angle = -1;
                em->spread = 0;
                em->angle = -1;
                em->spread = 0;
+               em->spawn_pending = 0;
                emitters.push_back(em);
 
                Rect cbox;
                emitters.push_back(em);
 
                Rect cbox;
-               calc_contrib_bounds(em, &cbox);
+               calc_contrib_bounds(em->pos, em->mass, &cbox);
                printf("bounds: %d,%d %dx%d\n", cbox.x, cbox.y, cbox.width, cbox.height);
        }
 
                printf("bounds: %d,%d %dx%d\n", cbox.x, cbox.y, cbox.width, cbox.height);
        }
 
@@ -260,19 +296,36 @@ void GameScreen::draw()
        glLoadMatrixf(view_matrix[0]);
 
        glPushAttrib(GL_ENABLE_BIT);
        glLoadMatrixf(view_matrix[0]);
 
        glPushAttrib(GL_ENABLE_BIT);
-       glDisable(GL_LIGHTING);
        glDisable(GL_CULL_FACE);
 
        glDisable(GL_CULL_FACE);
 
+       // draw gravitational field
        glEnable(GL_TEXTURE_2D);
        bind_texture(gvis_tex, 0);
        bind_texture(grid_tex, 1);
 
        glUseProgram(field_sdr);
        glEnable(GL_TEXTURE_2D);
        bind_texture(gvis_tex, 0);
        bind_texture(grid_tex, 1);
 
        glUseProgram(field_sdr);
-       assert(glGetError() == GL_NO_ERROR);
-
        glPatchParameteri(GL_PATCH_VERTICES, 4);
        draw_quadmesh(&field_mesh, GL_PATCHES);
 
        glPatchParameteri(GL_PATCH_VERTICES, 4);
        draw_quadmesh(&field_mesh, GL_PATCHES);
 
+       // draw particles
+       glUseProgram(particle_sdr);
+
+       for(int i=0; i<NUM_PARTICLE_LISTS; i++) {
+               Particle *p = plist[i];
+               while(p) {
+                       int gidx = pos_to_grid(p->pos.x, p->pos.y);
+
+                       glPushMatrix();
+                       glTranslatef(p->pos.x, -grid[gidx] * field_scale, p->pos.y);
+                       glScalef(p->radius, p->radius, p->radius);
+
+                       pmesh->draw();
+
+                       glPopMatrix();
+                       p = p->next;
+               }
+       }
+
        glUseProgram(0);
 
        glPopAttrib();
        glUseProgram(0);
 
        glPopAttrib();
@@ -382,6 +435,52 @@ static Vec2 grid_to_pos(int gx, int gy)
        return Vec2(x, y);
 }
 
        return Vec2(x, y);
 }
 
+static void calc_contrib_bounds(const Vec2 &pos, float mass, Rect *rect)
+{
+       int gidx = pos_to_grid(pos.x, pos.y);
+       int gx = GRID_X(gidx);
+       int gy = GRID_Y(gidx);
+       int maxrange = (int)ceil(CONTRIB_RANGE(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 add_influence(const Vec2 &pos, float mass, float radius, const Rect &cbox)
+{
+       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 - pos;
+                       float dsq = dot(dir, dir);
+                       float radsq = radius * radius;
+                       if(dsq < radsq) {
+                               dsq = radsq;
+                       }
+
+                       gptr[x] += mass / dsq;
+               }
+
+               startpos.y += GRID_DELTA;
+               gptr += GRID_SIZE;
+       }
+}
+
+
 static void destroy_quadmesh(QuadMesh *m)
 {
        glDeleteBuffers(1, &m->vbo_v);
 static void destroy_quadmesh(QuadMesh *m)
 {
        glDeleteBuffers(1, &m->vbo_v);
@@ -475,3 +574,79 @@ static void gen_quad_plane(QuadMesh *m, float width, float height, int usub, int
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
 }
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
 }
+
+static void spawn_particle(const Vec2 &pos, const Vec2 &vel, float mass)
+{
+       int gidx = pos_to_grid(pos.x, pos.y);
+
+       if(grid_part[gidx]) {
+               // merge with existing
+               Particle *p = grid_part[gidx];
+               p->vel += vel;
+               p->mass += mass;
+               p->radius = MASS_TO_RADIUS(p->mass);
+
+       } else {
+               Particle *p = alloc_particle();
+               p->pos = pos;
+               p->vel = vel;
+               p->mass = mass;
+               p->radius = MASS_TO_RADIUS(mass);
+               grid_part[gidx] = p;
+
+               add_particle(p, gidx);
+       }
+}
+
+static void add_particle(Particle *p, int grid_idx)
+{
+       int listidx = GRID_IDX_PLIST(grid_idx);
+       p->next = plist[listidx];
+       p->prev = 0;
+       plist[listidx] = p;
+}
+
+static void remove_particle(Particle *p, int grid_idx)
+{
+       Particle *prev = p->prev;
+       Particle *next = p->next;
+
+       p->prev = p->next = 0;
+       if(next) next->prev = prev;
+
+       if(prev) {
+               prev->next = next;
+       } else {
+               int listidx = GRID_IDX_PLIST(grid_idx);
+               plist[listidx] = next;
+       }
+}
+
+// particle allocator
+#define MAX_PFREE_SIZE 256
+static Particle *pfree_list;
+static int pfree_size;
+
+static Particle *alloc_particle()
+{
+       if(pfree_list) {
+               Particle *p = pfree_list;
+               pfree_list = pfree_list->next;
+               pfree_size--;
+               return p;
+       }
+
+       return new Particle;
+}
+
+void free_particle(Particle *p)
+{
+       if(pfree_size < MAX_PFREE_SIZE) {
+               p->next = pfree_list;
+               p->prev = 0;
+               pfree_list = p;
+               pfree_size++;
+       } else {
+               delete p;
+       }
+}