Merge pull request #27 from antopilo/NK-103-Camera-corrupted-transform-2
NK-103 Camera rotation now working when parented
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -44,6 +44,7 @@ class Scene {
|
||||
foreign static GetTranslation_(e)
|
||||
foreign static SetTranslation_(e, x, y, z)
|
||||
foreign static SetRotation_(e, x, y, z)
|
||||
foreign static SetLookAt_(e, x, y, z)
|
||||
foreign static GetRotation_(e)
|
||||
//foreign static SetScale_(e, x, y, z)
|
||||
|
||||
@@ -111,6 +112,9 @@ class TransformComponent {
|
||||
Scene.SetRotation_(_entityId, t.x, t.y, t.z)
|
||||
}
|
||||
|
||||
SetLookAt(t) {
|
||||
Scene.SetLookAt_(_entityId, t.x, t.y, t.z)
|
||||
}
|
||||
}
|
||||
|
||||
class Light {
|
||||
|
||||
@@ -36,7 +36,7 @@ GizmoDrawer::GizmoDrawer()
|
||||
_gizmos = std::map<std::string, Ref<Model>>();
|
||||
_gizmos["cam"] = loader.LoadModel("resources/Models/Camera.gltf");
|
||||
_gizmos["light"] = loader.LoadModel("resources/Models/Light.gltf");
|
||||
_gizmos["player"] = loader.LoadModel("resources/Models/Player.gltf");
|
||||
_gizmos["player"] = loader.LoadModel("resources/Models/Camera.gltf");
|
||||
}
|
||||
|
||||
void GizmoDrawer::GenerateSphereGizmo()
|
||||
|
||||
@@ -182,53 +182,63 @@ namespace Nuake {
|
||||
if (Selection.Type == EditorSelectionType::Entity && !Engine::IsPlayMode())
|
||||
{
|
||||
TransformComponent& tc = Selection.Entity.GetComponent<TransformComponent>();
|
||||
ParentComponent& parent = Selection.Entity.GetComponent<ParentComponent>();
|
||||
Matrix4 transform = tc.GetGlobalTransform();
|
||||
|
||||
auto editorCam = Engine::GetCurrentScene()->GetCurrentCamera();
|
||||
const auto& editorCam = Engine::GetCurrentScene()->GetCurrentCamera();
|
||||
Matrix4 cameraView = editorCam->GetTransform();
|
||||
Matrix4 cameraProjection = editorCam->GetPerspective();
|
||||
|
||||
// Imguizmo calculates the delta from the gizmo,
|
||||
ImGuizmo::Manipulate(
|
||||
glm::value_ptr(cameraView),
|
||||
glm::value_ptr(cameraProjection),
|
||||
CurrentOperation, CurrentMode,
|
||||
glm::value_ptr(transform)
|
||||
);
|
||||
Matrix4 oldTransform = transform;
|
||||
|
||||
if (ImGuizmo::IsUsing())
|
||||
{
|
||||
Vector3 globalPos = Vector3();
|
||||
Entity currentParent = Selection.Entity;
|
||||
// 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.
|
||||
Matrix4 localTransform = Matrix4(transform);
|
||||
ParentComponent& parent = Selection.Entity.GetComponent<ParentComponent>();
|
||||
if (parent.HasParent)
|
||||
{
|
||||
Matrix4 inverseParent = glm::inverse(parent.Parent.GetComponent<TransformComponent>().GetGlobalTransform());
|
||||
oldTransform *= inverseParent;
|
||||
const auto& parentTransformComponent = parent.Parent.GetComponent<TransformComponent>();
|
||||
const Matrix4& parentTransform = parentTransformComponent.GetGlobalTransform();
|
||||
localTransform = glm::inverse(parentTransform) * localTransform;
|
||||
}
|
||||
|
||||
Vector3 scale = Vector3();
|
||||
Quat rotation = Quat();
|
||||
Vector3 pos = Vector3();
|
||||
Vector3 skew = Vector3();
|
||||
Vector4 pesp = Vector4();
|
||||
glm::decompose(oldTransform, scale, rotation, pos, skew, pesp);
|
||||
// Decompose local transform
|
||||
float decomposedPosition[3];
|
||||
float decomposedEuler[3];
|
||||
float decomposedScale[3];
|
||||
ImGuizmo::DecomposeMatrixToComponents(glm::value_ptr(localTransform), decomposedPosition, decomposedEuler, decomposedScale);
|
||||
|
||||
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);
|
||||
const auto& localPosition = Vector3(decomposedPosition[0], decomposedPosition[1], decomposedPosition[2]);
|
||||
const auto& localScale = Vector3(decomposedScale[0], decomposedScale[1], decomposedScale[2]);
|
||||
|
||||
localTransform[0] /= localScale.x;
|
||||
localTransform[1] /= localScale.y;
|
||||
localTransform[2] /= localScale.z;
|
||||
const auto& rotationMatrix = Matrix3(localTransform);
|
||||
const Quat& localRotation = glm::normalize(Quat(rotationMatrix));
|
||||
|
||||
const Matrix4& rotationMatrix4 = glm::mat4_cast(localRotation);
|
||||
const Matrix4& scaleMatrix = glm::scale(Matrix4(1.0f), localScale);
|
||||
const Matrix4& translationMatrix = glm::translate(Matrix4(1.0f), localPosition);
|
||||
const Matrix4& newLocalTransform = translationMatrix * rotationMatrix4 * scaleMatrix;
|
||||
|
||||
tc.Translation = localPosition;
|
||||
|
||||
if (CurrentOperation != ImGuizmo::SCALE)
|
||||
{
|
||||
tc.Rotation = localRotation;
|
||||
}
|
||||
|
||||
tc.Scale = localScale;
|
||||
tc.LocalTransform = newLocalTransform;
|
||||
tc.Dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -361,9 +371,9 @@ 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);
|
||||
camDirection.z *= -1.0f;
|
||||
editorCam->SetTransform(glm::inverse(entity.GetComponent<TransformComponent>().GetGlobalTransform()));
|
||||
}
|
||||
ImGui::Separator();
|
||||
}
|
||||
|
||||
@@ -2,12 +2,79 @@
|
||||
|
||||
using namespace Nuake;
|
||||
|
||||
Quat Nuake::LookAt(Vector3 sourcePoint, Vector3 destPoint)
|
||||
{
|
||||
Vector3 forwardVector = glm::normalize(destPoint - sourcePoint);
|
||||
|
||||
float dot = glm::dot(Vector3(0, 0, 1), forwardVector);
|
||||
if (glm::abs(dot - (-1.0f)) < 0.000001f)
|
||||
{
|
||||
return Quat(0, 1, 0, 3.1415926535897932f);
|
||||
}
|
||||
if (glm::abs(dot - (1.0f)) < 0.000001f)
|
||||
{
|
||||
return Quat(); // identity
|
||||
}
|
||||
|
||||
float rotAngle = acos(dot);
|
||||
Vector3 rotAxis = glm::cross(Vector3(0, 0, 1), forwardVector);
|
||||
rotAxis = glm::normalize(rotAxis);
|
||||
return CreateFromAxisAngle(rotAxis, rotAngle);
|
||||
}
|
||||
|
||||
// just in case you need that function also
|
||||
Quat Nuake::CreateFromAxisAngle(Vector3 axis, float angle)
|
||||
{
|
||||
float halfAngle = angle * .5f;
|
||||
float s = sin(halfAngle);
|
||||
Quat q;
|
||||
q.x = axis.x * s;
|
||||
q.y = axis.y * s;
|
||||
q.z = axis.z * s;
|
||||
q.w = cos(halfAngle);
|
||||
return q;
|
||||
}
|
||||
|
||||
Quat Nuake::QuatFromEuler(float x, float y, float z)
|
||||
{
|
||||
return Quat(Vector3(Rad(z), Rad(y), Rad(x)));
|
||||
glm::quat pitchQuat = glm::angleAxis(Rad(x), glm::vec3(1.0f, 0.0f, 0.0f));
|
||||
glm::quat yawQuat = glm::angleAxis(Rad(y), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
glm::quat rollQuat = glm::angleAxis(Rad(z), glm::vec3(0.0f, 0.0f, -1.0f));
|
||||
glm::quat orientation = yawQuat * pitchQuat * rollQuat;
|
||||
|
||||
return glm::normalize(orientation);
|
||||
|
||||
return Quat(Vector3(Rad(x), Rad(y), Rad(z)));
|
||||
|
||||
Quat q;
|
||||
|
||||
float cr = cos(x * 0.5);
|
||||
float sr = sin(x * 0.5);
|
||||
float cp = cos(y * 0.5);
|
||||
float sp = sin(y * 0.5);
|
||||
float cy = cos(z * 0.5);
|
||||
float sy = sin(z * 0.5);
|
||||
|
||||
q.w = cr * cp * cy + sr * sp * sy;
|
||||
q.x = sr * cp * cy - cr * sp * sy;
|
||||
q.y = cr * sp * cy + sr * cp * sy;
|
||||
q.z = cr * cp * sy - sr * sp * cy;
|
||||
return q;
|
||||
}
|
||||
|
||||
Vector3 Nuake::QuatToDirection(const Quat& quat)
|
||||
{
|
||||
return glm::rotate(glm::inverse(quat), glm::vec3(1.0, 0.0, 0.0));
|
||||
return glm::normalize(quat * Vector3(0, 0, -1));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -20,6 +20,9 @@ namespace Nuake
|
||||
using Matrix3 = glm::mat3;
|
||||
#define Rad(degrees) glm::radians(degrees)
|
||||
|
||||
Quat LookAt(Vector3 sourcePoint, Vector3 destPoint);
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -292,8 +292,16 @@ namespace Nuake
|
||||
settings->mShape = GetJoltShape(cc->Shape);
|
||||
settings->mGravityFactor = 0.0f;
|
||||
|
||||
auto& joltPos = JPH::Vec3(cc->Position.x, cc->Position.y, cc->Position.z);
|
||||
JPH::Character* character = new JPH::Character(settings, joltPos, JPH::Quat::sIdentity(), cc->GetEntity().GetID() , _JoltPhysicsSystem.get());
|
||||
auto& joltPosition = JPH::Vec3(cc->Position.x, cc->Position.y, cc->Position.z);
|
||||
|
||||
Quat& bodyRotation = cc->Rotation;
|
||||
|
||||
// We need to add 180 degrees because our forward is -Z.
|
||||
const auto& yOffset = Vector3(0.0f, Rad(180.0), 0.0f);
|
||||
bodyRotation = glm::normalize(bodyRotation * Quat(yOffset));
|
||||
|
||||
const auto& joltRotation = JPH::Quat(bodyRotation.x, bodyRotation.y, bodyRotation.z, bodyRotation.w);
|
||||
JPH::Character* character = new JPH::Character(settings, joltPosition, joltRotation, cc->GetEntity().GetID() , _JoltPhysicsSystem.get());
|
||||
|
||||
character->AddToPhysicsSystem(JPH::EActivation::Activate);
|
||||
|
||||
@@ -362,17 +370,16 @@ namespace Nuake
|
||||
transformComponent.SetLocalPosition(pos);
|
||||
transformComponent.SetLocalRotation(Quat(bodyRotation.GetW(), bodyRotation.GetX(), bodyRotation.GetY(), bodyRotation.GetZ()));
|
||||
transformComponent.SetLocalTransform(transform);
|
||||
transformComponent.Dirty = false;
|
||||
transformComponent.Dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DynamicWorld::SyncCharactersTransforms()
|
||||
{
|
||||
// TODO(ANTO): Finish this to connect updated jolt transforms back to the entity.
|
||||
// The problem was that I dont know yet how to go from jolt body ptr to the entity
|
||||
// Combinations of find and iterators etc. I do not have the brain power rn zzz.
|
||||
// const auto& bodyInterface = _JoltPhysicsSystem->GetBodyInterface();
|
||||
|
||||
for (const auto& e : _registeredCharacters)
|
||||
{
|
||||
Entity entity { (entt::entity)e.first, Engine::GetCurrentScene().get()};
|
||||
@@ -399,7 +406,7 @@ namespace Nuake
|
||||
transformComponent.SetLocalPosition(pos);
|
||||
transformComponent.SetLocalRotation(Quat(bodyRotation.GetW(), bodyRotation.GetX(), bodyRotation.GetY(), bodyRotation.GetZ()));
|
||||
transformComponent.SetLocalTransform(transform);
|
||||
transformComponent.Dirty = false;
|
||||
transformComponent.Dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -437,29 +444,24 @@ namespace Nuake
|
||||
SyncCharactersTransforms();
|
||||
}
|
||||
|
||||
|
||||
void DynamicWorld::Clear()
|
||||
{
|
||||
_stepCount = 0;
|
||||
|
||||
if (_registeredBodies.empty())
|
||||
if (!_registeredBodies.empty())
|
||||
{
|
||||
return;
|
||||
_JoltBodyInterface->RemoveBodies(reinterpret_cast<JPH::BodyID*>(_registeredBodies.data()), _registeredBodies.size());
|
||||
_registeredBodies.clear();
|
||||
}
|
||||
|
||||
_JoltBodyInterface->RemoveBodies(reinterpret_cast<JPH::BodyID*>(_registeredBodies.data()), _registeredBodies.size());
|
||||
_registeredBodies.clear();
|
||||
|
||||
if (_registeredCharacters.empty())
|
||||
if (!_registeredCharacters.empty())
|
||||
{
|
||||
return;
|
||||
for (auto& character : _registeredCharacters)
|
||||
{
|
||||
character.second->RemoveFromPhysicsSystem();
|
||||
}
|
||||
_registeredCharacters.clear();
|
||||
}
|
||||
|
||||
for (auto& character : _registeredCharacters)
|
||||
{
|
||||
character.second->RemoveFromPhysicsSystem();
|
||||
}
|
||||
_registeredCharacters.clear();
|
||||
}
|
||||
|
||||
void DynamicWorld::MoveAndSlideCharacterController(const Entity& entity, const Vector3 velocity)
|
||||
|
||||
@@ -45,14 +45,15 @@ namespace Nuake
|
||||
|
||||
void Camera::SetDirection(Vector3 direction)
|
||||
{
|
||||
//cam->cameraDirection.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
||||
//cam->cameraDirection.y = sin(glm::radians(Pitch));
|
||||
//cam->cameraDirection.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
||||
//cam->cameraFront = glm::normalize(cam->cameraDirection);
|
||||
//cam->cameraRight = glm::normalize(glm::cross(cam->up, cam->cameraFront));
|
||||
Direction = glm::normalize(direction);
|
||||
Right = glm::normalize(glm::cross(Vector3(0, 1, 0), Direction));
|
||||
//Up = glm::normalize(glm::cross(Direction, Right))
|
||||
m_View = lookAt(Translation, Translation + glm::normalize(Direction), Vector3(0, 1, 0));
|
||||
}
|
||||
|
||||
void Camera::SetDirection(const Quat& direction)
|
||||
{
|
||||
// TODO: Calculate forward and Right from quaternion.
|
||||
assert("Not implemented!");
|
||||
}
|
||||
|
||||
Vector3 Camera::GetTranslation() {
|
||||
@@ -70,17 +71,21 @@ namespace Nuake
|
||||
return m_Perspective;
|
||||
}
|
||||
|
||||
void Camera::SetTransform(const Matrix4& transform)
|
||||
{
|
||||
m_View = transform;
|
||||
}
|
||||
|
||||
Matrix4 Camera::GetTransform()
|
||||
{
|
||||
glm::mat4 tr = lookAt(Translation, Translation + Direction, Vector3(0, 1, 0));
|
||||
return tr;
|
||||
return m_View;
|
||||
}
|
||||
|
||||
Matrix4 Camera::GetTransformRotation()
|
||||
{
|
||||
return lookAt(glm::vec3(), Direction, Up);
|
||||
}
|
||||
|
||||
|
||||
bool Camera::BoxFrustumCheck(const AABB& aabb)
|
||||
{
|
||||
m_Frustum = Frustum(GetPerspective() * GetTransform());
|
||||
@@ -98,6 +103,8 @@ namespace Nuake
|
||||
BEGIN_SERIALIZE();
|
||||
SERIALIZE_VAL(m_Type);
|
||||
SERIALIZE_VAL(AspectRatio);
|
||||
SERIALIZE_VEC3(Translation)
|
||||
SERIALIZE_VEC3(Direction)
|
||||
SERIALIZE_VAL(Fov);
|
||||
SERIALIZE_VAL(Exposure);
|
||||
SERIALIZE_VAL(Speed);
|
||||
@@ -109,6 +116,12 @@ namespace Nuake
|
||||
BEGIN_DESERIALIZE();
|
||||
j = j["CameraInstance"];
|
||||
this->m_Type = (CAMERA_TYPE)j["m_Type"];
|
||||
if(j.contains("Translation"))
|
||||
DESERIALIZE_VEC3(j["Translation"], Translation);
|
||||
|
||||
//if (j.contains("Direction"))
|
||||
// DESERIALIZE_VEC3(j["Direction"], Direction);
|
||||
|
||||
this->AspectRatio = j["AspectRatio"];
|
||||
this->Fov = j["Fov"];
|
||||
this->Exposure = j["Exposure"];
|
||||
|
||||
@@ -24,13 +24,15 @@ namespace Nuake
|
||||
Vector3 Rotation = { 0.0f, 0.0f, 0.0f };
|
||||
Vector3 Scale = { 1.0f, 1.0f, 1.0f };
|
||||
Vector3 Up = Vector3(0, 1, 0);
|
||||
Vector3 Right = Vector3(1, 0, 0);
|
||||
Matrix4 m_Perspective;
|
||||
Matrix4 m_View;
|
||||
|
||||
public:
|
||||
float AspectRatio = 16.0f / 9.0f;
|
||||
|
||||
Vector3 Direction = Vector3(0, 0, 1);
|
||||
Vector3 Right = Vector3(1, 0, 0);
|
||||
|
||||
Vector3 Translation = { 0.0f, 0.0f, 0.0f };
|
||||
float Fov = 88.0f;
|
||||
float Exposure = 1.0f;
|
||||
@@ -44,12 +46,14 @@ namespace Nuake
|
||||
void OnWindowResize(float x, float y);
|
||||
|
||||
void SetDirection(Vector3 direction);
|
||||
void SetDirection(const Quat& direction);
|
||||
|
||||
Vector3 GetTranslation();
|
||||
Vector3 GetDirection();
|
||||
Matrix4 GetPerspective();
|
||||
Matrix4 GetTransform();
|
||||
Matrix4 GetTransformRotation();
|
||||
void SetTransform(const Matrix4& transform);
|
||||
inline Vector3 GetRight() const { return Right; }
|
||||
inline Vector3 GetUp() const { return glm::cross(Direction, Right); }
|
||||
bool BoxFrustumCheck(const AABB& aabb);
|
||||
|
||||
@@ -46,11 +46,13 @@ namespace Nuake
|
||||
|
||||
json Serialize()
|
||||
{
|
||||
Rotation = glm::normalize(Rotation);
|
||||
BEGIN_SERIALIZE();
|
||||
SERIALIZE_VAL_LBL("Type", "TransformComponent");
|
||||
SERIALIZE_VEC3(Translation);
|
||||
SERIALIZE_VEC3(GlobalTranslation);
|
||||
SERIALIZE_VEC4(Rotation);
|
||||
SERIALIZE_VEC4(GlobalRotation);
|
||||
SERIALIZE_VEC3(GlobalScale);
|
||||
SERIALIZE_VEC3(Scale);
|
||||
END_SERIALIZE();
|
||||
}
|
||||
@@ -59,6 +61,16 @@ namespace Nuake
|
||||
{
|
||||
BEGIN_DESERIALIZE();
|
||||
this->Translation = Vector3(j["Translation"]["x"], j["Translation"]["y"], j["Translation"]["z"]);
|
||||
|
||||
if (j.contains("GlobalTranslation"))
|
||||
{
|
||||
float x = j["GlobalTranslation"]["x"];
|
||||
float y = j["GlobalTranslation"]["y"];
|
||||
float z = j["GlobalTranslation"]["z"];
|
||||
|
||||
this->GlobalTranslation = Vector3(x, y, z);
|
||||
}
|
||||
|
||||
if (j.contains("Rotation"))
|
||||
{
|
||||
float w = j["Rotation"]["w"];
|
||||
@@ -68,11 +80,31 @@ namespace Nuake
|
||||
|
||||
this->Rotation = Quat(w, x, y, z);
|
||||
}
|
||||
|
||||
if (j.contains("GlobalRotation"))
|
||||
{
|
||||
float w = j["GlobalRotation"]["w"];
|
||||
float x = j["GlobalRotation"]["x"];
|
||||
float y = j["GlobalRotation"]["y"];
|
||||
float z = j["GlobalRotation"]["z"];
|
||||
|
||||
this->GlobalRotation = Quat(w, x, y, z);
|
||||
}
|
||||
|
||||
if (j.contains("GlobalScale"))
|
||||
{
|
||||
float x = j["GlobalScale"]["x"];
|
||||
float y = j["GlobalScale"]["y"];
|
||||
float z = j["GlobalScale"]["z"];
|
||||
|
||||
this->GlobalScale = Vector3(x, y, z);
|
||||
}
|
||||
|
||||
this->Scale = Vector3(j["Scale"]["x"], j["Scale"]["y"], j["Scale"]["z"]);
|
||||
|
||||
LocalTransform = Matrix4(1);
|
||||
GlobalTransform = Matrix4(1);
|
||||
this->Dirty = true;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -148,7 +148,8 @@ namespace Nuake
|
||||
Direction.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
||||
Direction.y = sin(glm::radians(Pitch));
|
||||
Direction.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
||||
Direction = glm::normalize(Direction);
|
||||
|
||||
SetDirection(glm::normalize(Direction));
|
||||
Right = glm::normalize(glm::cross(Up, Direction));
|
||||
}
|
||||
else if (Input::IsMouseButtonDown(2))
|
||||
|
||||
@@ -135,22 +135,14 @@ namespace Nuake {
|
||||
void Scene::Draw(FrameBuffer& framebuffer)
|
||||
{
|
||||
Ref<Camera> cam = nullptr;
|
||||
Matrix4 camTransform = Matrix4();
|
||||
const auto& view = m_Registry.view<TransformComponent, CameraComponent, ParentComponent>();
|
||||
for (const auto& e : view)
|
||||
{
|
||||
auto view = m_Registry.view<TransformComponent, CameraComponent, ParentComponent>();
|
||||
for (auto e : view)
|
||||
{
|
||||
auto [transform, camera, parent] = view.get<TransformComponent, CameraComponent, ParentComponent>(e);
|
||||
cam = camera.CameraInstance;
|
||||
auto [transform, camera, parent] = view.get<TransformComponent, CameraComponent, ParentComponent>(e);
|
||||
cam = camera.CameraInstance;
|
||||
|
||||
cam->Translation = transform.GetGlobalPosition();
|
||||
|
||||
const Vector3& forward = QuatToDirection(transform.GetGlobalRotation());
|
||||
cam->SetDirection(forward);
|
||||
|
||||
camTransform = transform.GetGlobalTransform();
|
||||
break;
|
||||
}
|
||||
cam->Translation = transform.GetGlobalPosition();
|
||||
break;
|
||||
}
|
||||
|
||||
if (!cam)
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
#include "TransformSystem.h"
|
||||
|
||||
#include "src/Core/Maths.h"
|
||||
|
||||
#include "src/Scene/Scene.h"
|
||||
#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 {
|
||||
|
||||
namespace Nuake
|
||||
{
|
||||
TransformSystem::TransformSystem(Scene* scene)
|
||||
{
|
||||
m_Scene = scene;
|
||||
@@ -36,15 +36,6 @@ namespace Nuake {
|
||||
|
||||
void TransformSystem::UpdateTransform()
|
||||
{
|
||||
auto camView = m_Scene->m_Registry.view<TransformComponent, CameraComponent>();
|
||||
for (auto e : camView)
|
||||
{
|
||||
auto [transform, camera] = camView.get<TransformComponent, CameraComponent>(e);
|
||||
Matrix4 cameraTransform = camera.CameraInstance->GetTransformRotation();
|
||||
|
||||
camera.CameraInstance->Translation = transform.GlobalTranslation;
|
||||
}
|
||||
|
||||
// Calculate all local transforms
|
||||
auto localTransformView = m_Scene->m_Registry.view<TransformComponent>();
|
||||
for (auto tv : localTransformView)
|
||||
@@ -52,15 +43,15 @@ namespace Nuake {
|
||||
TransformComponent& transform = localTransformView.get<TransformComponent>(tv);
|
||||
if (transform.Dirty)
|
||||
{
|
||||
Matrix4 localTransform = Matrix4(1.0f);
|
||||
auto& localTranslate = transform.GetLocalPosition();
|
||||
auto& localRot = transform.GetLocalRotation();
|
||||
auto& localScale = transform.GetLocalScale();
|
||||
localRot.w *= -1.0;
|
||||
localTransform = glm::translate(localTransform, localTranslate);
|
||||
localTransform = localTransform * glm::toMat4(localRot);
|
||||
localTransform = glm::scale(localTransform, localScale);
|
||||
transform.SetLocalTransform(localTransform);
|
||||
const Vector3& localTranslate = transform.GetLocalPosition();
|
||||
const Quat& localRot = glm::normalize(transform.GetLocalRotation());
|
||||
const Vector3& localScale = transform.GetLocalScale();
|
||||
const Matrix4& translationMatrix = glm::translate(Matrix4(1.0f), localTranslate);
|
||||
const Matrix4& rotationMatrix = glm::mat4_cast(localRot);
|
||||
const Matrix4& scaleMatrix = glm::scale(Matrix4(1.0f), localScale);
|
||||
const Matrix4& newLocalTransform = translationMatrix * rotationMatrix * scaleMatrix;
|
||||
|
||||
transform.SetLocalTransform(newLocalTransform);
|
||||
transform.Dirty = false;
|
||||
}
|
||||
}
|
||||
@@ -70,9 +61,8 @@ namespace Nuake {
|
||||
for (auto e : transformView)
|
||||
{
|
||||
auto [parent, transform] = transformView.get<ParentComponent, TransformComponent>(e);
|
||||
|
||||
if (!parent.HasParent)
|
||||
{
|
||||
{
|
||||
// If no parents, then globalTransform is local transform.
|
||||
transform.SetGlobalTransform(transform.GetLocalTransform());
|
||||
transform.SetGlobalPosition(transform.GetLocalPosition());
|
||||
@@ -93,21 +83,41 @@ namespace Nuake {
|
||||
{
|
||||
TransformComponent& transformComponent = parentComponent.Parent.GetComponent<TransformComponent>();
|
||||
|
||||
globalPosition = transformComponent.GetLocalPosition() + (transformComponent.GetLocalRotation() * globalPosition);
|
||||
globalPosition = transformComponent.GetLocalPosition() + (globalPosition);
|
||||
|
||||
globalScale *= transformComponent.GetLocalScale();
|
||||
globalOrientation = transformComponent.GetLocalRotation() * globalOrientation;
|
||||
globalTransform = transformComponent.GetLocalTransform() * globalTransform;
|
||||
|
||||
globalOrientation = transformComponent.GetLocalRotation() * globalOrientation;
|
||||
globalScale *= transformComponent.GetLocalScale();
|
||||
|
||||
NameComponent& nameComponent = parentComponent.Parent.GetComponent<NameComponent>();
|
||||
|
||||
parentComponent = parentComponent.Parent.GetComponent<ParentComponent>();
|
||||
}
|
||||
|
||||
|
||||
transform.SetGlobalPosition(globalPosition);
|
||||
transform.SetGlobalRotation(globalOrientation);
|
||||
transform.SetGlobalScale(globalScale);
|
||||
transform.SetGlobalTransform(globalTransform);
|
||||
}
|
||||
|
||||
auto camView = m_Scene->m_Registry.view<TransformComponent, CameraComponent>();
|
||||
for (auto& e : camView)
|
||||
{
|
||||
auto [transform, camera] = camView.get<TransformComponent, CameraComponent>(e);
|
||||
Matrix4 cameraTransform = camera.CameraInstance->GetTransformRotation();
|
||||
|
||||
camera.CameraInstance->Translation = transform.GlobalTranslation;
|
||||
|
||||
auto globalRotation = transform.GetGlobalRotation();
|
||||
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;
|
||||
|
||||
Vector4 right = Vector4(1, 0, 0, 1);
|
||||
const auto globalRight = rotationMatrix * right;
|
||||
camera.CameraInstance->Direction = globalForward;
|
||||
camera.CameraInstance->Right = globalRight;
|
||||
; camera.CameraInstance->SetTransform(glm::inverse(translationMatrix * rotationMatrix));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ namespace Nuake {
|
||||
RegisterMethod("SetTranslation_(_,_,_,_)", (void*)SetTranslation);
|
||||
RegisterMethod("GetRotation_(_)", (void*)GetRotation);
|
||||
RegisterMethod("SetRotation_(_,_,_,_)", (void*)SetRotation);
|
||||
RegisterMethod("SetLookAt_(_,_,_,_)", (void*)SetLookAt);
|
||||
|
||||
RegisterMethod("SetLightIntensity_(_,_)", (void*)SetLightIntensity);
|
||||
RegisterMethod("GetLightIntensity_(_)", (void*)GetLightIntensity);
|
||||
|
||||
@@ -263,6 +265,20 @@ namespace Nuake {
|
||||
transform.SetLocalRotation(QuatFromEuler(x, y, z));
|
||||
}
|
||||
|
||||
static void SetLookAt(WrenVM* vm)
|
||||
{
|
||||
double handle = wrenGetSlotDouble(vm, 1);
|
||||
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
|
||||
auto& transform = ent.GetComponent<TransformComponent>();
|
||||
// set the slots
|
||||
float x = (float)wrenGetSlotDouble(vm, 2);
|
||||
float y = (float)wrenGetSlotDouble(vm, 3);
|
||||
float z = (float)wrenGetSlotDouble(vm, 4);
|
||||
Vector3 targetPosition = Vector3(x, y, z);
|
||||
Quat rotation = LookAt(transform.GetGlobalPosition(), transform.GetGlobalPosition() + targetPosition);
|
||||
transform.SetLocalRotation(rotation);
|
||||
}
|
||||
|
||||
static void SetTranslation(WrenVM* vm)
|
||||
{
|
||||
double handle = wrenGetSlotDouble(vm, 1);
|
||||
|
||||
Reference in New Issue
Block a user