Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.57 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.05f) :
  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.     float phi = 0;
  124.     float theta = 0;
  125.  
  126.     void dragTPSInput(Vector2D* mouseMoveDiff)
  127.     {
  128.         // m_position = glm::rotate(m_position, -mouseMoveDiff->getY() * m_mouseSensitivity, glm::vec3(1, 0, 0));
  129.         // m_position = glm::rotate(m_position, -mouseMoveDiff->getX() * m_mouseSensitivity, glm::vec3(0, 1, 0));
  130.  
  131.         /*
  132.         glm::quat rot = glm::angleAxis(glm::radians(-mouseMoveDiff->getY()), glm::vec3(1, 0, 0));
  133.         rot = rot * glm::angleAxis(glm::radians(-mouseMoveDiff->getX()), glm::vec3(0, 1, 0));
  134.  
  135.         glm::mat4 rotMatrix = glm::mat4_cast(rot);
  136.  
  137.         glm::vec4 pos = glm::vec4(m_position.x, m_position.y, m_position.z, 1.0f);
  138.  
  139.         pos = rotMatrix * pos;
  140.         */
  141.  
  142.         if (phi >= 3.10) {
  143.             phi = 3.14;
  144.         }
  145.  
  146.         if (phi <= 0) {
  147.             phi = 0;
  148.         }
  149.  
  150.         phi += mouseMoveDiff->getY() * 0.05f;
  151.         theta += mouseMoveDiff->getX() *  0.05f;
  152.  
  153.         float DistanceToOrigin = 10.0f;
  154.  
  155.         float x = DistanceToOrigin * sin(phi) * cos(theta);
  156.         float y = DistanceToOrigin * cos(phi);
  157.         float z = DistanceToOrigin * sin(phi) * sin(theta);
  158.  
  159.         m_position.x = x;
  160.         m_position.y = y;
  161.         m_position.z = z;
  162.  
  163.         updateCameraVectors();
  164.     }
  165.  
  166.     void onInput(bool drag = true, bool scroll = true, bool keyboard = false)
  167.     {
  168.         if (drag)
  169.             if (_inHandler->getMouseButtonState(_inHandler->mouse_buttons::LEFT))
  170.                 if(_inHandler->isMouseMovig())
  171.                     dragTPSInput(_inHandler->getMouseMoveDiff());
  172.  
  173.         if (scroll)
  174.             scrollInput();
  175.  
  176.         if (keyboard)
  177.             keyboardInput();
  178.     }
  179.  
  180.     // Returns the view matrix calculated using Eular Angles and the LookAt Matrix
  181.     glm::mat4 getViewMatrix()
  182.     {
  183.         glm::vec3 pos = glm::vec3(m_position.x, m_position.y, m_position.z);
  184.         return glm::lookAt(pos, m_front, m_up);
  185.     }
  186.  
  187. private:
  188.     void updateCameraVectors()
  189.     {
  190.         // Calculate the new Front vector
  191.         glm::vec3 front;
  192.         front.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
  193.         front.y = sin(glm::radians(m_pitch));
  194.         front.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
  195.  
  196.         m_front = glm::normalize(front);
  197.  
  198.         // Also re-calculate the Right and Up vector
  199.         // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
  200.         m_right = glm::normalize(glm::cross(m_front, m_worldup));
  201.        
  202.         m_up = glm::normalize(glm::cross(m_right, m_front));
  203.     }
  204.  
  205.     InputHandler* _inHandler = TheInputHandler::Instance();
  206. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement