From e984ed9085ce79863856852b60539de3643bcd15 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 6 Jun 2023 17:04:17 +0200 Subject: [PATCH] fixed cam distance --- include/misc/Maths.h | 12 +++++++++++- include/render/Renderer.h | 4 ++-- src/render/Renderer.cpp | 13 ++++++++----- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/include/misc/Maths.h b/include/misc/Maths.h index 783940c..edc0792 100644 --- a/include/misc/Maths.h +++ b/include/misc/Maths.h @@ -35,9 +35,14 @@ Vec3 operator- (const Vec3& vect, const Vec3& other) { return vect + (-other); } +template +T Length(const Vec3& vect) { + return std::sqrt(vect.x * vect.x + vect.y * vect.y + vect.z * vect.z); +} + template Vec3 Normalize(const Vec3& vect) { - T length = std::sqrt(vect.x * vect.x + vect.y * vect.y + vect.z * vect.z); + T length = Length(vect); return { vect.x / length, vect.y / length, vect.z / length }; } @@ -68,6 +73,11 @@ T Dot(const Vec4& vect, const Vec4& other) { return vect.x * other.x + vect.y * other.y + vect.z * other.z + vect.w * other.w; } +template +T Distance(const Vec3& vect, const Vec3& other) { + return Length(vect - other); +} + ////////////////////////////////////////////////////////////////// // Matricies // ////////////////////////////////////////////////////////////////// diff --git a/include/render/Renderer.h b/include/render/Renderer.h index ba9ebcd..8883c2e 100644 --- a/include/render/Renderer.h +++ b/include/render/Renderer.h @@ -15,8 +15,8 @@ struct Camera { Mat4f InvViewMatrix; Mat4f InvProjectionMatrix; - Vec3f CamPos {0, 25, 0}; - + float CamDistance = 25.0f; + Vec3f CamPos {0, CamDistance, 0}; Vec2f CamLook {}; float m_Yaw = -3.141592653f / 2.0f; diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp index a1d233c..fbf2845 100644 --- a/src/render/Renderer.cpp +++ b/src/render/Renderer.cpp @@ -88,6 +88,7 @@ void Renderer::Resize(int width, int height) { void Renderer::SetZoom(float zoom) { m_Camera.CamPos.y = std::max(1.0f, m_Camera.CamPos.y - zoom); + m_Camera.CamDistance = std::max(1.0f, m_Camera.CamDistance - zoom); SetCamLook(m_Camera.CamLook); } @@ -116,6 +117,8 @@ void Renderer::SetCamPos(const Vec3f& newPos) { } void Renderer::SetCamLook(const Vec2f& worldPos) { + static const float WORLD_HEIGHT = 0; + m_Camera.CamLook = worldPos; Vec3f front = { @@ -124,11 +127,11 @@ void Renderer::SetCamLook(const Vec2f& worldPos) { std::sin(m_Camera.m_Yaw) * std::cos(m_Camera.m_Pitch) }; - static const float WORLD_HEIGHT = 0; - - float lambda = (m_Camera.CamPos.y - WORLD_HEIGHT) / front.y; - - SetCamPos({lambda * front.x + m_Camera.CamLook.x, m_Camera.CamPos.y, lambda * front.z + m_Camera.CamLook.y}); + SetCamPos({ + -m_Camera.CamDistance * front.x + m_Camera.CamLook.x, + -m_Camera.CamDistance * front.y + WORLD_HEIGHT, + -m_Camera.CamDistance * front.z + m_Camera.CamLook.y + }); } Vec2f Renderer::GetCursorWorldPos(const Vec2f& cursorPos, float windowWidth, float windowHeight) {