Skip to content

Commit

Permalink
Added gizmos
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCherno committed Dec 3, 2020
1 parent 5f9c132 commit b833c1b
Show file tree
Hide file tree
Showing 16 changed files with 253 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
[submodule "Hazel/vendor/yaml-cpp"]
path = Hazel/vendor/yaml-cpp
url = https://github.com/thecherno/yaml-cpp
[submodule "Hazel/vendor/ImGuizmo"]
path = Hazel/vendor/ImGuizmo
url = https://github.com/thecherno/imguizmo
9 changes: 8 additions & 1 deletion Hazel/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ project "Hazel"
"vendor/stb_image/**.cpp",
"vendor/glm/glm/**.hpp",
"vendor/glm/glm/**.inl",

"vendor/ImGuizmo/ImGuizmo.h",
"vendor/ImGuizmo/ImGuizmo.cpp"
}

defines
Expand All @@ -36,7 +39,8 @@ project "Hazel"
"%{IncludeDir.glm}",
"%{IncludeDir.stb_image}",
"%{IncludeDir.entt}",
"%{IncludeDir.yaml_cpp}"
"%{IncludeDir.yaml_cpp}",
"%{IncludeDir.ImGuizmo}"
}

links
Expand All @@ -48,6 +52,9 @@ project "Hazel"
"opengl32.lib"
}

filter "files:vendor/ImGuizmo/**.cpp"
flags { "NoPCH" }

filter "system:windows"
systemversion "latest"

Expand Down
3 changes: 3 additions & 0 deletions Hazel/src/Hazel/ImGui/ImGuiLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <GLFW/glfw3.h>
#include <glad/glad.h>

#include "ImGuizmo.h"

namespace Hazel {

ImGuiLayer::ImGuiLayer()
Expand Down Expand Up @@ -84,6 +86,7 @@ namespace Hazel {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGuizmo::BeginFrame();
}

void ImGuiLayer::End()
Expand Down
81 changes: 81 additions & 0 deletions Hazel/src/Hazel/Math/Math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "hzpch.h"
#include "Math.h"

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/matrix_decompose.hpp>

namespace Hazel::Math {

bool DecomposeTransform(const glm::mat4& transform, glm::vec3& translation, glm::vec3& rotation, glm::vec3& scale)
{
// From glm::decompose in matrix_decompose.inl

using namespace glm;
using T = float;

mat4 LocalMatrix(transform);

// Normalize the matrix.
if (epsilonEqual(LocalMatrix[3][3], static_cast<float>(0), epsilon<T>()))
return false;

// First, isolate perspective. This is the messiest.
if (
epsilonNotEqual(LocalMatrix[0][3], static_cast<T>(0), epsilon<T>()) ||
epsilonNotEqual(LocalMatrix[1][3], static_cast<T>(0), epsilon<T>()) ||
epsilonNotEqual(LocalMatrix[2][3], static_cast<T>(0), epsilon<T>()))
{
// Clear the perspective partition
LocalMatrix[0][3] = LocalMatrix[1][3] = LocalMatrix[2][3] = static_cast<T>(0);
LocalMatrix[3][3] = static_cast<T>(1);
}

// Next take care of translation (easy).
translation = vec3(LocalMatrix[3]);
LocalMatrix[3] = vec4(0, 0, 0, LocalMatrix[3].w);

vec3 Row[3], Pdum3;

// Now get scale and shear.
for (length_t i = 0; i < 3; ++i)
for (length_t j = 0; j < 3; ++j)
Row[i][j] = LocalMatrix[i][j];

// Compute X scale factor and normalize first row.
scale.x = length(Row[0]);
Row[0] = detail::scale(Row[0], static_cast<T>(1));
scale.y = length(Row[1]);
Row[1] = detail::scale(Row[1], static_cast<T>(1));
scale.z = length(Row[2]);
Row[2] = detail::scale(Row[2], static_cast<T>(1));

// At this point, the matrix (in rows[]) is orthonormal.
// Check for a coordinate system flip. If the determinant
// is -1, then negate the matrix and the scaling factors.
#if 0
Pdum3 = cross(Row[1], Row[2]); // v3Cross(row[1], row[2], Pdum3);
if (dot(Row[0], Pdum3) < 0)
{
for (length_t i = 0; i < 3; i++)
{
scale[i] *= static_cast<T>(-1);
Row[i] *= static_cast<T>(-1);
}
}
#endif

rotation.y = asin(-Row[0][2]);
if (cos(rotation.y) != 0) {
rotation.x = atan2(Row[1][2], Row[2][2]);
rotation.z = atan2(Row[0][1], Row[0][0]);
}
else {
rotation.x = atan2(-Row[2][0], Row[1][1]);
rotation.z = 0;
}


return true;
}

}
9 changes: 9 additions & 0 deletions Hazel/src/Hazel/Math/Math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <glm/glm.hpp>

namespace Hazel::Math {

bool DecomposeTransform(const glm::mat4& transform, glm::vec3& outTranslation, glm::vec3& outRotation, glm::vec3& outScale);

}
7 changes: 4 additions & 3 deletions Hazel/src/Hazel/Scene/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/quaternion.hpp>

#include "SceneCamera.h"
#include "ScriptableEntity.h"

Expand Down Expand Up @@ -31,9 +34,7 @@ namespace Hazel {

glm::mat4 GetTransform() const
{
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), Rotation.x, { 1, 0, 0 })
* glm::rotate(glm::mat4(1.0f), Rotation.y, { 0, 1, 0 })
* glm::rotate(glm::mat4(1.0f), Rotation.z, { 0, 0, 1 });
glm::mat4 rotation = glm::toMat4(glm::quat(Rotation));

return glm::translate(glm::mat4(1.0f), Translation)
* rotation
Expand Down
12 changes: 12 additions & 0 deletions Hazel/src/Hazel/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ namespace Hazel {

}

Entity Scene::GetPrimaryCameraEntity()
{
auto view = m_Registry.view<CameraComponent>();
for (auto entity : view)
{
const auto& camera = view.get<CameraComponent>(entity);
if (camera.Primary)
return Entity{entity, this};
}
return {};
}

template<typename T>
void Scene::OnComponentAdded(Entity entity, T& component)
{
Expand Down
2 changes: 2 additions & 0 deletions Hazel/src/Hazel/Scene/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Hazel {

void OnUpdate(Timestep ts);
void OnViewportResize(uint32_t width, uint32_t height);

Entity GetPrimaryCameraEntity();
private:
template<typename T>
void OnComponentAdded(Entity entity, T& component);
Expand Down
1 change: 1 addition & 0 deletions Hazel/vendor/ImGuizmo
Submodule ImGuizmo added at 218d60
47 changes: 47 additions & 0 deletions Hazelnut/assets/scenes/PinkCube.hazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Scene: Untitled
Entities:
- Entity: 12837192831273
TagComponent:
Tag: Top
TransformComponent:
Translation: [0, 0.5, 0]
Rotation: [1.57079637, 0.785398185, 0]
Scale: [1, 1, 1]
SpriteRendererComponent:
Color: [0.876447856, 0, 0.834712803, 1]
- Entity: 12837192831273
TagComponent:
Tag: Camera
TransformComponent:
Translation: [0, 1.70000005, 4]
Rotation: [-0.404916406, 0, 0]
Scale: [1, 1, 1]
CameraComponent:
Camera:
ProjectionType: 0
PerspectiveFOV: 0.52359879
PerspectiveNear: 0.00999999978
PerspectiveFar: 1000
OrthographicSize: 10
OrthographicNear: -1
OrthographicFar: 1
Primary: true
FixedAspectRatio: false
- Entity: 12837192831273
TagComponent:
Tag: Right
TransformComponent:
Translation: [0.351999998, 0, 0.349999994]
Rotation: [0, 0.785398185, 0]
Scale: [1, 1, 1]
SpriteRendererComponent:
Color: [0.54842025, 0, 0.586872578, 1]
- Entity: 12837192831273
TagComponent:
Tag: Left
TransformComponent:
Translation: [-0.354999989, 0, 0.349999994]
Rotation: [0, -0.785398185, 0]
Scale: [1, 1, 1]
SpriteRendererComponent:
Color: [1, 0, 0.949807167, 1]
18 changes: 9 additions & 9 deletions Hazelnut/imgui.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Window][DockSpace Demo]
Pos=0,0
Size=1600,900
Size=2560,1387
Collapsed=0

[Window][Debug##Default]
Expand All @@ -16,14 +16,14 @@ Collapsed=0
DockId=0x00000004,0

[Window][Viewport]
Pos=372,24
Size=993,876
Pos=595,24
Size=1590,1363
Collapsed=0
DockId=0x00000003,0

[Window][Scene Hierarchy]
Pos=0,24
Size=370,406
Size=593,631
Collapsed=0
DockId=0x00000005,0

Expand All @@ -34,19 +34,19 @@ Size=1421,1027
Collapsed=0

[Window][Properties]
Pos=0,432
Size=370,468
Pos=0,657
Size=593,730
Collapsed=0
DockId=0x00000006,0

[Window][Stats]
Pos=1367,24
Size=233,876
Pos=2187,24
Size=373,1363
Collapsed=0
DockId=0x00000008,0

[Docking][Data]
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=221,241 Size=1600,876 Split=X Selected=0x995B0CF8
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=0,47 Size=2560,1363 Split=X Selected=0x995B0CF8
DockNode ID=0x00000007 Parent=0x3BC79352 SizeRef=1365,1094 Split=X
DockNode ID=0x00000001 Parent=0x00000007 SizeRef=370,696 Split=Y Selected=0x9A68760C
DockNode ID=0x00000005 Parent=0x00000001 SizeRef=593,322 Selected=0x9A68760C
Expand Down
3 changes: 2 additions & 1 deletion Hazelnut/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ project "Hazelnut"
"%{wks.location}/Hazel/src",
"%{wks.location}/Hazel/vendor",
"%{IncludeDir.glm}",
"%{IncludeDir.entt}"
"%{IncludeDir.entt}",
"%{IncludeDir.ImGuizmo}"
}

links
Expand Down
Loading

0 comments on commit b833c1b

Please sign in to comment.