Masterchoc

Untitled

Dec 26th, 2017
99
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "camera.h"
  2. #include <iostream>
  3. #include <glm/glm.hpp>
  4. #include <glm/gtx/transform.hpp>
  5. #include <SDL2\SDL.h>
  6. #include <string>
  7.  
  8. Camera::Camera(const glm::vec3& position, float fov, float aspect, float near, float far)
  9. {
  10.     m_position = position;
  11.     m_up = glm::vec3(0, 1, 0);
  12.     m_direction = glm::vec3(0, 0, 1);
  13.     m_friction = glm::vec3(0.85f, 0.85f, 0.85f);
  14.     m_projMatrix = glm::perspective(fov, aspect, near, far);
  15.     m_moveSpeed = 5.0f;
  16.     m_sensitivity = 50.0f;
  17.  
  18.     m_keys["FORWARD"] = 26;
  19.     m_keys["BACKWARD"] = 22;
  20.     m_keys["LEFT"] = 4;
  21.     m_keys["RIGHT"] = 7;
  22.     m_keys["UP"] = 44;
  23.     m_keys["DOWN"] = 225;
  24.  
  25.     m_keys_states["FORWARD"] = false;
  26.     m_keys_states["BACKWARD"] = false;
  27.     m_keys_states["LEFT"] = false;
  28.     m_keys_states["RIGHT"] = false;
  29.     m_keys_states["UP"] = false;
  30.     m_keys_states["DOWN"] = false;
  31.  
  32.     SDL_ShowCursor(SDL_DISABLE);
  33. }
  34.  
  35. void Camera::update(float delta)
  36. {
  37.     m_delta = delta;
  38.  
  39.     if (m_keys_states["FORWARD"])
  40.         m_position += m_moveSpeed * m_delta * m_direction;
  41.     if (m_keys_states["BACKWARD"])
  42.         m_position += -m_moveSpeed * m_delta * m_direction;
  43.     if (m_keys_states["LEFT"])
  44.         m_position += -m_moveSpeed * m_delta * glm::cross(m_direction, m_up);
  45.     if (m_keys_states["RIGHT"])
  46.         m_position += m_moveSpeed * m_delta * glm::cross(m_direction, m_up);
  47.     if (m_keys_states["UP"])
  48.         m_position += m_moveSpeed * m_delta * m_up;
  49.     if (m_keys_states["DOWN"])
  50.         m_position += -m_moveSpeed * m_delta * m_up;
  51. }
  52.  
  53. void Camera::onMouseMove(const glm::vec2& mouse)
  54. {
  55.     m_mouse = mouse;
  56.  
  57.     m_rotation.x = -glm::radians(m_mouse.x) * m_delta * m_sensitivity;
  58.     m_rotation.y = -glm::radians(m_mouse.y) * m_delta * m_sensitivity;
  59.  
  60.     m_direction = glm::mat3(glm::rotate(m_rotation.x, m_up)) * m_direction;
  61.     m_direction = glm::mat3(glm::rotate(m_rotation.y, glm::cross(m_direction, m_up))) * m_direction;
  62.  
  63.     m_oldMouse = mouse;
  64. }
  65.  
  66.  
  67. void Camera::onKeyboard(const SDL_KeyboardEvent& event)
  68. {
  69.     for (const auto &key : m_keys) {
  70.         if (event.keysym.scancode == key.second) {
  71.             m_keys_states[key.first] = (event.type == SDL_KEYDOWN);
  72.         }
  73.     }
  74. }
  75.  
  76. Camera::~Camera()
  77. {
  78.     SDL_ShowCursor(SDL_ENABLE);
  79. }
RAW Paste Data