Decompose matrix WIP

This commit is contained in:
maverickdgg 2022-10-31 15:02:28 +08:00
parent b0f28f98c5
commit ab46d0a96a
7 changed files with 84 additions and 20 deletions

View File

@ -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

View File

@ -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;

View File

@ -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<SHCameraComponent>();
}

View File

@ -33,7 +33,7 @@ namespace SHADE
SHVec3 position;
bool perspProj;
SHVec3 offset;

View File

@ -49,12 +49,7 @@ namespace SHADE
viewMatrix = camComponent->GetViewMatrix();
projMatrix = camComponent->GetProjMatrix();
}
SHCameraArmComponent* armComponent = SHComponentManager::GetComponent_s<SHCameraArmComponent>(mainCameraEID);
if (armComponent && camComponent)
{
SHMatrix tempView = armComponent->GetMatrix() * camComponent->GetViewMatrix();
viewMatrix = tempView;
}
}
void SHCameraDirector::SetMainCamera(SHCameraComponent& camera) noexcept

View File

@ -6,6 +6,7 @@
#include "Math/Vector/SHVec2.h"
#include "ECS_Base/Managers/SHComponentManager.h"
#include "Math/Transform/SHTransformComponent.h"
#include <math.h>
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<SHCameraArmComponent>(camera.GetEID()))
{
camera.offset = SHComponentManager::GetComponent<SHCameraArmComponent>(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<SHCameraArmComponent>(camera.GetEID()))
{
auto arm = SHComponentManager::GetComponent<SHCameraArmComponent>(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<SHCameraComponent>(eid) && directorIndex < directorHandleList.size())
directorHandleList[directorIndex]->SetMainCamera(*SHComponentManager::GetComponent<SHCameraComponent>(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<<translate.y<<translate.z << std::endl;
}
}

View File

@ -54,6 +54,8 @@ namespace SHADE
DirectorHandle GetDirector(size_t index) noexcept;
void ClampCameraRotation(SHCameraComponent& camera) noexcept;
void UpdateEditorCamera(double dt) noexcept;
void SetMainCamera(EntityID eid, size_t directorIndex) noexcept;
void DecomposeViewMatrix(SHMatrix& matrix, float& pitch, float& yaw, float& roll, SHVec3& pos) noexcept;
protected: