Guest User

Untitled

a guest
Jul 11th, 2020
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include "camera.h"
  2. #include "generic/debug.h"
  3.  
  4. namespace SF::Engine
  5. {
  6.     Camera::Camera(glm::vec3 position) :
  7.         m_position(position),
  8.         m_pitch(0.0f),
  9.         m_yaw(-90),
  10.         m_fov(45),
  11.         m_aspectRation(4.0f / 3.0f),
  12.         m_nearPlane(0.1f),
  13.         m_farPlane(100.0f),
  14.         m_speed(2.5f),
  15.         m_sensivity(0.1f)
  16.     {
  17.         updateCameraVector();
  18.     }
  19.  
  20.     Camera::~Camera()
  21.     {
  22.     }
  23.  
  24.     glm::mat4 Camera::getViewMatrix()
  25.     {
  26.         return glm::lookAt(m_position, m_position + m_front, m_up);
  27.     }
  28.  
  29.     glm::mat4 Camera::getProjectionMatrix()
  30.     {
  31.         return glm::perspective(glm::radians(m_fov), m_aspectRation, m_nearPlane, m_farPlane);
  32.     }
  33.  
  34.     float Camera::getFOV() const
  35.     {
  36.         return m_fov;
  37.     }
  38.  
  39.     void Camera::setFOV(const float a_fov)
  40.     {
  41.         m_fov = a_fov;
  42.     }
  43.  
  44.     void Camera::setAspectRatio(const float a_aspectRatio)
  45.     {
  46.         m_aspectRation = a_aspectRatio;
  47.     }
  48.  
  49.     void Camera::setClippingDistance(const float a_nearPlane, const float a_farPlane)
  50.     {
  51.         m_nearPlane = a_nearPlane;
  52.         m_farPlane  = a_farPlane;
  53.     }
  54.  
  55.     float Camera::getSpeed() const
  56.     {
  57.         return m_speed;
  58.     }
  59.  
  60.     void Camera::setSpeed(const float a_speed)
  61.     {
  62.         m_speed = a_speed;
  63.     }
  64.  
  65.     void Camera::processKeyboard(Direction key, float deltaTime)
  66.     {
  67.         float velocity = m_speed * deltaTime;
  68.  
  69.         switch (key)
  70.         {
  71.         case FORWARD:
  72.             m_position += m_front * velocity;
  73.             break;
  74.         case BACKWARD:
  75.             m_position -= m_front * velocity;
  76.             break;
  77.         case LEFT:
  78.             m_position -= m_right * velocity;
  79.             break;
  80.         case RIGHT:
  81.             m_position += m_right * velocity;
  82.             break;
  83.         case UP:
  84.             m_position += m_up * velocity;
  85.             break;
  86.         case DOWN:
  87.             m_position -= m_up * velocity;
  88.             break;
  89.         default:
  90.             break;
  91.         }
  92.     }
  93.  
  94.     void Camera::processMouse(float xoffset, float yoffset)
  95.     {
  96.         xoffset *= m_sensivity;
  97.         yoffset *= m_sensivity;
  98.  
  99.         m_pitch += yoffset;
  100.         m_pitch = std::clamp(m_pitch, -89.0f, 89.0f);
  101.  
  102.         m_yaw = glm::mod(m_yaw - xoffset, 360.0f);
  103.  
  104.         updateCameraVector();
  105.     }
  106.  
  107.     float Camera::getPitch() const
  108.     {
  109.         return m_pitch;
  110.     }
  111.  
  112.     float Camera::getYaw() const
  113.     {
  114.         return m_yaw;
  115.     }
  116.  
  117.     float Camera::getRoll() const
  118.     {
  119.         return 0; // TODO;
  120.     }
  121.  
  122.     void Camera::setRotation(float a_yaw, float a_pitch, float a_roll)
  123.     {
  124.         m_yaw   = a_yaw;
  125.         m_pitch = a_pitch;
  126.         //m_roll = 0;
  127.  
  128.         updateCameraVector();
  129.     }
  130.  
  131.     glm::vec3 Camera::getPosition()
  132.     {
  133.         return m_position;
  134.     }
  135.  
  136.     void Camera::setPosition(const glm::vec3 & a_position)
  137.     {
  138.         m_position = a_position;
  139.     }
  140.  
  141.     glm::vec3 Camera::getFront()
  142.     {
  143.         return m_front;
  144.     }
  145.  
  146.     void Camera::updateCameraVector()
  147.     {
  148.    
  149.         glm::vec3 front;
  150.    
  151.         front.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
  152.         //front.y = sin(glm::radians(m_pitch));
  153.         //front.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
  154.         front.z = sin(glm::radians(m_pitch));
  155.         front.y = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
  156.    
  157.         //front = glm::vec3(0, -1, 0);
  158.    
  159.         glm::vec3 up(0, 0, 1);
  160.  
  161.         m_front = glm::normalize(front);
  162.         m_right = glm::normalize(glm::cross(m_front, up));
  163.         m_up = glm::normalize(glm::cross(m_right, m_front));
  164.  
  165.         static int x = 0;
  166.     }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment