Advertisement
Guest User

Untitled

a guest
Oct 14th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <glm.hpp>
  4. #include <gtc/matrix_transform.hpp>
  5. #include <gtc/type_ptr.hpp>
  6.  
  7. #include "InputHandler.h"
  8. #include "Vector2D.h"
  9.  
  10. class Camera
  11. {
  12. public:
  13.  
  14.     glm::vec3 m_position;
  15.  
  16.     glm::vec3 m_front;
  17.     glm::vec3 m_up;
  18.     glm::vec3 m_right;
  19.  
  20.     glm::vec3 m_worldup;
  21.  
  22.     float m_yaw;
  23.     float m_pitch;
  24.  
  25.     float m_zoom;
  26.  
  27.     bool m_enableInput;
  28.  
  29.     // Mouse
  30.     float m_mouseSensitivity;
  31.  
  32.     enum class direction { in, out, right, left };
  33.  
  34.     Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
  35.         glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
  36.         float yaw = -90.0f,
  37.         float pitch = 0.0,
  38.         float zoom = 1.0f,
  39.         float mouseSensitivity = 0.25f) :
  40.         m_front(glm::vec3(0.0f, 0.0f, -1.0f)),
  41.         m_enableInput(false)
  42.     {
  43.         m_position = position;
  44.  
  45.         m_yaw = yaw;
  46.         m_pitch = pitch;
  47.  
  48.         m_worldup = up;
  49.  
  50.         m_zoom = zoom;
  51.         m_mouseSensitivity = mouseSensitivity;
  52.  
  53.         updateCameraVectors();
  54.     }
  55.  
  56.     void move(direction d)
  57.     {
  58.         if (d == direction::in)
  59.             m_position -= m_front;
  60.  
  61.         if (d == direction::out)
  62.             m_position += m_front;
  63.  
  64.         if(d == direction::right)
  65.             m_position += glm::normalize(glm::cross(m_front, m_up)) * m_mouseSensitivity;
  66.  
  67.         if(d == direction::left)
  68.             m_position -= glm::normalize(glm::cross(m_front, m_up)) * m_mouseSensitivity;
  69.     }
  70.  
  71.     void keyboardInput()
  72.     {
  73.         // todo: give the user ability to adjust input keys
  74.  
  75.         if (_inHandler->onKeyDown(SDL_SCANCODE_W)) {
  76.             move(direction::in);
  77.         }
  78.         if (_inHandler->onKeyDown(SDL_SCANCODE_S)) {
  79.             move(direction::out);
  80.         }
  81.         if (_inHandler->onKeyDown(SDL_SCANCODE_D)) {
  82.             move(direction::right);
  83.         }
  84.         if (_inHandler->onKeyDown(SDL_SCANCODE_A)) {
  85.             move(direction::left);
  86.         }
  87.     }
  88.  
  89.     void dragTPSInput(Vector2D* mouseMoveDiff)
  90.     {
  91.         glm::quat rot = glm::angleAxis(glm::radians(-mouseMoveDiff->getY()), glm::vec3(1, 0, 0));
  92.         rot = rot * glm::angleAxis(glm::radians(-mouseMoveDiff->getX()), glm::vec3(0, 1, 0));
  93.  
  94.         glm::mat4 rotMatrix = glm::mat4_cast(rot);
  95.  
  96.         glm::vec4 pos = glm::vec4(m_position.x, m_position.y, m_position.z, 1.0f);
  97.  
  98.         pos = rotMatrix * pos;
  99.  
  100.         m_position.x = pos.x;
  101.         m_position.y = pos.y;
  102.         m_position.z = pos.z;
  103.  
  104.         // If I'm not mistaking the code above does the same as:
  105.         // m_position = glm::rotate(m_position, -mouseMoveDiff->getY() * m_mouseSensitivity, glm::vec3(1, 0, 0));
  106.         // m_position = glm::rotate(m_position, -mouseMoveDiff->getX() * m_mouseSensitivity, glm::vec3(0, 1, 0));
  107.  
  108.         updateCameraVectors();
  109.     }
  110.  
  111.     void onInput(bool drag = true, bool scroll = true, bool keyboard = false)
  112.     {
  113.         if (drag)
  114.             if (_inHandler->getMouseButtonState(_inHandler->mouse_buttons::LEFT))
  115.                 if(_inHandler->isMouseMovig())
  116.                     dragTPSInput(_inHandler->getMouseMoveDiff());
  117.  
  118.         if (scroll)
  119.             scrollInput();
  120.  
  121.         if (keyboard)
  122.             keyboardInput();
  123.     }
  124.  
  125.     // Returns the view matrix calculated using Eular Angles and the LookAt Matrix
  126.     glm::mat4 getViewMatrix()
  127.     {
  128.         return glm::lookAt(m_position, m_front, m_up);
  129.     }
  130.  
  131. private:
  132.     void updateCameraVectors()
  133.     {
  134.         // Calculate the new Front vector
  135.         glm::vec3 front;
  136.         front.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
  137.         front.y = sin(glm::radians(m_pitch));
  138.         front.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
  139.  
  140.         m_front = glm::normalize(front);
  141.  
  142.         // Also re-calculate the Right and Up vector
  143.         // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
  144.         m_right = glm::normalize(glm::cross(m_front, m_worldup));
  145.  
  146.         m_up = glm::normalize(glm::cross(m_right, m_front));
  147.     }
  148.  
  149.     InputHandler* _inHandler = TheInputHandler::Instance();
  150. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement