diff --git a/SHADE_Engine/src/Camera/SHCameraArmComponent.cpp b/SHADE_Engine/src/Camera/SHCameraArmComponent.cpp index 8cd856e6..3b4351fa 100644 --- a/SHADE_Engine/src/Camera/SHCameraArmComponent.cpp +++ b/SHADE_Engine/src/Camera/SHCameraArmComponent.cpp @@ -7,15 +7,15 @@ namespace SHADE { SHCameraArmComponent::SHCameraArmComponent() - :pitch(0.0f), yaw(0.0f), armLength(1.0f),rtMatrix(), dirty(true) + :pitch(0.0f), yaw(0.0f), armLength(1.0f),offset(), dirty(true) { } - SHMatrix const& SHCameraArmComponent::GetMatrix() const noexcept + SHVec3 const& SHCameraArmComponent::GetOffset() const noexcept { - return rtMatrix; + return offset; } float SHCameraArmComponent::GetPitch() const noexcept diff --git a/SHADE_Engine/src/Camera/SHCameraArmComponent.h b/SHADE_Engine/src/Camera/SHCameraArmComponent.h index 8a189434..e3636b70 100644 --- a/SHADE_Engine/src/Camera/SHCameraArmComponent.h +++ b/SHADE_Engine/src/Camera/SHCameraArmComponent.h @@ -16,7 +16,7 @@ namespace SHADE float armLength; bool dirty; - SHMatrix rtMatrix; + SHVec3 offset; public: friend class SHCameraSystem; @@ -25,7 +25,8 @@ namespace SHADE //Getters - SHMatrix const& GetMatrix() const noexcept; + //SHMatrix const& GetMatrix() const noexcept; + SHVec3 const& GetOffset() const noexcept; float GetPitch() const noexcept; float GetYaw() const noexcept; float GetArmLength() const noexcept; diff --git a/SHADE_Engine/src/Camera/SHCameraComponent.cpp b/SHADE_Engine/src/Camera/SHCameraComponent.cpp index 31afe2ac..ac451df5 100644 --- a/SHADE_Engine/src/Camera/SHCameraComponent.cpp +++ b/SHADE_Engine/src/Camera/SHCameraComponent.cpp @@ -13,7 +13,7 @@ namespace SHADE , 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) , viewMatrix(), projMatrix() - , position() + , position(), offset() { ComponentFamily::GetID(); } diff --git a/SHADE_Engine/src/Camera/SHCameraComponent.h b/SHADE_Engine/src/Camera/SHCameraComponent.h index 846f4c2b..b778b8fa 100644 --- a/SHADE_Engine/src/Camera/SHCameraComponent.h +++ b/SHADE_Engine/src/Camera/SHCameraComponent.h @@ -33,7 +33,7 @@ namespace SHADE SHVec3 position; bool perspProj; - + SHVec3 offset; diff --git a/SHADE_Engine/src/Camera/SHCameraDirector.cpp b/SHADE_Engine/src/Camera/SHCameraDirector.cpp index f1e98b71..98341098 100644 --- a/SHADE_Engine/src/Camera/SHCameraDirector.cpp +++ b/SHADE_Engine/src/Camera/SHCameraDirector.cpp @@ -49,12 +49,7 @@ namespace SHADE viewMatrix = camComponent->GetViewMatrix(); projMatrix = camComponent->GetProjMatrix(); } - SHCameraArmComponent* armComponent = SHComponentManager::GetComponent_s(mainCameraEID); - if (armComponent && camComponent) - { - SHMatrix tempView = armComponent->GetMatrix() * camComponent->GetViewMatrix(); - viewMatrix = tempView; - } + } void SHCameraDirector::SetMainCamera(SHCameraComponent& camera) noexcept diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.cpp b/SHADE_Engine/src/Camera/SHCameraSystem.cpp index 115d83bb..be34ecf0 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.cpp +++ b/SHADE_Engine/src/Camera/SHCameraSystem.cpp @@ -6,6 +6,7 @@ #include "Math/Vector/SHVec2.h" #include "ECS_Base/Managers/SHComponentManager.h" #include "Math/Transform/SHTransformComponent.h" +#include namespace SHADE @@ -60,6 +61,7 @@ namespace SHADE } UpdateCameraComponent(editorCamera); + } void SHCameraSystem::EditorCameraUpdate::Execute(double dt) noexcept { @@ -113,6 +115,8 @@ namespace SHADE //std::cout << "Camera position: " << camera.position.x << " " << camera.position.y << std::endl; system->UpdateCameraComponent(system->editorCamera); + + system->DecomposeViewMatrix(camera.viewMatrix, camera.pitch, camera.yaw, camera.roll, camera.position); } void SHCameraSystem::Init(void) @@ -142,11 +146,18 @@ namespace SHADE { if (pivot.dirty) { - pivot.rtMatrix = SHMatrix::RotateX(SHMath::DegreesToRadians(pivot.GetPitch())) - * SHMatrix::RotateY(SHMath::DegreesToRadians(pivot.GetYaw())) - * SHMatrix::Translate(SHVec3(0.0f , 0.0f, pivot.GetArmLength())); - pivot.rtMatrix = SHMatrix::Inverse(pivot.rtMatrix); + SHVec3 offset{ 0.0f,0.0f, pivot.GetArmLength() }; + offset = SHVec3::RotateX(offset, (pivot.GetPitch())); + offset = SHVec3::RotateY(offset, (pivot.GetYaw())); + + + //pivot.rtMatrix = SHMatrix::RotateX(SHMath::DegreesToRadians(pivot.GetPitch())) + // * SHMatrix::RotateY(SHMath::DegreesToRadians(pivot.GetYaw())) + // * SHMatrix::Translate(SHVec3(0.0f , 0.0f, pivot.GetArmLength())); + + pivot.offset = offset; + // pivot.rtMatrix = SHMatrix::Inverse(pivot.rtMatrix); } } @@ -168,6 +179,12 @@ namespace SHADE if (camera.dirtyView) { + camera.offset = SHVec3{ 0.0f }; + if (SHComponentManager::HasComponent(camera.GetEID())) + { + camera.offset = SHComponentManager::GetComponent(camera.GetEID())->GetOffset(); + } + SHVec3 view, right, UP; @@ -188,9 +205,12 @@ namespace SHADE 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.viewMatrix(0, 3) = -right.Dot(camera.position + camera.offset); + camera.viewMatrix(1, 3) = -UP.Dot(camera.position + camera.offset); + camera.viewMatrix(2, 3) = -view.Dot(camera.position + camera.offset); + + + camera.dirtyView = false; } @@ -237,6 +257,13 @@ namespace SHADE SHVec3 target{ 0.0f,0.0f,-1.0f }; SHVec3 up = { 0.0f,1.0f,0.0f }; + if (SHComponentManager::HasComponent(camera.GetEID())) + { + auto arm = SHComponentManager::GetComponent(camera.GetEID()); + target = SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch + arm->GetPitch())); + target = SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw + arm->GetYaw())); + } + target = SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch)); target = SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw)); @@ -310,6 +337,45 @@ namespace SHADE if (camera.roll < -clampVal) camera.SetRoll(-clampVal); + while (camera.yaw > 360) + camera.yaw -= 360; + while (camera.yaw < -360) + camera.yaw += 360; + + } + + void SHCameraSystem::SetMainCamera(EntityID eid, size_t directorIndex) noexcept + { + if (SHComponentManager::HasComponent(eid) && directorIndex < directorHandleList.size()) + directorHandleList[directorIndex]->SetMainCamera(*SHComponentManager::GetComponent(eid)); + else + { + SHLOG_WARNING("Set Main Camera warning: Entity does not have camera component or director does not exist.") + } + } + + void SHCameraSystem::DecomposeViewMatrix(SHMatrix& viewMatrix, float& pitch, float& yaw, float& roll, SHVec3& pos) noexcept + { + + float initPitch = pitch; + SHVec3 initPos = pos; + SHVec3 translate3, scale; + SHQuaternion quat; + + SHMatrix viewInverse = viewMatrix; + + viewInverse.Decompose(translate3, quat, scale); + yaw = 180+ SHMath::RadiansToDegrees(quat.ToEuler().y); + pitch = -SHMath::RadiansToDegrees(quat.ToEuler().x); + SHVec4 translate = (viewMatrix * SHVec4(0.0f, 0.0f, 0.0f,1.0f) ); + + //float forwardLengthXZ = sqrt(viewMatrix(0,2) * viewMatrix(0,2) + viewMatrix(2, 2) * viewMatrix(2, 2)); + //yaw = SHMath::RadiansToDegrees( atan2f(viewMatrix(2,0), viewMatrix(2, 2))); + //pitch = SHMath::RadiansToDegrees( atan2f(viewMatrix(2, 1), forwardLengthXZ )); + //roll = SHMath::RadiansToDegrees( atan2f(viewMatrix(0, 1), viewMatrix(0,0))); + + //std::cout << "Init yaw: " << initPitch<< " , yaw: " << pitch << std::endl; + std::cout << "Init pos: " << initPos.x << initPos.y<< initPos.z << " , pos: " << translate.x<