Advertisement
Guest User

Untitled

a guest
Jul 12th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.03 KB | None | 0 0
  1. // camera.cpp
  2.  
  3. #include "camera.h"
  4. #include "generic/debug.h"
  5.  
  6. #include <algorithm>
  7. #include <glm/gtc/matrix_transform.hpp>
  8.  
  9. namespace SF::Engine
  10. {
  11.     Camera::Camera(glm::vec3 position) :
  12.         m_position(position),
  13.         m_pitch(0.0f),
  14.         m_yaw(-90),
  15.         m_fov(45),
  16.         m_aspectRation(4.0f / 3.0f),
  17.         m_nearPlane(0.1f),
  18.         m_farPlane(100.0f),
  19.         m_speed(2.5f),
  20.         m_sensivity(0.1f)
  21.     {
  22.         updateCameraVector();
  23.     }
  24.  
  25.     Camera::~Camera()
  26.     {
  27.     }
  28.  
  29.     glm::mat4 Camera::getViewMatrix()
  30.     {
  31.         return glm::lookAtLH(m_position, m_position + m_front, m_up);
  32.     }
  33.  
  34.     glm::mat4 Camera::getProjectionMatrix()
  35.     {
  36.         return glm::perspectiveLH(glm::radians(m_fov), m_aspectRation, m_nearPlane, m_farPlane);
  37.     }
  38.  
  39.     float Camera::getFOV() const
  40.     {
  41.         return m_fov;
  42.     }
  43.  
  44.     void Camera::setFOV(const float a_fov)
  45.     {
  46.         m_fov = a_fov;
  47.     }
  48.  
  49.     void Camera::setAspectRatio(const float a_aspectRatio)
  50.     {
  51.         m_aspectRation = a_aspectRatio;
  52.     }
  53.  
  54.     void Camera::setClippingDistance(const float a_nearPlane, const float a_farPlane)
  55.     {
  56.         m_nearPlane = a_nearPlane;
  57.         m_farPlane  = a_farPlane;
  58.     }
  59.  
  60.     float Camera::getSpeed() const
  61.     {
  62.         return m_speed;
  63.     }
  64.  
  65.     void Camera::setSpeed(const float a_speed)
  66.     {
  67.         m_speed = a_speed;
  68.     }
  69.  
  70.     void Camera::processKeyboard(Direction key, float deltaTime)
  71.     {
  72.         float velocity = m_speed * deltaTime;
  73.  
  74.         switch (key)
  75.         {
  76.         case FORWARD:
  77.             m_position += m_front * velocity;
  78.             break;
  79.         case BACKWARD:
  80.             m_position -= m_front * velocity;
  81.             break;
  82.         case LEFT:
  83.             m_position -= m_right * velocity;
  84.             break;
  85.         case RIGHT:
  86.             m_position += m_right * velocity;
  87.             break;
  88.         case UP:
  89.             m_position += m_up * velocity;
  90.             break;
  91.         case DOWN:
  92.             m_position -= m_up * velocity;
  93.             break;
  94.         default:
  95.             break;
  96.         }
  97.     }
  98.  
  99.     void Camera::processMouse(float xoffset, float yoffset)
  100.     {
  101.         xoffset *= m_sensivity;
  102.         yoffset *= m_sensivity;
  103.  
  104.         m_pitch += yoffset;
  105.         m_pitch = std::clamp(m_pitch, -89.0f, 89.0f);
  106.  
  107.         m_yaw = glm::mod(m_yaw + xoffset, 360.0f);
  108.  
  109.         updateCameraVector();
  110.     }
  111.  
  112.     float Camera::getPitch() const
  113.     {
  114.         return m_pitch;
  115.     }
  116.  
  117.     float Camera::getYaw() const
  118.     {
  119.         return m_yaw;
  120.     }
  121.  
  122.     float Camera::getRoll() const
  123.     {
  124.         return 0; // TODO;
  125.     }
  126.  
  127.     void Camera::setRotation(float a_yaw, float a_pitch, float a_roll)
  128.     {
  129.         m_yaw   = a_yaw;
  130.         m_pitch = a_pitch;
  131.         //m_roll = 0;
  132.  
  133.         updateCameraVector();
  134.     }
  135.  
  136.     glm::vec3 Camera::getPosition()
  137.     {
  138.         return m_position;
  139.     }
  140.  
  141.     void Camera::setPosition(const glm::vec3 & a_position)
  142.     {
  143.         m_position = a_position;
  144.     }
  145.  
  146.     glm::vec3 Camera::getFront()
  147.     {
  148.         return m_front;
  149.     }
  150.  
  151.     void Camera::updateCameraVector()
  152.     {
  153.         glm::vec3 front;
  154.    
  155.         front.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
  156.         front.y = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
  157.         front.z = sin(glm::radians(m_pitch));
  158.        
  159.         glm::vec3 up(0, 0, 1);
  160.  
  161.         m_front = glm::normalize(front);
  162.         m_right = glm::normalize(glm::cross(up, m_front));
  163.         m_up = glm::normalize(glm::cross(m_front, m_right));
  164.  
  165.         static int x = 0;
  166.     }
  167.  
  168. }
  169.  
  170. // camera.h
  171.  
  172. #ifndef SF_ENGINE_CAMERA_H
  173. #define SF_ENGINE_CAMERA_H
  174.  
  175. #include <glm/glm.hpp>
  176.  
  177. namespace SF::Engine
  178. {
  179.     enum Direction {
  180.         FORWARD,
  181.         BACKWARD,
  182.         LEFT,
  183.         RIGHT,
  184.         UP,
  185.         DOWN,
  186.     };
  187.  
  188.     class Camera {
  189.     public:
  190.         Camera(glm::vec3 position);
  191.         virtual ~Camera();
  192.  
  193.         glm::mat4 getViewMatrix();
  194.         glm::mat4 getProjectionMatrix();
  195.  
  196.         glm::vec3 getPosition();
  197.         void setPosition(const glm::vec3& a_position);
  198.         glm::vec3 getFront();
  199.  
  200.         float getFOV() const;
  201.         void  setFOV(const float a_fov);
  202.  
  203.         void setAspectRatio(const float a_aspectRatio);
  204.         void setClippingDistance(const float a_nearPlane, const float a_farPlane);
  205.  
  206.         float getSpeed() const;
  207.         void  setSpeed(const float a_speed);
  208.  
  209.         void processKeyboard(Direction key, float deltaTime);
  210.         void processMouse(float xoffset, float yoffset);
  211.  
  212.         float getPitch() const;
  213.         float getYaw() const;
  214.         float getRoll() const;
  215.    
  216.         void setRotation(float a_yaw, float a_pitch, float a_roll);
  217.  
  218.     private:
  219.         glm::vec3 m_position;
  220.         glm::vec3 m_front;
  221.         glm::vec3 m_up;
  222.         glm::vec3 m_right;
  223.  
  224.         float m_fov;
  225.         float m_aspectRation;
  226.         float m_nearPlane;
  227.         float m_farPlane;
  228.  
  229.         float m_pitch;
  230.         float m_yaw;
  231.         //float m_roll;
  232.  
  233.         float m_speed;
  234.         float m_sensivity;
  235.  
  236.         void updateCameraVector();
  237.  
  238.     };
  239.  
  240. }
  241.  
  242. #endif
  243.  
  244. // calling processKeyBoard(...)
  245.  
  246.     void Scenario::processKey(Key a_key, KeyState a_keyState, float a_deltaTime)
  247.     {
  248.         if (a_keyState == KeyState::PRESSED)
  249.         {
  250.             switch (a_key)
  251.             {
  252.             case Key::W:
  253.                 m_camera.processKeyboard(Direction::FORWARD, a_deltaTime);
  254.                 break;
  255.             case Key::A:
  256.                 m_camera.processKeyboard(Direction::LEFT, a_deltaTime);
  257.                 break;
  258.             case Key::S:
  259.                 m_camera.processKeyboard(Direction::BACKWARD, a_deltaTime);
  260.                 break;
  261.             case Key::D:
  262.                 m_camera.processKeyboard(Direction::RIGHT, a_deltaTime);
  263.                 break;
  264.             case Key::SPACE:
  265.                 m_camera.processKeyboard(Direction::UP, a_deltaTime);
  266.                 break;
  267.             case Key::CTRL_LEFT:
  268.                 m_camera.processKeyboard(Direction::DOWN, a_deltaTime);
  269.                 break;
  270.             }
  271.         }
  272.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement