camera stuff
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include "bx/math.h"
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <minwindef.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -33,11 +34,8 @@ void Transform::Translate(bx::Vec3 offset)
|
||||
|
||||
void Transform::TranslateLocal(bx::Vec3 offset)
|
||||
{
|
||||
UpdateMatrix();
|
||||
float offsetPtr[4]{offset.x, offset.y, offset.z, 0.0f};
|
||||
float localOffset[4]{0.0f};
|
||||
bx::vec4MulMtx(localOffset, offsetPtr, MI.M);
|
||||
Position = bx::add(Position, {localOffset[0], localOffset[1], localOffset[2]});
|
||||
bx::Vec3 localOffset = GlobalToLocalDirection(offset);
|
||||
Position = bx::add(Position, localOffset);
|
||||
}
|
||||
|
||||
void Transform::Rotate(bx::Vec3 rotation)
|
||||
@@ -122,3 +120,35 @@ namespace Game
|
||||
return reinterpret_cast<void*>(ptrAligned);
|
||||
}
|
||||
} // 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 Rotate(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 Up() const;
|
||||
bx::Vec3 Forward() const;
|
||||
|
||||
@@ -25,7 +25,8 @@ namespace Game
|
||||
Transform FreeflyCamTransform;
|
||||
float FreeflyXRot = 0.0f;
|
||||
float FreeflyYRot = 0.0f;
|
||||
Transform PlayerTransform;
|
||||
float WalkXRot = 0.0f;
|
||||
float WalkYRot = 0.0f;
|
||||
Transform PlayerCamTransform;
|
||||
PlayerMode Mode = PlayerMode::Freefly;
|
||||
};
|
||||
|
||||
@@ -67,9 +67,11 @@ namespace Game
|
||||
|
||||
void Level::Update()
|
||||
{
|
||||
START_PERF();
|
||||
PlayerData& player = GetInstance().Player;
|
||||
|
||||
float delta = GetInstance().Time.Delta;
|
||||
delta = 1.0f / 144.0f;
|
||||
constexpr float moveSpeed = 10.0f;
|
||||
constexpr float rotSpeed = 0.6f;
|
||||
|
||||
@@ -78,25 +80,42 @@ namespace Game
|
||||
bx::Vec3 moveInput = bx::Vec3{rightInput, forwardInput, 0.0f};
|
||||
moveInput = bx::normalize(moveInput);
|
||||
bx::Vec3 inputVec = {moveInput.x * delta * moveSpeed, 0.0f, moveInput.y * delta * moveSpeed};
|
||||
Vec2 mouseMovement = GetMouseMovement();
|
||||
bx::Vec3 rotInput = {mouseMovement.y * delta * rotSpeed, mouseMovement.x * delta * rotSpeed, 0.0f};
|
||||
|
||||
bx::Vec3 camForward = player.FreeflyCamTransform.Forward();
|
||||
bx::Vec3 camRight = player.FreeflyCamTransform.Right();
|
||||
|
||||
if (GetMouseButton(MouseButton::Left))
|
||||
if (GetKeyPressedNow(ScanCode::F1))
|
||||
{
|
||||
Vec2 mouseMovement = GetMouseMovement();
|
||||
bx::Vec3 rotInput = {mouseMovement.y * delta * rotSpeed, mouseMovement.x * delta * rotSpeed, 0.0f};
|
||||
player.FreeflyXRot += rotInput.x;
|
||||
player.FreeflyYRot += rotInput.y;
|
||||
bx::mtxRotateY(player.FreeflyCamTransform.Rotation.M, player.FreeflyYRot);
|
||||
player.FreeflyCamTransform.RotateLocal({player.FreeflyXRot, 0.0f, 0.0f});
|
||||
player.Mode = player.Mode == PlayerMode::Walk ? PlayerMode::Freefly : PlayerMode::Walk;
|
||||
}
|
||||
|
||||
player.FreeflyCamTransform.TranslateLocal({0.0f, 0.0f, -inputVec.z});
|
||||
player.FreeflyCamTransform.TranslateLocal({-inputVec.x, 0.0f, 0.0f});
|
||||
if (player.Mode == PlayerMode::Freefly)
|
||||
{
|
||||
if (GetMouseButton(MouseButton::Left))
|
||||
{
|
||||
player.FreeflyXRot += rotInput.x;
|
||||
player.FreeflyYRot += rotInput.y;
|
||||
bx::mtxRotateY(player.FreeflyCamTransform.Rotation.M, player.FreeflyYRot);
|
||||
player.FreeflyCamTransform.RotateLocal({player.FreeflyXRot, 0.0f, 0.0f});
|
||||
}
|
||||
|
||||
player.FreeflyCamTransform.TranslateLocal({0.0f, 0.0f, -inputVec.z});
|
||||
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();
|
||||
Tests.Update();
|
||||
END_PERF(GetShared().Window.PerfCounters, PerfCounterType::GameLevelUpdate, GetShared().Window.FrameCounter);
|
||||
}
|
||||
|
||||
void Cube::Setup()
|
||||
|
||||
@@ -45,6 +45,7 @@ namespace Game
|
||||
Log("Game instance size changed, resetting!");
|
||||
instance = {};
|
||||
}
|
||||
instance.UsedScratchAmount = 0;
|
||||
SetShared(shared);
|
||||
SetInstance(instance);
|
||||
SetupInstance.Rendering.Setup();
|
||||
@@ -55,7 +56,7 @@ namespace Game
|
||||
void Update()
|
||||
{
|
||||
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.NowHP = newNowHP;
|
||||
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
|
||||
{
|
||||
// TODO
|
||||
player.PlayerCamTransform.UpdateMatrix();
|
||||
bgfx::setViewTransform(0, player.PlayerCamTransform.M.M, proj);
|
||||
}
|
||||
|
||||
// Set view 0 default viewport.
|
||||
@@ -291,15 +292,15 @@ namespace Game
|
||||
GetInstance().GameLevel.Tests.Render(Models, Materials);
|
||||
|
||||
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1fs", GetInstance().Time.Now);
|
||||
bgfx::dbgTextPrintf(1,
|
||||
2,
|
||||
0x0F,
|
||||
"Window Events: %.3fms",
|
||||
GetShared().Window.PerfCounters[(int32_t)PerfCounterType::WindowEvents].GetMax());
|
||||
bgfx::dbgTextPrintf(
|
||||
1, 3, 0x0F, "Delta: %.3fms", GetShared().Window.PerfCounters[(int32_t)PerfCounterType::GameDelta].GetMax());
|
||||
for (int32_t i = 0; i < (int32_t)PerfCounterType::COUNT; ++i)
|
||||
{
|
||||
bgfx::dbgTextPrintf(
|
||||
1, 2 + i, 0x0F, "%s Max: %.3fs", PerfCounterNames[i], shared.Window.PerfCounters[i].GetMax());
|
||||
}
|
||||
|
||||
START_PERF();
|
||||
bgfx::frame();
|
||||
END_PERF(shared.Window.PerfCounters, PerfCounterType::Submit, shared.Window.FrameCounter);
|
||||
}
|
||||
|
||||
void GameRendering::Shutdown()
|
||||
|
||||
Reference in New Issue
Block a user