From d9136de134b772deaec6558cc8a14efac659bb58 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Wed, 19 Oct 2022 16:53:45 +0800 Subject: [PATCH] Integrated Daniel's camera Still need to integrate camera directors --- Assets/Editor/Layouts/UserLayout.ini | 14 ++-- SHADE_Engine/src/Camera/SHCameraComponent.cpp | 2 +- SHADE_Engine/src/Camera/SHCameraSystem.cpp | 65 ++++++++++++------- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 5 +- .../MiddleEnd/Interface/SHRenderer.cpp | 4 +- 5 files changed, 52 insertions(+), 38 deletions(-) diff --git a/Assets/Editor/Layouts/UserLayout.ini b/Assets/Editor/Layouts/UserLayout.ini index fea3559a..ded63438 100644 --- a/Assets/Editor/Layouts/UserLayout.ini +++ b/Assets/Editor/Layouts/UserLayout.ini @@ -10,7 +10,7 @@ Collapsed=0 [Window][Hierarchy Panel] Pos=0,142 -Size=381,918 +Size=650,918 Collapsed=0 DockId=0x00000004,0 @@ -27,22 +27,22 @@ DockId=0x00000006,0 [Window][Profiler] Pos=0,48 -Size=381,92 +Size=650,92 Collapsed=0 DockId=0x00000003,0 [Window][Viewport] -Pos=383,48 -Size=1264,1012 +Pos=652,48 +Size=995,1012 Collapsed=0 DockId=0x00000002,0 [Docking][Data] -DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=90,200 Size=1920,1012 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=0x00000001 Parent=0x00000005 SizeRef=381,1036 Split=Y Selected=0x1E6EB881 + DockNode ID=0x00000001 Parent=0x00000005 SizeRef=650,1036 Split=Y Selected=0x1E6EB881 DockNode ID=0x00000003 Parent=0x00000001 SizeRef=225,94 Selected=0x1E6EB881 DockNode ID=0x00000004 Parent=0x00000001 SizeRef=225,940 Selected=0xE096E5AE - DockNode ID=0x00000002 Parent=0x00000005 SizeRef=1264,1036 CentralNode=1 Selected=0x13926F0B + DockNode ID=0x00000002 Parent=0x00000005 SizeRef=995,1036 CentralNode=1 Selected=0x13926F0B DockNode ID=0x00000006 Parent=0xC5C9B8AB SizeRef=271,1036 Selected=0xE7039252 diff --git a/SHADE_Engine/src/Camera/SHCameraComponent.cpp b/SHADE_Engine/src/Camera/SHCameraComponent.cpp index d4078622..650ed3c5 100644 --- a/SHADE_Engine/src/Camera/SHCameraComponent.cpp +++ b/SHADE_Engine/src/Camera/SHCameraComponent.cpp @@ -8,7 +8,7 @@ namespace SHADE SHCameraComponent::SHCameraComponent() :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) - , perspProj(false), dirtyView(true), dirtyProj(true) + , perspProj(true), dirtyView(true), dirtyProj(true) , viewMatrix(), projMatrix() , position() { diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.cpp b/SHADE_Engine/src/Camera/SHCameraSystem.cpp index 907dfa4f..86fd9f80 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.cpp +++ b/SHADE_Engine/src/Camera/SHCameraSystem.cpp @@ -11,29 +11,30 @@ namespace SHADE void SHCameraSystem::EditorCameraUpdate::Execute(double dt) noexcept { SHCameraSystem* system = static_cast(GetSystem()); - - SHVec3 target{ 0.0f,0.0f,-1.0f }; - SHVec3 up = { 0.0f,1.0f,0.0f }; + auto& camera = system->editorCamera; + SHVec3 target{ 0.0f,0.0f,-1.0f }; + SHVec3 up = { 0.0f,1.0f,0.0f }; - auto& camera = system->editorCamera; - SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw)); - SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch)); - //SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll)); + target += camera.position; - target = SHVec3::Normalise(target); + //SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw)); + //SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch)); + ////SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll)); - SHVec3::RotateZ(up, camera.roll); - up = SHVec3::Normalise(up); + //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); + 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)) { - system->editorCamera.position -= SHVec3(1.0,0.0,0.0) * dt * camera.movementSpeed; + system->editorCamera.position -= right * dt * camera.movementSpeed; system->editorCamera.dirtyView = true; } if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::D)) @@ -70,7 +71,7 @@ namespace SHADE editorCamera.SetPitch(0.0f); editorCamera.SetYaw(0.0f); editorCamera.SetRoll(0.0f); - //editorCamera.movementSpeed = 100.0f; + editorCamera.movementSpeed = 2.0f; } @@ -91,13 +92,15 @@ namespace SHADE SHVec3 target{ 0.0f,0.0f,-1.0f }; SHVec3 up = { 0.0f,1.0f,0.0f }; - SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw)); - SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch)); - //SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll)); + target += camera.position; - target = SHVec3::Normalise(target); + //SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw)); + //SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch)); + ////SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll)); - SHVec3::RotateZ(up, camera.roll); + //target = SHVec3::Normalise(target); + + //SHVec3::RotateZ(up, camera.roll); up = SHVec3::Normalise(up); @@ -128,14 +131,28 @@ namespace SHADE { if (camera.perspProj == true) { - const float ASPECT_RATIO = camera.GetAspectRatio(); - const float TAN_HALF_FOV = tan(camera.fov * 0.5f); + const float ASPECT_RATIO = (camera.GetAspectRatio()); + const float TAN_HALF_FOV = tan(SHMath::DegreesToRadians(camera.fov) * 0.5f); camera.projMatrix = SHMatrix::Identity; camera.projMatrix(0, 0) = 1.0f / (ASPECT_RATIO * TAN_HALF_FOV); camera.projMatrix(1, 1) = 1.0f / TAN_HALF_FOV; camera.projMatrix(2, 2) = camera.zFar / (camera.zFar - camera.zNear); - camera.projMatrix(2, 3) = 1.0f; - camera.projMatrix(3, 2) = -(camera.zFar * camera.zNear) / (camera.zFar - camera.zNear); + camera.projMatrix(3, 3) = 0.0f; + + camera.projMatrix(3, 2) = 1.0f; + 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; } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 90c46ada..d1dc5201 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -328,10 +328,7 @@ namespace SHADE auto editorSystem = SHSystemManager::GetSystem(); if (editorSystem->editorState != SHEditor::State::PLAY) { - //cameraSystem->GetEditorCamera()->GetViewMatrix(); - //cameraSystem->GetEditorCamera()->GetProjMatrix(); - worldRenderer->SetViewProjectionMatrix(cameraSystem->GetEditorCamera()->GetViewMatrix() ); - //worldRenderer->SetViewProjectionMatrix(SHMatrix::Inverse(cameraSystem->GetEditorCamera()->GetViewMatrix()/* * cameraSystem->GetEditorCamera()->GetProjMatrix()*/)); + worldRenderer->SetViewProjectionMatrix(SHMatrix::Transpose(cameraSystem->GetEditorCamera()->GetProjMatrix() * cameraSystem->GetEditorCamera()->GetViewMatrix())); } else { diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp index f2fb78f0..2532f308 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp @@ -92,8 +92,8 @@ namespace SHADE void SHRenderer::SetViewProjectionMatrix(SHMatrix const& vpMatrix) noexcept { - cpuCameraData.viewProjectionMatrix = camera->GetViewMatrix() * camera->GetProjectionMatrix(); - //cpuCameraData.viewProjectionMatrix = vpMatrix * camera->GetProjectionMatrix(); + //cpuCameraData.viewProjectionMatrix = camera->GetViewMatrix() * camera->GetProjectionMatrix(); + cpuCameraData.viewProjectionMatrix = vpMatrix; } Handle SHRenderer::GetRenderGraph(void) const noexcept