Files
OpenVulkano/openVulkanoCpp/Scene/PlaneCameraController.cpp
2025-01-25 22:15:14 +01:00

86 lines
3.1 KiB
C++

/*
* 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));
}
}