Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // camera.cpp
- #include "camera.h"
- #include "generic/debug.h"
- #include <algorithm>
- #include <glm/gtc/matrix_transform.hpp>
- namespace SF::Engine
- {
- Camera::Camera(glm::vec3 position) :
- m_position(position),
- m_pitch(0.0f),
- m_yaw(-90),
- m_fov(45),
- m_aspectRation(4.0f / 3.0f),
- m_nearPlane(0.1f),
- m_farPlane(100.0f),
- m_speed(2.5f),
- m_sensivity(0.1f)
- {
- updateCameraVector();
- }
- Camera::~Camera()
- {
- }
- glm::mat4 Camera::getViewMatrix()
- {
- return glm::lookAtLH(m_position, m_position + m_front, m_up);
- }
- glm::mat4 Camera::getProjectionMatrix()
- {
- return glm::perspectiveLH(glm::radians(m_fov), m_aspectRation, m_nearPlane, m_farPlane);
- }
- float Camera::getFOV() const
- {
- return m_fov;
- }
- void Camera::setFOV(const float a_fov)
- {
- m_fov = a_fov;
- }
- void Camera::setAspectRatio(const float a_aspectRatio)
- {
- m_aspectRation = a_aspectRatio;
- }
- void Camera::setClippingDistance(const float a_nearPlane, const float a_farPlane)
- {
- m_nearPlane = a_nearPlane;
- m_farPlane = a_farPlane;
- }
- float Camera::getSpeed() const
- {
- return m_speed;
- }
- void Camera::setSpeed(const float a_speed)
- {
- m_speed = a_speed;
- }
- void Camera::processKeyboard(Direction key, float deltaTime)
- {
- float velocity = m_speed * deltaTime;
- switch (key)
- {
- case FORWARD:
- m_position += m_front * velocity;
- break;
- case BACKWARD:
- m_position -= m_front * velocity;
- break;
- case LEFT:
- m_position -= m_right * velocity;
- break;
- case RIGHT:
- m_position += m_right * velocity;
- break;
- case UP:
- m_position += m_up * velocity;
- break;
- case DOWN:
- m_position -= m_up * velocity;
- break;
- default:
- break;
- }
- }
- void Camera::processMouse(float xoffset, float yoffset)
- {
- xoffset *= m_sensivity;
- yoffset *= m_sensivity;
- m_pitch += yoffset;
- m_pitch = std::clamp(m_pitch, -89.0f, 89.0f);
- m_yaw = glm::mod(m_yaw + xoffset, 360.0f);
- updateCameraVector();
- }
- float Camera::getPitch() const
- {
- return m_pitch;
- }
- float Camera::getYaw() const
- {
- return m_yaw;
- }
- float Camera::getRoll() const
- {
- return 0; // TODO;
- }
- void Camera::setRotation(float a_yaw, float a_pitch, float a_roll)
- {
- m_yaw = a_yaw;
- m_pitch = a_pitch;
- //m_roll = 0;
- updateCameraVector();
- }
- glm::vec3 Camera::getPosition()
- {
- return m_position;
- }
- void Camera::setPosition(const glm::vec3 & a_position)
- {
- m_position = a_position;
- }
- glm::vec3 Camera::getFront()
- {
- return m_front;
- }
- void Camera::updateCameraVector()
- {
- glm::vec3 front;
- front.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
- front.y = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
- front.z = sin(glm::radians(m_pitch));
- glm::vec3 up(0, 0, 1);
- m_front = glm::normalize(front);
- m_right = glm::normalize(glm::cross(up, m_front));
- m_up = glm::normalize(glm::cross(m_front, m_right));
- static int x = 0;
- }
- }
- // camera.h
- #ifndef SF_ENGINE_CAMERA_H
- #define SF_ENGINE_CAMERA_H
- #include <glm/glm.hpp>
- namespace SF::Engine
- {
- enum Direction {
- FORWARD,
- BACKWARD,
- LEFT,
- RIGHT,
- UP,
- DOWN,
- };
- class Camera {
- public:
- Camera(glm::vec3 position);
- virtual ~Camera();
- glm::mat4 getViewMatrix();
- glm::mat4 getProjectionMatrix();
- glm::vec3 getPosition();
- void setPosition(const glm::vec3& a_position);
- glm::vec3 getFront();
- float getFOV() const;
- void setFOV(const float a_fov);
- void setAspectRatio(const float a_aspectRatio);
- void setClippingDistance(const float a_nearPlane, const float a_farPlane);
- float getSpeed() const;
- void setSpeed(const float a_speed);
- void processKeyboard(Direction key, float deltaTime);
- void processMouse(float xoffset, float yoffset);
- float getPitch() const;
- float getYaw() const;
- float getRoll() const;
- void setRotation(float a_yaw, float a_pitch, float a_roll);
- private:
- glm::vec3 m_position;
- glm::vec3 m_front;
- glm::vec3 m_up;
- glm::vec3 m_right;
- float m_fov;
- float m_aspectRation;
- float m_nearPlane;
- float m_farPlane;
- float m_pitch;
- float m_yaw;
- //float m_roll;
- float m_speed;
- float m_sensivity;
- void updateCameraVector();
- };
- }
- #endif
- // calling processKeyBoard(...)
- void Scenario::processKey(Key a_key, KeyState a_keyState, float a_deltaTime)
- {
- if (a_keyState == KeyState::PRESSED)
- {
- switch (a_key)
- {
- case Key::W:
- m_camera.processKeyboard(Direction::FORWARD, a_deltaTime);
- break;
- case Key::A:
- m_camera.processKeyboard(Direction::LEFT, a_deltaTime);
- break;
- case Key::S:
- m_camera.processKeyboard(Direction::BACKWARD, a_deltaTime);
- break;
- case Key::D:
- m_camera.processKeyboard(Direction::RIGHT, a_deltaTime);
- break;
- case Key::SPACE:
- m_camera.processKeyboard(Direction::UP, a_deltaTime);
- break;
- case Key::CTRL_LEFT:
- m_camera.processKeyboard(Direction::DOWN, a_deltaTime);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement