3 #include <gmath/gmath.h>
13 * - whistle hhgg music
14 * - select objects and center camera on them
25 Particle *next, *prev;
47 int num_verts, num_idx, num_quads;
49 unsigned int vbo_v, vbo_uv, ibo;
54 #define GRID_SIZE 2048
56 #define GRID_X(idx) (((idx) >> GRID_BITS) & (GRID_SIZE - 1))
57 #define GRID_Y(idx) ((idx) & (GRID_SIZE - 1))
58 #define GRID_DELTA ((float)FIELD_SIZE / (float)GRID_SIZE)
60 #define FIELD_SIZE 2048
61 #define MIN_CAM_DIST 1.0f
62 #define MAX_CAM_DIST 350.0f
64 #define MASS_TO_RADIUS(m) log((m) + 1.0)
66 #define CONTRIB_THRES 0.005
67 #define CONTRIB_RANGE(m) sqrt((m) / CONTRIB_THRES)
69 /* gravitational strength */
70 #define GRAV_STR 16.0f
72 static int pos_to_grid_x_noclamp(float x);
73 static int pos_to_grid_y_noclamp(float y);
74 static int pos_to_grid(float x, float y);
75 static Vec2 grid_to_pos(int gx, int gy);
77 static void calc_contrib_bounds(const Vec2 &pos, float mass, Rect *rect);
78 static void add_influence(const Vec2 &pos, float mass, float radius, const Rect &cbox);
80 static Vec2 calc_field_grad(int gidx);
82 static void destroy_quadmesh(QuadMesh *m);
83 static void draw_quadmesh(const QuadMesh *m, unsigned int prim = GL_QUADS);
84 static void gen_quad_plane(QuadMesh *mesh, float width, float height, int usub, int vsub);
86 static void spawn_particle(const Vec2 &pos, const Vec2 &vel, float mass);
87 static void add_particle(Particle *p);
88 static void remove_particle(Particle *p);
89 static Particle *alloc_particle();
90 void free_particle(Particle *p);
92 static float grid[GRID_SIZE * GRID_SIZE];
93 static Particle *grid_part[GRID_SIZE * GRID_SIZE];
94 static Texture *grid_tex;
96 static Particle *plist;
98 static std::vector<Emitter*> emitters;
100 static Texture *gvis_tex; // texture tile for visualizing a grid
101 static unsigned int field_sdr;
102 static int tess_level = 64;
103 static float field_scale = 16.0f;
105 static QuadMesh field_mesh;
108 static unsigned int particle_sdr;
110 static float cam_theta;
111 static float cam_dist = 100.0f;
112 static Vec2 *targ_pos;
113 static Mat4 view_matrix, proj_matrix;
115 static bool wireframe;
117 // emitter placement data (filled by event handlers, completed in update)
118 static bool emit_place_pending;
119 static Vec2 emit_place_pos;
122 bool GameScreen::init()
124 grid_tex = new Texture;
125 grid_tex->create(GRID_SIZE, GRID_SIZE, TEX_2D, GL_LUMINANCE32F_ARB);
126 grid_tex->set_anisotropy(glcaps.max_aniso);
128 gvis_tex = new Texture;
129 if(!gvis_tex->load("data/purple_grid.png")) {
132 gvis_tex->set_anisotropy(glcaps.max_aniso);
134 unsigned int vsdr, tcsdr, tesdr, psdr;
136 if(!(vsdr = load_vertex_shader("sdr/field.v.glsl")) ||
137 !(tcsdr = load_tessctl_shader("sdr/field.tc.glsl")) ||
138 !(tesdr = load_tesseval_shader("sdr/field.te.glsl")) ||
139 !(psdr = load_pixel_shader("sdr/field.p.glsl"))) {
143 if(!(field_sdr = create_program_link(vsdr, tcsdr, tesdr, psdr, 0))) {
146 set_uniform_int(field_sdr, "gvis_tex", 0);
147 set_uniform_int(field_sdr, "field_tex", 1);
148 set_uniform_float(field_sdr, "gvis_scale", FIELD_SIZE / 32.0f);
149 set_uniform_int(field_sdr, "tess_level", tess_level);
150 set_uniform_float(field_sdr, "field_scale", field_scale);
152 gen_quad_plane(&field_mesh, FIELD_SIZE, FIELD_SIZE, 32, 32);
155 gen_geosphere(pmesh, 1.0, 2);
157 if(!(particle_sdr = create_program_load("sdr/sph.v.glsl", "sdr/sph.p.glsl"))) {
162 emit_place_pos = Vec2(0, 0);
163 emit_place_pending = true;
165 assert(glGetError() == GL_NO_ERROR);
169 void GameScreen::destroy()
171 free_program(field_sdr);
174 destroy_quadmesh(&field_mesh);
178 static void simstep()
180 // move existing particles
183 // calculate the field gradient at the particle position
184 int gidx = pos_to_grid(p->pos.x, p->pos.y);
185 Vec2 grad = calc_field_grad(gidx) * GRAV_STR;
187 p->vel += grad * SIM_DT;
188 p->pos += p->vel * SIM_DT;
190 // if it moved outside of the simulation field, remove it
191 int gx = pos_to_grid_x_noclamp(p->pos.x);
192 int gy = pos_to_grid_y_noclamp(p->pos.y);
193 if(gx < 0 || gx >= GRID_SIZE || gy < 0 || gy >= GRID_SIZE) {
194 Particle *next = p->next;
202 // find the grid cell it's moving to
203 int gidx_next = pos_to_grid(p->pos.x, p->pos.y);
204 p->vis_height = 0.0f;//-grid[gidx_next] * field_scale;
206 if(gidx_next == gidx) {
211 Particle *destp = grid_part[gidx_next];
214 // another particle at the destination, merge them
215 destp->vel += p->vel;
216 destp->mass += p->mass;
217 destp->radius = MASS_TO_RADIUS(destp->mass);
221 Particle *next = p->next;
226 // destination is empty, go there
227 if(gidx != gidx_next) {
229 grid_part[gidx_next] = p;
236 // TODO destroy particles which left the simulation field
239 int num_emitters = emitters.size();
240 for(int i=0; i<num_emitters; i++) {
241 Emitter *em = emitters[i];
242 em->spawn_pending += em->rate * SIM_DT;
243 while(em->spawn_pending >= 1.0f && em->mass > 0.0f) {
244 Vec2 pvel; // XXX calc eject velocity
246 float angle = (float)rand() / (float)RAND_MAX * (M_PI * 2.0);
247 float emradius = MASS_TO_RADIUS(em->mass);
248 Vec2 ppos = em->pos + Vec2(cos(angle), sin(angle)) * emradius * 1.00001;
250 float pmass = em->chunk > em->mass ? em->mass : em->chunk;
251 spawn_particle(ppos, pvel, pmass);
254 em->spawn_pending -= 1.0f;
258 // remove dead emitters
259 std::vector<Emitter*>::iterator it = emitters.begin();
260 while(it != emitters.end()) {
263 if(em->mass <= 0.0f) {
264 printf("emitter depleted\n");
265 it = emitters.erase(it);
272 // calculate gravitational field - assume field within radius constant: m / r^2
273 // first clear the field, and then add contributions
274 memset(grid, 0, sizeof grid);
276 // contribution of emitters
277 for(int i=0; i<num_emitters; i++) {
278 Emitter *em = emitters[i];
280 calc_contrib_bounds(em->pos, em->mass, &cbox);
281 float emradius = MASS_TO_RADIUS(em->mass);
283 add_influence(em->pos, -em->mass, emradius, cbox);
286 // contribution of particles
290 calc_contrib_bounds(p->pos, p->mass, &cbox);
291 add_influence(p->pos, p->mass, p->radius, cbox);
296 assert(glGetError() == GL_NO_ERROR);
298 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GRID_SIZE, GRID_SIZE, GL_LUMINANCE,
300 assert(glGetError() == GL_NO_ERROR);
305 if(emit_place_pending) {
306 emit_place_pending = false;
307 Emitter *em = new Emitter;
308 em->pos = emit_place_pos;
311 em->chunk = 0.001 * em->mass;
314 em->spawn_pending = 0;
315 emitters.push_back(em);
318 calc_contrib_bounds(em->pos, em->mass, &cbox);
319 printf("bounds: %d,%d %dx%d\n", cbox.x, cbox.y, cbox.width, cbox.height);
323 static float interval;
324 interval += frame_dt;
325 if(interval >= SIM_DT) {
328 assert(glGetError() == GL_NO_ERROR);
331 // update projection matrix
332 proj_matrix.perspective(deg_to_rad(60.0f), win_aspect, 0.5, 5000.0);
334 // update view matrix
337 targ.x = targ_pos->x;
338 targ.z = targ_pos->y;
341 float theta = -deg_to_rad(cam_theta);
342 Vec3 camdir = Vec3(sin(theta) * cam_dist, pow(cam_dist * 0.1, 2.0) + 0.5, cos(theta) * cam_dist);
343 Vec3 campos = targ + camdir;
345 view_matrix.inv_lookat(campos, targ, Vec3(0, 1, 0));
348 void GameScreen::draw()
352 glClearColor(0.01, 0.01, 0.01, 1);
353 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
355 glMatrixMode(GL_PROJECTION);
356 glLoadMatrixf(proj_matrix[0]);
357 glMatrixMode(GL_MODELVIEW);
358 glLoadMatrixf(view_matrix[0]);
360 // draw gravitational field
362 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
364 float amb[] = {0.5, 0.5, 0.5, 1.0};
365 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
367 float amb[] = {0.01, 0.01, 0.01, 1.0};
368 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
371 bind_texture(gvis_tex, 0);
372 bind_texture(grid_tex, 1);
374 glUseProgram(field_sdr);
375 glPatchParameteri(GL_PATCH_VERTICES, 4);
376 draw_quadmesh(&field_mesh, GL_PATCHES);
379 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
383 glUseProgram(particle_sdr);
387 int gidx = pos_to_grid(p->pos.x, p->pos.y);
390 glTranslatef(p->pos.x, p->vis_height, p->pos.y);
391 glScalef(p->radius, p->radius, p->radius);
401 assert(glGetError() == GL_NO_ERROR);
404 void GameScreen::reshape(int x, int y)
410 void GameScreen::keyboard(int key, bool pressed)
419 if(tess_level < glcaps.max_tess_level) {
421 printf("tessellation level: %d\n", tess_level);
422 set_uniform_int(field_sdr, "tess_level", tess_level);
430 printf("tessellation level: %d\n", tess_level);
431 set_uniform_int(field_sdr, "tess_level", tess_level);
438 printf("field scale: %f\n", field_scale);
439 set_uniform_float(field_sdr, "field_scale", field_scale);
444 if(field_scale < 0.0f) {
447 printf("field scale: %f\n", field_scale);
448 set_uniform_float(field_sdr, "field_scale", field_scale);
452 wireframe = !wireframe;
461 static int prev_x, prev_y;
463 void GameScreen::mbutton(int bn, bool pressed, int x, int y)
469 void GameScreen::mmotion(int x, int y)
476 if(game_bnstate(2)) {
477 cam_theta += dx * 0.5;
481 void GameScreen::mwheel(int dir, int x, int y)
483 cam_dist -= dir * cam_dist * 0.05f;
484 if(cam_dist <= MIN_CAM_DIST) cam_dist = MIN_CAM_DIST;
485 if(cam_dist > MAX_CAM_DIST) cam_dist = MAX_CAM_DIST;
488 static int pos_to_grid_x_noclamp(float x)
490 return ((x / (float)FIELD_SIZE) + 0.5f) * (float)GRID_SIZE;
493 static int pos_to_grid_y_noclamp(float y)
495 return ((y / (float)FIELD_SIZE) + 0.5f) * (float)GRID_SIZE;
498 static int pos_to_grid(float x, float y)
500 int gx = pos_to_grid_x_noclamp(x);
501 int gy = pos_to_grid_y_noclamp(y);
504 if(gx >= GRID_SIZE) gx = GRID_SIZE - 1;
506 if(gy >= GRID_SIZE) gy = GRID_SIZE - 1;
508 return (gx << GRID_BITS) | gy;
511 static Vec2 grid_to_pos(int gx, int gy)
513 float x = (((float)gx / (float)GRID_SIZE) - 0.5f) * (float)FIELD_SIZE;
514 float y = (((float)gy / (float)GRID_SIZE) - 0.5f) * (float)FIELD_SIZE;
519 static void calc_contrib_bounds(const Vec2 &pos, float mass, Rect *rect)
521 int gidx = pos_to_grid(pos.x, pos.y);
522 int gx = GRID_X(gidx);
523 int gy = GRID_Y(gidx);
524 int maxrange = (int)ceil(CONTRIB_RANGE(mass));
526 int sx = gx - maxrange;
527 int sy = gy - maxrange;
528 int ex = gx + maxrange;
529 int ey = gy + maxrange;
531 if(ex > GRID_SIZE) ex = GRID_SIZE;
532 if(ey > GRID_SIZE) ey = GRID_SIZE;
534 rect->x = sx < 0 ? 0 : sx;
535 rect->y = sy < 0 ? 0 : sy;
536 rect->width = ex - sx;
537 rect->height = ey - sy;
540 static void add_influence(const Vec2 &pos, float mass, float radius, const Rect &cbox)
542 float *gptr = grid + cbox.y * GRID_SIZE + cbox.x;
543 Vec2 startpos = grid_to_pos(cbox.x, cbox.y);
545 for(int y=0; y<cbox.height; y++) {
546 for(int x=0; x<cbox.width; x++) {
547 Vec2 cellpos = Vec2(startpos.x + (float)x * GRID_DELTA, startpos.y);
549 Vec2 dir = cellpos - pos;
550 float dsq = dot(dir, dir);
551 float radsq = radius * radius;
556 gptr[x] += mass / dsq;
559 startpos.y += GRID_DELTA;
564 static Vec2 calc_field_grad(int gidx)
566 int gx = GRID_X(gidx);
567 int gy = GRID_Y(gidx);
569 int nidx = ((gx + 1 >= GRID_SIZE ? gx : gx + 1) << GRID_BITS) | gy;
570 int pidx = ((gx > 0 ? gx - 1 : gx) << GRID_BITS) | gy;
571 float dfdx = grid[nidx] - grid[pidx];
573 nidx = (gx << GRID_BITS) | (gy + 1 >= GRID_SIZE ? gy : gy + 1);
574 pidx = (gx << GRID_BITS) | (gy > 0 ? gy - 1 : gy);
575 float dfdy = grid[nidx] - grid[pidx];
577 return Vec2(dfdx, dfdy);
581 // ---- quad mesh operations ----
583 static void destroy_quadmesh(QuadMesh *m)
585 glDeleteBuffers(1, &m->vbo_v);
586 glDeleteBuffers(1, &m->vbo_uv);
587 glDeleteBuffers(1, &m->ibo);
594 static void draw_quadmesh(const QuadMesh *m, unsigned int prim)
596 glEnableClientState(GL_VERTEX_ARRAY);
597 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
599 glBindBuffer(GL_ARRAY_BUFFER, m->vbo_v);
600 glVertexPointer(3, GL_FLOAT, 0, 0);
602 glBindBuffer(GL_ARRAY_BUFFER, m->vbo_uv);
603 glTexCoordPointer(2, GL_FLOAT, 0, 0);
605 glBindBuffer(GL_ARRAY_BUFFER, 0);
607 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
608 glDrawElements(prim, m->num_idx, GL_UNSIGNED_SHORT, 0);
609 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
611 glDisableClientState(GL_VERTEX_ARRAY);
612 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
615 static void gen_quad_plane(QuadMesh *m, float width, float height, int usub, int vsub)
621 if(usub < 1) usub = 1;
622 if(vsub < 1) vsub = 1;
624 int uverts = usub + 1;
625 int vverts = vsub + 1;
626 m->num_verts = uverts * vverts;
627 m->num_quads = usub * vsub;
628 m->num_idx = m->num_quads * 4;
630 vptr = m->v = new Vec3[m->num_verts];
631 uvptr = m->uv = new Vec2[m->num_verts];
632 iptr = m->idx = new uint16_t[m->num_idx];
634 float du = 1.0f / (float)usub;
635 float dv = 1.0f / (float)vsub;
638 for(int i=0; i<uverts; i++) {
639 float x = (u - 0.5f) * width;
641 for(int j=0; j<vverts; j++) {
642 float y = (v - 0.5f) * height;
644 *vptr++ = Vec3(x, 0, y);
645 *uvptr++ = Vec2(u, v);
647 if(i < usub && j < vsub) {
648 int idx = i * vverts + j;
652 *iptr++ = idx + vverts + 1;
653 *iptr++ = idx + vverts;
661 glGenBuffers(1, &m->vbo_v);
662 glBindBuffer(GL_ARRAY_BUFFER, m->vbo_v);
663 glBufferData(GL_ARRAY_BUFFER, m->num_verts * 3 * sizeof(float), m->v, GL_STATIC_DRAW);
665 glGenBuffers(1, &m->vbo_uv);
666 glBindBuffer(GL_ARRAY_BUFFER, m->vbo_uv);
667 glBufferData(GL_ARRAY_BUFFER, m->num_verts * 2 * sizeof(float), m->uv, GL_STATIC_DRAW);
669 glGenBuffers(1, &m->ibo);
670 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
671 glBufferData(GL_ELEMENT_ARRAY_BUFFER, m->num_idx * sizeof(uint16_t), m->idx, GL_STATIC_DRAW);
673 glBindBuffer(GL_ARRAY_BUFFER, 0);
674 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
677 static void spawn_particle(const Vec2 &pos, const Vec2 &vel, float mass)
679 int gidx = pos_to_grid(pos.x, pos.y);
681 if(grid_part[gidx]) {
682 // merge with existing
683 Particle *p = grid_part[gidx];
686 p->radius = MASS_TO_RADIUS(p->mass);
689 Particle *p = alloc_particle();
693 p->radius = MASS_TO_RADIUS(mass);
700 static void add_particle(Particle *p)
702 if(plist) plist->prev = p;
709 static void remove_particle(Particle *p)
711 assert(plist->prev == 0);
714 assert(p->prev == 0);
718 p->prev->next = p->next;
721 p->next->prev = p->prev;
723 p->prev = p->next = 0;
726 // particle allocator
727 #define MAX_PFREE_SIZE 256
728 static Particle *pfree_list;
729 static int pfree_size;
731 static Particle *alloc_particle()
734 Particle *p = pfree_list;
735 pfree_list = pfree_list->next;
743 void free_particle(Particle *p)
745 if(pfree_size < MAX_PFREE_SIZE) {
746 p->next = pfree_list;