Added a debug controller
Made a default template for cameras. Added delta to mouse state so we can check delta for debug Camera.
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
#include <Controller/DebugCameraController.h>
|
||||
|
||||
#include <Controller/ControllerUtils.h>
|
||||
#include <Core/Common/CoreUtils.h>
|
||||
#include <Core/HAL/Keyboard/Keyboard.h>
|
||||
#include <Core/HAL/Mouse/Mouse.h>
|
||||
#include <Graphics/Camera.h>
|
||||
#include <imgui.h>
|
||||
#include <math.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
bool gIsDebugCameraActive = false;
|
||||
index_t gPreviousCameraIndex = 0;
|
||||
|
||||
float gPitch = 0.0f;
|
||||
float gYaw = 0.0f;
|
||||
bool gFirstUpdate = true;
|
||||
bool gIsFpsModeActive = false;
|
||||
bool gWasRightMouseButtonDown = false;
|
||||
} // namespace
|
||||
|
||||
void ActivateDebugController()
|
||||
{
|
||||
Assert(gIsDebugCameraActive == false);
|
||||
|
||||
Juliet::Camera* currentCam = Juliet::GetCurrentCamera();
|
||||
gPreviousCameraIndex = currentCam->Index;
|
||||
|
||||
Juliet::SetCurrentCamera(kDebugCamera);
|
||||
|
||||
gIsDebugCameraActive = true;
|
||||
gFirstUpdate = true;
|
||||
gIsFpsModeActive = false;
|
||||
gWasRightMouseButtonDown = false;
|
||||
}
|
||||
|
||||
void DeactivateDebugController()
|
||||
{
|
||||
Assert(gIsDebugCameraActive);
|
||||
|
||||
gIsDebugCameraActive = false;
|
||||
|
||||
Juliet::SetCurrentCamera(gPreviousCameraIndex);
|
||||
}
|
||||
|
||||
bool IsDebugControllerActive()
|
||||
{
|
||||
return gIsDebugCameraActive;
|
||||
}
|
||||
|
||||
void UpdateDebugController(float dt)
|
||||
{
|
||||
Juliet::Camera* currentCam = Juliet::GetCurrentCamera();
|
||||
|
||||
if (gFirstUpdate)
|
||||
{
|
||||
Juliet::Vector3 dir = Juliet::Vector3{ 0.0f, 0.0f, 0.0f } - currentCam->Position;
|
||||
dir = Juliet::Normalize(dir);
|
||||
gPitch = asinf(dir.z);
|
||||
gYaw = atan2f(dir.y, dir.x);
|
||||
gFirstUpdate = false;
|
||||
|
||||
Juliet::Vector3 forward;
|
||||
forward.x = cosf(gPitch) * cosf(gYaw);
|
||||
forward.y = cosf(gPitch) * sinf(gYaw);
|
||||
forward.z = sinf(gPitch);
|
||||
Juliet::Vector3 right = Juliet::Normalize(Juliet::Cross(Juliet::Vector3{ 0.0f, 0.0f, 1.0f }, forward));
|
||||
currentCam->Target = currentCam->Position + forward;
|
||||
currentCam->Up = Juliet::Cross(forward, right);
|
||||
}
|
||||
|
||||
bool isRightMouseButtonDown = Juliet::IsMouseButtonDown(Juliet::MouseButton::Right);
|
||||
if (isRightMouseButtonDown && !gWasRightMouseButtonDown)
|
||||
{
|
||||
gIsFpsModeActive = !gIsFpsModeActive;
|
||||
}
|
||||
gWasRightMouseButtonDown = isRightMouseButtonDown;
|
||||
|
||||
if (!gIsFpsModeActive)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Juliet::MousePosition mouseDelta = Juliet::GetMouseDelta();
|
||||
|
||||
float sensitivity = 0.005f;
|
||||
gYaw += mouseDelta.X * sensitivity;
|
||||
gPitch -= mouseDelta.Y * sensitivity;
|
||||
|
||||
if (gPitch > 1.5f)
|
||||
{
|
||||
gPitch = 1.5f;
|
||||
}
|
||||
if (gPitch < -1.5f)
|
||||
{
|
||||
gPitch = -1.5f;
|
||||
}
|
||||
|
||||
if (Juliet::IsKeyDown(Juliet::ScanCode::Q))
|
||||
{
|
||||
gYaw -= 2.0f * dt;
|
||||
}
|
||||
if (Juliet::IsKeyDown(Juliet::ScanCode::E))
|
||||
{
|
||||
gYaw += 2.0f * dt;
|
||||
}
|
||||
|
||||
Juliet::Vector3 forward;
|
||||
forward.x = cosf(gPitch) * cosf(gYaw);
|
||||
forward.y = cosf(gPitch) * sinf(gYaw);
|
||||
forward.z = sinf(gPitch);
|
||||
|
||||
Juliet::Vector3 right = Juliet::Normalize(Juliet::Cross(Juliet::Vector3{ 0.0f, 0.0f, 1.0f }, forward));
|
||||
Juliet::Vector3 defaultUp = Juliet::Cross(forward, right);
|
||||
|
||||
static const float kMovementPerFrame = 10.f; // 10m/s
|
||||
|
||||
float speedPerFrame = kMovementPerFrame;
|
||||
if (Juliet::IsKeyDown(Juliet::ScanCode::LeftShift))
|
||||
{
|
||||
speedPerFrame *= 10.f; // 100m/s
|
||||
}
|
||||
|
||||
if (Juliet::IsKeyDown(Juliet::ScanCode::W))
|
||||
{
|
||||
currentCam->Position = currentCam->Position + forward * (speedPerFrame * dt);
|
||||
}
|
||||
if (Juliet::IsKeyDown(Juliet::ScanCode::S))
|
||||
{
|
||||
currentCam->Position = currentCam->Position - forward * (speedPerFrame * dt);
|
||||
}
|
||||
if (Juliet::IsKeyDown(Juliet::ScanCode::D))
|
||||
{
|
||||
currentCam->Position = currentCam->Position + right * (speedPerFrame * dt);
|
||||
}
|
||||
if (Juliet::IsKeyDown(Juliet::ScanCode::A))
|
||||
{
|
||||
currentCam->Position = currentCam->Position - right * (speedPerFrame * dt);
|
||||
}
|
||||
if (Juliet::IsKeyDown(Juliet::ScanCode::Space))
|
||||
{
|
||||
currentCam->Position = currentCam->Position + defaultUp * (speedPerFrame * dt);
|
||||
}
|
||||
if (Juliet::IsKeyDown(Juliet::ScanCode::LeftControl))
|
||||
{
|
||||
currentCam->Position = currentCam->Position - defaultUp * (speedPerFrame * dt);
|
||||
}
|
||||
|
||||
currentCam->Target = currentCam->Position + forward;
|
||||
currentCam->Up = defaultUp;
|
||||
}
|
||||
|
||||
#if JULIET_DEBUG
|
||||
void RenderImGuiDebugController(float dt)
|
||||
{
|
||||
Juliet::Camera* currentCam = Juliet::GetCurrentCamera();
|
||||
|
||||
ImGui::Text("Delta time: %f", dt);
|
||||
|
||||
ImGui::Text("Camera:");
|
||||
ImGui::Indent();
|
||||
ImGui::BulletText("Index: %zu", (size_t)currentCam->Index);
|
||||
ImGui::BulletText("Position: (%.3f, %.3f, %.3f)", currentCam->Position.x, currentCam->Position.y,
|
||||
currentCam->Position.z);
|
||||
ImGui::BulletText("Target: (%.3f, %.3f, %.3f)", currentCam->Target.x, currentCam->Target.y, currentCam->Target.z);
|
||||
ImGui::BulletText("Up: (%.3f, %.3f, %.3f)", currentCam->Up.x, currentCam->Up.y, currentCam->Up.z);
|
||||
ImGui::BulletText("FOV: %.3f rad", currentCam->FOV);
|
||||
ImGui::BulletText("AspectRatio: %.3f", currentCam->AspectRatio);
|
||||
ImGui::BulletText("NearPlane: %.3f", currentCam->NearPlane);
|
||||
ImGui::BulletText("FarPlane: %.3f", currentCam->FarPlane);
|
||||
ImGui::BulletText("Pitch: %.3f, Yaw: %.3f", gPitch, gYaw);
|
||||
ImGui::BulletText("FPS Mode: %s", gIsFpsModeActive ? "Active" : "Inactive");
|
||||
ImGui::Unindent();
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user