From 2ecac98ccea28ff1706d992bc1d47113658d3bb7 Mon Sep 17 00:00:00 2001
From: Tobias Frisch <tfrisch@uni-koblenz.de>
Date: Mon, 5 Jul 2021 18:42:31 +0200
Subject: [PATCH] [#56] Added work-around to trackball camera

Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de>
---
 .../vkcv/camera/TrackballCameraController.hpp   |  2 ++
 .../src/vkcv/camera/PilotCameraController.cpp   |  5 ++---
 .../vkcv/camera/TrackballCameraController.cpp   | 17 ++++++-----------
 modules/scene/src/vkcv/scene/Mesh.cpp           |  2 +-
 4 files changed, 11 insertions(+), 15 deletions(-)

diff --git a/modules/camera/include/vkcv/camera/TrackballCameraController.hpp b/modules/camera/include/vkcv/camera/TrackballCameraController.hpp
index 4166bda9..20336f7a 100644
--- a/modules/camera/include/vkcv/camera/TrackballCameraController.hpp
+++ b/modules/camera/include/vkcv/camera/TrackballCameraController.hpp
@@ -14,6 +14,8 @@ namespace vkcv::camera {
         float m_cameraSpeed;
         float m_scrollSensitivity;
         float m_radius;
+        float m_pitch;
+        float m_yaw;
 
         /**
          * @brief Updates the current radius of @p camera in respect to the @p offset.
diff --git a/modules/camera/src/vkcv/camera/PilotCameraController.cpp b/modules/camera/src/vkcv/camera/PilotCameraController.cpp
index 5460858a..28ef7c69 100644
--- a/modules/camera/src/vkcv/camera/PilotCameraController.cpp
+++ b/modules/camera/src/vkcv/camera/PilotCameraController.cpp
@@ -50,12 +50,11 @@ namespace vkcv::camera {
         }
 
         // handle yaw rotation
-        float yaw = camera.getYaw() + static_cast<float>(xOffset);
-        yaw += 360.0f * (yaw < -180.0f) - 360.0f * (yaw > 180.0f);
+        float yaw = camera.getYaw() + static_cast<float>(xOffset) * m_cameraSpeed;
         camera.setYaw(yaw);
 
         // handle pitch rotation
-        float pitch = camera.getPitch() - static_cast<float>(yOffset);
+        float pitch = camera.getPitch() - static_cast<float>(yOffset) * m_cameraSpeed;
         pitch = glm::clamp(pitch, -89.0f, 89.0f);
         camera.setPitch(pitch);
     }
diff --git a/modules/camera/src/vkcv/camera/TrackballCameraController.cpp b/modules/camera/src/vkcv/camera/TrackballCameraController.cpp
index cdd66cdb..b149a168 100644
--- a/modules/camera/src/vkcv/camera/TrackballCameraController.cpp
+++ b/modules/camera/src/vkcv/camera/TrackballCameraController.cpp
@@ -8,6 +8,8 @@ namespace vkcv::camera {
         m_radius = 3.0f;
         m_cameraSpeed = 2.5f;
         m_scrollSensitivity = 0.2f;
+        m_pitch = 0.0f;
+        m_yaw = 0.0f;
     }
 
     void TrackballCameraController::setRadius(const float radius) {
@@ -21,14 +23,10 @@ namespace vkcv::camera {
         }
 
         // handle yaw rotation
-        float yaw = camera.getYaw() + static_cast<float>(xOffset) * m_cameraSpeed;
-        yaw += 360.0f * (yaw < 0.0f) - 360.0f * (yaw > 360.0f);
-        camera.setYaw(yaw);
+        m_yaw = m_yaw + static_cast<float>(xOffset) * m_cameraSpeed;
 
         // handle pitch rotation
-        float pitch = camera.getPitch() + static_cast<float>(yOffset) * m_cameraSpeed;
-        pitch += 360.0f * (pitch < 0.0f) - 360.0f * (pitch > 360.0f);
-        camera.setPitch(pitch);
+        m_pitch = m_pitch + static_cast<float>(yOffset) * m_cameraSpeed;
     }
 
     void TrackballCameraController::updateRadius(double offset, Camera &camera) {
@@ -44,14 +42,11 @@ namespace vkcv::camera {
     }
 
     void TrackballCameraController::updateCamera(double deltaTime, Camera &camera) {
-		float yaw = camera.getYaw();
-		float pitch = camera.getPitch();
-		
 		const glm::vec3 yAxis = glm::vec3(0.0f, 1.0f, 0.0f);
 		const glm::vec3 xAxis = glm::vec3(1.0f, 0.0f, 0.0f);
 	
-		const glm::mat4 rotationY = glm::rotate(glm::mat4(1.0f), glm::radians(yaw), yAxis);
-		const glm::mat4 rotationX = glm::rotate(rotationY, -glm::radians(pitch), xAxis);
+		const glm::mat4 rotationY = glm::rotate(glm::mat4(1.0f), glm::radians(m_yaw), yAxis);
+		const glm::mat4 rotationX = glm::rotate(rotationY, -glm::radians(m_pitch), xAxis);
 		const glm::vec3 translation = glm::vec3(
 				rotationX * glm::vec4(0.0f, 0.0f, m_radius, 0.0f)
 		);
diff --git a/modules/scene/src/vkcv/scene/Mesh.cpp b/modules/scene/src/vkcv/scene/Mesh.cpp
index c7ba1e0f..8f134cdf 100644
--- a/modules/scene/src/vkcv/scene/Mesh.cpp
+++ b/modules/scene/src/vkcv/scene/Mesh.cpp
@@ -121,7 +121,7 @@ namespace vkcv::scene {
 			
 			if (!checkFrustum(aabb)) {
 				m_drawcalls[i].instanceCount = 2;
-				continue;
+				//continue;
 			}
 			
 			matrices.push_back(transform);
-- 
GitLab