Browse Source

add up and down camera controls

master
cinnaboot 8 years ago
parent
commit
a6cb9a3e1b
  1. 15
      src/hexgame.cpp
  2. 4
      src/hexgame.h
  3. 28
      src/renderer.h

15
src/hexgame.cpp

@ -348,19 +348,23 @@ processSDLEvents()
switch (e.key.keysym.sym)
{
case SDLK_ESCAPE: run = false; break;
case SDLK_e: g->is_moveup = true; break;
case SDLK_e: g->is_moveforward = true; break;
case SDLK_s: g->is_moveleft = true; break;
case SDLK_d: g->is_movedown = true; break;
case SDLK_d: g->is_movebackward = true; break;
case SDLK_f: g->is_moveright = true; break;
case SDLK_SPACE: g->is_moveup = true; break;
case SDLK_c: g->is_movedown = true; break;
}
break;
case SDL_KEYUP:
switch (e.key.keysym.sym)
{
case SDLK_e: g->is_moveup = false; break;
case SDLK_e: g->is_moveforward = false; break;
case SDLK_s: g->is_moveleft = false; break;
case SDLK_d: g->is_movedown = false; break;
case SDLK_d: g->is_movebackward = false; break;
case SDLK_f: g->is_moveright = false; break;
case SDLK_SPACE: g->is_moveup = false; break;
case SDLK_c: g->is_movedown = false; break;
}
break;
case SDL_MOUSEBUTTONDOWN:
@ -508,7 +512,8 @@ int main(int argc, char* argv[])
SDL_Delay(16); // ~60hz
game_state* g = g_game_state;
render_state* r = g_render_state;
moveCamera(g->is_moveup, g->is_moveleft, g->is_movedown, g->is_moveright);
moveCamera(g->is_moveup, g->is_moveleft, g->is_movedown, g->is_moveright,
g->is_moveforward, g->is_movebackward);
renderFrame(g->hex_array);
if (r->is_debug_draw && g->draw_mode == CONE_FILL)
renderDebug(g_polygon_select_vertices);

4
src/hexgame.h

@ -87,9 +87,11 @@ struct game_state
{
bool is_selecting = false;
bool is_camera_rotate = false;
bool is_moveforward = false;
bool is_movebackward = false;
bool is_moveup = false;
bool is_moveleft = false;
bool is_movedown = false;
bool is_moveleft = false;
bool is_moveright = false;
std::vector<hex_info> *hex_array;
hex_info *start_hex;

28
src/renderer.h

@ -123,7 +123,7 @@ camera g_camera;
bool initRenderer(SDL_Handles &handles, v2i vpDims);
bool addTexture(SDL_Handles &handles, std::string path);
bool createScene(std::vector<hex_info>* hexes);
void moveCamera(bool up, bool left, bool down, bool right);
void moveCamera(bool up, bool left, bool down, bool right, bool forward, bool backward);
void rotateCamera(int32 xrel, int32 yrel);
void renderFrame(const std::vector<hex_info> *hexes);
void renderDebug(std::vector<Point> &vertices);
@ -274,7 +274,7 @@ initMatrices(projection_type p)
if (p == PERSPECTIVE)
{
g_scene_matrices.projection = glm::perspective(
glm::radians(45.f), // FoV
glm::radians(60.f), // FoV
16.f / 9.f, // ascpect ratio
0.1f, // near clip plane
1000.0f // far clip plane
@ -289,6 +289,7 @@ initMatrices(projection_type p)
g_camera.vAngle = glm::atan((t.z - p.z) / (t.y - p.y));
//////
// TODO: add call to rotate camera here to remove duplicate code
camera &c = g_camera;
float &h = c.hAngle;
float &v = c.vAngle;
@ -385,20 +386,24 @@ createScene(std::vector<hex_info>* hexes)
}
void
moveCamera(bool up, bool left, bool down, bool right)
moveCamera(bool up, bool left, bool down, bool right, bool forward, bool backward)
{
if (!up && !left && !down && !right)
if (!up && !left && !down && !right && !forward && !backward)
return;
glm::mat4 &m = g_scene_matrices.view;
// TODO: fix vector addition bug doubling move speed vector magnitude when
// moving in 2 directions
float d = 5.f; // units per frame
glm::vec3 f = g_camera.forward;
glm::vec3 u = g_camera.up;
glm::vec3 old = g_camera.position;
glm::vec3 &p = g_camera.position;
if (up) p += (f * d);
if (down) p -= (f * d);
if (forward) p += f * d;
if (backward) p -= f * d;
if (up) p += u * d;
if (down) p -= u * d;
if (left)
{
glm::vec3 l = glm::cross(f, u);
@ -411,8 +416,8 @@ moveCamera(bool up, bool left, bool down, bool right)
}
glm::vec3 diff = old - p;
m = glm::translate(m, diff);
g_scene_matrices.MVP = g_scene_matrices.projection * m * g_scene_matrices.model;
g_scene_matrices.view = glm::translate(g_scene_matrices.view, diff);
g_scene_matrices.MVP = g_scene_matrices.projection * g_scene_matrices.view * g_scene_matrices.model;
}
void
@ -429,9 +434,8 @@ rotateCamera(int32 xrel, int32 yrel)
glm::sin(v)
);
glm::mat4 &m = g_scene_matrices.view;
m = glm::lookAt(c.position, c.position + c.forward, c.up);
g_scene_matrices.MVP = g_scene_matrices.projection * m * g_scene_matrices.model;
g_scene_matrices.view = glm::lookAt(c.position, c.position + c.forward, c.up);
g_scene_matrices.MVP = g_scene_matrices.projection * g_scene_matrices.view * g_scene_matrices.model;
}
void

Loading…
Cancel
Save