Refactor CameraController logic
This commit is contained in:
@@ -11,11 +11,10 @@
|
|||||||
|
|
||||||
namespace openVulkanoCpp
|
namespace openVulkanoCpp
|
||||||
{
|
{
|
||||||
ArCameraController::ArCameraController() : m_camera(nullptr)
|
ArCameraController::ArCameraController() = default;
|
||||||
{}
|
|
||||||
|
|
||||||
ArCameraController::ArCameraController(Scene::Camera* camera, const std::shared_ptr<AR::ArSession>& session, const Math::Matrix4f& registration)
|
ArCameraController::ArCameraController(Scene::Camera* camera, const std::shared_ptr<AR::ArSession>& session, const Math::Matrix4f& registration)
|
||||||
: m_arSession(session), m_camera(camera), m_registration(registration), m_updated(false)
|
: CameraController(camera), m_arSession(session), m_registration(registration), m_updated(false)
|
||||||
{
|
{
|
||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
@@ -49,14 +48,14 @@ namespace openVulkanoCpp
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
Logger::AR->critical("Failed to create AR session for ArCameraController! Error: " + createResult.statusMessage);
|
Logger::AR->critical("Failed to create AR session for ArCameraController! Error: " + createResult.statusMessage);
|
||||||
m_camera = nullptr;
|
SetCamera(nullptr);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Logger::AR->critical("No native AR implementation available! Can't start ArCameraController.");
|
Logger::AR->critical("No native AR implementation available! Can't start ArCameraController.");
|
||||||
m_camera = nullptr;
|
SetCamera(nullptr);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,28 +66,28 @@ namespace openVulkanoCpp
|
|||||||
|
|
||||||
void ArCameraController::Init(Scene::Camera* camera, const std::shared_ptr<AR::ArSession>& session, const Math::Matrix4f& registration)
|
void ArCameraController::Init(Scene::Camera* camera, const std::shared_ptr<AR::ArSession>& session, const Math::Matrix4f& registration)
|
||||||
{
|
{
|
||||||
if (m_camera) return;
|
if (GetCamera()) return;
|
||||||
|
CameraController::Init(camera);
|
||||||
m_arSession = session;
|
m_arSession = session;
|
||||||
m_camera = camera;
|
|
||||||
m_registration = registration;
|
m_registration = registration;
|
||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArCameraController::Tick()
|
void ArCameraController::Tick()
|
||||||
{
|
{
|
||||||
if (!m_camera || !m_updated) return;
|
if (!GetCamera() || !m_updated) return;
|
||||||
std::unique_lock lock(m_mutex);
|
std::unique_lock lock(m_mutex);
|
||||||
|
|
||||||
Math::Matrix4f mat = m_registration * Math::Utils::inverse(m_matrixToUpdate);
|
Math::Matrix4f mat = m_registration * Math::Utils::inverse(m_matrixToUpdate);
|
||||||
m_camera->SetMatrix(mat);
|
GetCamera()->SetMatrix(mat);
|
||||||
m_updated = false;
|
m_updated = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArCameraController::Close()
|
void ArCameraController::Close()
|
||||||
{
|
{
|
||||||
if (!m_camera) return;
|
if (!GetCamera()) return;
|
||||||
m_arSession->OnNewCameraViewMatrix -= EventHandler(this, &ArCameraController::UpdateCameraTransformation);
|
m_arSession->OnNewCameraViewMatrix -= EventHandler(this, &ArCameraController::UpdateCameraTransformation);
|
||||||
m_camera = nullptr;
|
SetCamera(nullptr);
|
||||||
m_arSession = nullptr;
|
m_arSession = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Base/ITickable.hpp"
|
#include "CameraController.hpp"
|
||||||
#include "Base/ICloseable.hpp"
|
|
||||||
#include "Math/Math.hpp"
|
#include "Math/Math.hpp"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@@ -20,15 +19,9 @@ namespace openVulkanoCpp
|
|||||||
class ArSession;
|
class ArSession;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace Scene
|
class ArCameraController final : public CameraController
|
||||||
{
|
|
||||||
class Camera;
|
|
||||||
}
|
|
||||||
|
|
||||||
class ArCameraController final : public ITickable, public ICloseable
|
|
||||||
{
|
{
|
||||||
std::shared_ptr<AR::ArSession> m_arSession;
|
std::shared_ptr<AR::ArSession> m_arSession;
|
||||||
Scene::Camera* m_camera;
|
|
||||||
Math::Matrix4f m_registration;
|
Math::Matrix4f m_registration;
|
||||||
Math::Matrix4f m_matrixToUpdate;
|
Math::Matrix4f m_matrixToUpdate;
|
||||||
std::mutex m_mutex;
|
std::mutex m_mutex;
|
||||||
|
|||||||
39
openVulkanoCpp/Controller/CameraController.hpp
Normal file
39
openVulkanoCpp/Controller/CameraController.hpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Base/ITickable.hpp"
|
||||||
|
#include "Base/ICloseable.hpp"
|
||||||
|
|
||||||
|
namespace openVulkanoCpp
|
||||||
|
{
|
||||||
|
namespace Scene
|
||||||
|
{
|
||||||
|
class Camera;
|
||||||
|
}
|
||||||
|
|
||||||
|
class CameraController : public ITickable, ICloseable
|
||||||
|
{
|
||||||
|
Scene::Camera* m_camera;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
CameraController(Scene::Camera* camera = nullptr)
|
||||||
|
: m_camera(camera)
|
||||||
|
{}
|
||||||
|
|
||||||
|
public:
|
||||||
|
~CameraController() override = default;
|
||||||
|
|
||||||
|
virtual void Init(Scene::Camera* camera) { m_camera = camera; }
|
||||||
|
|
||||||
|
void Close() override { m_camera = nullptr; }
|
||||||
|
|
||||||
|
void SetCamera(Scene::Camera* camera) { m_camera = camera; }
|
||||||
|
|
||||||
|
Scene::Camera* GetCamera() { return m_camera; }
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
namespace openVulkanoCpp
|
namespace openVulkanoCpp
|
||||||
{
|
{
|
||||||
FreeCamCameraController::FreeCamCameraController(Scene::Camera* camera) : m_camera(camera)
|
FreeCamCameraController::FreeCamCameraController(Scene::Camera* camera) : CameraController(camera)
|
||||||
{
|
{
|
||||||
auto input = Input::InputManager::GetInstance();
|
auto input = Input::InputManager::GetInstance();
|
||||||
m_actionForward = input->GetAction("forward");
|
m_actionForward = input->GetAction("forward");
|
||||||
@@ -48,7 +48,7 @@ namespace openVulkanoCpp
|
|||||||
m_position += rot * vec;
|
m_position += rot * vec;
|
||||||
Math::Matrix4f camTransformation = Math::Utils::toMat4(rot);
|
Math::Matrix4f camTransformation = Math::Utils::toMat4(rot);
|
||||||
camTransformation[3] = Math::Vector4f(m_position, 1);
|
camTransformation[3] = Math::Vector4f(m_position, 1);
|
||||||
m_camera->SetMatrix(camTransformation);
|
GetCamera()->SetMatrix(camTransformation);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FreeCamCameraController::SetDefaultKeybindings()
|
void FreeCamCameraController::SetDefaultKeybindings()
|
||||||
|
|||||||
@@ -6,8 +6,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Base/ITickable.hpp"
|
#include "CameraController.hpp"
|
||||||
#include "Base/ICloseable.hpp"
|
|
||||||
#include "Math/Math.hpp"
|
#include "Math/Math.hpp"
|
||||||
|
|
||||||
namespace openVulkanoCpp
|
namespace openVulkanoCpp
|
||||||
@@ -22,10 +21,8 @@ namespace openVulkanoCpp
|
|||||||
class InputAction;
|
class InputAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
class FreeCamCameraController final : public ITickable, public ICloseable
|
class FreeCamCameraController final : public CameraController
|
||||||
{
|
{
|
||||||
Scene::Camera* m_camera = nullptr;
|
|
||||||
|
|
||||||
float m_yaw = 0, m_pitch = 0, m_boostFactor = 2;
|
float m_yaw = 0, m_pitch = 0, m_boostFactor = 2;
|
||||||
Math::Vector3f_SIMD m_position = { 0, 0, 0 };
|
Math::Vector3f_SIMD m_position = { 0, 0, 0 };
|
||||||
|
|
||||||
@@ -41,9 +38,7 @@ namespace openVulkanoCpp
|
|||||||
|
|
||||||
~FreeCamCameraController() override = default;
|
~FreeCamCameraController() override = default;
|
||||||
|
|
||||||
void Init(Scene::Camera* camera) { m_camera = camera; ResetPose(); }
|
void Init(Scene::Camera* camera) override { CameraController::Init(camera); ResetPose(); }
|
||||||
|
|
||||||
void SetCamera(Scene::Camera* camera) { m_camera = camera; }
|
|
||||||
|
|
||||||
void ResetPose() { m_yaw = m_pitch = 0; m_position = { 0, 0, 0 }; }
|
void ResetPose() { m_yaw = m_pitch = 0; m_position = { 0, 0, 0 }; }
|
||||||
|
|
||||||
@@ -55,8 +50,6 @@ namespace openVulkanoCpp
|
|||||||
|
|
||||||
void Tick() override;
|
void Tick() override;
|
||||||
|
|
||||||
void Close() override { m_camera = nullptr; }
|
|
||||||
|
|
||||||
void SetDefaultKeybindings();
|
void SetDefaultKeybindings();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user