From 284ee12205264708f7c99a58ae888d4bef51b628 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Wed, 19 Oct 2022 15:32:07 +0800 Subject: [PATCH 1/2] Broken Camera --- Assets/Editor/Layouts/UserLayout.ini | 14 +- .../src/Application/SBApplication.cpp | 5 + SHADE_Engine/src/Camera/SHCameraComponent.cpp | 131 ++++++++++++++ SHADE_Engine/src/Camera/SHCameraComponent.h | 85 +++++++++ SHADE_Engine/src/Camera/SHCameraSystem.cpp | 162 ++++++++++++++++++ SHADE_Engine/src/Camera/SHCameraSystem.h | 47 +++++ .../Graphics/MiddleEnd/Interface/SHCamera.cpp | 10 +- .../Graphics/MiddleEnd/Interface/SHCamera.h | 118 ++++++------- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 23 +++ .../MiddleEnd/Interface/SHRenderer.cpp | 8 +- .../Graphics/MiddleEnd/Interface/SHRenderer.h | 1 + 11 files changed, 534 insertions(+), 70 deletions(-) create mode 100644 SHADE_Engine/src/Camera/SHCameraComponent.cpp create mode 100644 SHADE_Engine/src/Camera/SHCameraComponent.h create mode 100644 SHADE_Engine/src/Camera/SHCameraSystem.cpp create mode 100644 SHADE_Engine/src/Camera/SHCameraSystem.h diff --git a/Assets/Editor/Layouts/UserLayout.ini b/Assets/Editor/Layouts/UserLayout.ini index e55aeb81..fea3559a 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=321,918 +Size=381,918 Collapsed=0 DockId=0x00000004,0 @@ -27,22 +27,22 @@ DockId=0x00000006,0 [Window][Profiler] Pos=0,48 -Size=321,92 +Size=381,92 Collapsed=0 DockId=0x00000003,0 [Window][Viewport] -Pos=323,48 -Size=1324,1012 +Pos=383,48 +Size=1264,1012 Collapsed=0 DockId=0x00000002,0 [Docking][Data] -DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=617,298 Size=1920,1012 Split=X +DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=90,200 Size=1920,1012 Split=X DockNode ID=0x00000005 Parent=0xC5C9B8AB SizeRef=1992,1036 Split=X - DockNode ID=0x00000001 Parent=0x00000005 SizeRef=321,1036 Split=Y Selected=0x1E6EB881 + DockNode ID=0x00000001 Parent=0x00000005 SizeRef=381,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=1324,1036 CentralNode=1 Selected=0x13926F0B + DockNode ID=0x00000002 Parent=0x00000005 SizeRef=1264,1036 CentralNode=1 Selected=0x13926F0B DockNode ID=0x00000006 Parent=0xC5C9B8AB SizeRef=271,1036 Selected=0xE7039252 diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 2d21be3f..8733e7b9 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -30,6 +30,7 @@ #include "Input/SHInputManager.h" #include "FRC/SHFramerateController.h" #include "AudioSystem/SHAudioSystem.h" +#include "Camera/SHCameraSystem.h" // Components #include "Graphics/MiddleEnd/Interface/SHRenderable.h" @@ -67,6 +68,7 @@ namespace Sandbox SHSystemManager::CreateSystem(); SHGraphicsSystem* graphicsSystem = static_cast(SHSystemManager::GetSystem()); SHSystemManager::CreateSystem(); + SHSystemManager::CreateSystem(); #ifdef SHEDITOR SDL_Init(SDL_INIT_VIDEO); @@ -89,6 +91,8 @@ namespace Sandbox SHSystemManager::RegisterRoutine(); SHSystemManager::RegisterRoutine(); + + SHSystemManager::RegisterRoutine(); #ifdef SHEDITOR SHSystemManager::RegisterRoutine(); @@ -101,6 +105,7 @@ namespace Sandbox SHComponentManager::CreateComponentSparseSet(); SHComponentManager::CreateComponentSparseSet(); SHComponentManager::CreateComponentSparseSet(); + SHComponentManager::CreateComponentSparseSet(); //TODO: REMOVE AFTER PRESENTATION //SHAssetManager::LoadDataTemp("../../Assets/racoon.gltf"); diff --git a/SHADE_Engine/src/Camera/SHCameraComponent.cpp b/SHADE_Engine/src/Camera/SHCameraComponent.cpp new file mode 100644 index 00000000..d4078622 --- /dev/null +++ b/SHADE_Engine/src/Camera/SHCameraComponent.cpp @@ -0,0 +1,131 @@ +#include "SHpch.h" +#include "SHCameraComponent.h" +#include "ECS_Base/Managers/SHComponentManager.h" + + +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) + , viewMatrix(), projMatrix() + , position() + { + ComponentFamily::GetID(); + } + + SHCameraComponent::~SHCameraComponent() + { + } + + void SHCameraComponent::SetYaw(float yaw) noexcept + { + this->yaw = yaw; + dirtyView = true; + } + + void SHCameraComponent::SetPitch(float pitch) noexcept + { + this->pitch = pitch; + dirtyView = true; + } + + void SHCameraComponent::SetRoll(float roll) noexcept + { + this->roll = roll; + dirtyView = true; + } + void SHCameraComponent::SetPositionX(float x) noexcept + { + position.x = x; + dirtyView = true; + } + void SHCameraComponent::SetPositionY(float y) noexcept + { + position.y = y; + dirtyView = true; + } + void SHCameraComponent::SetPositionZ(float z) noexcept + { + position.z = z; + dirtyView = true; + } + void SHCameraComponent::SetPosition(float x,float y, float z) noexcept + { + position.x = x; + position.y = y; + position.z = z; + dirtyView = true; + } + void SHCameraComponent::SetPosition(SHVec3& pos) noexcept + { + this->position = pos; + dirtyView = true; + } + + void SHCameraComponent::SetWidth(float width) noexcept + { + this->width = width; + dirtyProj = true; + } + + + void SHCameraComponent::SetHeight(float height) noexcept + { + this->height = height; + dirtyProj = true; + } + + void SHCameraComponent::SetNear(float znear) noexcept + { + this->zNear = znear; + dirtyProj = true; + } + + void SHCameraComponent::SetFar(float zFar) noexcept + { + this->zFar = zFar; + dirtyProj = true; + } + + void SHCameraComponent::SetFOV(float fov) noexcept + { + this->fov = fov; + dirtyProj = true; + } + + float SHCameraComponent::GetYaw() const noexcept + { + return yaw; + } + + float SHCameraComponent::GetPitch() const noexcept + { + return pitch; + } + float SHCameraComponent::GetRoll() const noexcept + { + return roll; + } + float SHCameraComponent::GetAspectRatio() const noexcept + { + return width/height; + } + + float SHCameraComponent::GetFOV() const noexcept + { + return fov; + } + + const SHMatrix& SHCameraComponent::GetViewMatrix() const noexcept + { + return viewMatrix; + } + + const SHMatrix& SHCameraComponent::GetProjMatrix() const noexcept + { + return projMatrix; + } + +} diff --git a/SHADE_Engine/src/Camera/SHCameraComponent.h b/SHADE_Engine/src/Camera/SHCameraComponent.h new file mode 100644 index 00000000..c86fa160 --- /dev/null +++ b/SHADE_Engine/src/Camera/SHCameraComponent.h @@ -0,0 +1,85 @@ +#pragma once + +#include "ECS_Base/Components/SHComponent.h" +#include "Math/Vector/SHVec3.h" +#include "Math/SHMatrix.h" +#include "SH_API.h" + +namespace SHADE +{ + + class SH_API SHCameraComponent final : public SHComponent + { + private: + + float yaw; + float pitch; + float roll; + + float width; + float height; + float zNear; + float zFar; + float fov; + + bool dirtyView; + bool dirtyProj; + + + SHMatrix viewMatrix; + SHMatrix projMatrix; + SHVec3 position; + + bool perspProj; + + + + + public: + friend class SHCameraSystem; + + SHCameraComponent(); + ~SHCameraComponent(); + + + //Getters and setters. + void SetYaw(float yaw) noexcept; + void SetPitch(float pitch) noexcept; + void SetRoll(float roll) noexcept; + void SetPositionX(float x) noexcept; + void SetPositionY(float y) noexcept; + void SetPositionZ(float z) noexcept; + void SetPosition(float x, float y, float z) noexcept; + void SetPosition(SHVec3& pos) noexcept; + + void SetWidth(float width) noexcept; + void SetHeight(float height) noexcept; + void SetNear(float znear) noexcept; + void SetFar(float zfar) noexcept; + void SetFOV(float fov) noexcept; + + + + float GetYaw() const noexcept; + float GetPitch() const noexcept; + float GetRoll() const noexcept; + + float GetAspectRatio() const noexcept; + float GetFOV() const noexcept; + + const SHMatrix& GetViewMatrix() const noexcept; + const SHMatrix& GetProjMatrix() const noexcept; + + + float movementSpeed; + SHVec3 turnSpeed; + + protected: + + + + + }; + + +} diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.cpp b/SHADE_Engine/src/Camera/SHCameraSystem.cpp new file mode 100644 index 00000000..907dfa4f --- /dev/null +++ b/SHADE_Engine/src/Camera/SHCameraSystem.cpp @@ -0,0 +1,162 @@ +#include "SHpch.h" +#include "SHCameraSystem.h" +#include "Math/SHMathHelpers.h" +#include "Input/SHInputManager.h" + + + +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::RotateY(target, SHMath::DegreesToRadians(camera.yaw)); + SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch)); + //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)) + { + system->editorCamera.position -= SHVec3(1.0,0.0,0.0) * dt * camera.movementSpeed; + system->editorCamera.dirtyView = true; + } + if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::D)) + { + system->editorCamera.position += right * dt * camera.movementSpeed; + system->editorCamera.dirtyView = true; + } + if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::W)) + { + system->editorCamera.position += view * dt * camera.movementSpeed; + system->editorCamera.dirtyView = true; + } + if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::S)) + { + system->editorCamera.position -= view * dt * camera.movementSpeed; + system->editorCamera.dirtyView = true; + } + if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::Q)) + { + system->editorCamera.position += UP * dt * camera.movementSpeed; + system->editorCamera.dirtyView = true; + } + if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::E)) + { + system->editorCamera.position -= UP * dt * camera.movementSpeed; + system->editorCamera.dirtyView = true; + } + system->UpdateCameraComponent(system->editorCamera); + } + + void SHCameraSystem::Init(void) + { + editorCamera.SetPosition(0.0f, 0.0f, 0.0f); + editorCamera.SetPitch(0.0f); + editorCamera.SetYaw(0.0f); + editorCamera.SetRoll(0.0f); + //editorCamera.movementSpeed = 100.0f; + + } + + void SHCameraSystem::Exit(void) + { + + } + + SHCameraComponent* SHCameraSystem::GetEditorCamera(void) noexcept + { + return &editorCamera; + } + + void SHCameraSystem::UpdateCameraComponent(SHCameraComponent& camera) noexcept + { + if (camera.dirtyView) + { + 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 = 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(0, 0) = right[0]; + camera.viewMatrix(0, 1) = right[1]; + camera.viewMatrix(0, 2) = right[2]; + + camera.viewMatrix(1, 0) = UP[0]; + camera.viewMatrix(1, 1) = UP[1]; + camera.viewMatrix(1, 2) = UP[2]; + + camera.viewMatrix(2, 0) = view[0]; + camera.viewMatrix(2, 1) = view[1]; + camera.viewMatrix(2, 2) = view[2]; + + camera.viewMatrix(0, 3) = -right.Dot(camera.position); + camera.viewMatrix(1, 3) = -UP.Dot(camera.position); + camera.viewMatrix(2, 3) = -view.Dot(camera.position); + + camera.dirtyView = false; + } + if (camera.dirtyProj == true) + { + if (camera.perspProj == true) + { + const float ASPECT_RATIO = camera.GetAspectRatio(); + const float TAN_HALF_FOV = tan(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.dirtyProj = false; + } + else + { + const float R = camera.width * 0.5f; + const float L = -R; + const float T = camera.height * 0.5f; + const float B = -T; + + camera.projMatrix = SHMatrix::Identity; + camera.projMatrix(0, 0) = 2.0f / (R - L); + camera.projMatrix(1, 1) = 2.0f / (B - T); + camera.projMatrix(2, 2) = 1.0f / (camera.zFar - camera.zNear); + camera.projMatrix(3, 0) = -(R + L) / (R - L); + camera.projMatrix(3, 1) = -(B + T) / (B - T); + camera.projMatrix(3, 2) = -camera.zNear / (camera.zFar - camera.zNear); + + camera.dirtyProj = false; + } + } + } + +} diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.h b/SHADE_Engine/src/Camera/SHCameraSystem.h new file mode 100644 index 00000000..fe7fd145 --- /dev/null +++ b/SHADE_Engine/src/Camera/SHCameraSystem.h @@ -0,0 +1,47 @@ +#pragma once + +#include "ECS_Base/System/SHSystem.h" +#include "SHCameraComponent.h" +#include "ECS_Base/System/SHSystemRoutine.h" +#include "SH_API.h" + + +namespace SHADE +{ + class SH_API SHCameraSystem final : public SHSystem + { + private: + //A camera component that represents editor camera. + //This is not tied to any entity. Hence this EID should not be used. + SHCameraComponent editorCamera; + + + + public: + SHCameraSystem(void) = default; + virtual ~SHCameraSystem(void) = default; + + void Init (void); + void Exit (void); + + class SH_API EditorCameraUpdate final : public SHSystemRoutine + { + public: + + EditorCameraUpdate() : SHSystemRoutine("Editor Camera Update", true) { }; + virtual void Execute(double dt) noexcept override final; + + }; + friend class EditorCameraUpdate; + + SHCameraComponent* GetEditorCamera (void) noexcept; + + protected: + + void UpdateCameraComponent(SHCameraComponent& camera) noexcept; + + }; + + + +} diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHCamera.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHCamera.cpp index 2abdfa86..194febbf 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHCamera.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHCamera.cpp @@ -97,14 +97,16 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - SHMatrix SHCamera::GetViewMatrix() const + SHMatrix SHCamera::GetViewMatrix() { - return viewMatrix; + updateMatrices(); + return viewMatrix; } - SHMatrix SHCamera::GetProjectionMatrix() const + SHMatrix SHCamera::GetProjectionMatrix() { - return projMatrix; + updateMatrices(); + return projMatrix; } SHMatrix SHCamera::GetViewProjectionMatrix() { diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHCamera.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHCamera.h index 3a945109..b23614aa 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHCamera.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHCamera.h @@ -3,11 +3,11 @@ \author Tng Kah Wei, kahwei.tng, 390009620 \par email: kahwei.tng\@digipen.edu \date Aug 21, 2022 -\brief +\brief + - Copyright (C) 2022 DigiPen Institute of Technology. -Reproduction or disclosure of this file or its contents without the prior written consent +Reproduction or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited. *//*************************************************************************************/ #pragma once @@ -17,64 +17,66 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { - /***********************************************************************************/ - /*! - \brief - Object that manages the view and projection transformations for rendering - a 3D scene. - */ - /***********************************************************************************/ - class SHCamera - { - public: - /*-----------------------------------------------------------------------------*/ - /* Constructor/Destructors */ - /*-----------------------------------------------------------------------------*/ - /*-----------------------------------------------------------------------------*/ - /* View Set Functions */ - /*-----------------------------------------------------------------------------*/ - void SetLookAt(const SHVec3& pos, const SHVec3& target, const SHVec3& up); + /***********************************************************************************/ + /*! + \brief + Object that manages the view and projection transformations for rendering + a 3D scene. + */ + /***********************************************************************************/ + class SHCamera + { + public: + /*-----------------------------------------------------------------------------*/ + /* Constructor/Destructors */ + /*-----------------------------------------------------------------------------*/ + /*-----------------------------------------------------------------------------*/ + /* View Set Functions */ + /*-----------------------------------------------------------------------------*/ + void SetLookAt(const SHVec3& pos, const SHVec3& target, const SHVec3& up); - /*-----------------------------------------------------------------------------*/ - /* Projection Set Functions */ - /*-----------------------------------------------------------------------------*/ - void SetPerspective(float fov, float width, float height, float zNear, float zFar); - void SetOrthographic(float width, float height, float zNear, float zFar); + /*-----------------------------------------------------------------------------*/ + /* Projection Set Functions */ + /*-----------------------------------------------------------------------------*/ + void SetPerspective(float fov, float width, float height, float zNear, float zFar); + void SetOrthographic(float width, float height, float zNear, float zFar); + + //void SetPerspectiveMatrixExplicit ( - /*-----------------------------------------------------------------------------*/ - /* Getter Functions */ - /*-----------------------------------------------------------------------------*/ - SHMatrix GetViewMatrix() const; - SHMatrix GetProjectionMatrix() const; - SHMatrix GetViewProjectionMatrix(); - SHMatrix GetInverseViewMatrix() const; - SHMatrix GetInverseProjectionMatrix() const; - SHMatrix GetInverseViewProjectionMatrix(); + /*-----------------------------------------------------------------------------*/ + /* Getter Functions */ + /*-----------------------------------------------------------------------------*/ + SHMatrix GetViewMatrix(); + SHMatrix GetProjectionMatrix(); + SHMatrix GetViewProjectionMatrix(); + SHMatrix GetInverseViewMatrix() const; + SHMatrix GetInverseProjectionMatrix() const; + SHMatrix GetInverseViewProjectionMatrix(); - /*-----------------------------------------------------------------------------*/ - /* Mapping Functions */ - /*-----------------------------------------------------------------------------*/ - SHVec3 ScreenToWorld(const SHVec3& vec) const; - SHVec3 WorldToScreen(const SHVec3& vec) const; - SHVec3 CameraToWorld(const SHVec3& vec) const; - SHVec3 WorldToCamera(const SHVec3& vec) const; + /*-----------------------------------------------------------------------------*/ + /* Mapping Functions */ + /*-----------------------------------------------------------------------------*/ + SHVec3 ScreenToWorld(const SHVec3& vec) const; + SHVec3 WorldToScreen(const SHVec3& vec) const; + SHVec3 CameraToWorld(const SHVec3& vec) const; + SHVec3 WorldToCamera(const SHVec3& vec) const; - private: - /*-----------------------------------------------------------------------------*/ - /* Data Members */ - /*-----------------------------------------------------------------------------*/ - SHMatrix viewMatrix; - SHMatrix projMatrix; - SHMatrix vpMatrix; - SHMatrix inverseViewMatrix; - SHMatrix inverseProjMatrix; - SHMatrix inverseVpMatrix; - bool isDirty = true; + private: + /*-----------------------------------------------------------------------------*/ + /* Data Members */ + /*-----------------------------------------------------------------------------*/ + SHMatrix viewMatrix; + SHMatrix projMatrix; + SHMatrix vpMatrix; + SHMatrix inverseViewMatrix; + SHMatrix inverseProjMatrix; + SHMatrix inverseVpMatrix; + bool isDirty = true; - /*-----------------------------------------------------------------------------*/ - /* Helper Functions */ - /*-----------------------------------------------------------------------------*/ - void updateMatrices(); - static SHVec3 multiplyHomogenous(const SHMatrix& mat, const SHVec3& vec); - }; + /*-----------------------------------------------------------------------------*/ + /* Helper Functions */ + /*-----------------------------------------------------------------------------*/ + void updateMatrices(); + static SHVec3 multiplyHomogenous(const SHMatrix& mat, const SHVec3& vec); + }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 143f3bf5..90c46ada 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -16,6 +16,9 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/Instance/SHVkInstance.h" #include "Graphics/Windowing/Surface/SHVkSurface.h" #include "Graphics/Swapchain/SHVkSwapchain.h" +#include "Camera/SHCameraSystem.h" +#include "Editor/SHEditor.hpp" +#include "ECS_Base/Managers/SHSystemManager.h" //#include "SHRenderer.h" #include "Graphics/Windowing/SHWindow.h" #include "Graphics/MiddleEnd/PerFrame/SHPerFrameData.h" @@ -318,6 +321,26 @@ namespace SHADE // Bind textures + auto cameraSystem = SHSystemManager::GetSystem(); + +#ifdef SHEDITOR + + 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()*/)); + } + else + { + // main camera + } + +#else + // main camera +#endif // For every viewport for (int vpIndex = 0; vpIndex < static_cast(viewports.size()); ++vpIndex) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp index 425e9c81..f2fb78f0 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp @@ -77,7 +77,7 @@ namespace SHADE { if (camera) { - cpuCameraData.viewProjectionMatrix = camera->GetViewProjectionMatrix(); + //cpuCameraData.viewProjectionMatrix = camera->GetViewProjectionMatrix(); cameraBuffer->WriteToMemory(&cpuCameraData, sizeof(SHShaderCameraData), 0, cameraDataAlignedSize * frameIndex); std::array dynamicOffsets{ frameIndex * cameraDataAlignedSize }; @@ -90,6 +90,12 @@ namespace SHADE { } + void SHRenderer::SetViewProjectionMatrix(SHMatrix const& vpMatrix) noexcept + { + cpuCameraData.viewProjectionMatrix = camera->GetViewMatrix() * camera->GetProjectionMatrix(); + //cpuCameraData.viewProjectionMatrix = vpMatrix * camera->GetProjectionMatrix(); + } + Handle SHRenderer::GetRenderGraph(void) const noexcept { return renderGraph; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h index 0feea840..57c63e7f 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h @@ -78,6 +78,7 @@ namespace SHADE void Draw(uint32_t frameIndex, Handle descPool) noexcept; void UpdateDataAndBind (Handle cmdBuffer, uint32_t frameIndex) noexcept; void UpdateCameraDataToBuffer (void) noexcept; + void SetViewProjectionMatrix (SHMatrix const& vpMatrix) noexcept; /*-----------------------------------------------------------------------------*/ /* Setters and Getters */ -- 2.40.1 From d9136de134b772deaec6558cc8a14efac659bb58 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Wed, 19 Oct 2022 16:53:45 +0800 Subject: [PATCH 2/2] 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 -- 2.40.1