Merge remote-tracking branch 'origin/plane_camera_controller'

# Conflicts:
#	examples/ExampleApps/MovingCubeApp.cpp
This commit is contained in:
2024-07-14 18:30:15 +02:00
3 changed files with 147 additions and 0 deletions

View 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 = CURRENT_FRAME.frameTime;
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));
}
}

View File

@@ -0,0 +1,43 @@
/*
* 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, const Math::Vector3f& planeNormal);
void Init(Camera* camera, DefaultAxis axis);
void Tick() override;
void SetDefaultKeybindings();
void SetPlaneNormal(const Math::Vector3f& planeNormal);
};
}