164 lines
5.4 KiB
C
164 lines
5.4 KiB
C
#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 <Core/Memory/MemoryArena.h>
|
|
#include <Graphics/Camera.h>
|
|
#include <dcimgui.h>
|
|
#include <math.h>
|
|
|
|
global bool8 gIsDebugCameraActive = false;
|
|
global index_t gPreviousCameraIndex = 0;
|
|
global float gPitch = 0.0f;
|
|
global float gYaw = 0.0f;
|
|
global bool8 gFirstUpdate = true;
|
|
global bool8 gIsFpsModeActive = false;
|
|
global bool8 gWasRightMouseButtonDown = false;
|
|
|
|
void ActivateDebugController(void)
|
|
{
|
|
Assert(gIsDebugCameraActive == false);
|
|
|
|
Camera* currentCam = GetCurrentCamera();
|
|
gPreviousCameraIndex = currentCam->Index;
|
|
|
|
SetCurrentCamera(kDebugCamera);
|
|
|
|
gIsDebugCameraActive = true;
|
|
gFirstUpdate = true;
|
|
gIsFpsModeActive = false;
|
|
gWasRightMouseButtonDown = false;
|
|
}
|
|
|
|
void DeactivateDebugController(void)
|
|
{
|
|
Assert(gIsDebugCameraActive);
|
|
|
|
gIsDebugCameraActive = false;
|
|
|
|
SetCurrentCamera(gPreviousCameraIndex);
|
|
}
|
|
|
|
bool8 IsDebugControllerActive(void)
|
|
{
|
|
return gIsDebugCameraActive;
|
|
}
|
|
|
|
void UpdateDebugController(float dt)
|
|
{
|
|
Camera* currentCam = GetCurrentCamera();
|
|
|
|
if (gFirstUpdate)
|
|
{
|
|
Vector3 origin = { 0.0f, 0.0f, 0.0f };
|
|
Vector3 dir = Vector3Sub(origin, currentCam->Position);
|
|
dir = Normalize(dir);
|
|
gPitch = asinf(dir.z);
|
|
gYaw = atan2f(dir.y, dir.x);
|
|
gFirstUpdate = false;
|
|
|
|
Vector3 forward = { cosf(gPitch) * cosf(gYaw), cosf(gPitch) * sinf(gYaw), sinf(gPitch) };
|
|
Vector3 worldUp = { 0.0f, 0.0f, 1.0f };
|
|
Vector3 right = Normalize(Cross(worldUp, forward));
|
|
currentCam->Target = Vector3Add(currentCam->Position, forward);
|
|
currentCam->Up = Cross(forward, right);
|
|
}
|
|
|
|
bool8 isRightMouseButtonDown = IsMouseButtonDown(MouseButton_Right);
|
|
if (isRightMouseButtonDown && !gWasRightMouseButtonDown)
|
|
{
|
|
gIsFpsModeActive = !gIsFpsModeActive;
|
|
}
|
|
gWasRightMouseButtonDown = isRightMouseButtonDown;
|
|
|
|
if (gIsFpsModeActive)
|
|
{
|
|
MousePosition mouseDelta = GetMouseDelta();
|
|
|
|
float sensitivity = 0.005f;
|
|
gYaw += (float)mouseDelta.X * sensitivity;
|
|
gPitch -= (float)mouseDelta.Y * sensitivity;
|
|
|
|
gPitch = Min(gPitch, 1.5f);
|
|
gPitch = Max(gPitch, -1.5f);
|
|
|
|
if (IsKeyDown(ScanCode_Q))
|
|
{
|
|
gYaw -= 2.0f * dt;
|
|
}
|
|
if (IsKeyDown(ScanCode_E))
|
|
{
|
|
gYaw += 2.0f * dt;
|
|
}
|
|
|
|
Vector3 forward = { cosf(gPitch) * cosf(gYaw), cosf(gPitch) * sinf(gYaw), sinf(gPitch) };
|
|
Vector3 worldUp = { 0.0f, 0.0f, 1.0f };
|
|
Vector3 right = Normalize(Cross(worldUp, forward));
|
|
Vector3 defaultUp = Cross(forward, right);
|
|
|
|
const float kMovementPerFrame = 10.f; // 10m/s
|
|
|
|
float speedPerFrame = kMovementPerFrame;
|
|
if (IsKeyDown(ScanCode_LeftShift))
|
|
{
|
|
speedPerFrame *= 10.f; // 100m/s
|
|
}
|
|
|
|
if (IsKeyDown(ScanCode_W))
|
|
{
|
|
currentCam->Position = Vector3Add(currentCam->Position, Vector3Scale(forward, speedPerFrame * dt));
|
|
}
|
|
if (IsKeyDown(ScanCode_S))
|
|
{
|
|
currentCam->Position = Vector3Sub(currentCam->Position, Vector3Scale(forward, speedPerFrame * dt));
|
|
}
|
|
if (IsKeyDown(ScanCode_D))
|
|
{
|
|
currentCam->Position = Vector3Add(currentCam->Position, Vector3Scale(right, speedPerFrame * dt));
|
|
}
|
|
if (IsKeyDown(ScanCode_A))
|
|
{
|
|
currentCam->Position = Vector3Sub(currentCam->Position, Vector3Scale(right, speedPerFrame * dt));
|
|
}
|
|
if (IsKeyDown(ScanCode_Space))
|
|
{
|
|
currentCam->Position = Vector3Add(currentCam->Position, Vector3Scale(defaultUp, speedPerFrame * dt));
|
|
}
|
|
if (IsKeyDown(ScanCode_LeftControl))
|
|
{
|
|
currentCam->Position = Vector3Sub(currentCam->Position, Vector3Scale(defaultUp, speedPerFrame * dt));
|
|
}
|
|
|
|
currentCam->Target = Vector3Add(currentCam->Position, forward);
|
|
currentCam->Up = defaultUp;
|
|
}
|
|
}
|
|
|
|
#if JULIET_DEBUG
|
|
void RenderImGuiDebugController(float dt)
|
|
{
|
|
Camera* currentCam = GetCurrentCamera();
|
|
|
|
ImGui_Text("Delta time: %f", (double)dt);
|
|
|
|
ImGui_Text("Camera:");
|
|
ImGui_Indent();
|
|
ImGui_BulletText("Index: %zu", (size_t)currentCam->Index);
|
|
ImGui_BulletText("Position: (%.3f, %.3f, %.3f)", (double)currentCam->Position.x, (double)currentCam->Position.y,
|
|
(double)currentCam->Position.z);
|
|
ImGui_BulletText("Target: (%.3f, %.3f, %.3f)", (double)currentCam->Target.x, (double)currentCam->Target.y,
|
|
(double)currentCam->Target.z);
|
|
ImGui_BulletText("Up: (%.3f, %.3f, %.3f)", (double)currentCam->Up.x, (double)currentCam->Up.y,
|
|
(double)currentCam->Up.z);
|
|
ImGui_BulletText("FOV: %.3f rad", (double)currentCam->FOV);
|
|
ImGui_BulletText("AspectRatio: %.3f", (double)currentCam->AspectRatio);
|
|
ImGui_BulletText("NearPlane: %.3f", (double)currentCam->NearPlane);
|
|
ImGui_BulletText("FarPlane: %.3f", (double)currentCam->FarPlane);
|
|
ImGui_BulletText("Pitch: %.3f, Yaw: %.3f", (double)gPitch, (double)gYaw);
|
|
ImGui_BulletText("FPS Mode: %s", gIsFpsModeActive ? "Active" : "Inactive");
|
|
ImGui_Unindent();
|
|
}
|
|
#endif
|