camera stuff
This commit is contained in:
@@ -9,9 +9,17 @@ enum class PerfCounterType
|
|||||||
{
|
{
|
||||||
WindowEvents,
|
WindowEvents,
|
||||||
GameDelta,
|
GameDelta,
|
||||||
|
GameLevelUpdate,
|
||||||
|
Submit,
|
||||||
COUNT
|
COUNT
|
||||||
};
|
};
|
||||||
|
|
||||||
|
constexpr const char* PerfCounterNames[(int32_t)PerfCounterType::COUNT]{
|
||||||
|
"WindowEvt",
|
||||||
|
"Delta",
|
||||||
|
"Level",
|
||||||
|
"Submit",
|
||||||
|
};
|
||||||
struct PerfCounter
|
struct PerfCounter
|
||||||
{
|
{
|
||||||
static constexpr int32_t TimeWindow = 128;
|
static constexpr int32_t TimeWindow = 128;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "SDL3/SDL_events.h"
|
#include "SDL3/SDL_events.h"
|
||||||
|
#include "SDL3/SDL_mouse.h"
|
||||||
#include "Shared.h"
|
#include "Shared.h"
|
||||||
#include "Window.h"
|
#include "Window.h"
|
||||||
#include "bx/timer.h"
|
#include "bx/timer.h"
|
||||||
@@ -27,6 +28,7 @@ void EngineWindow::Startup(SharedWindowData& shared)
|
|||||||
printf("Failed to get window pointer!\n");
|
printf("Failed to get window pointer!\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
SDL_SetWindowRelativeMouseMode(Window, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EngineWindow::Update(SharedWindowData& shared)
|
void EngineWindow::Update(SharedWindowData& shared)
|
||||||
@@ -59,8 +61,6 @@ void EngineWindow::Update(SharedWindowData& shared)
|
|||||||
}
|
}
|
||||||
case SDL_EVENT_MOUSE_MOTION:
|
case SDL_EVENT_MOUSE_MOTION:
|
||||||
{
|
{
|
||||||
shared.MouseDeltaX += evt.motion.xrel;
|
|
||||||
shared.MouseDeltaY += evt.motion.yrel;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
||||||
@@ -77,6 +77,8 @@ void EngineWindow::Update(SharedWindowData& shared)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
SDL_GetRelativeMouseState(&shared.MouseDeltaX, &shared.MouseDeltaY);
|
||||||
|
|
||||||
END_PERF(shared.PerfCounters, PerfCounterType::WindowEvents, shared.FrameCounter);
|
END_PERF(shared.PerfCounters, PerfCounterType::WindowEvents, shared.FrameCounter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "bx/math.h"
|
#include "bx/math.h"
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <minwindef.h>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
@@ -33,11 +34,8 @@ void Transform::Translate(bx::Vec3 offset)
|
|||||||
|
|
||||||
void Transform::TranslateLocal(bx::Vec3 offset)
|
void Transform::TranslateLocal(bx::Vec3 offset)
|
||||||
{
|
{
|
||||||
UpdateMatrix();
|
bx::Vec3 localOffset = GlobalToLocalDirection(offset);
|
||||||
float offsetPtr[4]{offset.x, offset.y, offset.z, 0.0f};
|
Position = bx::add(Position, localOffset);
|
||||||
float localOffset[4]{0.0f};
|
|
||||||
bx::vec4MulMtx(localOffset, offsetPtr, MI.M);
|
|
||||||
Position = bx::add(Position, {localOffset[0], localOffset[1], localOffset[2]});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transform::Rotate(bx::Vec3 rotation)
|
void Transform::Rotate(bx::Vec3 rotation)
|
||||||
@@ -122,3 +120,35 @@ namespace Game
|
|||||||
return reinterpret_cast<void*>(ptrAligned);
|
return reinterpret_cast<void*>(ptrAligned);
|
||||||
}
|
}
|
||||||
} // namespace Game
|
} // namespace Game
|
||||||
|
bx::Vec3 Transform::GlobalToLocalDirection(bx::Vec3 global)
|
||||||
|
{
|
||||||
|
UpdateMatrix();
|
||||||
|
float in[4]{global.x, global.y, global.z, 0.0f};
|
||||||
|
float out[4]{0.0f};
|
||||||
|
bx::vec4MulMtx(out, in, MI.M);
|
||||||
|
return {out[0], out[1], out[2]};
|
||||||
|
}
|
||||||
|
bx::Vec3 Transform::GlobalToLocalPoint(bx::Vec3 global)
|
||||||
|
{
|
||||||
|
UpdateMatrix();
|
||||||
|
float in[4]{global.x, global.y, global.z, 1.0f};
|
||||||
|
float out[4]{0.0f};
|
||||||
|
bx::vec4MulMtx(out, in, MI.M);
|
||||||
|
return {out[0], out[1], out[2]};
|
||||||
|
}
|
||||||
|
bx::Vec3 Transform::LocalToGlobalDirection(bx::Vec3 local)
|
||||||
|
{
|
||||||
|
UpdateMatrix();
|
||||||
|
float in[4]{local.x, local.y, local.z, 0.0f};
|
||||||
|
float out[4]{0.0f};
|
||||||
|
bx::vec4MulMtx(out, in, M.M);
|
||||||
|
return {out[0], out[1], out[2]};
|
||||||
|
}
|
||||||
|
bx::Vec3 Transform::LocalToGlobalPoint(bx::Vec3 local)
|
||||||
|
{
|
||||||
|
UpdateMatrix();
|
||||||
|
float in[4]{local.x, local.y, local.z, 1.0f};
|
||||||
|
float out[4]{0.0f};
|
||||||
|
bx::vec4MulMtx(out, in, M.M);
|
||||||
|
return {out[0], out[1], out[2]};
|
||||||
|
}
|
||||||
|
|||||||
@@ -59,6 +59,10 @@ struct Transform
|
|||||||
void TranslateLocal(bx::Vec3 offset);
|
void TranslateLocal(bx::Vec3 offset);
|
||||||
void Rotate(bx::Vec3 rotation);
|
void Rotate(bx::Vec3 rotation);
|
||||||
void RotateLocal(bx::Vec3 rotation);
|
void RotateLocal(bx::Vec3 rotation);
|
||||||
|
bx::Vec3 LocalToGlobalPoint(bx::Vec3 local);
|
||||||
|
bx::Vec3 LocalToGlobalDirection(bx::Vec3 local);
|
||||||
|
bx::Vec3 GlobalToLocalPoint(bx::Vec3 global);
|
||||||
|
bx::Vec3 GlobalToLocalDirection(bx::Vec3 global);
|
||||||
bx::Vec3 Right() const;
|
bx::Vec3 Right() const;
|
||||||
bx::Vec3 Up() const;
|
bx::Vec3 Up() const;
|
||||||
bx::Vec3 Forward() const;
|
bx::Vec3 Forward() const;
|
||||||
|
|||||||
@@ -25,7 +25,8 @@ namespace Game
|
|||||||
Transform FreeflyCamTransform;
|
Transform FreeflyCamTransform;
|
||||||
float FreeflyXRot = 0.0f;
|
float FreeflyXRot = 0.0f;
|
||||||
float FreeflyYRot = 0.0f;
|
float FreeflyYRot = 0.0f;
|
||||||
Transform PlayerTransform;
|
float WalkXRot = 0.0f;
|
||||||
|
float WalkYRot = 0.0f;
|
||||||
Transform PlayerCamTransform;
|
Transform PlayerCamTransform;
|
||||||
PlayerMode Mode = PlayerMode::Freefly;
|
PlayerMode Mode = PlayerMode::Freefly;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -67,9 +67,11 @@ namespace Game
|
|||||||
|
|
||||||
void Level::Update()
|
void Level::Update()
|
||||||
{
|
{
|
||||||
|
START_PERF();
|
||||||
PlayerData& player = GetInstance().Player;
|
PlayerData& player = GetInstance().Player;
|
||||||
|
|
||||||
float delta = GetInstance().Time.Delta;
|
float delta = GetInstance().Time.Delta;
|
||||||
|
delta = 1.0f / 144.0f;
|
||||||
constexpr float moveSpeed = 10.0f;
|
constexpr float moveSpeed = 10.0f;
|
||||||
constexpr float rotSpeed = 0.6f;
|
constexpr float rotSpeed = 0.6f;
|
||||||
|
|
||||||
@@ -78,14 +80,18 @@ namespace Game
|
|||||||
bx::Vec3 moveInput = bx::Vec3{rightInput, forwardInput, 0.0f};
|
bx::Vec3 moveInput = bx::Vec3{rightInput, forwardInput, 0.0f};
|
||||||
moveInput = bx::normalize(moveInput);
|
moveInput = bx::normalize(moveInput);
|
||||||
bx::Vec3 inputVec = {moveInput.x * delta * moveSpeed, 0.0f, moveInput.y * delta * moveSpeed};
|
bx::Vec3 inputVec = {moveInput.x * delta * moveSpeed, 0.0f, moveInput.y * delta * moveSpeed};
|
||||||
|
|
||||||
bx::Vec3 camForward = player.FreeflyCamTransform.Forward();
|
|
||||||
bx::Vec3 camRight = player.FreeflyCamTransform.Right();
|
|
||||||
|
|
||||||
if (GetMouseButton(MouseButton::Left))
|
|
||||||
{
|
|
||||||
Vec2 mouseMovement = GetMouseMovement();
|
Vec2 mouseMovement = GetMouseMovement();
|
||||||
bx::Vec3 rotInput = {mouseMovement.y * delta * rotSpeed, mouseMovement.x * delta * rotSpeed, 0.0f};
|
bx::Vec3 rotInput = {mouseMovement.y * delta * rotSpeed, mouseMovement.x * delta * rotSpeed, 0.0f};
|
||||||
|
|
||||||
|
if (GetKeyPressedNow(ScanCode::F1))
|
||||||
|
{
|
||||||
|
player.Mode = player.Mode == PlayerMode::Walk ? PlayerMode::Freefly : PlayerMode::Walk;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (player.Mode == PlayerMode::Freefly)
|
||||||
|
{
|
||||||
|
if (GetMouseButton(MouseButton::Left))
|
||||||
|
{
|
||||||
player.FreeflyXRot += rotInput.x;
|
player.FreeflyXRot += rotInput.x;
|
||||||
player.FreeflyYRot += rotInput.y;
|
player.FreeflyYRot += rotInput.y;
|
||||||
bx::mtxRotateY(player.FreeflyCamTransform.Rotation.M, player.FreeflyYRot);
|
bx::mtxRotateY(player.FreeflyCamTransform.Rotation.M, player.FreeflyYRot);
|
||||||
@@ -94,9 +100,22 @@ namespace Game
|
|||||||
|
|
||||||
player.FreeflyCamTransform.TranslateLocal({0.0f, 0.0f, -inputVec.z});
|
player.FreeflyCamTransform.TranslateLocal({0.0f, 0.0f, -inputVec.z});
|
||||||
player.FreeflyCamTransform.TranslateLocal({-inputVec.x, 0.0f, 0.0f});
|
player.FreeflyCamTransform.TranslateLocal({-inputVec.x, 0.0f, 0.0f});
|
||||||
|
}
|
||||||
|
else if (player.Mode == PlayerMode::Walk)
|
||||||
|
{
|
||||||
|
player.PlayerCamTransform.TranslateLocal({0.0f, 0.0f, -inputVec.z});
|
||||||
|
player.PlayerCamTransform.TranslateLocal({-inputVec.x, 0.0f, 0.0f});
|
||||||
|
player.PlayerCamTransform.Position.y = -3.0f;
|
||||||
|
|
||||||
|
player.WalkXRot += rotInput.x;
|
||||||
|
player.WalkYRot += rotInput.y;
|
||||||
|
bx::mtxRotateY(player.PlayerCamTransform.Rotation.M, player.WalkYRot);
|
||||||
|
player.PlayerCamTransform.RotateLocal({player.WalkXRot, 0.0f, 0.0f});
|
||||||
|
}
|
||||||
|
|
||||||
Cubes.Update();
|
Cubes.Update();
|
||||||
Tests.Update();
|
Tests.Update();
|
||||||
|
END_PERF(GetShared().Window.PerfCounters, PerfCounterType::GameLevelUpdate, GetShared().Window.FrameCounter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cube::Setup()
|
void Cube::Setup()
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ namespace Game
|
|||||||
Log("Game instance size changed, resetting!");
|
Log("Game instance size changed, resetting!");
|
||||||
instance = {};
|
instance = {};
|
||||||
}
|
}
|
||||||
|
instance.UsedScratchAmount = 0;
|
||||||
SetShared(shared);
|
SetShared(shared);
|
||||||
SetInstance(instance);
|
SetInstance(instance);
|
||||||
SetupInstance.Rendering.Setup();
|
SetupInstance.Rendering.Setup();
|
||||||
@@ -55,7 +56,7 @@ namespace Game
|
|||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
auto& inst = GetInstance();
|
auto& inst = GetInstance();
|
||||||
int64_t newNowHP = bx::getHPCounter() - GetInstance().Time.StartTime;
|
int64_t newNowHP = bx::getHPCounter() - inst.Time.StartTime;
|
||||||
inst.Time.DeltaHP = newNowHP - inst.Time.NowHP;
|
inst.Time.DeltaHP = newNowHP - inst.Time.NowHP;
|
||||||
inst.Time.NowHP = newNowHP;
|
inst.Time.NowHP = newNowHP;
|
||||||
inst.Time.Now = (double)inst.Time.NowHP / bx::getHPFrequency();
|
inst.Time.Now = (double)inst.Time.NowHP / bx::getHPFrequency();
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -280,7 +280,8 @@ namespace Game
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// TODO
|
player.PlayerCamTransform.UpdateMatrix();
|
||||||
|
bgfx::setViewTransform(0, player.PlayerCamTransform.M.M, proj);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set view 0 default viewport.
|
// Set view 0 default viewport.
|
||||||
@@ -291,15 +292,15 @@ namespace Game
|
|||||||
GetInstance().GameLevel.Tests.Render(Models, Materials);
|
GetInstance().GameLevel.Tests.Render(Models, Materials);
|
||||||
|
|
||||||
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1fs", GetInstance().Time.Now);
|
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1fs", GetInstance().Time.Now);
|
||||||
bgfx::dbgTextPrintf(1,
|
for (int32_t i = 0; i < (int32_t)PerfCounterType::COUNT; ++i)
|
||||||
2,
|
{
|
||||||
0x0F,
|
|
||||||
"Window Events: %.3fms",
|
|
||||||
GetShared().Window.PerfCounters[(int32_t)PerfCounterType::WindowEvents].GetMax());
|
|
||||||
bgfx::dbgTextPrintf(
|
bgfx::dbgTextPrintf(
|
||||||
1, 3, 0x0F, "Delta: %.3fms", GetShared().Window.PerfCounters[(int32_t)PerfCounterType::GameDelta].GetMax());
|
1, 2 + i, 0x0F, "%s Max: %.3fs", PerfCounterNames[i], shared.Window.PerfCounters[i].GetMax());
|
||||||
|
}
|
||||||
|
|
||||||
|
START_PERF();
|
||||||
bgfx::frame();
|
bgfx::frame();
|
||||||
|
END_PERF(shared.Window.PerfCounters, PerfCounterType::Submit, shared.Window.FrameCounter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameRendering::Shutdown()
|
void GameRendering::Shutdown()
|
||||||
|
|||||||
Reference in New Issue
Block a user