Fixed scripting engine crash and improved transformation system

This commit is contained in:
Antoine Pilote
2023-03-05 18:19:41 -05:00
parent 435d50e655
commit b1edeb387f
11 changed files with 99 additions and 32 deletions

View File

@@ -38,13 +38,14 @@ public:
ImGui::TableNextColumn();
Vector3 eulerAngles = glm::eulerAngles(component.GetLocalRotation());
ImGuiHelper::DrawVec3("Rotation", &eulerAngles);
Vector3 eulerDegrees = Vector3(glm::degrees(eulerAngles.x), glm::degrees(eulerAngles.y), glm::degrees(eulerAngles.z));
ImGuiHelper::DrawVec3("Rotation", &eulerDegrees);
Vector3 eulerAnglesDelta = Vector3(glm::radians(eulerDegrees.x), glm::radians(eulerDegrees.y), glm::radians(eulerDegrees.z));
Vector3 eulerLocal = glm::eulerAngles(component.GetLocalRotation());
if (eulerLocal != eulerAngles)
if (eulerAngles != eulerAnglesDelta)
{
Quat rotation = QuatFromEuler(eulerAngles.x, eulerAngles.y, eulerAngles.z);
component.SetLocalRotation(rotation);
Quat rotation = QuatFromEuler(eulerAnglesDelta.x, eulerAnglesDelta.y, eulerAnglesDelta.z);
//component.SetLocalRotation(rotation);
}
ImGui::TableNextColumn();

View File

@@ -111,8 +111,8 @@ namespace Nuake {
{
TransformComponent& tc = Selection.Entity.GetComponent<TransformComponent>();
ParentComponent& parent = Selection.Entity.GetComponent<ParentComponent>();
Matrix4 transform = tc.GetLocalTransform();
Matrix4 transform = tc.GetGlobalTransform();
auto editorCam = Engine::GetCurrentScene()->GetCurrentCamera();
Matrix4 cameraView = editorCam->GetTransform();
Matrix4 cameraProjection = editorCam->GetPerspective();
@@ -123,11 +123,44 @@ namespace Nuake {
CurrentOperation, CurrentMode,
glm::value_ptr(transform)
);
Matrix4 oldTransform = transform;
if (ImGuizmo::IsUsing())
{
tc.SetLocalTransform(transform);
Vector3 globalPos = Vector3();
Entity currentParent = Selection.Entity;
if (parent.HasParent)
{
Matrix4 inverseParent = glm::inverse(parent.Parent.GetComponent<TransformComponent>().GetGlobalTransform());
oldTransform *= inverseParent;
}
Vector3 scale = Vector3();
Quat rotation = Quat();
Vector3 pos = Vector3();
Vector3 skew = Vector3();
Vector4 pesp = Vector4();
glm::decompose(oldTransform, scale, rotation, pos, skew, pesp);
tc.Translation = pos;
tc.Rotation = rotation;
tc.Scale = scale;
tc.LocalTransform = oldTransform;
Vector3 gscale = Vector3();
Quat grotation = Quat();
Vector3 gpos = Vector3();
Vector3 gskew = Vector3();
Vector4 gpesp = Vector4();
glm::decompose(transform, gscale, grotation, gpos, skew, pesp);
tc.SetGlobalPosition(gpos);
tc.SetGlobalRotation(grotation);
tc.SetGlobalScale(gscale);
tc.SetGlobalTransform(transform);
}
}
}
@@ -1285,6 +1318,7 @@ namespace Nuake {
}
Ref<Scene> SceneSnapshot;
void EditorInterface::Draw()
{
Init();

View File

@@ -99,12 +99,10 @@ namespace Nuake
// Dont trigger exit if already not in play mode.
if (IsPlayMode)
{
Logger::Log("Cannot exit play mode. Scene must be in play mode.", WARNING);
GetCurrentScene()->OnExit();
Input::ShowMouse();
IsPlayMode = false;
}
Input::ShowMouse();
IsPlayMode = false;
}
void Engine::Draw()

View File

@@ -5,8 +5,9 @@ using namespace Nuake;
Quat Nuake::QuatFromEuler(float x, float y, float z)
{
// Taken from: https://gamedev.stackexchange.com/questions/13436/glm-euler-angles-to-quaternion
Quat QuatAroundX = Quat(x, Vector3(1.0, 0.0, 0.0));
Quat QuatAroundY = Quat(y, Vector3(0.0, 1.0, 0.0));
Quat QuatAroundZ = Quat(z, Vector3(0.0, 0.0, 1.0));
return QuatAroundX * QuatAroundY * QuatAroundZ;
Quat QuatAroundX = glm::angleAxis(glm::radians(x), Vector3(1.0, 0.0, 0.0));
Quat QuatAroundY = glm::angleAxis(glm::radians(y), Vector3(0.0, 1.0, 0.0));
Quat QuatAroundZ = glm::angleAxis(glm::radians(z), Vector3(0.0, 0.0, 1.0));
return QuatAroundZ * QuatAroundX * QuatAroundY;
}

View File

@@ -34,6 +34,7 @@ namespace Nuake {
void Volumetric::Draw(Matrix4 projection, Matrix4 view, const Vector3& camPos, std::vector<LightComponent>& lights)
{
mFinalFramebuffer->Bind();
mFinalFramebuffer->Clear();
RenderCommand::Disable(RendererEnum::FACE_CULL);
@@ -62,6 +63,7 @@ namespace Nuake {
Renderer::DrawQuad();
mFinalFramebuffer->Unbind();
/*

View File

@@ -1,5 +1,6 @@
#pragma once
#include "../Entities/Entity.h"
#include "Engine.h"
namespace Nuake
{
@@ -47,6 +48,7 @@ namespace Nuake
if (HasParent)
{
this->ParentID = j["ParentID"];
//this->Parent = Entity{ j["Parent"], Engine::GetCurrentScene().get() };
}
return true;

View File

@@ -20,7 +20,7 @@ namespace Nuake
void TransformComponent::SetLocalRotation(const Quat& quat)
{
Rotation = quat;
Rotation = normalize(quat);
Dirty = true;
}
@@ -89,8 +89,6 @@ namespace Nuake
void TransformComponent::SetGlobalTransform(const Matrix4& transform)
{
GlobalTransform = transform;
GlobalTranslation = Vector3(transform[3]);
}
Matrix4 TransformComponent::GetLocalTransform() const
@@ -101,6 +99,5 @@ namespace Nuake
void TransformComponent::SetLocalTransform(const Matrix4& transform)
{
LocalTransform = transform;
Translation = Vector3(transform[3]);
}
}

View File

@@ -5,7 +5,7 @@
namespace Nuake
{
class TransformComponent {
private:
public:
Vector3 Translation;
Quat Rotation;
Vector3 Scale;

View File

@@ -79,14 +79,14 @@ namespace Nuake {
{
NameComponent& nameC = idView.get<NameComponent>(e);
if (nameC.ID == id)
{
return Entity{ e, this };
}
}
assert("Not found");
}
bool Scene::OnInit()
{
for (auto& system : m_Systems)
@@ -132,6 +132,7 @@ namespace Nuake {
void Scene::Draw(FrameBuffer& framebuffer)
{
Ref<Camera> cam = nullptr;
Matrix4 camTransform = Matrix4();
{
auto view = m_Registry.view<TransformComponent, CameraComponent, ParentComponent>();
for (auto e : view)
@@ -139,7 +140,9 @@ namespace Nuake {
auto [transform, camera, parent] = view.get<TransformComponent, CameraComponent, ParentComponent>(e);
cam = camera.CameraInstance;
cam->Translation = transform.GetGlobalPosition();
cam->SetDirection(Vector3(Vector4(0, 0, 1, 0) * transform.GetGlobalTransform()));
cam->SetDirection(Vector3(Vector4(0, 0, -1, 0) * transform.GetGlobalTransform()));
camTransform = transform.GetGlobalTransform();
break;
}
}
@@ -322,6 +325,21 @@ namespace Nuake {
}
CopyComponent<ParentComponent>(sceneCopy->m_Registry, this->m_Registry);
/*auto parentView = m_Registry.view<ParentComponent, NameComponent>();
for (auto e : parentView)
{
auto& [parentComponent, nameComponent] = m_Registry.get<ParentComponent, NameComponent>(e);
if (parentComponent.HasParent)
{
int id = parentComponent.ParentID;
auto newEnt = sceneCopy->GetEntityByID(nameComponent.ID);
sceneCopy->GetEntityByID(id).AddChild(newEnt);
}
}*/
CopyComponent<VisibilityComponent>(sceneCopy->m_Registry, this->m_Registry);
CopyComponent<TransformComponent>(sceneCopy->m_Registry, this->m_Registry);
CopyComponent<LightComponent>(sceneCopy->m_Registry, this->m_Registry);

View File

@@ -41,6 +41,8 @@ namespace Nuake {
{
auto [transform, camera] = camView.get<TransformComponent, CameraComponent>(e);
Matrix4 cameraTransform = camera.CameraInstance->GetTransformRotation();
camera.CameraInstance->Translation = transform.GlobalTranslation;
}
// Calculate all local transforms
@@ -51,13 +53,17 @@ namespace Nuake {
if (transform.Dirty)
{
Matrix4 localTransform = Matrix4(1);
localTransform = glm::translate(localTransform, transform.GetLocalPosition());
localTransform = localTransform * glm::toMat4(transform.GetLocalRotation());
localTransform = glm::scale(localTransform, transform.GetLocalScale());
transform.SetLocalTransform(localTransform);
Matrix4 localTransform = Matrix4(1.0f);
auto& localTranslate = transform.GetLocalPosition();
auto& localRot = transform.GetLocalRotation();
auto& localScale = transform.GetLocalScale();
Matrix4 rotationMatrix = glm::toMat4(localRot);
localTransform = glm::translate(localTransform, localTranslate);
localTransform = glm::toMat4(localRot) * localTransform;
localTransform = glm::scale(localTransform, localScale);
transform.SetLocalTransform(localTransform);
transform.Dirty = false;
}
}
@@ -72,6 +78,9 @@ namespace Nuake {
{
// If no parents, then globalTransform is local transform.
transform.SetGlobalTransform(transform.GetLocalTransform());
transform.SetGlobalPosition(transform.GetLocalPosition());
transform.SetGlobalRotation(transform.GetLocalRotation());
transform.SetGlobalScale(transform.GetLocalScale());
continue;
}
@@ -93,6 +102,9 @@ namespace Nuake {
globalOrientation *= transformComponent.GetLocalRotation();
globalScale *= transformComponent.GetLocalScale();
NameComponent& nameComponent = parentComponent.Parent.GetComponent<NameComponent>();
//std::cout << "Name:" << nameComponent.Name << std::endl;
parentComponent = parentComponent.Parent.GetComponent<ParentComponent>();
}

View File

@@ -82,7 +82,9 @@ namespace Nuake {
path += ".wren";
std::string str = FileSystem::ReadFile(path, true);
result.source = str.c_str();
char* c = strcpy(new char[str.length() + 1], str.c_str());
result.source = c;
return result;
}