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

@@ -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{}