View matrix decomposition and set view matrix

View matrix decomposition does not decompose roll yet but there isn't much use case
This commit is contained in:
maverickdgg 2022-10-31 15:39:04 +08:00
parent ab46d0a96a
commit 0e2b017716
2 changed files with 29 additions and 20 deletions

View File

@ -354,7 +354,7 @@ namespace SHADE
} }
} }
void SHCameraSystem::DecomposeViewMatrix(SHMatrix& viewMatrix, float& pitch, float& yaw, float& roll, SHVec3& pos) noexcept void SHCameraSystem::DecomposeViewMatrix(SHMatrix const& viewMatrix, float& pitch, float& yaw, float& roll, SHVec3& pos) noexcept
{ {
float initPitch = pitch; float initPitch = pitch;
@ -362,20 +362,29 @@ namespace SHADE
SHVec3 translate3, scale; SHVec3 translate3, scale;
SHQuaternion quat; SHQuaternion quat;
SHMatrix viewInverse = viewMatrix; //SHMatrix viewInverse = viewMatrix;
viewInverse.Decompose(translate3, quat, scale); viewMatrix.Decompose(translate3, quat, scale);
yaw = 180+ SHMath::RadiansToDegrees(quat.ToEuler().y); yaw = 180+ SHMath::RadiansToDegrees(quat.ToEuler().y);
pitch = -SHMath::RadiansToDegrees(quat.ToEuler().x); 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)); SHVec4 dotPos{ -viewMatrix(0,3),-viewMatrix(1,3), -viewMatrix(2,3), 1.0f };
//yaw = SHMath::RadiansToDegrees( atan2f(viewMatrix(2,0), viewMatrix(2, 2))); SHMatrix mtx = viewMatrix;
//pitch = SHMath::RadiansToDegrees( atan2f(viewMatrix(2, 1), forwardLengthXZ )); mtx(0, 3) = 0.0f;
//roll = SHMath::RadiansToDegrees( atan2f(viewMatrix(0, 1), viewMatrix(0,0))); mtx(1, 3) = 0.0f;
mtx(2, 3) = 0.0f;
mtx.Transpose();
mtx = SHMatrix::Inverse(mtx);
SHVec4 translate = mtx* dotPos;
//std::cout << "Init yaw: " << initPitch<< " , yaw: " << pitch << std::endl; pos.x = translate.x;
std::cout << "Init pos: " << initPos.x << initPos.y<< initPos.z << " , pos: " << translate.x<<translate.y<<translate.z << std::endl; pos.y = translate.y;
} pos.z = translate.z;
} }
void SHCameraSystem::SetCameraViewMatrix(SHCameraComponent& camera, SHMatrix const& viewMatrix) noexcept
{
DecomposeViewMatrix(viewMatrix, camera.pitch, camera.yaw, camera.roll, camera.position);
camera.dirtyView = true;
}
}

View File

@ -22,6 +22,11 @@ namespace SHADE
SHResourceLibrary<SHCameraDirector> directorLibrary; SHResourceLibrary<SHCameraDirector> directorLibrary;
std::vector<DirectorHandle> directorHandleList; std::vector<DirectorHandle> directorHandleList;
void UpdateCameraComponent(SHCameraComponent& camera) noexcept;
void UpdatePivotArmComponent(SHCameraArmComponent& pivot) noexcept;
public: public:
SHCameraSystem(void) = default; SHCameraSystem(void) = default;
virtual ~SHCameraSystem(void) = default; virtual ~SHCameraSystem(void) = default;
@ -55,13 +60,8 @@ namespace SHADE
void ClampCameraRotation(SHCameraComponent& camera) noexcept; void ClampCameraRotation(SHCameraComponent& camera) noexcept;
void UpdateEditorCamera(double dt) noexcept; void UpdateEditorCamera(double dt) noexcept;
void SetMainCamera(EntityID eid, size_t directorIndex) noexcept; void SetMainCamera(EntityID eid, size_t directorIndex) noexcept;
void DecomposeViewMatrix(SHMatrix& matrix, float& pitch, float& yaw, float& roll, SHVec3& pos) noexcept; void DecomposeViewMatrix(SHMatrix const& matrix, float& pitch, float& yaw, float& roll, SHVec3& pos) noexcept;
void SetCameraViewMatrix(SHCameraComponent& camera, SHMatrix const& viewMatrix) noexcept;
protected:
void UpdateCameraComponent(SHCameraComponent& camera) noexcept;
void UpdatePivotArmComponent(SHCameraArmComponent& pivot) noexcept;
}; };