Commit 7297fd95 authored by Alexander Gauggel's avatar Alexander Gauggel
Browse files

Merge branch '42-kamera-trackballkamera' into 'develop'

Resolve "Kamera - Trackballkamera"

Closes #42

See merge request !35
parents 055a35fb f4ee0e9d
Pipeline #25730 passed with stages
in 3 minutes and 30 seconds
......@@ -11,10 +11,13 @@ set(vkcv_camera_include ${PROJECT_SOURCE_DIR}/include)
set(vkcv_camera_sources
${vkcv_camera_include}/vkcv/camera/Camera.hpp
${vkcv_camera_source}/vkcv/camera/Camera.cpp
${vkcv_camera_include}/vkcv/camera/TrackballCamera.hpp
${vkcv_camera_source}/vkcv/camera/TrackballCamera.cpp
${vkcv_camera_include}/vkcv/camera/CameraManager.hpp
${vkcv_camera_source}/vkcv/camera/CameraManager.cpp
${vkcv_camera_include}/vkcv/camera/CameraController.hpp
${vkcv_camera_include}/vkcv/camera/PilotCameraController.hpp
${vkcv_camera_source}/vkcv/camera/PilotCameraController.cpp
${vkcv_camera_include}/vkcv/camera/TrackballCameraController.hpp
${vkcv_camera_source}/vkcv/camera/TrackballCameraController.cpp
)
# adding source files to the project
......
#pragma once
#define GLM_DEPTH_ZERO_TO_ONE
#define GLM_FORCE_LEFT_HANDED
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_access.hpp>
namespace vkcv {
namespace vkcv::camera {
class Camera {
/**
* @brief Used to create a camera which governs the view and projection matrices.
*/
class Camera final {
protected:
glm::mat4 m_view;
glm::mat4 m_projection;
int m_width;
int m_height;
float m_near;
float m_far;
float m_fov;
float m_ratio;
glm::vec3 m_up;
glm::vec3 m_up;
glm::vec3 m_position;
float m_cameraSpeed;
glm::vec3 m_center;
float m_pitch;
float m_yaw;
int m_fov_nsteps;
float m_fov_min;
float m_fov_max;
bool m_forward;
bool m_backward;
bool m_left;
bool m_right;
/**
* @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:
Camera();
virtual ~Camera();
/**
* @brief The default constructor of the camera
*/
Camera();
/**
* @brief The destructor of the camera (default behavior)
*/
~Camera();
/**
* @brief Sets the perspective object according to @p fov, @p ratio, @p near and @p far. This leads to changes in the projection matrix of the camera
* @param[in] fov The desired field of view in radians
* @param[in] ratio The aspect ratio
* @param[in] near Distance to near clipping plane
* @param[in] far Distance to far clipping plane
*/
void setPerspective(float fov, float ratio, float near, float far);
const glm::mat4 getView() const;
void getView(glm::vec3 &x, glm::vec3 &y, glm::vec3 &z, glm::vec3 &pos);
glm::mat4 updateView(double deltatime);
void lookAt(glm::vec3 position, glm::vec3 center, glm::vec3 up);
/**
* @brief Gets the view matrix of the camera
* @return The view matrix of the camera
*/
const glm::mat4& getView() const;
/**
* @brief Sets the view matrix of the camera according to @p position, @p center and @p up
* @param[in] position The position of the camera
* @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(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
*/
const glm::mat4& getProjection() const;
void setProjection(const glm::mat4 projection);
/**
* @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;
/**
* @brief Gets the near and far bounds of the view frustum of the camera.
* @param[out] near The near bound of the view frustum
* @param[out] far The far bound of the view frustum
*/
void getNearFar(float &near, float &far) const;
void setUp(const glm::vec3 &Up);
/**
* @brief Gets the current field of view of the camera in radians
* @return[in] The current field of view in radians
*/
float getFov() const;
/**
* @brief Sets the field of view of the camera to @p fov in radians
* @param[in] fov The new field of view in radians
*/
void setFov(float fov);
void changeFov(double fov);
void updateRatio(int width, int height);
/**
* @brief Gets the current aspect ratio of the camera
* @return The current aspect ratio of the camera
*/
float getRatio() const;
/**
* @brief Updates the aspect ratio of the camera with @p ratio and, thus, changes the projection matrix
* @param[in] ratio The new aspect ratio of the camera
*/
void setRatio(float ratio);
/**
* @brief Sets @p near and @p far as new values for the view frustum of the camera. This leads to changes in the projection matrix according to these two values.
* @param[in] near The new near bound of the view frustum
* @param[in] far The new far bound of the view frustum
*/
void setNearFar(float near, float far);
/**
* @brief Gets the current front vector of the camera in world space
* @return The current front vector of the camera
*/
glm::vec3 getFront() const;
glm::vec3 getPosition() const;
void setPosition( glm::vec3 position );
/**
* @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
*/
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( const glm::vec3& position );
/**
* @brief Gets the center point.
* @return The center point.
*/
const glm::vec3& getCenter() const;
/**
* @brief Sets @p center as the new center point.
* @param[in] center The new center point.
*/
void setCenter(const glm::vec3& center);
/**
* @brief Gets the pitch value of the camera in degrees.
* @return The pitch value in degrees.
*/
float getPitch() const;
/**
* @brief Sets the pitch value of the camera to @p pitch in degrees.
* @param[in] pitch The new pitch value in degrees.
*/
void setPitch(float pitch);
/**
* @brief Gets the yaw value of the camera in degrees.
* @return The yaw value in degrees.
*/
float getYaw() const;
/**
* @brief Sets the yaw value of the camera to @p yaw.
* @param[in] yaw The new yaw value in degrees.
*/
void setYaw(float yaw);
void panView( double xOffset, double yOffset );
void updatePosition(double deltatime);
void moveForward(int action);
void moveBackward(int action);
void moveLeft(int action);
void moveRight(int action);
/**
* @brief Gets the up vector.
* @return The up vector.
*/
const glm::vec3& getUp() const;
/**
* @brief Sets @p up as the new up vector.
* @param[in] up The new up vector.
*/
void setUp(const glm::vec3 &up);
};
}
#pragma once
#include "Camera.hpp"
#include "vkcv/Window.hpp"
namespace vkcv::camera {
/**
* @brief Used as a base class for defining camera controller classes with different behaviors, e.g. the
* #PilotCameraController.
*/
class CameraController {
public:
/**
* @brief The constructor of the #CameraController (default behavior).
*/
CameraController() = default;
/**
* @brief Updates @p camera in respect to @p deltaTime.
* @param[in] deltaTime The time that has passed since last update.
* @param[in] camera The camera object.
*/
virtual void updateCamera(double deltaTime, Camera &camera) = 0;
/**
* @brief A callback function for key events.
* @param[in] key The keyboard key.
* @param[in] scancode The platform-specific scancode.
* @param[in] action The key action.
* @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) = 0;
/**
* @brief A callback function for mouse scrolling events.
* @param[in] offsetX The offset in horizontal direction.
* @param[in] offsetY The offset in vertical direction.
* @param[in] camera The camera object.
*/
virtual void scrollCallback( double offsetX, double offsetY, Camera &camera) = 0;
/**
* @brief A callback function for mouse movement events.
* @param[in] x The horizontal mouse position.
* @param[in] y The vertical mouse position.
* @param[in] camera The camera object.
*/
virtual void mouseMoveCallback(double offsetX, double offsetY, Camera &camera) = 0;
/**
* @brief A callback function for mouse button events.
* @param[in] button The mouse button.
* @param[in] action The button action.
* @param[in] mods The modifier bits.
* @param[in] camera The camera object.
*/
virtual void mouseButtonCallback(int button, int action, int mods, Camera &camera) = 0;
};
}
\ No newline at end of file
#pragma once
#include "TrackballCamera.hpp"
#include "PilotCameraController.hpp"
#include "TrackballCameraController.hpp"
#include "CameraController.hpp"
#include "vkcv/Window.hpp"
#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
* #CameraManager.
*/
enum class ControllerType {
NONE,
PILOT,
TRACKBALL,
TRACE
};
/**
* @brief Used for managing an arbitrary amount of camera controllers.
*/
class CameraManager{
private:
std::function<void(int, int, int, int)> e_keyHandle;
std::function<void(double, double)> e_mouseMoveHandle;
std::function<void(double, double)> e_mouseScrollHandle;
std::function<void(int, int, int)> e_mouseButtonHandle;
std::function<void(int, int)> e_resizeHandle;
Window &m_window;
Camera m_camera;
float m_width;
float m_height;
// std::shared_ptr<vkcv::TrackballCamera> m_trackball;
bool m_roationActive = false;
double m_lastX ;
double m_lastY ;
void bindCamera();
std::function<void(int, int, int, int)> m_keyHandle;
std::function<void(double, double)> m_mouseMoveHandle;
std::function<void(double, double)> m_mouseScrollHandle;
std::function<void(int, int, int)> m_mouseButtonHandle;
std::function<void(int, int)> m_resizeHandle;
Window& m_window;
std::vector<Camera> m_cameras;
std::vector<ControllerType> m_cameraControllerTypes;
uint32_t m_activeCameraIndex;
PilotCameraController m_pilotController;
TrackballCameraController m_trackController;
double m_lastX;
double m_lastY;
/**
* @brief Binds the camera object to the window event handles.
*/
void bindCameraToEvents();
/**
* @brief A callback function for key events. Currently, cycling between all existing camera controllers via Tab,
* window closure via Esc and polling key events from the active camera controller are supported.
* @param[in] key The keyboard key.
* @param[in] scancode The platform-specific scancode.
* @param[in] action The key action.
* @param[in] mods The modifier bits.
*/
void keyCallback(int key, int scancode, int action, int mods);
void scrollCallback( double offsetX, double offsetY);
void mouseMoveCallback( double offsetX, double offsetY);
/**
* @brief A callback function for mouse scrolling events.
* @param[in] offsetX The offset in horizontal direction.
* @param[in] offsetY The offset in vertical direction.
*/
void scrollCallback(double offsetX, double offsetY);
/**
* @brief A callback function for mouse movement events.
* @param[in] x The horizontal mouse position.
* @param[in] y The vertical mouse position.
*/
void mouseMoveCallback(double x, double y);
/**
* @brief A callback function for mouse button events.
* @param[in] button The mouse button.
* @param[in] action The button action.
* @param[in] mods The modifier bits.
*/
void mouseButtonCallback(int button, int action, int mods);
/**
* @brief A callback function for handling the window resizing event. Each existing camera is resized in respect
* of the window size.
* @param[in] width The new width of the window.
* @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:
CameraManager(Window &window, float width, float height, glm::vec3 up = glm::vec3(0.0f,-1.0f,0.0f), glm::vec3 position = glm::vec3(0.0f,0.0f,0.0f));
Camera &getCamera();
/**
* @brief The constructor of the #CameraManager.
* @param[in] window The window.
* @param[in] width The width of the window.
* @param[in] height The height of the window.
*/
CameraManager(Window &window, float width, float height);
/**
* @brief The destructor of the #CameraManager. Destroying the #CameraManager leads to deletion of all stored
* camera objects and camera controller objects.
*/
~CameraManager();
/**
* @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.
*/
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.
* @param[in] cameraIndex The camera index.
* @return The camera object at @p cameraIndex.
* @throws std::runtime_error If @p cameraIndex is not a valid camera index.
*/
Camera& getCamera(uint32_t cameraIndex);
/**
* @brief Gets the stored camera object set as the active camera.
* @return The active camera.
*/
Camera& getActiveCamera();
/**
* @brief Sets the stored camera object located at @p cameraIndex as the active camera.
* @param[in] cameraIndex The camera index.
* @throws std::runtime_error If @p cameraIndex is not a valid camera index.
*/
void setActiveCamera(uint32_t cameraIndex);
/**
* @brief Gets the index of the stored active camera object.
* @return The active camera index.
*/
uint32_t getActiveCameraIndex() const;
/**
* @brief Binds a stored camera object located at @p cameraIndex to a camera controller of specified
* @p controllerType.
* @param[in] cameraIndex The camera index.
* @param[in] controllerType The type of the camera controller.
* @throws std::runtime_error If @p cameraIndex is not a valid camera index.
*/
void setControllerType(uint32_t cameraIndex, ControllerType controllerType);
/**
* @brief Gets the currently bound camera controller type of the stored camera object located at @p cameraIndex.
* @param[in] cameraIndex The camera index.
* @return The type of the camera controller of the specified camera object.
* @throws std::runtime_error If @p cameraIndex is not a valid camera index.
*/
ControllerType getControllerType(uint32_t cameraIndex);
/**
* @brief Updates all stored camera controllers in respect to @p deltaTime.
* @param[in] deltaTime The time that has passed since last update.
*/
void update(double deltaTime);
};
}
#pragma once
#include <vkcv/camera/CameraController.hpp>
namespace vkcv::camera {
/**
* @brief Used to move around a camera object in world space.
*/
class PilotCameraController final : public CameraController {
private:
// camera movement indicators
bool m_forward;
bool m_backward;
bool m_upward;
bool m_downward;
bool m_left;
bool m_right;
bool m_rotationActive;
float m_cameraSpeed;
int m_fov_nsteps;
float m_fov_min;
float m_fov_max;
/**
* @brief Indicates forward movement of the camera depending on the performed @p action.
* @param[in] action The performed action.
*/
void moveForward(int action);
/**
* @brief Indicates backward movement of the camera depending on the performed @p action.
* @param[in] action The performed action.
*/
void moveBackward(int action);
/**
* @brief Indicates left movement of the camera depending on the performed @p action.
* @param[in] action The performed action.
*/
void moveLeft(int action);
/**
* @brief Indicates right movement of the camera depending on the performed @p action.
* @param[in] action The performed action.
*/
void moveRight(int action);
/**
* @brief Indicates upward movement of the camera depending on the performed @p action.
* @param[in] action The performed action.
*/
void moveUpward(int action);
/**
* @brief Indicates downward movement of the camera depending on the performed @p action.
* @param[in] action The performed action.
*/
void moveDownward(int action);