Guest User

Untitled

a guest
Oct 14th, 2017
195
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 lookAt()
  57.     {
  58.     }
  59.  
  60.     void move(direction d)
  61.     {
  62.         if (d == direction::in)
  63.             m_position -= m_front;
  64.        
  65.         if (d == direction::out)
  66.             m_position += m_front;
  67.  
  68.         if(d == direction::right)
  69.             m_position += glm::normalize(glm::cross(m_front, m_up)) * m_mouseSensitivity;
  70.  
  71.         if(d == direction::left)
  72.             m_position -= glm::normalize(glm::cross(m_front, m_up)) * m_mouseSensitivity;
  73.     }
  74.    
  75.     void rotate()
  76.     {
  77.     }
  78.  
  79.     void zoom()
  80.     {
  81.     }
  82.  
  83.     void scrollInput()
  84.     {
  85.     }
  86.  
  87.     void keyboardInput()
  88.     {
  89.         // todo: give the user ability to adjust input keys
  90.  
  91.         if (_inHandler->onKeyDown(SDL_SCANCODE_W)) {
  92.             move(direction::in);
  93.         }
  94.         if (_inHandler->onKeyDown(SDL_SCANCODE_S)) {
  95.             move(direction::out);
  96.         }
  97.         if (_inHandler->onKeyDown(SDL_SCANCODE_D)) {
  98.             move(direction::right);
  99.         }
  100.         if (_inHandler->onKeyDown(SDL_SCANCODE_A)) {
  101.             move(direction::left);
  102.         }
  103.     }
  104.  
  105.     void dragFPSInput(Vector2D* mouseMoveDiff, bool constrainPitch = true)
  106.     {
  107.         m_yaw += mouseMoveDiff->getX(); // offsetx
  108.         m_pitch += -(mouseMoveDiff->getY()); // offsety
  109.  
  110.         // Make sure that when pitch is out of bounds, screen doesn't get flipped
  111.         if (constrainPitch)
  112.         {
  113.             if (m_pitch > 89.0f)
  114.                 m_pitch = 89.0f;
  115.             if (m_pitch < -89.0f)
  116.                 m_pitch = -89.0f;
  117.         }
  118.  
  119.         // Update Front, Right and Up Vectors using the updated Eular angles
  120.         updateCameraVectors();
  121.     }
  122.  
  123.     void dragTPSInput(Vector2D* mouseMoveDiff)
  124.     {
  125.         m_position = glm::rotate(m_position, -mouseMoveDiff->getY() * m_mouseSensitivity, glm::vec3(1, 0, 0));
  126.         m_position = glm::rotate(m_position, -mouseMoveDiff->getX() * m_mouseSensitivity, glm::vec3(0, 1, 0));
  127.  
  128.         updateCameraVectors();
  129.     }
  130.  
  131.     void onInput(bool drag = true, bool scroll = true, bool keyboard = false)
  132.     {
  133.         if (drag)
  134.             if (_inHandler->getMouseButtonState(_inHandler->mouse_buttons::LEFT))
  135.                 if(_inHandler->isMouseMovig())
  136.                     dragTPSInput(_inHandler->getMouseMoveDiff());
  137.  
  138.         if (scroll)
  139.             scrollInput();
  140.  
  141.         if (keyboard)
  142.             keyboardInput();
  143.     }
  144.  
  145.     // Returns the view matrix calculated using Eular Angles and the LookAt Matrix
  146.     glm::mat4 getViewMatrix()
  147.     {
  148.         return glm::lookAt(m_position, m_front, m_up);
  149.     }
  150.  
  151. private:
  152.     void updateCameraVectors()
  153.     {
  154.         // Calculate the new Front vector
  155.         glm::vec3 front;
  156.         front.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
  157.         front.y = sin(glm::radians(m_pitch));
  158.         front.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
  159.  
  160.         m_front = glm::normalize(front);
  161.  
  162.         // Also re-calculate the Right and Up vector
  163.         // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
  164.         m_right = glm::normalize(glm::cross(m_front, m_worldup));
  165.        
  166.         m_up = glm::normalize(glm::cross(m_right, m_front));
  167.     }
  168.  
  169.     InputHandler* _inHandler = TheInputHandler::Instance();
  170. };
RAW Paste Data