Browse Source

clamp camera rotation to +z and clamp vAngle

master
cinnaboot 8 years ago
parent
commit
843e3591e3
  1. 1
      .gdb_history
  2. 5
      src/hexgame.cpp
  3. 16
      src/hexgame.h
  4. 37
      src/renderer.h

1
.gdb_history

@ -0,0 +1 @@
q

5
src/hexgame.cpp

@ -354,6 +354,8 @@ processSDLEvents()
case SDLK_f: g->is_moveright = true; break;
case SDLK_SPACE: g->is_moveup = true; break;
case SDLK_c: g->is_movedown = true; break;
case SDLK_r: g->is_rotateCW = true; break;
case SDLK_w: g->is_rotateCCW = true; break;
}
break;
case SDL_KEYUP:
@ -365,6 +367,8 @@ processSDLEvents()
case SDLK_f: g->is_moveright = false; break;
case SDLK_SPACE: g->is_moveup = false; break;
case SDLK_c: g->is_movedown = false; break;
case SDLK_r: g->is_rotateCW = false; break;
case SDLK_w: g->is_rotateCCW = false; break;
}
break;
case SDL_MOUSEBUTTONDOWN:
@ -514,6 +518,7 @@ int main(int argc, char* argv[])
render_state* r = g_render_state;
moveCamera(g->is_moveup, g->is_moveleft, g->is_movedown, g->is_moveright,
g->is_moveforward, g->is_movebackward);
rollCamera(g->is_rotateCW, g->is_rotateCCW);
renderFrame(g->hex_array);
if (r->is_debug_draw && g->draw_mode == CONE_FILL)
renderDebug(g_polygon_select_vertices);

16
src/hexgame.h

@ -85,6 +85,14 @@ struct render_state
struct game_state
{
std::vector<hex_info> *hex_array;
hex_info *start_hex;
hex_info *current_hex;
vector<hex_info> selected_hexes;
HexDrawMode draw_mode;
const Layout hex_layout;
// movement controls
bool is_selecting = false;
bool is_camera_rotate = false;
bool is_moveforward = false;
@ -93,12 +101,8 @@ struct game_state
bool is_movedown = false;
bool is_moveleft = false;
bool is_moveright = false;
std::vector<hex_info> *hex_array;
hex_info *start_hex;
hex_info *current_hex;
vector<hex_info> selected_hexes;
HexDrawMode draw_mode;
const Layout hex_layout;
bool is_rotateCW = false;
bool is_rotateCCW = false;
game_state(Layout &l) : hex_layout(l) {}
};

37
src/renderer.h

@ -124,6 +124,7 @@ 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, bool forward, bool backward);
void rollCamera(bool CW, bool CCW);
void rotateCamera(int32 xrel, int32 yrel);
void renderFrame(const std::vector<hex_info> *hexes);
void renderDebug(std::vector<Point> &vertices);
@ -273,11 +274,10 @@ initMatrices(projection_type p)
{
if (p == PERSPECTIVE)
{
g_scene_matrices.projection = glm::perspective(
g_scene_matrices.projection = glm::infinitePerspective(
glm::radians(60.f), // FoV
16.f / 9.f, // ascpect ratio
0.1f, // near clip plane
1000.0f // far clip plane
0.1f // near clip plane
);
g_camera.position = glm::vec3(640,0,100);
@ -400,6 +400,7 @@ moveCamera(bool up, bool left, bool down, bool right, bool forward, bool backwar
glm::vec3 u = g_camera.up;
glm::vec3 old = g_camera.position;
glm::vec3 &p = g_camera.position;
if (forward) p += f * d;
if (backward) p -= f * d;
if (up) p += u * d;
@ -428,16 +429,46 @@ rotateCamera(int32 xrel, int32 yrel)
float &v = c.vAngle;
h += 0.005f * xrel;
v -= 0.005f * yrel;
// clamp vAngle to + or - 85 degrees to prevent gimbal lock
float a = glm::radians(85.f);
if (v < (-1 * a)) v = (-1 * a);
if (v > a) v = a;
c.forward = glm::vec3(
glm::cos(v) * glm::sin(h),
glm::cos(v) * glm::cos(h),
glm::sin(v)
);
c.up = glm::vec3(0,0,1);
c.left = glm::cross(c.forward, c.up);
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;
}
// NOTE: don't need this yet
void
rollCamera(bool CW, bool CCW)
{
#if 0
if ((!CW && !CCW) || (CW && CCW))
return;
float a = 0.005f;
if (CW) a *= 1;
if (CCW) a *= -1;
camera &c = g_camera;
glm::mat4 m = glm::rotate(glm::mat4(1.f), a, c.forward);
glm::vec4 v(c.up.x, c.up.y, c.up.z, 0);
v = v * m;
g_camera.up = glm::vec3(v.x, v.y, v.z);
g_scene_matrices.view *= m;
g_scene_matrices.MVP = g_scene_matrices.projection * g_scene_matrices.view * g_scene_matrices.model;
#endif
}
void
fillTriangleBufferFromHex(GLfloat buf[], int idx, const hex_info &hex)
{

Loading…
Cancel
Save