Move free cam camera controlls into it's own class

This commit is contained in:
2021-05-29 15:26:23 +02:00
parent d80249cd1c
commit a5d5f53ae7
5 changed files with 167 additions and 36 deletions

View File

@@ -0,0 +1,23 @@
/*
* 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 <cinttypes>
namespace openVulkanoCpp
{
class FrameMetadata final
{
public:
uint64_t frameId = 0;
double frameTime = 1;
FrameMetadata() = default;
};
inline FrameMetadata CURRENT_FRAME = {};
}

View 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 "FreeCamCameraController.hpp"
#include "Base/FrameMetadata.hpp"
#include "Scene/Camera.hpp"
#include "Input/InputManager.hpp"
#include "Input/InputKey.hpp"
namespace openVulkanoCpp
{
FreeCamCameraController::FreeCamCameraController(Scene::Camera* camera) : m_camera(camera)
{
auto input = Input::InputManager::GetInstance();
m_actionForward = input->GetAction("forward");
m_actionSide = input->GetAction("side");
m_actionUp = input->GetAction("up");
m_actionLookUp = input->GetAction("look up");
m_actionLookSide = input->GetAction("look side");
m_actionBoost = input->GetAction("boost");
}
void FreeCamCameraController::Tick()
{
auto input = Input::InputManager::GetInstance();
Math::Vector3f_SIMD vec(input->GetAxis(m_actionSide), input->GetAxis(m_actionUp), -input->GetAxis(m_actionForward));
if(Math::Utils::length2(vec) > 1.0f)
{
vec = Math::Utils::normalize(vec);
}
float timeScale = CURRENT_FRAME.frameTime; //TODO
vec = vec * timeScale * 3.0f; // scale vector
if (input->GetButton(m_actionBoost))
{
vec *= m_boostFactor;
}
m_yaw -= input->GetAxis(m_actionLookSide) * timeScale / 2.0;
m_pitch -= input->GetAxis(m_actionLookUp) * timeScale / 2.0;
m_pitch = std::min(1.4f, std::max(-1.4f, m_pitch));
const Math::QuaternionF rot(Math::Vector3f(m_pitch, m_yaw, 0));
m_position += rot * vec;
Math::Matrix4f camTransformation = Math::Utils::toMat4(rot);
camTransformation[3] = Math::Vector4f(m_position, 1);
m_camera->SetMatrix(camTransformation);
}
void FreeCamCameraController::SetDefaultKeybindings()
{
m_actionForward->BindKey(Input::InputKey::Controller::AXIS_LEFT_Y);
m_actionForward->BindAxisButtons(Input::InputKey::Keyboard::KEY_W, Input::InputKey::Keyboard::KEY_S);
m_actionSide->BindKey(Input::InputKey::Controller::AXIS_LEFT_X);
m_actionSide->BindAxisButtons(Input::InputKey::Keyboard::KEY_D, Input::InputKey::Keyboard::KEY_A);
m_actionLookUp->BindKey(Input::InputKey::Controller::AXIS_RIGHT_Y);
m_actionLookSide->BindKey(Input::InputKey::Controller::AXIS_RIGHT_X);
m_actionLookUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_DOWN, Input::InputKey::Keyboard::KEY_UP);
m_actionLookSide->BindAxisButtons(Input::InputKey::Keyboard::KEY_RIGHT, Input::InputKey::Keyboard::KEY_LEFT);
m_actionLookUp->BindKey(Input::InputKey::Mouse::AXIS_Y);
m_actionLookSide->BindKey(Input::InputKey::Mouse::AXIS_X);
m_actionUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_SPACE, Input::InputKey::Keyboard::KEY_LEFT_CONTROL);
m_actionBoost->BindKey(Input::InputKey::Keyboard::KEY_LEFT_SHIFT);
}
}

View File

@@ -0,0 +1,62 @@
/*
* 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"
#include "Math/Math.hpp"
namespace openVulkanoCpp
{
namespace Scene
{
class Camera;
}
namespace Input
{
class InputAction;
}
class FreeCamCameraController final : public ITickable, public ICloseable
{
Scene::Camera* m_camera = nullptr;
float m_yaw = 0, m_pitch = 0, m_boostFactor = 2;
Math::Vector3f_SIMD m_position = { 0, 0, 0 };
Input::InputAction* m_actionForward;
Input::InputAction* m_actionSide;
Input::InputAction* m_actionUp;
Input::InputAction* m_actionLookUp;
Input::InputAction* m_actionLookSide;
Input::InputAction* m_actionBoost;
public:
FreeCamCameraController(Scene::Camera* camera = nullptr);
~FreeCamCameraController() override = default;
void Init(Scene::Camera* camera) { m_camera = camera; ResetPose(); }
void SetCamera(Scene::Camera* camera) { m_camera = camera; }
void ResetPose() { m_yaw = m_pitch = 0; m_position = { 0, 0, 0 }; }
void SetPosition(const Math::Vector3f_SIMD& position) { m_position = position; }
void SetOrientation(float yaw, float pitch) { m_yaw = yaw; m_pitch = pitch; }
void SetBoostFactor(float factor = 2) { m_boostFactor = factor; }
void Tick() override;
void Close() override { m_camera = nullptr; }
void SetDefaultKeybindings();
};
}

View File

@@ -11,6 +11,8 @@
#include "Input/InputManager.hpp"
#include "Host/GraphicsAppManager.hpp"
#include "Math/Math.hpp"
#include "Base/EngineConfiguration.hpp"
#include "Controller/FreeCamCameraController.hpp"
#pragma clang diagnostic push
#pragma ide diagnostic ignored "cert-msc50-cpp"
@@ -26,6 +28,7 @@ class CubesExampleAppImpl final : public CubesExampleApp
{
Scene scene;
PerspectiveCamera cam;
openVulkanoCpp::FreeCamCameraController camController;
Material mat;
Shader shader;
std::vector<Drawable> drawablesPool;
@@ -46,6 +49,10 @@ public:
void Init() override
{
auto engineConfig = openVulkanoCpp::EngineConfiguration::GetEngineConfiguration();
engineConfig->SetNumThreads(4);
engineConfig->SetPreferFramebufferFormatSRGB(false);
std::srand(1); // Fix seed for random numbers
scene.Init();
cam.Init(70, 16, 9, 0.1f, 100);
@@ -75,22 +82,8 @@ public:
GetGraphicsAppManager()->GetRenderer()->SetScene(&scene);
auto input = InputManager::GetInstance();
actionForward = input->GetAction("forward");
actionSide = input->GetAction("side");
actionLookUp = input->GetAction("look up");
actionLookSide = input->GetAction("look side");
actionForward->BindKey(InputKey(InputKey::Controller::AXIS_LEFT_Y));
actionForward->BindAxisButtons(InputKey(InputKey::Keyboard::KEY_S), InputKey(InputKey::Keyboard::KEY_W));
actionSide->BindKey(InputKey(InputKey::Controller::AXIS_LEFT_X));
actionSide->BindAxisButtons(InputKey(InputKey::Keyboard::KEY_D), InputKey(InputKey::Keyboard::KEY_A));
actionLookUp->BindKey(InputKey(InputKey::Controller::AXIS_RIGHT_Y));
actionLookSide->BindKey(InputKey(InputKey::Controller::AXIS_RIGHT_X));
actionLookUp->BindAxisButtons(InputKey(InputKey::Keyboard::KEY_DOWN), InputKey(InputKey::Keyboard::KEY_UP));
actionLookSide->BindAxisButtons(InputKey(InputKey::Keyboard::KEY_RIGHT), InputKey(InputKey::Keyboard::KEY_LEFT));
actionLookUp->BindKey(InputKey(InputKey::Mouse::AXIS_Y));
actionLookSide->BindKey(InputKey(InputKey::Mouse::AXIS_X));
camController.Init(&cam);
camController.SetDefaultKeybindings();
}
void Tick() override
@@ -100,26 +93,7 @@ public:
nodesPool[i].SetMatrix(glm::translate(glm::mat4x4(1), glm::vec3((std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5)));
}
auto input = InputManager::GetInstance();
Vector3f_SIMD vec(input->GetAxis(actionSide), 0, -input->GetAxis(actionForward));
if(Utils::length2(vec) > 1.0f)
{
vec = Utils::normalize(vec);
}
float tiemScale = 0.004f;
vec = vec * tiemScale; // scale vector
yaw += input->GetAxis(actionLookSide) * tiemScale * 0.5f;
pitch += input->GetAxis(actionLookUp) * tiemScale * 0.5f;
QuaternionF rot(Vector3f(pitch, yaw, 0));
Matrix4f rotMat = Utils::toMat4(rot);
vec = Vector3f_SIMD(rotMat * Vector4f(vec, 1));
position += vec;
cam.SetMatrix(Utils::translate(Matrix4f(1), position) * rotMat);
camController.Tick();
}
void Close() override{}

View File

@@ -7,6 +7,7 @@
#include "GraphicsAppManager.hpp"
#include "Base/IPlatform.hpp"
#include "Base/Logger.hpp"
#include "Base/FrameMetadata.hpp"
#include "PlatformProducer.hpp"
#include <chrono>
#include <thread>
@@ -130,6 +131,8 @@ namespace openVulkanoCpp
renderer->Tick();
frameTimer.Tick();
UpdateFps();
CURRENT_FRAME.frameId = frameCount;
CURRENT_FRAME.frameTime = frameTimer.GetTickSeconds();
}
}