2 #include <gmath/gmath.h>
10 * - colliding particles merge
11 * - select objects and center camera on them
30 #define GRID_SIZE 4096
32 #define GRID_X(idx) (((idx) >> GRID_BITS) & (GRID_SIZE - 1))
33 #define GRID_Y(idx) ((idx) & (GRID_SIZE - 1))
35 #define FIELD_SIZE 2048
36 #define MIN_CAM_DIST 1.0f
37 #define MAX_CAM_DIST 350.0f
39 static int pos_to_grid(float x, float y);
41 static float grid[GRID_SIZE * GRID_SIZE];
42 static Particle grid_part[GRID_SIZE * GRID_SIZE];
44 static std::vector<Emitter> emitters;
46 static Texture *grid_tex;
48 static float cam_theta;
49 static float cam_dist = 100.0f;
50 static Vec2 *targ_pos;
51 static Mat4 view_matrix, proj_matrix;
54 bool GameScreen::init()
56 grid_tex = new Texture;
57 if(!grid_tex->load("data/purple_grid.png")) {
60 grid_tex->set_anisotropy(glcaps.max_aniso);
65 void GameScreen::destroy()
76 static float interval;
79 if(interval >= SIM_DT) {
84 // update projection matrix
85 proj_matrix.perspective(deg_to_rad(60.0f), win_aspect, 0.5, 5000.0);
94 float theta = -deg_to_rad(cam_theta);
95 Vec3 camdir = Vec3(sin(theta) * cam_dist, pow(cam_dist * 0.1, 2.0) + 0.5, cos(theta) * cam_dist);
96 Vec3 campos = targ + camdir;
98 view_matrix.inv_lookat(campos, targ, Vec3(0, 1, 0));
101 void GameScreen::draw()
105 glClearColor(0.01, 0.01, 0.01, 1);
106 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
108 glMatrixMode(GL_PROJECTION);
109 glLoadMatrixf(proj_matrix[0]);
110 glMatrixMode(GL_MODELVIEW);
111 glLoadMatrixf(view_matrix[0]);
113 glPushAttrib(GL_ENABLE_BIT);
114 glDisable(GL_LIGHTING);
115 glDisable(GL_CULL_FACE);
117 glEnable(GL_TEXTURE_2D);
118 bind_texture(grid_tex);
120 float maxu = FIELD_SIZE / 32.0f;
121 float maxv = FIELD_SIZE / 32.0f;
122 float hsz = FIELD_SIZE * 0.5f;
127 glVertex3f(-hsz, 0, -hsz);
128 glTexCoord2f(maxu, 0);
129 glVertex3f(hsz, 0, -hsz);
130 glTexCoord2f(maxu, maxv);
131 glVertex3f(hsz, 0, hsz);
132 glTexCoord2f(0, maxv);
133 glVertex3f(-hsz, 0, hsz);
139 void GameScreen::reshape(int x, int y)
145 void GameScreen::keyboard(int key, bool pressed)
159 static int prev_x, prev_y;
161 void GameScreen::mbutton(int bn, bool pressed, int x, int y)
167 void GameScreen::mmotion(int x, int y)
174 if(game_bnstate(2)) {
175 cam_theta += dx * 0.5;
179 void GameScreen::mwheel(int dir, int x, int y)
181 cam_dist -= dir * cam_dist * 0.05f;
182 if(cam_dist <= MIN_CAM_DIST) cam_dist = MIN_CAM_DIST;
183 if(cam_dist > MAX_CAM_DIST) cam_dist = MAX_CAM_DIST;