From 199adb369750fbc581f35672d0a5312a2ab62b3c Mon Sep 17 00:00:00 2001
From: Vanessa Karolek <vaka1997@uni-koblenz.de>
Date: Thu, 20 May 2021 18:59:37 +0200
Subject: [PATCH] [#35] initial work on camera objects

we added a basic implementation of a camera object and a trackball implementation
---
 CMakeLists.txt                                |  3 +
 modules/testing/CMakeLists.txt                | 11 ++-
 modules/testing/GLFW.cmake                    | 18 ++++
 modules/testing/GLM.cmake                     | 22 +++++
 .../testing/include/vkcv/camera/Camera.hpp    | 61 +++++++++++++
 .../include/vkcv/camera/TrackballCamera.hpp   | 46 ++++++++++
 modules/testing/src/vkcv/camera/Camera.cpp    | 69 +++++++++++++++
 .../src/vkcv/camera/TrackballCamera.cpp       | 86 +++++++++++++++++++
 projects/first_triangle/src/main.cpp          |  1 +
 9 files changed, 316 insertions(+), 1 deletion(-)
 create mode 100644 modules/testing/GLFW.cmake
 create mode 100644 modules/testing/GLM.cmake
 create mode 100644 modules/testing/include/vkcv/camera/Camera.hpp
 create mode 100644 modules/testing/include/vkcv/camera/TrackballCamera.hpp
 create mode 100644 modules/testing/src/vkcv/camera/Camera.cpp
 create mode 100644 modules/testing/src/vkcv/camera/TrackballCamera.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 569efcf2..ddcb8274 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -72,12 +72,15 @@ endif()
 
 # add include directories from dependencies as system includes
 target_include_directories(vkcv SYSTEM BEFORE PRIVATE ${vkcv_includes})
+message(STATUS ${vkcv_includes})
 
 # add the own include directory for public headers
 target_include_directories(vkcv BEFORE PUBLIC ${vkcv_include})
+message(STATUS ${vkcv_include})
 
 # link the framework using all required libraries
 target_link_libraries(vkcv ${vkcv_libraries})
+message(STATUS ${vkcv_libraries})
 
 # add sub-projects/examples as targets
 add_subdirectory(projects)
diff --git a/modules/testing/CMakeLists.txt b/modules/testing/CMakeLists.txt
index e869c797..822a27c1 100644
--- a/modules/testing/CMakeLists.txt
+++ b/modules/testing/CMakeLists.txt
@@ -11,10 +11,19 @@ set(vkcv_testing_include ${PROJECT_SOURCE_DIR}/include)
 set(vkcv_testing_sources
 		${vkcv_testing_include}/vkcv/testing/Test.hpp
 		${vkcv_testing_source}/vkcv/testing/Test.cpp
-)
+		${vkcv_testing_include}/vkcv/camera/Camera.hpp
+		${vkcv_testing_source}/vkcv/camera/Camera.cpp
+		src/vkcv/camera/TrackballCamera.cpp)
 
 # adding source files to the project
 add_library(vkcv_testing STATIC ${vkcv_testing_sources})
 
+#include(GLM.cmake)                                               # libglm-dev
+#include(GLFW.cmake)												 # libglfw3-dev
+find_package(glfw3 QUIET)
+find_package(glm QUIET)
+target_link_libraries(vkcv_testing PUBLIC glm glfw)
+
 # add the own include directory for public headers
 target_include_directories(vkcv_testing BEFORE PUBLIC ${vkcv_testing_include})
+
diff --git a/modules/testing/GLFW.cmake b/modules/testing/GLFW.cmake
new file mode 100644
index 00000000..1b68d8aa
--- /dev/null
+++ b/modules/testing/GLFW.cmake
@@ -0,0 +1,18 @@
+
+find_package(glfw3 QUIET)
+
+if (glfw3_FOUND)
+    list(APPEND vkcv_libraries glfw)
+
+    message(${vkcv_config_msg} " GLFW    -   " ${glfw3_VERSION})
+else()
+    if (EXISTS "${vkcv_lib_path}/glfw")
+        add_subdirectory(${vkcv_lib}/glfw)
+
+        list(APPEND vkcv_libraries glfw)
+
+        message(${vkcv_config_msg} " GLFW    -   " ${glfw3_VERSION})
+    else()
+        message(WARNING "GLFW is required..! Update the submodules!")
+    endif ()
+endif ()
diff --git a/modules/testing/GLM.cmake b/modules/testing/GLM.cmake
new file mode 100644
index 00000000..151886dc
--- /dev/null
+++ b/modules/testing/GLM.cmake
@@ -0,0 +1,22 @@
+find_package(glm REQUIRED)
+
+if (glm_FOUND)
+
+#    list(APPEND vkcv_includes ${GLM_INCLUDE_DIRS})
+#    list(APPEND vkcv_libraries glm)
+    message(STATUS "GLM included at ${GLM_INCLUDE_DIR}")
+    message(STATUS ${GLM_INCLUDE_DIR})
+    message(STATUS ${GLM_INCLUDE_DIRS})
+    message(STATUS ${GLM_LIBRARIES})
+    message(${vkcv_config_msg} " GLM    -   " ${glm_VERSION})
+else()
+    if (EXISTS "${vkcv_lib_path}/glfw")
+        add_subdirectory(${vkcv_lib}/glfw)
+
+        list(APPEND vkcv_libraries glfw)
+
+        message(${vkcv_config_msg} " GLFW    -   " ${glfw3_VERSION})
+    else()
+        message(WARNING "GLFW is required..! Update the submodules!")
+    endif ()
+endif ()
\ No newline at end of file
diff --git a/modules/testing/include/vkcv/camera/Camera.hpp b/modules/testing/include/vkcv/camera/Camera.hpp
new file mode 100644
index 00000000..588cf18b
--- /dev/null
+++ b/modules/testing/include/vkcv/camera/Camera.hpp
@@ -0,0 +1,61 @@
+#pragma once
+
+#include <GLFW/glfw3.h>
+#include <glm/glm.hpp>
+#include <glm/gtc/matrix_transform.hpp>
+#include <glm/gtc/matrix_access.hpp>
+
+namespace vkcv {
+
+    class Camera {
+    protected:
+        GLFWwindow *m_window;
+
+        glm::mat4 m_view, m_projection;
+
+        int m_width, m_height;
+
+        float m_oldX, m_oldY;
+
+        glm::vec3 m_position, m_direction, m_up;
+
+        float m_near, m_far;
+        float m_fov, m_ratio;
+
+    public:
+        Camera();
+
+        ~Camera();
+
+        virtual void update(GLFWwindow* window);
+
+        void setPerspective(float fov, float ratio, float near, float far);
+
+        const glm::mat4& getView();
+
+        void getView(glm::vec3 &x, glm::vec3 &y, glm::vec3 &z, glm::vec3 &pos);
+
+        void setView( const glm::mat4 view );
+
+        void lookAt(glm::vec3 position, glm::vec3 center, glm::vec3 up);
+
+        const glm::mat4& Camera::getProjection();
+
+        void setProjection( const glm::mat4 projection);
+
+        void getNearFar( float &near, float &far);
+
+        float getFov();
+
+        void setFov( float fov);
+
+        void updateRatio( float ratio);
+
+        float getRatio();
+
+        void setNearFar( float near, float far);
+
+
+    };
+
+}
diff --git a/modules/testing/include/vkcv/camera/TrackballCamera.hpp b/modules/testing/include/vkcv/camera/TrackballCamera.hpp
new file mode 100644
index 00000000..8fb41dd3
--- /dev/null
+++ b/modules/testing/include/vkcv/camera/TrackballCamera.hpp
@@ -0,0 +1,46 @@
+#pragma once
+
+#include "vkcv/camera/Camera.hpp"
+
+namespace vkcv {
+
+    class TrackballCamera : public vkcv::Camera {
+    public:
+
+        TrackballCamera(int width, int height);
+
+        ~TrackballCamera();
+
+        void update( GLFWwindow* window);
+
+        float getSensitivity() const;
+
+        void setSensitivity(float sensitivity);
+
+        float getStepSize() const;
+
+        void setStepSize(float stepSize);
+
+        float getTheta() const;
+
+        void setTheta(float theta);
+
+        float getPhi() const;
+
+        void setPhi(float phi);
+
+        float getRadius() const;
+
+        void setRadius(float radius);
+
+        const glm::vec3& getCenter() const;
+
+        void setCenter(const glm::vec3 &center);
+
+    private:
+        float m_sensitivity;
+        float m_stepSize, m_theta, m_phi, m_radius;
+        glm::vec3 m_center;
+    };
+
+}
\ No newline at end of file
diff --git a/modules/testing/src/vkcv/camera/Camera.cpp b/modules/testing/src/vkcv/camera/Camera.cpp
new file mode 100644
index 00000000..ba651f7f
--- /dev/null
+++ b/modules/testing/src/vkcv/camera/Camera.cpp
@@ -0,0 +1,69 @@
+#include "vkcv/camera/Camera.hpp"
+
+namespace vkcv {
+
+    Camera::Camera() = default;
+
+    Camera::~Camera() = default;
+
+    void Camera::lookAt(glm::vec3 position, glm::vec3 center, glm::vec3 up){
+        m_view = glm::lookAt(position, center, up);
+    }
+
+    void Camera::getView(glm::vec3 &x, glm::vec3 &y, glm::vec3 &z, glm::vec3 &pos){
+        x = glm::vec3( glm::row(m_view, 0));
+        y = glm::vec3( glm::row(m_view, 1));
+        z = glm::vec3( glm::row(m_view, 2));
+        pos = glm::vec3( glm::column(m_view, 3));
+        glm::mat3 mat_inv = glm::inverse( glm::mat3(m_view));
+        pos = -mat_inv * pos;
+    }
+
+    void Camera::getNearFar( float &near, float &far)
+    {
+        near = m_near;
+        far = m_far;
+    }
+
+
+    const glm::mat4& Camera::getView() {
+        return m_view;
+    }
+
+    void Camera::setView( const glm::mat4 view ) {
+        m_view = view;
+    }
+
+    const glm::mat4& Camera::getProjection() {
+        return m_projection;
+    }
+
+    void Camera::setProjection(const glm::mat4 projection) {
+        m_projection = projection;
+    }
+
+    float Camera::getFov(){
+        return m_fov;
+    }
+
+    void Camera::setFov( float fov){
+        m_fov = fov;
+        setPerspective( m_fov, m_ratio, m_near, m_far);
+    }
+
+    void Camera::updateRatio( float ratio){
+        m_ratio = ratio;
+        setPerspective( m_fov, m_ratio, m_near, m_far);
+    }
+
+    float Camera::getRatio(){
+        return 0.0f;
+    }
+
+    void Camera::setNearFar( float near, float far){
+        m_near = near;
+        m_far = far;
+        setPerspective(m_fov, m_ratio, m_near, m_far);
+    }
+
+}
\ No newline at end of file
diff --git a/modules/testing/src/vkcv/camera/TrackballCamera.cpp b/modules/testing/src/vkcv/camera/TrackballCamera.cpp
new file mode 100644
index 00000000..1c51be9d
--- /dev/null
+++ b/modules/testing/src/vkcv/camera/TrackballCamera.cpp
@@ -0,0 +1,86 @@
+#include "vkcv/camera/TrackballCamera.hpp"
+
+namespace vkcv{
+
+
+    TrackballCamera::TrackballCamera(int width, int height)
+    {
+        m_position = glm::vec3(0.0f, 0.0f, 5.0);
+        m_center = glm::vec3( 0.0f, 0.0f, 0.0f);
+        m_up = glm::vec3(0.0f, 1.0f, 0.0f);
+
+        m_width = width;
+        m_height = height;
+
+        m_sensitivity = 0.010f;
+        m_stepSize = 0.1f;
+        m_theta = glm::pi<float>() / 2.0f;
+        m_phi = 0.f;
+        m_radius = 1.5;
+
+        m_view = glm::lookAt( m_center + m_position, m_center, m_up);
+
+        m_oldX = width/2.f;
+        m_oldY = height/2.f;
+
+        m_fov = glm::radians(60.f);
+        m_ratio = m_width / (float) m_height;
+        m_near = 0.001f;
+        m_far = 10.f;
+        glm::mat4 projection = glm::perspective(m_fov, m_ratio, m_near, m_far);
+
+        setProjection(projection);
+    }
+
+    void TrackballCamera::update( GLFWwindow* window) {
+
+    }
+
+    float TrackballCamera::getSensitivity() const {
+        return m_sensitivity;
+    }
+
+    void TrackballCamera::setSensitivity(float sensitivity) {
+        m_sensitivity = sensitivity;
+    }
+
+    float TrackballCamera::getStepSize() const {
+        return m_stepSize;
+    }
+
+    void TrackballCamera::setStepSize(float stepSize) {
+        m_stepSize = stepSize;
+    }
+
+    float TrackballCamera::getTheta() const {
+        return m_theta;
+    }
+
+    void TrackballCamera::setTheta(float theta) {
+        m_theta = theta;
+    }
+
+    float TrackballCamera::getPhi() const {
+        return m_phi;
+    }
+
+    void TrackballCamera::setPhi(float phi) {
+        m_phi = phi;
+    }
+
+    float TrackballCamera::getRadius() const {
+        return m_radius;
+    }
+
+    void TrackballCamera::setRadius(float radius) {
+        m_radius = radius;
+    }
+
+    const glm::vec3& TrackballCamera::getCenter() const {
+        return m_center;
+    }
+
+    void TrackballCamera::setCenter(const glm::vec3 &center) {
+        m_center = center;
+    }
+}
\ No newline at end of file
diff --git a/projects/first_triangle/src/main.cpp b/projects/first_triangle/src/main.cpp
index 3c3f91de..297dfa3d 100644
--- a/projects/first_triangle/src/main.cpp
+++ b/projects/first_triangle/src/main.cpp
@@ -2,6 +2,7 @@
 #include <vkcv/Core.hpp>
 #include <vkcv/Window.hpp>
 #include <vkcv/ShaderProgram.hpp>
+#include <vkcv/camera/Camera.hpp>
 
 int main(int argc, const char** argv) {
     const char* applicationName = "First Triangle";
-- 
GitLab