From 9eb1636681b8445f932fc738b921d6532d427858 Mon Sep 17 00:00:00 2001
From: Vanessa Karolek <vaka1997@uni-koblenz.de>
Date: Mon, 7 Jun 2021 18:50:42 +0200
Subject: [PATCH] [#42][Fix] fix up vector, camera coordinate system, depth

Vulkan uses a left handed coordinate system. Thus, we need to define GLM_FORCE_LEFT_HANDED and adjust left and right movement of the camera.

We also need to define GLM_DEPTH_ZERO_TO_ONE for the depth values which range between 0 and 1 in Vulkan.

Although the up vector is pointing upwards again, it seems that the rendered scene is not flipped along y axis... is this correct?
---
 modules/camera/include/vkcv/camera/Camera.hpp        | 2 ++
 modules/camera/include/vkcv/camera/CameraManager.hpp | 4 ++--
 modules/camera/src/vkcv/camera/Camera.cpp            | 6 +++---
 modules/camera/src/vkcv/camera/CameraManager.cpp     | 2 +-
 modules/camera/src/vkcv/camera/TrackballCamera.cpp   | 4 ++--
 5 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/modules/camera/include/vkcv/camera/Camera.hpp b/modules/camera/include/vkcv/camera/Camera.hpp
index 999ad35c..aa0ec519 100644
--- a/modules/camera/include/vkcv/camera/Camera.hpp
+++ b/modules/camera/include/vkcv/camera/Camera.hpp
@@ -1,5 +1,7 @@
 #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>
diff --git a/modules/camera/include/vkcv/camera/CameraManager.hpp b/modules/camera/include/vkcv/camera/CameraManager.hpp
index b9ffdb9e..917d1915 100644
--- a/modules/camera/include/vkcv/camera/CameraManager.hpp
+++ b/modules/camera/include/vkcv/camera/CameraManager.hpp
@@ -67,10 +67,10 @@ namespace vkcv{
          * @param[in] window The window
          * @param[in] width The width of the window
          * @param[in] height The height of the window
-         * @param[in] up The up vector of the camera. Per default: @code{.cpp} up = glm::vec3(0.0f, -1.0f, 0.0f) @endcode
+         * @param[in] up The up vector of the camera. Per default: @code{.cpp} up = glm::vec3(0.0f, 1.0f, 0.0f) @endcode
          * @param[in] position The position of the camera. Per default: @code{.cpp} position = glm::vec3(0.0f,0.0f,0.0f) @endcode
          */
-        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));
+        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));
 
         /**
          * @brief Gets the camera object
diff --git a/modules/camera/src/vkcv/camera/Camera.cpp b/modules/camera/src/vkcv/camera/Camera.cpp
index 2f7ccf93..dfda62db 100644
--- a/modules/camera/src/vkcv/camera/Camera.cpp
+++ b/modules/camera/src/vkcv/camera/Camera.cpp
@@ -4,7 +4,7 @@
 namespace vkcv {
 
     Camera::Camera(){
-        m_up = glm::vec3(0.0f, -1.0f, 0.0f);
+        m_up = glm::vec3(0.0f, 1.0f, 0.0f);
         m_position = glm::vec3(0.0f, 0.0f, 0.0f);
         m_cameraSpeed = 2.f;
 
@@ -154,8 +154,8 @@ namespace vkcv {
     void Camera::updatePosition(double deltaTime ){
         m_position += (m_cameraSpeed * getFront() * static_cast<float> (m_forward) * static_cast<float>(deltaTime));
         m_position -= (m_cameraSpeed * getFront() * static_cast<float> (m_backward) * static_cast<float>(deltaTime));
-        m_position -= (glm::normalize(glm::cross(getFront(), m_up)) * m_cameraSpeed * static_cast<float> (m_left) * static_cast<float>(deltaTime));
-        m_position += (glm::normalize(glm::cross(getFront(), m_up)) * m_cameraSpeed * static_cast<float> (m_right) * static_cast<float>(deltaTime));
+        m_position += (glm::normalize(glm::cross(getFront(), m_up)) * m_cameraSpeed * static_cast<float> (m_left) * static_cast<float>(deltaTime));
+        m_position -= (glm::normalize(glm::cross(getFront(), m_up)) * m_cameraSpeed * static_cast<float> (m_right) * static_cast<float>(deltaTime));
         m_position -= m_up * m_cameraSpeed * static_cast<float> (m_top) * static_cast<float>(deltaTime);
         m_position += m_up * m_cameraSpeed * static_cast<float> (m_bottom) * static_cast<float>(deltaTime);
     }
diff --git a/modules/camera/src/vkcv/camera/CameraManager.cpp b/modules/camera/src/vkcv/camera/CameraManager.cpp
index a08684ec..1358d39f 100644
--- a/modules/camera/src/vkcv/camera/CameraManager.cpp
+++ b/modules/camera/src/vkcv/camera/CameraManager.cpp
@@ -58,7 +58,6 @@ namespace vkcv{
     }
 
     void CameraManager::keyCallback(int key, int scancode, int action, int mods) {
-
         switch (key) {
             case GLFW_KEY_W:
                 m_camera.moveForward(action);
@@ -85,6 +84,7 @@ namespace vkcv{
                 break;
         }
     }
+
     Camera& CameraManager::getCamera(){
         return m_camera;
     }
diff --git a/modules/camera/src/vkcv/camera/TrackballCamera.cpp b/modules/camera/src/vkcv/camera/TrackballCamera.cpp
index e5ee35f5..e99540e9 100644
--- a/modules/camera/src/vkcv/camera/TrackballCamera.cpp
+++ b/modules/camera/src/vkcv/camera/TrackballCamera.cpp
@@ -68,8 +68,8 @@ namespace vkcv {
     }
 
     void TrackballCamera::updatePosition(double deltaTime) {
-        glm::mat4 rotationY = glm::rotate(glm::mat4(1.0f), glm::radians(m_yaw), glm::vec3(0.0f, -1.0f, 0.0f));
-        glm::mat4 rotationX = glm::rotate(rotationY, -glm::radians(m_pitch), glm::vec3(1.0f, 0.0f, 0.0f));
+        glm::mat4 rotationY = glm::rotate(glm::mat4(1.0f), glm::radians(m_yaw), glm::vec3(0.0f, 1.0f, 0.0f));
+        glm::mat4 rotationX = glm::rotate(rotationY, glm::radians(m_pitch), glm::vec3(1.0f, 0.0f, 0.0f));
         glm::vec3 translate = glm::vec3(0.0f,0.0f,m_radius);
         translate = glm::vec3(rotationX * glm::vec4(translate, 0.0f));
         m_position = m_center + translate;
-- 
GitLab