Progress towards mental freedom

This commit is contained in:
Antoine Pilote
2023-07-13 23:31:07 -04:00
parent 13fb6be430
commit 9f60963102
5 changed files with 82 additions and 50 deletions

File diff suppressed because one or more lines are too long

View File

@@ -197,6 +197,10 @@ namespace Nuake {
if (ImGuizmo::IsUsing())
{
// TODO: CALCAULATE DELTA BETWEEN THE GLOBAL TRANSFORMS, CALCULATE ROTAION, SCALe DELTAS
// AND add Them to each component.
//
// const Matrix4& transformDelta =
// Since imguizmo returns a transform in global space and we want the local transform,
// we need to multiply by the inverse of the parent's global transform in order to revert
// the changes from the parent transform.
@@ -217,32 +221,47 @@ namespace Nuake {
Vector4 pesp = Vector4();
glm::decompose(localTransform, scale, rotation, pos, skew, pesp);
tc.Translation = pos;
rotation.w *= -1.0f;
tc.Rotation = rotation;
tc.Scale = scale;
tc.LocalTransform = localTransform;
//tc.Dirty = true;
float newScale[3];
float newRot[3];
float rotates[3];
float newPos[3];
ImGuizmo::DecomposeMatrixToComponents(glm::value_ptr(localTransform), newPos, newRot, newScale);
rotates[0] = -newRot[0];
rotates[1] = -newRot[1];
rotates[2] = newRot[2];
const Matrix4& translationMatrix = glm::translate(Matrix4(1.0f), pos);
const Matrix4& rotationMatrix = glm::mat4_cast(rotation);
auto rott = QuatFromEuler(rotates[0], rotates[1], rotates[2]);
const Matrix4& rotationMatrix = glm::mat4_cast(glm::conjugate(rott));
const Matrix4& scaleMatrix = glm::scale(Matrix4(1.0f), scale);
const Matrix4& translationMatrix = glm::translate(Matrix4(1.0f), pos);
const Matrix4& newLocalTransform = translationMatrix * rotationMatrix * scaleMatrix;
//assert(newLocalTransform == localTransform);
tc.Translation = pos;
tc.Rotation = glm::normalize(glm::conjugate(rotation));
tc.Scale = scale;
tc.LocalTransform = newLocalTransform;
tc.Dirty = true;
// Decompose global trasnform
Vector3 gscale = Vector3();
Quat grotation = Quat();
Vector3 gpos = Vector3();
Vector3 gskew = Vector3();
Vector4 gpesp = Vector4();
glm::decompose(transform, gscale, grotation, gpos, gskew, gpesp);
//glm::quat rotationQuaternionG = glm::quat_cast(transform);
tc.SetGlobalPosition(gpos);
tc.SetGlobalRotation(grotation);
tc.SetGlobalScale(gscale);
tc.SetGlobalTransform(localTransform);
//Vector3 gscale = Vector3();
//Quat grotation = Quat();
//Vector3 gpos = Vector3();
//Vector3 gskew = Vector3();
//Vector4 gpesp = Vector4();
//glm::decompose(transform, gscale, grotation, gpos, gskew, gpesp);
//
////glm::quat rotationQuaternionG = glm::quat_cast(transform);
//tc.SetGlobalPosition(gpos);
//tc.SetGlobalRotation(glm::conjugate(grotation));
//tc.SetGlobalScale(gscale);
//tc.SetGlobalTransform(localTransform);
}
}
@@ -375,9 +394,8 @@ namespace Nuake {
if (ImGui::Selectable("Focus camera"))
{
Ref<EditorCamera> editorCam = Engine::GetCurrentScene()->m_EditorCamera;
editorCam->Translation = entity.GetComponent<TransformComponent>().GetGlobalPosition();
Vector3 camDirection = entity.GetComponent<CameraComponent>().CameraInstance->GetDirection();
editorCam->SetDirection(camDirection);
editorCam->SetTransform(glm::inverse(entity.GetComponent<TransformComponent>().GetGlobalTransform()));
}
ImGui::Separator();
}

View File

@@ -66,4 +66,16 @@ Vector3 Nuake::QuatToDirection(const Quat& quat)
{
return glm::normalize(quat * Vector3(0, 0, -1));
//return glm::normalize(glm::rotate(glm::inverse(quat), glm::vec3(-1.0, 0.0, 0.0)));
}
void Nuake::Decompose(const Matrix4& m, Vector3& pos, Quat& rot, Vector3& scale)
{
pos = m[3];
for (int i = 0; i < 3; i++)
scale[i] = glm::length(Vector3(m[i]));
const glm::mat3 rotMtx(
glm::vec3(m[0]) / scale[0],
glm::vec3(m[1]) / scale[1],
glm::vec3(m[2]) / scale[2]);
rot = glm::quat_cast(rotMtx);
}

View File

@@ -24,4 +24,5 @@ namespace Nuake
Quat CreateFromAxisAngle(Vector3 axis, float angle);
Quat QuatFromEuler(float x, float y, float z);
Vector3 QuatToDirection(const Quat& quat);
void Decompose(const Matrix4& m, Vector3& pos, Quat& rot, Vector3& scale);
}

View File

@@ -44,15 +44,16 @@ namespace Nuake
if (transform.Dirty)
{
auto& localTranslate = transform.GetLocalPosition();
auto& localRot = transform.GetLocalRotation();
auto& localRot = glm::normalize(transform.GetLocalRotation());
auto& localScale = transform.GetLocalScale();
const Matrix4& translationMatrix = glm::translate(Matrix4(1.0f), localTranslate);
//localRot.w *= -1.0f;
const Matrix4& rotationMatrix = glm::mat4_cast(localRot);
const Matrix4& scaleMatrix = glm::scale(Matrix4(1.0f), localScale);
const Matrix4& newLocalTransform = translationMatrix * rotationMatrix * scaleMatrix;
//localRot *= -1.0f;
//if(localRot.y != 0)
// assert(newLocalTransform == transform.GetLocalTransform());
@@ -114,7 +115,7 @@ namespace Nuake
camera.CameraInstance->Translation = transform.GlobalTranslation;
auto globalRotation = transform.GetGlobalRotation();
auto& trnaslationMatrix = glm::translate(Matrix4(1.0f), transform.GetGlobalPosition());
auto& translationMatrix = glm::translate(Matrix4(1.0f), transform.GetGlobalPosition());
const Matrix4& rotationMatrix = glm::mat4_cast(globalRotation);
Vector4 forward = Vector4(0, 0, -1, 1);
const auto globalForward = rotationMatrix * forward;
@@ -123,7 +124,7 @@ namespace Nuake
const auto globalRight = rotationMatrix * right;
camera.CameraInstance->Direction = globalForward;
camera.CameraInstance->Right = globalRight;
; camera.CameraInstance->SetTransform(glm::inverse(trnaslationMatrix * rotationMatrix));
; camera.CameraInstance->SetTransform(glm::inverse(translationMatrix * rotationMatrix));
}
}
}