Advertisement
Masterchoc

Untitled

Dec 24th, 2017
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include "camera.h"
  2. #include <iostream>
  3. #include <glm/glm.hpp>
  4. #include <glm/gtx/transform.hpp>
  5. #include <SDL2\SDL.h>
  6.  
  7. Camera::Camera(const glm::vec3& position, float fov, float aspect, float near, float far)
  8. {
  9.     m_position = position;
  10.     m_up = glm::vec3(0, 1, 0);
  11.     m_direction = glm::vec3(0, 0, 1);
  12.     m_projMatrix = glm::perspective(fov, aspect, near, far);
  13.     m_moveSpeed = 0.01f;
  14.     //SDL_ShowCursor(SDL_DISABLE);
  15. }
  16.  
  17. void Camera::update(float delta)
  18. {
  19.  
  20. }
  21.  
  22. void Camera::onMouseMove(const glm::vec2& mouse)
  23. {
  24.     glm::vec2 delta = mouse - m_oldMouse;
  25.  
  26.     m_rotation.x = delta.x * 0.001f;
  27.     m_rotation.y = delta.y * 0.001f;
  28.  
  29.     m_direction = glm::mat3(glm::rotate(m_rotation.x, m_up)) * m_direction;
  30.     m_direction = glm::mat3(glm::rotate(m_rotation.y, glm::cross(m_direction, m_up))) * m_direction;
  31.  
  32.     m_oldMouse = mouse;
  33. }
  34.  
  35. void Camera::onKeyboard(const SDL_KeyboardEvent& event)
  36. {
  37.     if (event.keysym.scancode == Keys::FORWARD) {
  38.         m_position += m_moveSpeed * m_direction;
  39.     }
  40.     if (event.keysym.scancode == Keys::BACKWARD) {
  41.         m_position += -m_moveSpeed * m_direction;
  42.     }
  43.     if (event.keysym.scancode == Keys::LEFT) {
  44.         m_position += -m_moveSpeed * glm::cross(m_direction, m_up);
  45.     }
  46.     if (event.keysym.scancode == Keys::RIGHT) {
  47.         m_position += m_moveSpeed * glm::cross(m_direction, m_up);
  48.     }
  49.     if (event.keysym.scancode == Keys::UP) {
  50.         m_position += m_moveSpeed * m_up;
  51.     }
  52.     if (event.keysym.scancode == Keys::DOWN) {
  53.         m_position += -m_moveSpeed * m_up;
  54.     }
  55. }
  56.  
  57. Camera::~Camera()
  58. {
  59.     SDL_ShowCursor(SDL_ENABLE);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement