Verified Commit a66bb2d3 authored by Tobias Frisch's avatar Tobias Frisch
Browse files

[#42] Simplified update methods, replaced clones to references, added...

[#42

] Simplified update methods, replaced clones to references, added namespace, corrected applications

Signed-off-by: Tobias Frisch's avatarTobias Frisch <tfrisch@uni-koblenz.de>
parent ed0c0b5a
......@@ -14,7 +14,6 @@ set(vkcv_camera_sources
${vkcv_camera_include}/vkcv/camera/CameraManager.hpp
${vkcv_camera_source}/vkcv/camera/CameraManager.cpp
${vkcv_camera_include}/vkcv/camera/CameraController.hpp
${vkcv_camera_source}/vkcv/camera/CameraController.cpp
${vkcv_camera_include}/vkcv/camera/PilotCameraController.hpp
${vkcv_camera_source}/vkcv/camera/PilotCameraController.cpp
${vkcv_camera_include}/vkcv/camera/TrackballCameraController.hpp
......
......@@ -6,7 +6,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_access.hpp>
namespace vkcv {
namespace vkcv::camera {
/**
* @brief Used to create a camera which governs the view and projection matrices.
......@@ -18,8 +18,6 @@ namespace vkcv {
float m_near;
float m_far;
float m_fov;
float m_ratio;
glm::vec3 m_up;
glm::vec3 m_position;
......@@ -27,6 +25,18 @@ namespace vkcv {
float m_pitch;
float m_yaw;
/**
* @brief Sets the view matrix of the camera to @p view
* @param[in] view The view matrix
*/
void setView(const glm::mat4& view);
/**
* @brief Sets the projection matrix of the camera to @p projection
* @param[in] projection The projection matrix
*/
void setProjection(const glm::mat4& projection);
public:
......@@ -53,7 +63,7 @@ namespace vkcv {
* @brief Gets the view matrix of the camera
* @return The view matrix of the camera
*/
glm::mat4& getView();
const glm::mat4& getView() const;
/**
* @brief Sets the view matrix of the camera according to @p position, @p center and @p up
......@@ -61,22 +71,16 @@ namespace vkcv {
* @param[in] center The target position the camera is looking at
* @param[in] up The vector that defines which direction is 'up' depending on the camera's orientation
*/
void lookAt(glm::vec3 position, glm::vec3 center, glm::vec3 up);
void lookAt(const glm::vec3& position, const glm::vec3& center, const glm::vec3& up);
/**
* @brief Gets the current projection of the camera
* @return The current projection matrix
*/
glm::mat4& getProjection();
const glm::mat4& getProjection() const;
/**
* @brief Sets the projection matrix of the camera to @p projection
* @param[in] projection The projection matrix
*/
void setProjection(const glm::mat4 projection);
/**
* @brief Gets the model-view-projection matrix of the camera
* @brief Gets the model-view-projection matrix of the camera with y-axis-correction applied
* @return The model-view-projection matrix
*/
glm::mat4 getMVP() const;
......@@ -124,30 +128,36 @@ namespace vkcv {
* @return The current front vector of the camera
*/
glm::vec3 getFront() const;
/**
* @brief Sets the front vector of the camera in world space to @p front
* @param[in] front The new front vector of the camera
*/
void setFront(const glm::vec3& front);
/**
* @brief Gets the current position of the camera in world space
* @return The current position of the camera in world space
*/
glm::vec3 getPosition() const;
const glm::vec3& getPosition() const;
/**
* @brief Sets the position of the camera to @p position
* @param[in] position The new position of the camera
*/
void setPosition( glm::vec3 position );
void setPosition( const glm::vec3& position );
/**
* @brief Gets the center point.
* @return The center point.
*/
glm::vec3 getCenter() const;
const glm::vec3& getCenter() const;
/**
* @brief Sets @p center as the new center point.
* @param[in] center The new center point.
*/
void setCenter(glm::vec3 center);
void setCenter(const glm::vec3& center);
/**
* @brief Gets the pitch value of the camera in degrees.
......@@ -177,7 +187,7 @@ namespace vkcv {
* @brief Gets the up vector.
* @return The up vector.
*/
glm::vec3 getUp() const;
const glm::vec3& getUp() const;
/**
* @brief Sets @p up as the new up vector.
......
......@@ -3,7 +3,7 @@
#include "Camera.hpp"
#include "vkcv/Window.hpp"
namespace vkcv {
namespace vkcv::camera {
/**
* @brief Used as a base class for defining camera controller classes with different behaviors, e.g. the
......@@ -23,7 +23,7 @@ namespace vkcv {
* @param[in] deltaTime The time that has passed since last update.
* @param[in] camera The camera object.
*/
virtual void updateCamera(double deltaTime, Camera &camera);
virtual void updateCamera(double deltaTime, Camera &camera) = 0;
/**
* @brief A callback function for key events.
......@@ -33,7 +33,7 @@ namespace vkcv {
* @param[in] mods The modifier bits.
* @param[in] camera The camera object.
*/
virtual void keyCallback(int key, int scancode, int action, int mods, Camera &camera);
virtual void keyCallback(int key, int scancode, int action, int mods, Camera &camera) = 0;
/**
* @brief A callback function for mouse scrolling events.
......@@ -41,7 +41,7 @@ namespace vkcv {
* @param[in] offsetY The offset in vertical direction.
* @param[in] camera The camera object.
*/
virtual void scrollCallback( double offsetX, double offsetY, Camera &camera);
virtual void scrollCallback( double offsetX, double offsetY, Camera &camera) = 0;
/**
* @brief A callback function for mouse movement events.
......@@ -49,7 +49,7 @@ namespace vkcv {
* @param[in] y The vertical mouse position.
* @param[in] camera The camera object.
*/
virtual void mouseMoveCallback(double offsetX, double offsetY, Camera &camera);
virtual void mouseMoveCallback(double offsetX, double offsetY, Camera &camera) = 0;
/**
* @brief A callback function for mouse button events.
......@@ -58,7 +58,7 @@ namespace vkcv {
* @param[in] mods The modifier bits.
* @param[in] camera The camera object.
*/
virtual void mouseButtonCallback(int button, int action, int mods, Camera &camera);
virtual void mouseButtonCallback(int button, int action, int mods, Camera &camera) = 0;
};
}
\ No newline at end of file
......@@ -7,7 +7,7 @@
#include <GLFW/glfw3.h>
#include <functional>
namespace vkcv {
namespace vkcv::camera {
/**
* @brief Used for specifying existing types of camera controllers when adding a new controller object to the
......@@ -86,7 +86,19 @@ namespace vkcv {
* @param[in] height The new height of the window.
*/
void resizeCallback(int width, int height);
/**
* @brief Gets a camera controller object of specified @p controllerType.
* @param[in] controllerType The type of the camera controller.
* @return The specified camera controller object.
*/
CameraController& getControllerByType(ControllerType controllerType);
/**
* @briof A method to get the currently active controller for the active camera.
* @return Reference to the active #CameraController
*/
CameraController& getActiveController();
public:
......@@ -104,19 +116,22 @@ namespace vkcv {
*/
~CameraManager();
/**
* @brief Adds a new camera object to the #CameraManager. The camera is not binded to a controller type.
* @return The index of the newly created camera object.
*/
int addCamera();
/**
* @brief Adds a new camera object to the #CameraManager and binds it to a camera controller object of specified
* @p controllerType.
* @param[in] controllerType The type of the camera controller.
* @return The index of the newly created camera object.
*/
int addCamera(ControllerType controllerType);
uint32_t addCamera(ControllerType controllerType = ControllerType::NONE);
/**
* @brief Adds a new camera object to the #CameraManager and binds it to a camera controller object of specified
* @p controllerType.
* @param[in] controllerType The type of the camera controller.
* @param[in] camera The new camera object.
* @return The index of the newly bound camera object.
*/
uint32_t addCamera(ControllerType controllerType, const Camera& camera);
/**
* @brief Gets the stored camera object located at @p cameraIndex.
......@@ -143,7 +158,7 @@ namespace vkcv {
* @brief Gets the index of the stored active camera object.
* @return The active camera index.
*/
uint32_t getActiveCameraIndex();
uint32_t getActiveCameraIndex() const;
/**
* @brief Binds a stored camera object located at @p cameraIndex to a camera controller of specified
......@@ -162,13 +177,6 @@ namespace vkcv {
*/
ControllerType getControllerType(uint32_t cameraIndex);
/**
* @brief Gets a camera controller object of specified @p controllerType.
* @param[in] controllerType The type of the camera controller.
* @return The specified camera controller object.
*/
CameraController& getControllerByType(ControllerType controllerType);
/**
* @brief Updates all stored camera controllers in respect to @p deltaTime.
* @param[in] deltaTime The time that has passed since last update.
......
......@@ -2,7 +2,7 @@
#include <vkcv/camera/CameraController.hpp>
namespace vkcv {
namespace vkcv::camera {
/**
* @brief Used to move around a camera object in world space.
......@@ -25,23 +25,6 @@ namespace vkcv {
float m_fov_min;
float m_fov_max;
/**
* @brief Updates the position of @p camera with respect to @p deltaTime.
* @param[in] deltaTime The time that has passed since last update.
* @param[in] camera The camera object.
* @return The updated camera position.
*/
glm::vec3 updatePosition(double deltaTime, Camera &camera);
/**
* @brief Updates the view matrix of @p camera with respect to @p deltaTime.
* @param[in] deltaTime The time that has passed since last update.
* @param[in] camera The camera object.
* @return The updated view matrix of the camera.
*/
glm::mat4 updateView(double deltaTime, Camera &camera);
/**
* @brief Indicates forward movement of the camera depending on the performed @p action.
* @param[in] action The performed action.
......
......@@ -2,7 +2,7 @@
#include "CameraController.hpp"
namespace vkcv {
namespace vkcv::camera {
/**
* @brief Used to orbit a camera around its center point.
......@@ -15,22 +15,6 @@ namespace vkcv {
float m_scrollSensitivity;
float m_radius;
/**
* @brief Updates the position of @p camera.
* @param[in] camera The camera object.
* @param[in] camera The camera object.
* @return The updated camera position.
*/
glm::vec3 updatePosition(Camera &camera);
/**
* @brief Updates the view matrix of @p camera.
* @param[in] camera The camera object.
* @return The updated view matrix of the camera.
*/
glm::mat4 updateView(Camera &camera);
/**
* @brief Updates the current radius of @p camera in respect to the @p offset.
* @param[in] offset The offset between the old and new radius.
......
#include "vkcv/camera/Camera.hpp"
#include <iostream>
namespace vkcv {
namespace vkcv::camera {
Camera::Camera() {
m_position = glm::vec3(0.0f, 0.0f, -1.0f);
m_up = glm::vec3(0.0f, 1.0f, 0.0f);
m_center = glm::vec3(0.0f, 0.0f, 0.0f);
lookAt(m_position, m_center, m_up);
glm::vec3 front = glm::normalize(m_center - m_position);
m_pitch = std::atan2(front.y, std::sqrt(front.x * front.x + front.z * front.z));
m_yaw = std::atan2(front.x, front.z);
lookAt(
glm::vec3(0.0f, 0.0f, -1.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f)
);
setFront(glm::normalize(m_center - m_position));
}
Camera::~Camera() = default;
void Camera::lookAt(glm::vec3 position, glm::vec3 center, glm::vec3 up){
m_position = position;
m_center = center;
m_up = up;
m_view = glm::lookAt(m_position, m_center, m_up);
void Camera::lookAt(const glm::vec3& position, const glm::vec3& center, const glm::vec3& up) {
m_position = position;
m_center = center;
m_up = up;
setView(glm::lookAt(position, center, up));
}
void Camera::getNearFar( float &near, float &far) const {
......@@ -27,55 +28,67 @@ namespace vkcv {
far = m_far;
}
glm::mat4& Camera::getView() {
const glm::mat4& Camera::getView() const {
return m_view;
}
void Camera::setView(const glm::mat4 &view) {
m_view = view;
}
glm::mat4& Camera::getProjection() {
const glm::mat4& Camera::getProjection() const {
return m_projection;
}
void Camera::setProjection(const glm::mat4 projection){
m_projection = projection;
void Camera::setProjection(const glm::mat4& projection) {
m_projection = projection;
}
glm::mat4 Camera::getMVP() const {
return m_projection * m_view;
const glm::mat4 y_correction (
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
return y_correction * m_projection * m_view;
}
float Camera::getFov() const {
return m_fov;
const float tanHalfFovy = 1.0f / m_projection[1][1];
float halfFovy = std::atan(tanHalfFovy);
if (halfFovy < 0) {
halfFovy += M_PIf32;
}
return halfFovy * 2.0f;
}
void Camera::setFov( float fov){
setPerspective(fov, m_ratio, m_near, m_far);
setPerspective(fov, getRatio(), m_near, m_far);
}
float Camera::getRatio() const {
return m_ratio;
const float aspectProduct = 1.0f / m_projection[0][0];
const float tanHalfFovy = 1.0f / m_projection[1][1];
return aspectProduct / tanHalfFovy;
}
void Camera::setRatio(float ratio){
setPerspective( m_fov, ratio, m_near, m_far);
setPerspective( getFov(), ratio, m_near, m_far);
}
void Camera::setNearFar(float near, float far){
setPerspective(m_fov, m_ratio, near, far);
setPerspective(getFov(), getRatio(), near, far);
}
void Camera::setPerspective(float fov, float ratio, float near, float far) {
const glm::mat4 y_correction (
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
m_fov = fov;
m_ratio = ratio;
m_near = near;
m_far = far;
m_projection = y_correction * glm::perspective(m_fov, m_ratio, m_near, m_far);
m_near = near;
m_far = far;
setProjection(glm::perspective(fov, ratio, near, far));
}
glm::vec3 Camera::getFront() const {
......@@ -85,22 +98,35 @@ namespace vkcv {
direction.z = std::cos(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
return glm::normalize(direction);
}
void Camera::setFront(const glm::vec3 &front) {
m_pitch = std::atan2(front.y, std::sqrt(front.x * front.x + front.z * front.z));
m_yaw = std::atan2(front.x, front.z);
}
glm::vec3 Camera::getPosition() const {
const glm::vec3& Camera::getPosition() const {
return m_position;
}
void Camera::setPosition( glm::vec3 position ){
m_position = position;
void Camera::setPosition( const glm::vec3& position ){
lookAt(position, m_center, m_up);
}
glm::vec3 Camera::getCenter() const {
const glm::vec3& Camera::getCenter() const {
return m_center;
}
void Camera::setCenter(glm::vec3 center) {
m_center = center;
void Camera::setCenter(const glm::vec3& center) {
lookAt(m_position, center, m_up);
}
const glm::vec3& Camera::getUp() const {
return m_up;
}
void Camera::setUp(const glm::vec3 &up) {
lookAt(m_position, m_center, up);
}
float Camera::getPitch() const {
return m_pitch;
......@@ -118,12 +144,4 @@ namespace vkcv {
m_yaw = yaw;
}
glm::vec3 Camera::getUp() const {
return m_up;
}
void Camera::setUp(const glm::vec3 &up) {
m_up = up;
}
}
\ No newline at end of file
#include "vkcv/camera/CameraController.hpp"
#include <iostream>
namespace vkcv {
void CameraController::updateCamera(double deltaTime, Camera &camera) {
}
void CameraController::keyCallback(int key, int scancode, int action, int mods, Camera &camera) {}
void CameraController::scrollCallback( double offsetX, double offsetY, Camera &camera) {}
void CameraController::mouseMoveCallback(double offsetX, double offsetY, Camera &camera) {}
void CameraController::mouseButtonCallback(int button, int action, int mods, Camera &camera) {}
}
\ No newline at end of file
#include <iostream>
#include <string>
#include "vkcv/camera/CameraManager.hpp"
namespace vkcv{
#include <vkcv/Logger.hpp>
namespace vkcv::camera {
CameraManager::CameraManager(Window &window, float width, float height)
: m_window(window)
{
bindCameraToEvents();
m_activeCameraIndex = 0;
m_lastX = window.getWidth() / 2.0f;
m_lastY = window.getHeight() / 2.0f;
m_lastX = static_cast<float>(window.getWidth()) / 2.0f;
m_lastY = static_cast<float>(window.getHeight()) / 2.0f;
}
CameraManager::~CameraManager() {}
......@@ -37,7 +37,7 @@ namespace vkcv{
else if(button == GLFW_MOUSE_BUTTON_2 && action == GLFW_RELEASE){
glfwSetInputMode(m_window.getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
getControllerByType(getControllerType(getActiveCameraIndex())).mouseButtonCallback(button, action, mods, getActiveCamera());
getActiveController().mouseButtonCallback(button, action, mods, getActiveCamera());
}
void CameraManager::mouseMoveCallback(double x, double y){
......@@ -45,11 +45,11 @@ namespace vkcv{
auto yoffset = static_cast<float>(y - m_lastY);
m_lastX = x;
m_lastY = y;
getControllerByType(getControllerType(getActiveCameraIndex())).mouseMoveCallback(xoffset, yoffset, getActiveCamera());
getActiveController().mouseMoveCallback(xoffset, yoffset, getActiveCamera());
}
void CameraManager::scrollCallback(double offsetX, double offsetY) {
getControllerByType(getControllerType(getActiveCameraIndex())).scrollCallback(offsetX, offsetY, getActiveCamera());
getActiveController().scrollCallback(offsetX, offsetY, getActiveCamera());
}
void CameraManager::keyCallback(int key, int scancode, int action, int mods) {
......@@ -67,33 +67,39 @@ namespace vkcv{
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(m_window.getWindow(), 1);
return;
default:
break;
}
default:
getControllerByType(getControllerType(getActiveCameraIndex())).keyCallback(key, scancode, action, mods, getActiveCamera());
getActiveController().keyCallback(key, scancode, action, mods, getActiveCamera());
break;
}
}
int CameraManager::addCamera() {
Camera camera;
m_cameras.push_back(camera);
m_cameras.back().setPerspective(glm::radians(60.0f), m_window.getWidth() / m_window.getHeight(), 0.1f, 10.0f);
m_cameraControllerTypes.push_back(ControllerType::NONE);
return m_cameras.size() - 1;
CameraController& CameraManager::getActiveController() {
const ControllerType type = getControllerType(getActiveCameraIndex());
return getControllerByType(type);