3 #include <gmath/gmath.h>
13 * - whistle hhgg music
14 * - colliding particles merge
15 * - select objects and center camera on them
16 * - tesselate only where necessary
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 static int pos_to_grid(float x, float y);
70 static Vec2 grid_to_pos(int gx, int gy);
72 static void calc_contrib_bounds(const Vec2 &pos, float mass, Rect *rect);
73 static void add_influence(const Vec2 &pos, float mass, float radius, const Rect &cbox);
75 static void destroy_quadmesh(QuadMesh *m);
76 static void draw_quadmesh(const QuadMesh *m, unsigned int prim = GL_QUADS);
77 static void gen_quad_plane(QuadMesh *mesh, float width, float height, int usub, int vsub);
79 static void spawn_particle(const Vec2 &pos, const Vec2 &vel, float mass);
80 static void add_particle(Particle *p, int grid_idx);
81 static void remove_particle(Particle *p, int grid_idx);
82 static Particle *alloc_particle();
83 void free_particle(Particle *p);
85 static float grid[GRID_SIZE * GRID_SIZE];
86 static Particle *grid_part[GRID_SIZE * GRID_SIZE];
87 static Texture *grid_tex;
89 #define NUM_PARTICLE_LISTS 16
90 static Particle *plist[NUM_PARTICLE_LISTS];
91 #define GRID_IDX_PLIST(idx) ((idx) * NUM_PARTICLE_LISTS / (GRID_SIZE * GRID_SIZE))
93 static std::vector<Emitter*> emitters;
95 static Texture *gvis_tex; // texture tile for visualizing a grid
96 static unsigned int field_sdr;
97 static int tess_level = 32;
98 static float field_scale = 16.0f;
100 static QuadMesh field_mesh;
103 static unsigned int particle_sdr;
105 static float cam_theta;
106 static float cam_dist = 100.0f;
107 static Vec2 *targ_pos;
108 static Mat4 view_matrix, proj_matrix;
110 // emitter placement data (filled by event handlers, completed in update)
111 static bool emit_place_pending;
112 static Vec2 emit_place_pos;
115 bool GameScreen::init()
117 grid_tex = new Texture;
118 grid_tex->create(GRID_SIZE, GRID_SIZE, TEX_2D, GL_LUMINANCE32F_ARB);
119 grid_tex->set_anisotropy(glcaps.max_aniso);
121 gvis_tex = new Texture;
122 if(!gvis_tex->load("data/purple_grid.png")) {
125 gvis_tex->set_anisotropy(glcaps.max_aniso);
127 unsigned int vsdr, tcsdr, tesdr, psdr;
129 if(!(vsdr = load_vertex_shader("sdr/field.v.glsl")) ||
130 !(tcsdr = load_tessctl_shader("sdr/field.tc.glsl")) ||
131 !(tesdr = load_tesseval_shader("sdr/field.te.glsl")) ||
132 !(psdr = load_pixel_shader("sdr/field.p.glsl"))) {
136 if(!(field_sdr = create_program_link(vsdr, tcsdr, tesdr, psdr, 0))) {
139 set_uniform_int(field_sdr, "gvis_tex", 0);
140 set_uniform_int(field_sdr, "field_tex", 1);
141 set_uniform_float(field_sdr, "gvis_scale", FIELD_SIZE / 32.0f);
142 set_uniform_int(field_sdr, "tess_level", tess_level);
143 set_uniform_float(field_sdr, "field_scale", field_scale);
145 gen_quad_plane(&field_mesh, FIELD_SIZE, FIELD_SIZE, 32, 32);
148 gen_geosphere(pmesh, 1.0, 2);
150 if(!(particle_sdr = create_program_load("sdr/sph.v.glsl", "sdr/sph.p.glsl"))) {
155 emit_place_pos = Vec2(0, 0);
156 emit_place_pending = true;
158 assert(glGetError() == GL_NO_ERROR);
162 void GameScreen::destroy()
164 free_program(field_sdr);
167 destroy_quadmesh(&field_mesh);
171 static void simstep()
174 int num_emitters = emitters.size();
175 for(int i=0; i<num_emitters; i++) {
176 Emitter *em = emitters[i];
177 em->spawn_pending += em->rate * SIM_DT;
178 while(em->spawn_pending >= 1.0f && em->mass > 0.0f) {
179 Vec2 pvel; // XXX calc eject velocity
181 float angle = (float)rand() / (float)RAND_MAX * (M_PI * 2.0);
182 float emradius = MASS_TO_RADIUS(em->mass);
183 Vec2 ppos = em->pos + Vec2(cos(angle), sin(angle)) * emradius * 1.00001;
185 float pmass = em->chunk > em->mass ? em->mass : em->chunk;
186 spawn_particle(ppos, pvel, pmass);
189 em->spawn_pending -= 1.0f;
193 // remove dead emitters
194 std::vector<Emitter*>::iterator it = emitters.begin();
195 while(it != emitters.end()) {
198 if(em->mass <= 0.0f) {
199 printf("emitter depleted\n");
200 it = emitters.erase(it);
207 // calculate gravitational field - assume field within radius constant: m / r^2
209 // first clear the field, and then add contributions
210 memset(grid, 0, sizeof grid);
212 // contribution of emitters
213 for(int i=0; i<num_emitters; i++) {
214 Emitter *em = emitters[i];
216 calc_contrib_bounds(em->pos, em->mass, &cbox);
217 float emradius = MASS_TO_RADIUS(em->mass);
219 add_influence(em->pos, -em->mass, emradius, cbox);
222 // contribution of particles
223 for(int i=0; i<NUM_PARTICLE_LISTS; i++) {
224 Particle *p = plist[i];
227 calc_contrib_bounds(p->pos, p->mass, &cbox);
228 add_influence(p->pos, p->mass, p->radius, cbox);
234 assert(glGetError() == GL_NO_ERROR);
236 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GRID_SIZE, GRID_SIZE, GL_LUMINANCE,
238 assert(glGetError() == GL_NO_ERROR);
243 if(emit_place_pending) {
244 emit_place_pending = false;
245 Emitter *em = new Emitter;
246 em->pos = emit_place_pos;
249 em->chunk = 0.05 * em->mass;
252 em->spawn_pending = 0;
253 emitters.push_back(em);
256 calc_contrib_bounds(em->pos, em->mass, &cbox);
257 printf("bounds: %d,%d %dx%d\n", cbox.x, cbox.y, cbox.width, cbox.height);
261 static float interval;
262 interval += frame_dt;
263 if(interval >= SIM_DT) {
266 assert(glGetError() == GL_NO_ERROR);
269 // update projection matrix
270 proj_matrix.perspective(deg_to_rad(60.0f), win_aspect, 0.5, 5000.0);
272 // update view matrix
275 targ.x = targ_pos->x;
276 targ.z = targ_pos->y;
279 float theta = -deg_to_rad(cam_theta);
280 Vec3 camdir = Vec3(sin(theta) * cam_dist, pow(cam_dist * 0.1, 2.0) + 0.5, cos(theta) * cam_dist);
281 Vec3 campos = targ + camdir;
283 view_matrix.inv_lookat(campos, targ, Vec3(0, 1, 0));
286 void GameScreen::draw()
290 glClearColor(0.01, 0.01, 0.01, 1);
291 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
293 glMatrixMode(GL_PROJECTION);
294 glLoadMatrixf(proj_matrix[0]);
295 glMatrixMode(GL_MODELVIEW);
296 glLoadMatrixf(view_matrix[0]);
298 glPushAttrib(GL_ENABLE_BIT);
299 glDisable(GL_CULL_FACE);
301 // draw gravitational field
302 glEnable(GL_TEXTURE_2D);
303 bind_texture(gvis_tex, 0);
304 bind_texture(grid_tex, 1);
306 glUseProgram(field_sdr);
307 glPatchParameteri(GL_PATCH_VERTICES, 4);
308 draw_quadmesh(&field_mesh, GL_PATCHES);
311 glUseProgram(particle_sdr);
313 for(int i=0; i<NUM_PARTICLE_LISTS; i++) {
314 Particle *p = plist[i];
316 int gidx = pos_to_grid(p->pos.x, p->pos.y);
319 glTranslatef(p->pos.x, -grid[gidx] * field_scale, p->pos.y);
320 glScalef(p->radius, p->radius, p->radius);
333 assert(glGetError() == GL_NO_ERROR);
336 void GameScreen::reshape(int x, int y)
342 void GameScreen::keyboard(int key, bool pressed)
351 if(tess_level < glcaps.max_tess_level) {
353 printf("tessellation level: %d\n", tess_level);
354 set_uniform_int(field_sdr, "tess_level", tess_level);
362 printf("tessellation level: %d\n", tess_level);
363 set_uniform_int(field_sdr, "tess_level", tess_level);
370 printf("field scale: %f\n", field_scale);
371 set_uniform_float(field_sdr, "field_scale", field_scale);
376 if(field_scale < 0.0f) {
379 printf("field scale: %f\n", field_scale);
380 set_uniform_float(field_sdr, "field_scale", field_scale);
389 static int prev_x, prev_y;
391 void GameScreen::mbutton(int bn, bool pressed, int x, int y)
397 void GameScreen::mmotion(int x, int y)
404 if(game_bnstate(2)) {
405 cam_theta += dx * 0.5;
409 void GameScreen::mwheel(int dir, int x, int y)
411 cam_dist -= dir * cam_dist * 0.05f;
412 if(cam_dist <= MIN_CAM_DIST) cam_dist = MIN_CAM_DIST;
413 if(cam_dist > MAX_CAM_DIST) cam_dist = MAX_CAM_DIST;
417 static int pos_to_grid(float x, float y)
419 int gx = ((x / (float)FIELD_SIZE) + 0.5f) * (float)GRID_SIZE;
420 int gy = ((y / (float)FIELD_SIZE) + 0.5f) * (float)GRID_SIZE;
423 if(gx >= GRID_SIZE) gx = GRID_SIZE - 1;
425 if(gy >= GRID_SIZE) gy = GRID_SIZE - 1;
427 return (gx << GRID_BITS) | gy;
430 static Vec2 grid_to_pos(int gx, int gy)
432 float x = (((float)gx / (float)GRID_SIZE) - 0.5f) * (float)FIELD_SIZE;
433 float y = (((float)gy / (float)GRID_SIZE) - 0.5f) * (float)FIELD_SIZE;
438 static void calc_contrib_bounds(const Vec2 &pos, float mass, Rect *rect)
440 int gidx = pos_to_grid(pos.x, pos.y);
441 int gx = GRID_X(gidx);
442 int gy = GRID_Y(gidx);
443 int maxrange = (int)ceil(CONTRIB_RANGE(mass));
445 int sx = gx - maxrange;
446 int sy = gy - maxrange;
447 int ex = gx + maxrange;
448 int ey = gy + maxrange;
450 if(ex > GRID_SIZE) ex = GRID_SIZE;
451 if(ey > GRID_SIZE) ey = GRID_SIZE;
453 rect->x = sx < 0 ? 0 : sx;
454 rect->y = sy < 0 ? 0 : sy;
455 rect->width = ex - sx;
456 rect->height = ey - sy;
459 static void add_influence(const Vec2 &pos, float mass, float radius, const Rect &cbox)
461 float *gptr = grid + cbox.y * GRID_SIZE + cbox.x;
462 Vec2 startpos = grid_to_pos(cbox.x, cbox.y);
464 for(int y=0; y<cbox.height; y++) {
465 for(int x=0; x<cbox.width; x++) {
466 Vec2 cellpos = Vec2(startpos.x + (float)x * GRID_DELTA, startpos.y);
468 Vec2 dir = cellpos - pos;
469 float dsq = dot(dir, dir);
470 float radsq = radius * radius;
475 gptr[x] += mass / dsq;
478 startpos.y += GRID_DELTA;
484 static void destroy_quadmesh(QuadMesh *m)
486 glDeleteBuffers(1, &m->vbo_v);
487 glDeleteBuffers(1, &m->vbo_uv);
488 glDeleteBuffers(1, &m->ibo);
495 static void draw_quadmesh(const QuadMesh *m, unsigned int prim)
497 glEnableClientState(GL_VERTEX_ARRAY);
498 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
500 glBindBuffer(GL_ARRAY_BUFFER, m->vbo_v);
501 glVertexPointer(3, GL_FLOAT, 0, 0);
503 glBindBuffer(GL_ARRAY_BUFFER, m->vbo_uv);
504 glTexCoordPointer(2, GL_FLOAT, 0, 0);
506 glBindBuffer(GL_ARRAY_BUFFER, 0);
508 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
509 glDrawElements(prim, m->num_idx, GL_UNSIGNED_SHORT, 0);
510 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
512 glDisableClientState(GL_VERTEX_ARRAY);
513 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
516 static void gen_quad_plane(QuadMesh *m, float width, float height, int usub, int vsub)
522 if(usub < 1) usub = 1;
523 if(vsub < 1) vsub = 1;
525 int uverts = usub + 1;
526 int vverts = vsub + 1;
527 m->num_verts = uverts * vverts;
528 m->num_quads = usub * vsub;
529 m->num_idx = m->num_quads * 4;
531 vptr = m->v = new Vec3[m->num_verts];
532 uvptr = m->uv = new Vec2[m->num_verts];
533 iptr = m->idx = new uint16_t[m->num_idx];
535 float du = 1.0f / (float)usub;
536 float dv = 1.0f / (float)vsub;
539 for(int i=0; i<uverts; i++) {
540 float x = (u - 0.5f) * width;
542 for(int j=0; j<vverts; j++) {
543 float y = (v - 0.5f) * height;
545 *vptr++ = Vec3(x, 0, y);
546 *uvptr++ = Vec2(u, v);
548 if(i < usub && j < vsub) {
549 int idx = i * vverts + j;
552 *iptr++ = idx + vverts;
553 *iptr++ = idx + vverts + 1;
562 glGenBuffers(1, &m->vbo_v);
563 glBindBuffer(GL_ARRAY_BUFFER, m->vbo_v);
564 glBufferData(GL_ARRAY_BUFFER, m->num_verts * 3 * sizeof(float), m->v, GL_STATIC_DRAW);
566 glGenBuffers(1, &m->vbo_uv);
567 glBindBuffer(GL_ARRAY_BUFFER, m->vbo_uv);
568 glBufferData(GL_ARRAY_BUFFER, m->num_verts * 2 * sizeof(float), m->uv, GL_STATIC_DRAW);
570 glGenBuffers(1, &m->ibo);
571 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
572 glBufferData(GL_ELEMENT_ARRAY_BUFFER, m->num_idx * sizeof(uint16_t), m->idx, GL_STATIC_DRAW);
574 glBindBuffer(GL_ARRAY_BUFFER, 0);
575 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
578 static void spawn_particle(const Vec2 &pos, const Vec2 &vel, float mass)
580 int gidx = pos_to_grid(pos.x, pos.y);
582 if(grid_part[gidx]) {
583 // merge with existing
584 Particle *p = grid_part[gidx];
587 p->radius = MASS_TO_RADIUS(p->mass);
590 Particle *p = alloc_particle();
594 p->radius = MASS_TO_RADIUS(mass);
597 add_particle(p, gidx);
601 static void add_particle(Particle *p, int grid_idx)
603 int listidx = GRID_IDX_PLIST(grid_idx);
604 p->next = plist[listidx];
609 static void remove_particle(Particle *p, int grid_idx)
611 Particle *prev = p->prev;
612 Particle *next = p->next;
614 p->prev = p->next = 0;
615 if(next) next->prev = prev;
620 int listidx = GRID_IDX_PLIST(grid_idx);
621 plist[listidx] = next;
625 // particle allocator
626 #define MAX_PFREE_SIZE 256
627 static Particle *pfree_list;
628 static int pfree_size;
630 static Particle *alloc_particle()
633 Particle *p = pfree_list;
634 pfree_list = pfree_list->next;
642 void free_particle(Particle *p)
644 if(pfree_size < MAX_PFREE_SIZE) {
645 p->next = pfree_list;