From f2bbd45ad513c9ef96433e2ce5149804a694d6b6 Mon Sep 17 00:00:00 2001
From: Josh Morgenstern <josh@morgenstern.dev>
Date: Mon, 28 Jun 2021 18:07:16 +0200
Subject: [PATCH] [#54] add (simple) linear camera interpolation

---
 modules/camera/CMakeLists.txt                 |  2 +
 .../vkcv/camera/InterpolationLinear.hpp       | 30 +++++++++++
 .../src/vkcv/camera/InterpolationLinear.cpp   | 50 +++++++++++++++++++
 3 files changed, 82 insertions(+)
 create mode 100644 modules/camera/include/vkcv/camera/InterpolationLinear.hpp
 create mode 100644 modules/camera/src/vkcv/camera/InterpolationLinear.cpp

diff --git a/modules/camera/CMakeLists.txt b/modules/camera/CMakeLists.txt
index 60cfca4c..41617da3 100644
--- a/modules/camera/CMakeLists.txt
+++ b/modules/camera/CMakeLists.txt
@@ -18,6 +18,8 @@ set(vkcv_camera_sources
 		${vkcv_camera_source}/vkcv/camera/PilotCameraController.cpp
 		${vkcv_camera_include}/vkcv/camera/TrackballCameraController.hpp
 		${vkcv_camera_source}/vkcv/camera/TrackballCameraController.cpp
+		${vkcv_camera_include}/vkcv/camera/InterpolationLinear.hpp
+		${vkcv_camera_source}/vkcv/camera/InterpolationLinear.cpp
 )
 
 # adding source files to the project
diff --git a/modules/camera/include/vkcv/camera/InterpolationLinear.hpp b/modules/camera/include/vkcv/camera/InterpolationLinear.hpp
new file mode 100644
index 00000000..578ca75d
--- /dev/null
+++ b/modules/camera/include/vkcv/camera/InterpolationLinear.hpp
@@ -0,0 +1,30 @@
+#include <glm/glm.hpp>
+#include <GLFW/glfw3.h>
+#include <vkcv/camera/Camera.hpp>
+#include <vector>
+
+namespace vkcv::camera {
+
+    class InterpolationLinear{
+
+    private:
+        
+        Camera& m_camera;
+        double m_timeSinceStart; 
+        std::vector<glm::vec3> m_positions;
+
+
+    public:
+
+        InterpolationLinear(Camera &camera);
+        
+        void addPosition(const glm::vec3& pos);
+        
+        void setCamera(Camera camera);
+
+        void resetTimer();
+        
+        void updateCamera();
+    };
+
+}
\ No newline at end of file
diff --git a/modules/camera/src/vkcv/camera/InterpolationLinear.cpp b/modules/camera/src/vkcv/camera/InterpolationLinear.cpp
new file mode 100644
index 00000000..ac73f095
--- /dev/null
+++ b/modules/camera/src/vkcv/camera/InterpolationLinear.cpp
@@ -0,0 +1,50 @@
+#include "vkcv/camera/InterpolationLinear.hpp"
+#include <vkcv/Logger.hpp>
+#include <iostream>
+
+
+namespace vkcv::camera {
+
+    InterpolationLinear::InterpolationLinear(Camera &camera)
+    : m_camera(camera) {
+        resetTimer();
+    }
+
+    void InterpolationLinear::resetTimer() {
+        m_timeSinceStart = glfwGetTime();
+    }
+
+    void InterpolationLinear::addPosition(const glm::vec3& pos) {
+        m_positions.push_back(pos);
+    }
+
+    glm::vec3 interpolate(glm::vec3 posA, glm::vec3 posB, double time, double duration) {
+        glm::vec3 posDiff = posB - posA;
+        glm::vec3 posInterp = posA + (posDiff * static_cast<float>(time / duration));
+        return posInterp;
+    }
+
+    void InterpolationLinear::updateCamera() {
+
+        if (m_positions.size() < 2) {
+            vkcv_log(LogLevel::ERROR, "At least 2 positions needed for interpolation.");
+            return;
+        }
+        double t = glfwGetTime() - m_timeSinceStart;
+        double segmentDuration = 2.0;
+        int numberOfSegments = m_positions.size()-1;
+        double totalDuration = numberOfSegments * segmentDuration;
+        double modt = fmod(t, totalDuration);
+
+        int currentSegment = static_cast<int>((modt / totalDuration) *  numberOfSegments);
+
+        glm::vec3 posInterp = interpolate(m_positions[currentSegment], m_positions[currentSegment+1], fmod(modt, segmentDuration), segmentDuration);
+
+        // const glm::vec3 front = m_camera.getFront();
+		// const glm::vec3 up = m_camera.getUp();
+
+        // m_camera.lookAt(posInterp, front+posInterp, up);
+
+        m_camera.setPosition(posInterp); // always look at center
+    }
+}
\ No newline at end of file
-- 
GitLab