Work on quaternion rotation

This commit is contained in:
antopilo
2022-07-10 18:36:10 -04:00
parent 32e63a5137
commit a4cdf4b524
8 changed files with 44 additions and 34 deletions

View File

@@ -30,7 +30,7 @@ public:
//ImGuiHelper::DrawVec3("Rotation", &component.Rotation);
ImGui::TableNextColumn();
ComponentTableReset(component.Rotation, Nuake::Vector3(0, 0, 0));
//ComponentTableReset(component.Rotation, Nuake::Vector3(0, 0, 0));
}
ImGui::TableNextColumn();
{

View File

@@ -93,6 +93,7 @@ namespace Nuake {
ImGui::Image((void*)texture->GetID(), regionAvail, ImVec2(0, 1), ImVec2(1, 0));
ImGuizmo::SetDrawlist();
ImGuizmo::AllowAxisFlip(false);
ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight());
if (m_DrawGrid && !Engine::IsPlayMode)
@@ -111,14 +112,19 @@ namespace Nuake {
auto editorCam = Engine::GetCurrentScene()->GetCurrentCamera();
Matrix4 cameraView = editorCam->GetTransform();
Matrix4 cameraProjection = editorCam->GetPerspective();
ImGuizmo::Manipulate(
glm::value_ptr(cameraView),
glm::value_ptr(cameraProjection),
CurrentOperation, CurrentMode, glm::value_ptr(transform), 0, 0
CurrentOperation, CurrentMode,
glm::value_ptr(transform)
);
if (ImGuizmo::IsUsing())
{
tc.LocalTransform = transform;
tc.GlobalTransform = transform;
//Entity currentParent = Selection.Entity;
//if (parent.HasParent)
//{
@@ -126,18 +132,17 @@ namespace Nuake {
// transform *= inverseParent;
//}
Vector3 scale;
glm::quat rotation;
Vector3 translation;
Vector3 skew;
Vector4 perspective;
glm::decompose(transform, scale, rotation, translation, skew, perspective);
rotation = glm::conjugate(rotation);
tc.Translation = translation;
tc.Orientation = rotation;
tc.Scale = scale;
//Vector3 scale;
//Quat rotation;
//Vector3 translation;
//Vector3 skew;
//Vector4 perspective;
//glm::decompose(transform, scale, rotation, translation, skew, perspective);
//rotation = glm::conjugate(rotation);
//
//tc.Translation = translation;
//tc.Orientation = rotation;
//tc.Scale = scale;
}
}
}

View File

@@ -10,7 +10,7 @@ namespace Nuake
tc.Translation = glm::vec3(from.x(), from.y(), from.z());
tc.Scale = glm::vec3(1.0f);
tc.Rotation = glm::vec3(0.0f);
//tc.Rotation = glm::vec3(0.0f);
glBegin(GL_LINES);
glColor3f((float)(color.x()), (float)(color.y()), (float)(color.z()));

View File

@@ -49,7 +49,7 @@ namespace Nuake {
glm::vec3 newPosition = m_Rigidbody->GetPosition();
glm::vec3 newRotation = m_Rigidbody->GetRotation();
tc->Translation = newPosition;
tc->Rotation = newRotation;
//tc->Rotation = newRotation;
}
void RigidBodyComponent::SyncWithTransform(TransformComponent* tc)
@@ -62,7 +62,7 @@ namespace Nuake {
newTransform.setOrigin(btVector3(tc->Translation.x, tc->Translation.t, tc->Translation.z));
btQuaternion quat;
quat.setEulerZYX(tc->Rotation.x, tc->Rotation.y, tc->Rotation.z);
//quat.setEulerZYX(tc->Rotation.x, tc->Rotation.y, tc->Rotation.z);
newTransform.setRotation(quat);
m_Rigidbody->UpdateTransform(newTransform);
}

View File

@@ -9,7 +9,7 @@ namespace Nuake
Translation = Vector3(0, 0, 0);
Orientation = Quat(0, 0, 0, 0);
GlobalOrientation = Orientation;
Rotation = Vector3(0, 0, 0);
//Rotation = Vector3(0, 0, 0);
Scale = Vector3(1, 1, 1);
}
@@ -24,7 +24,7 @@ namespace Nuake
Matrix4 TransformComponent::GetGlobalTransform() const
{
return GlobalTransform;
return LocalTransform;
}
Matrix4 TransformComponent::GetLocalTransform() const

View File

@@ -6,14 +6,14 @@ namespace Nuake
{
class TransformComponent {
public:
bool Dirty = false;
bool Dirty = true;
Vector3 Translation;
Quat Orientation;
Vector3 Rotation; // TODO: Should use quaternions.
//Vector3 Rotation; // TODO: Should use quaternions.
Vector3 Scale;
Vector3 GlobalTranslation;
Vector3 GlobalRotation;
//Vector3 GlobalRotation;
Quat GlobalOrientation;
Vector3 GlobalScale;
@@ -32,7 +32,7 @@ namespace Nuake
BEGIN_SERIALIZE();
SERIALIZE_VAL_LBL("Type", "TransformComponent");
SERIALIZE_VEC3(Translation);
SERIALIZE_VEC3(Rotation);
//SERIALIZE_VEC3(Rotation);
SERIALIZE_VEC3(Scale);
END_SERIALIZE();
}
@@ -41,7 +41,7 @@ namespace Nuake
{
BEGIN_DESERIALIZE();
this->Translation = Vector3(j["Translation"]["x"], j["Translation"]["y"], j["Translation"]["z"]);
this->Rotation = Vector3(j["Rotation"]["x"], j["Rotation"]["y"], j["Rotation"]["z"]);
//this->Rotation = Vector3(j["Rotation"]["x"], j["Rotation"]["y"], j["Rotation"]["z"]);
this->Scale = Vector3(j["Scale"]["x"], j["Scale"]["y"], j["Scale"]["z"]);
return true;
}

View File

@@ -6,7 +6,7 @@
#include <src/Scene/Components/TransformComponent.h>
#include <src/Scene/Components/CameraComponent.h>
#include <src/Scene/Components/ParentComponent.h>
#include "src/Vendors/glm/gtx/matrix_decompose.hpp"
namespace Nuake {
TransformSystem::TransformSystem(Scene* scene)
{
@@ -49,12 +49,17 @@ namespace Nuake {
{
TransformComponent& transform = localTransformView.get<TransformComponent>(tv);
Matrix4 localTransform = Matrix4(1.0f);
localTransform = glm::translate(localTransform, transform.Translation);
localTransform *= glm::toMat4(transform.Orientation);
localTransform = glm::scale(localTransform, transform.Scale);
if (transform.Dirty)
{
Vector3 dum;
Vector4 dum2;
glm::decompose(transform.LocalTransform, transform.Scale, transform.Orientation, transform.Translation, dum, dum2);
transform.LocalTransform = localTransform;
transform.LocalTransform = Matrix4(1);
transform.LocalTransform = glm::translate(transform.LocalTransform, transform.Translation);
transform.Dirty = false;
}
}
// Calculate all global transforms

View File

@@ -234,9 +234,9 @@ namespace Nuake {
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& transform = ent.GetComponent<TransformComponent>();
// set the slots
wrenSetSlotDouble(vm, 1, transform.Rotation.x);
wrenSetSlotDouble(vm, 2, transform.Rotation.y);
wrenSetSlotDouble(vm, 3, transform.Rotation.z);
//wrenSetSlotDouble(vm, 1, transform.Rotation.x);
//wrenSetSlotDouble(vm, 2, transform.Rotation.y);
//wrenSetSlotDouble(vm, 3, transform.Rotation.z);
// Fill the list
wrenSetSlotNewList(vm, 0);
@@ -255,7 +255,7 @@ namespace Nuake {
float y = wrenGetSlotDouble(vm, 3);
float z = wrenGetSlotDouble(vm, 4);
transform.Rotation = Vector3(x, y, z);
transform.SetRotation(x, y, z);
}
static void SetTranslation(WrenVM* vm)