Move some controllers
This commit is contained in:
69
openVulkanoCpp/Controller/MorphableCameraController.cpp
Normal file
69
openVulkanoCpp/Controller/MorphableCameraController.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
#include "MorphableCameraController.hpp"
|
||||
#include "Base/FrameMetadata.hpp"
|
||||
#include "Input/InputManager.hpp"
|
||||
#include "Input/InputKey.hpp"
|
||||
#include "Scene/Camera.hpp"
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
MorphableCameraController::MorphableCameraController(MorphableCamera* camera)
|
||||
: CameraController(camera)
|
||||
, m_animationDuration(1.0f)
|
||||
, m_currentTime(0.0f)
|
||||
, m_isMorphing(false)
|
||||
, m_targetMorphStatePerspective(true)
|
||||
{}
|
||||
|
||||
void MorphableCameraController::Init(MorphableCamera* camera)
|
||||
{
|
||||
CameraController::Init(camera);
|
||||
}
|
||||
|
||||
void MorphableCameraController::Tick()
|
||||
{
|
||||
|
||||
if (m_isMorphing)
|
||||
{
|
||||
m_currentTime += CURRENT_FRAME.frameTime;
|
||||
if (m_currentTime > m_animationDuration) m_currentTime = m_animationDuration;
|
||||
float t = m_currentTime / m_animationDuration;
|
||||
if (t >= 1.0f)
|
||||
{
|
||||
t = 1.0f;
|
||||
m_isMorphing = false;
|
||||
}
|
||||
const float newState = m_targetMorphStatePerspective ? (1.0f - t) : t;
|
||||
static_cast<MorphableCamera*>(GetCamera())->SetMorphState(newState);
|
||||
}
|
||||
}
|
||||
|
||||
void MorphableCameraController::SetTargetState(const bool toPerspective)
|
||||
{
|
||||
if (toPerspective == m_targetMorphStatePerspective) return;
|
||||
m_targetMorphStatePerspective = toPerspective;
|
||||
m_isMorphing = true;
|
||||
}
|
||||
|
||||
MorphableCameraControllerWithInput::MorphableCameraControllerWithInput(MorphableCamera* camera)
|
||||
: m_controller(camera)
|
||||
, m_actionMorph(Input::InputManager::GetInstance()->GetAction("morph"))
|
||||
{
|
||||
m_actionMorph->BindKey(Input::InputKey::Keyboard::KEY_P);
|
||||
}
|
||||
|
||||
void MorphableCameraControllerWithInput::Tick()
|
||||
{
|
||||
if (Input::InputManager::GetInstance()->GetButtonDown(m_actionMorph))
|
||||
{
|
||||
SetTargetState(!m_controller.IsTargetPerspective());
|
||||
m_controller.Reset();
|
||||
}
|
||||
m_controller.Tick();
|
||||
}
|
||||
}
|
||||
70
openVulkanoCpp/Controller/MorphableCameraController.hpp
Normal file
70
openVulkanoCpp/Controller/MorphableCameraController.hpp
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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 "Scene/MorphableCamera.hpp"
|
||||
#include "CameraController.hpp"
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
namespace Input
|
||||
{
|
||||
class InputAction;
|
||||
}
|
||||
|
||||
namespace Scene
|
||||
{
|
||||
class MorphableCameraController final : public CameraController
|
||||
{
|
||||
double m_animationDuration;
|
||||
double m_currentTime;
|
||||
bool m_isMorphing;
|
||||
bool m_targetMorphStatePerspective; // true for perspective, false for orthographic
|
||||
|
||||
public:
|
||||
MorphableCameraController(MorphableCamera* camera = nullptr);
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Woverloaded-virtual"
|
||||
void Init(MorphableCamera* camera);
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
void Tick() override;
|
||||
|
||||
void SetDuration(const double duration) { m_animationDuration = duration; }
|
||||
|
||||
[[nodiscard]] double GetDuration() const { return m_animationDuration; }
|
||||
|
||||
void SetTargetState(bool toPerspective);
|
||||
|
||||
[[nodiscard]] bool IsTargetPerspective() const { return m_targetMorphStatePerspective; }
|
||||
|
||||
void Reset() { m_currentTime = 0; }
|
||||
};
|
||||
|
||||
class MorphableCameraControllerWithInput final
|
||||
{
|
||||
MorphableCameraController m_controller;
|
||||
Input::InputAction* m_actionMorph;
|
||||
|
||||
public:
|
||||
MorphableCameraControllerWithInput(MorphableCamera* camera = nullptr);
|
||||
|
||||
void Init(MorphableCamera* camera) { m_controller.Init(camera); }
|
||||
|
||||
void Tick();
|
||||
|
||||
void SetDuration(const double duration) { m_controller.SetDuration(duration); }
|
||||
|
||||
[[nodiscard]] double GetDuration() const { return m_controller.GetDuration(); }
|
||||
|
||||
void SetTargetState(const bool toPerspective) { m_controller.SetTargetState(toPerspective); }
|
||||
|
||||
void Reset() { m_controller.Reset(); }
|
||||
};
|
||||
}
|
||||
}
|
||||
86
openVulkanoCpp/Controller/PlaneCameraController.cpp
Normal file
86
openVulkanoCpp/Controller/PlaneCameraController.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
#include "PlaneCameraController.hpp"
|
||||
#include "Base/FrameMetadata.hpp"
|
||||
#include "Input/InputManager.hpp"
|
||||
#include "Input/InputKey.hpp"
|
||||
#include "Scene/Camera.hpp"
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
PlaneCameraController::PlaneCameraController(Camera* camera)
|
||||
: CameraController(camera), m_planeNormal(0.0f, 1.0f, 0.0f), m_position(0.0f, 0.0f, 0.0f)
|
||||
{
|
||||
auto input = OpenVulkano::Input::InputManager::GetInstance();
|
||||
m_actionForward = input->GetAction("forward");
|
||||
m_actionBackward = input->GetAction("backward");
|
||||
m_actionLeft = input->GetAction("left");
|
||||
m_actionRight = input->GetAction("right");
|
||||
m_actionUp = input->GetAction("up");
|
||||
}
|
||||
|
||||
void PlaneCameraController::Init(Camera* camera, const Math::Vector3f& planeNormal)
|
||||
{
|
||||
CameraController::Init(camera);
|
||||
SetPlaneNormal(planeNormal);
|
||||
SetDefaultKeybindings();
|
||||
}
|
||||
|
||||
void PlaneCameraController::Init(Camera* camera, DefaultAxis axis)
|
||||
{
|
||||
Math::Vector3f vector;
|
||||
switch (axis)
|
||||
{
|
||||
case DefaultAxis::OXY: vector = Math::Vector3f(0, 0, 1); break;
|
||||
case DefaultAxis::OXZ: vector = Math::Vector3f(0, 1, 0); break;
|
||||
case DefaultAxis::OYZ: vector = Math::Vector3f(1, 0, 0); break;
|
||||
}
|
||||
Init(camera, vector);
|
||||
}
|
||||
|
||||
void PlaneCameraController::Tick()
|
||||
{
|
||||
auto input = OpenVulkano::Input::InputManager::GetInstance();
|
||||
|
||||
Math::Vector3f direction(0.0f, 0.0f, 0.0f);
|
||||
|
||||
if (input->GetButton(m_actionForward)) { direction += m_planeForward; }
|
||||
if (input->GetButton(m_actionBackward)) { direction -= m_planeForward; }
|
||||
if (input->GetButton(m_actionLeft)) { direction -= m_planeRight; }
|
||||
if (input->GetButton(m_actionRight)) { direction += m_planeRight; }
|
||||
|
||||
if (Math::Utils::length2(direction) > 0.0f) { direction = Math::Utils::normalize(direction); }
|
||||
|
||||
float timeScale = input->GetTimeScale();
|
||||
float speed = 3.0f;
|
||||
direction *= timeScale * speed;
|
||||
direction = direction - Math::Utils::dot(direction, m_planeNormal) * m_planeNormal;
|
||||
m_position += direction;
|
||||
|
||||
Math::Matrix4f transformation = Math::Utils::translate(m_position);
|
||||
GetCamera()->SetMatrix(transformation);
|
||||
}
|
||||
|
||||
void PlaneCameraController::SetDefaultKeybindings()
|
||||
{
|
||||
m_actionForward->BindKey(Input::InputKey::Keyboard::KEY_W);
|
||||
m_actionBackward->BindKey(Input::InputKey::Keyboard::KEY_S);
|
||||
m_actionLeft->BindKey(Input::InputKey::Keyboard::KEY_A);
|
||||
m_actionRight->BindKey(Input::InputKey::Keyboard::KEY_D);
|
||||
m_actionUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_SPACE, Input::InputKey::Keyboard::KEY_LEFT_CONTROL);
|
||||
}
|
||||
|
||||
void PlaneCameraController::SetPlaneNormal(const Math::Vector3f& planeNormal)
|
||||
{
|
||||
m_planeNormal = Math::Utils::normalize(planeNormal);
|
||||
|
||||
Math::Vector3f arbitraryVector = (fabs(m_planeNormal.x) > 0.9f) ? Math::Vector3f(0.0f, 1.0f, 0.0f) :
|
||||
Math::Vector3f(1.0f, 0.0f, 0.0f);
|
||||
m_planeRight = Math::Utils::normalize(Math::Utils::cross(m_planeNormal, arbitraryVector));
|
||||
m_planeForward = Math::Utils::normalize(Math::Utils::cross(m_planeNormal, m_planeRight));
|
||||
}
|
||||
}
|
||||
44
openVulkanoCpp/Controller/PlaneCameraController.hpp
Normal file
44
openVulkanoCpp/Controller/PlaneCameraController.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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 "Controller/CameraController.hpp"
|
||||
#include "Input/InputAction.hpp"
|
||||
#include "Math/Math.hpp"
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
class PlaneCameraController final : public CameraController
|
||||
{
|
||||
Math::Vector3f m_planeNormal;
|
||||
Math::Vector3f m_position;
|
||||
Math::Vector3f m_planeRight;
|
||||
Math::Vector3f m_planeForward;
|
||||
Input::InputAction* m_actionForward;
|
||||
Input::InputAction* m_actionBackward;
|
||||
Input::InputAction* m_actionLeft;
|
||||
Input::InputAction* m_actionRight;
|
||||
Input::InputAction* m_actionUp;
|
||||
|
||||
public:
|
||||
enum class DefaultAxis
|
||||
{
|
||||
OXY,
|
||||
OXZ,
|
||||
OYZ,
|
||||
};
|
||||
|
||||
PlaneCameraController(Camera* camera = nullptr);
|
||||
|
||||
void Init(Camera* camera) override { Init(camera, DefaultAxis::OXZ); }
|
||||
void Init(Camera* camera, const Math::Vector3f& planeNormal);
|
||||
void Init(Camera* camera, DefaultAxis axis);
|
||||
void Tick() override;
|
||||
void SetDefaultKeybindings();
|
||||
void SetPlaneNormal(const Math::Vector3f& planeNormal);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
#include "Input/InputManager.hpp"
|
||||
#include "SceneIntersectionTestController.hpp"
|
||||
#include "Scene/Scene.hpp"
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
void SceneIntersectionTestController::Tick()
|
||||
{
|
||||
Input::InputManager* input = Input::InputManager::GetInstance();
|
||||
if (input->GetButtonDown(m_actionClick))
|
||||
{
|
||||
const float x = input->GetAxis(m_actionClickX);
|
||||
const float y = input->GetAxis(m_actionClickY);
|
||||
const Ray ray = m_camera->CastRay(Math::Vector2i(x, y));
|
||||
const auto& camPos = m_camera->GetPosition();
|
||||
const auto scene = m_camera->GetScene();
|
||||
std::optional<DrawableRayHit> res;
|
||||
for (Drawable* d : scene->rayHittableDrawables)
|
||||
{
|
||||
for (Node* n : d->GetNodes())
|
||||
{
|
||||
const auto& m = n->GetWorldMatrix();
|
||||
const Math::Vector3f rayLocalDir = normalize(inverse(m) * Math::Vector4f(ray.GetDir(), 0));
|
||||
const Math::Vector4f rayLocalPos = inverse(m) * camPos;
|
||||
const Ray ray(rayLocalPos, rayLocalDir);
|
||||
if (auto hit = d->Intersect(ray))
|
||||
{
|
||||
// choose the closest one
|
||||
if (!res || (hit->distance2 < res->distance2))
|
||||
{
|
||||
res = hit;
|
||||
res->drawable = d;
|
||||
res->node = n;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (res)
|
||||
{
|
||||
OnHit.NotifyAll(*res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SceneIntersectionTestController::SetDefaultKeybindings()
|
||||
{
|
||||
auto input = Input::InputManager::GetInstance();
|
||||
m_actionClick = input->GetAction("ClickIntersection");
|
||||
m_actionClick->BindKey(Input::InputKey::Mouse::BUTTON_1);
|
||||
m_actionClick->BindKey(Input::InputKey::Touch::BUTTON_TAP);
|
||||
|
||||
m_actionClickX = input->GetAction("ClickIntersectionPosX");
|
||||
m_actionClickX->BindKey(Input::InputKey::Touch::Axis::AXIS_TAP_X_ABS);
|
||||
m_actionClickX->BindKey(Input::InputKey::Mouse::Axis::AXIS_X_ABS);
|
||||
m_actionClickY = input->GetAction("ClickIntersectionPosY");
|
||||
m_actionClickY->BindKey(Input::InputKey::Touch::Axis::AXIS_TAP_Y_ABS);
|
||||
m_actionClickY->BindKey(Input::InputKey::Mouse::Axis::AXIS_Y_ABS);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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/Event.hpp"
|
||||
#include "Scene/Ray.hpp"
|
||||
#include "Input/InputAction.hpp"
|
||||
#include "Scene/Camera.hpp"
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
class SceneIntersectionTestController : public ITickable
|
||||
{
|
||||
public:
|
||||
SceneIntersectionTestController() = default;
|
||||
SceneIntersectionTestController(Camera* camera) : m_camera(camera) {}
|
||||
void SetCamera(Camera* camera) { m_camera = camera; }
|
||||
[[nodiscard]] Camera* GetCamera() const { return m_camera; }
|
||||
void Tick() override;
|
||||
void SetDefaultKeybindings();
|
||||
Event<DrawableRayHit> OnHit;
|
||||
|
||||
private:
|
||||
Input::InputAction* m_actionClick = nullptr;
|
||||
Input::InputAction* m_actionClickX = nullptr;
|
||||
Input::InputAction* m_actionClickY = nullptr;
|
||||
Camera* m_camera;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user