Added camera turning

This commit is contained in:
maverickdgg 2022-10-20 03:21:42 +08:00
parent c23da81f68
commit 736d7b652b
4 changed files with 67 additions and 71 deletions

View File

@ -1,16 +1,16 @@
[Window][MainStatusBar] [Window][MainStatusBar]
Pos=0,1389 Pos=0,1060
Size=2547,20 Size=1920,20
Collapsed=0 Collapsed=0
[Window][SHEditorMenuBar] [Window][SHEditorMenuBar]
Pos=0,48 Pos=0,48
Size=2547,1341 Size=1920,1012
Collapsed=0 Collapsed=0
[Window][Hierarchy Panel] [Window][Hierarchy Panel]
Pos=0,172 Pos=0,142
Size=571,1217 Size=571,918
Collapsed=0 Collapsed=0
DockId=0x00000004,0 DockId=0x00000004,0
@ -20,25 +20,25 @@ Size=400,400
Collapsed=0 Collapsed=0
[Window][Inspector] [Window][Inspector]
Pos=2276,48 Pos=1649,48
Size=271,1341 Size=271,1012
Collapsed=0 Collapsed=0
DockId=0x00000006,0 DockId=0x00000006,0
[Window][Profiler] [Window][Profiler]
Pos=0,48 Pos=0,48
Size=571,122 Size=571,92
Collapsed=0 Collapsed=0
DockId=0x00000003,0 DockId=0x00000003,0
[Window][Viewport] [Window][Viewport]
Pos=573,48 Pos=573,48
Size=1701,1341 Size=1074,1012
Collapsed=0 Collapsed=0
DockId=0x00000002,0 DockId=0x00000002,0
[Docking][Data] [Docking][Data]
DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=8,79 Size=2547,1341 Split=X DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=8,79 Size=1920,1012 Split=X
DockNode ID=0x00000005 Parent=0xC5C9B8AB SizeRef=1992,1036 Split=X DockNode ID=0x00000005 Parent=0xC5C9B8AB SizeRef=1992,1036 Split=X
DockNode ID=0x00000001 Parent=0x00000005 SizeRef=571,1036 Split=Y Selected=0x1E6EB881 DockNode ID=0x00000001 Parent=0x00000005 SizeRef=571,1036 Split=Y Selected=0x1E6EB881
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=225,94 Selected=0x1E6EB881 DockNode ID=0x00000003 Parent=0x00000001 SizeRef=225,94 Selected=0x1E6EB881

View File

@ -7,7 +7,7 @@ namespace SHADE
{ {
SHCameraComponent::SHCameraComponent() SHCameraComponent::SHCameraComponent()
:yaw(0.0f), pitch(0.0f), roll(0.0f) :yaw(0.0f), pitch(0.0f), roll(0.0f)
, width(1920.0f), height(1080.0f), zNear(0.01f), zFar(10000.0f), fov(90.0f), movementSpeed(1.0f), turnSpeed(1.0f) , width(1920.0f), height(1080.0f), zNear(0.01f), zFar(10000.0f), fov(90.0f), movementSpeed(1.0f), turnSpeed(0.5f)
, perspProj(true), dirtyView(true), dirtyProj(true) , perspProj(true), dirtyView(true), dirtyProj(true)
, viewMatrix(), projMatrix() , viewMatrix(), projMatrix()
, position() , position()

View File

@ -2,6 +2,7 @@
#include "SHCameraSystem.h" #include "SHCameraSystem.h"
#include "Math/SHMathHelpers.h" #include "Math/SHMathHelpers.h"
#include "Input/SHInputManager.h" #include "Input/SHInputManager.h"
#include "Math/Vector/SHVec2.h"
@ -12,25 +13,8 @@ namespace SHADE
{ {
SHCameraSystem* system = static_cast<SHCameraSystem*>(GetSystem()); SHCameraSystem* system = static_cast<SHCameraSystem*>(GetSystem());
auto& camera = system->editorCamera; auto& camera = system->editorCamera;
SHVec3 target{ 0.0f,0.0f,-1.0f }; SHVec3 view, right, UP;
SHVec3 up = { 0.0f,1.0f,0.0f }; system->GetCameraAxis(camera, view, right, UP);
SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw));
SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch));
target += camera.position;
////SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll));
//target = SHVec3::Normalise(target);
SHVec3::RotateZ(up, camera.roll);
up = SHVec3::Normalise(up);
SHVec3 view = target - camera.position; view = SHVec3::Normalise(view);
SHVec3 right = SHVec3::Cross(view, up); right = SHVec3::Normalise(right);
const SHVec3 UP = SHVec3::Cross(view, right);
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::A)) if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::A))
{ {
@ -62,6 +46,18 @@ namespace SHADE
system->editorCamera.position -= UP * dt * camera.movementSpeed; system->editorCamera.position -= UP * dt * camera.movementSpeed;
system->editorCamera.dirtyView = true; system->editorCamera.dirtyView = true;
} }
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::RMB))
{
double mouseX, mouseY;
SHInputManager::GetMouseVelocity(&mouseX,&mouseY);
//std::cout << camera.yaw << std::endl;
system->editorCamera.pitch -= mouseY * dt * camera.turnSpeed.x;
system->editorCamera.yaw -= mouseX * dt * camera.turnSpeed.y;
system->editorCamera.dirtyView = true;
}
system->UpdateCameraComponent(system->editorCamera); system->UpdateCameraComponent(system->editorCamera);
} }
@ -89,24 +85,9 @@ namespace SHADE
{ {
if (camera.dirtyView) if (camera.dirtyView)
{ {
SHVec3 target{ 0.0f,0.0f,-1.0f };
SHVec3 up = { 0.0f,1.0f,0.0f };
SHVec3 view, right, UP;
SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw)); GetCameraAxis(camera, view, right, UP);
SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch));
target += camera.position;
////SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll));
//target = SHVec3::Normalise(target);
SHVec3::RotateZ(up, camera.roll);
up = SHVec3::Normalise(up);
SHVec3 view = target - camera.position; view = SHVec3::Normalise(view);
SHVec3 right = SHVec3::Cross(view, up); right = SHVec3::Normalise(right);
const SHVec3 UP = SHVec3::Cross(view, right);
camera.viewMatrix = SHMatrix::Identity; camera.viewMatrix = SHMatrix::Identity;
camera.viewMatrix(0, 0) = right[0]; camera.viewMatrix(0, 0) = right[0];
@ -142,38 +123,51 @@ namespace SHADE
camera.projMatrix(3, 2) = 1.0f; camera.projMatrix(3, 2) = 1.0f;
camera.projMatrix(2, 3) = -(camera.zFar * camera.zNear) / (camera.zFar - camera.zNear); camera.projMatrix(2, 3) = -(camera.zFar * camera.zNear) / (camera.zFar - camera.zNear);
//const float fov_rad = SHMath::DegreesToRadians(camera.fov);
//const float focal_length = 1.0f / tan(fov_rad * 0.5f);
//camera.projMatrix(0,0) = focal_length / camera.GetAspectRatio();
//camera.projMatrix(1,1) = -focal_length;
//camera.projMatrix(2,2) = camera.zNear / (camera.zFar - camera.zNear);
//camera.projMatrix(2,3) = camera.zFar * (camera.zNear / (camera.zFar - camera.zNear));
//camera.projMatrix(3,2) = -1.0f;
//camera.projMatrix(3,3) = 0.0f;
//camera.projMatrix = SHMatrix::Inverse(camera.projMatrix);
camera.dirtyProj = false; camera.dirtyProj = false;
} }
else else
{ {
const float R = camera.width * 0.5f; //const float R = camera.width * 0.5f;
const float L = -R; //const float L = -R;
const float T = camera.height * 0.5f; //const float T = camera.height * 0.5f;
const float B = -T; //const float B = -T;
camera.projMatrix = SHMatrix::Identity; //camera.projMatrix = SHMatrix::Identity;
camera.projMatrix(0, 0) = 2.0f / (R - L); //camera.projMatrix(0, 0) = 2.0f / (R - L);
camera.projMatrix(1, 1) = 2.0f / (B - T); //camera.projMatrix(1, 1) = 2.0f / (B - T);
camera.projMatrix(2, 2) = 1.0f / (camera.zFar - camera.zNear); //camera.projMatrix(2, 2) = 1.0f / (camera.zFar - camera.zNear);
camera.projMatrix(3, 0) = -(R + L) / (R - L); //camera.projMatrix(3, 0) = -(R + L) / (R - L);
camera.projMatrix(3, 1) = -(B + T) / (B - T); //camera.projMatrix(3, 1) = -(B + T) / (B - T);
camera.projMatrix(3, 2) = -camera.zNear / (camera.zFar - camera.zNear); //camera.projMatrix(3, 2) = -camera.zNear / (camera.zFar - camera.zNear);
camera.dirtyProj = false; camera.dirtyProj = false;
} }
} }
} }
void SHCameraSystem::GetCameraAxis(SHCameraComponent const& camera, SHVec3& forward, SHVec3& right, SHVec3& upVec) const noexcept
{
SHVec3 target{ 0.0f,0.0f,-1.0f };
SHVec3 up = { 0.0f,1.0f,0.0f };
target = SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw));
target =SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch));
std::cout << "Target vec: " << target.x<<", "<<target.y<<", "<<target.z << std::endl;
target += camera.position;
////SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll));
//target = SHVec3::Normalise(target);
SHVec3::RotateZ(up, camera.roll);
up = SHVec3::Normalise(up);
forward = target - camera.position; forward = SHVec3::Normalise(forward);
right = SHVec3::Cross(forward, up); right = SHVec3::Normalise(right);
upVec = SHVec3::Cross(forward, right);
}
} }

View File

@ -40,6 +40,8 @@ namespace SHADE
void UpdateCameraComponent(SHCameraComponent& camera) noexcept; void UpdateCameraComponent(SHCameraComponent& camera) noexcept;
void GetCameraAxis(SHCameraComponent const& camera, SHVec3& forward, SHVec3& right, SHVec3& up) const noexcept;
}; };