Skip to content
Snippets Groups Projects
Verified Commit f2bbd45a authored by Josch Morgenstern's avatar Josch Morgenstern
Browse files

[#54] add (simple) linear camera interpolation

parent 6f3bdf7b
No related branches found
No related tags found
1 merge request!53Resolve "Kamera - Kamerafahrt mit Interpolation"
...@@ -18,6 +18,8 @@ set(vkcv_camera_sources ...@@ -18,6 +18,8 @@ set(vkcv_camera_sources
${vkcv_camera_source}/vkcv/camera/PilotCameraController.cpp ${vkcv_camera_source}/vkcv/camera/PilotCameraController.cpp
${vkcv_camera_include}/vkcv/camera/TrackballCameraController.hpp ${vkcv_camera_include}/vkcv/camera/TrackballCameraController.hpp
${vkcv_camera_source}/vkcv/camera/TrackballCameraController.cpp ${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 # adding source files to the project
......
#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
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment