Skip to content

Commit 93fee59

Browse files
committed
Added ImGuizmo and implemented a sample grid to the editor
1 parent e6ba53d commit 93fee59

File tree

6 files changed

+141
-84
lines changed

6 files changed

+141
-84
lines changed

src/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ file(GLOB "IMGUI_SRC"
2828
"${PUBLIC_INCLUDE}/imgui/*.h"
2929
)
3030

31+
file(GLOB "IMGUIZMO_SRC"
32+
"${PUBLIC_INCLUDE}/ImGuizmo/*.cpp"
33+
"${PUBLIC_INCLUDE}/ImGuizmo/*.h"
34+
)
35+
3136
add_library("ImGUI" STATIC ${IMGUI_SRC})
37+
add_library("ImGuizmo" STATIC ${IMGUIZMO_SRC})
3238

3339
target_link_libraries(Exeon "d3d12.lib;dxgi.lib;d3dcompiler.lib")
3440
target_link_libraries(Exeon "${LIB}/assimp-vc143-mt.lib;${LIB}/DirectXTK12.lib;${LIB}/spdlogd.lib")
3541
target_link_libraries(Exeon ImGUI)
42+
target_link_libraries(Exeon ImGuizmo)
3643

3744
target_include_directories(Exeon PRIVATE "${PRIVATE_INCLUDE}")
38-
target_include_directories(Exeon PUBLIC "${PUBLIC_INCLUDE}")
45+
include_directories("${PUBLIC_INCLUDE}")

src/Shader/LightPass.hlsl

Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -99,89 +99,16 @@ float3 ReconstructPosition(int2 pixelCoord, uint index)
9999
}
100100

101101
float3 ViewDirectionFromUV(float2 uv)
102-
{
103-
float2 ndc = uv * 2.0f - 1.0f;
104-
105-
float3 rayView = normalize(mul(float4(-ndc.x, ndc.y, 1.0f, 0.0f), InverseProjection).xyz);
106-
107-
float3 rayWorld = normalize(mul(float4(rayView, 0.0f), InverseView).xyz);
102+
{
103+
float2 uvOffset = uv * 2.0f - 1.0f;
104+
uvOffset.y *= -1.0f;
108105

109-
return -rayWorld;
110-
}
111-
112-
113-
float3 SampleSkybox(float3 dir)
114-
{
115-
float3 absDir = abs(dir);
116-
float ma;
117-
float2 uv;
118-
float3 color;
119-
120-
if (absDir.x >= absDir.y && absDir.x >= absDir.z)
121-
{
122-
ma = absDir.x;
123-
if (dir.x > 0)
124-
{
125-
// Right face (+X)
126-
uv = float2(-dir.z, -dir.y) / ma * 0.5 + 0.5;
127-
color = rightTex.Sample(skyboxSampler, uv).rgb;
128-
}
129-
else
130-
{
131-
// Left face (-X)
132-
uv = float2(dir.z, -dir.y) / ma * 0.5 + 0.5;
133-
color = leftTex.Sample(skyboxSampler, uv).rgb;
134-
}
135-
}
136-
else if (absDir.y >= absDir.x && absDir.y >= absDir.z)
137-
{
138-
ma = absDir.y;
139-
if (dir.y > 0)
140-
{
141-
// Top face (+Y)
142-
uv = float2(dir.x, dir.z) / ma * 0.5 + 0.5;
143-
color = topTex.Sample(skyboxSampler, uv).rgb;
144-
}
145-
else
146-
{
147-
// Bottom face (-Y)
148-
uv = float2(dir.x, -dir.z) / ma * 0.5 + 0.5;
149-
color = bottomTex.Sample(skyboxSampler, uv).rgb;
150-
}
151-
}
152-
else
153-
{
154-
ma = absDir.z;
155-
if (dir.z > 0)
156-
{
157-
// Front face (+Z)
158-
uv = float2(dir.x, -dir.y) / ma * 0.5 + 0.5;
159-
color = frontTex.Sample(skyboxSampler, uv).rgb;
160-
}
161-
else
162-
{
163-
// Back face (-Z)
164-
uv = float2(-dir.x, -dir.y) / ma * 0.5 + 0.5;
165-
color = backTex.Sample(skyboxSampler, uv).rgb;
166-
}
167-
}
168-
169-
return color;
106+
float3 dir = float3(uvOffset.x, uvOffset.y, 1.0f);
107+
return normalize(dir);
170108
}
171109

172110
PixelOutput PixelMain(VertexOutput input, uint index : SV_SampleIndex)
173-
{
174-
//float depth = depthTex.Load(input.position.xy, index).r;
175-
//if (depth >= 1.0f - 1e-5)
176-
//{
177-
// float3 dir = ViewDirectionFromUV(input.uv);
178-
// float3 skyColor = SampleSkybox(dir);
179-
180-
// PixelOutput outSky;
181-
// outSky.screen = float4(skyColor, 1.0f);
182-
// return outSky;
183-
//}
184-
111+
{
185112
float3 lightPos = float3(0.f, 1.f, -2.f);
186113
float3 lightColor = float3(100.f, 100.f, 100.f);
187114

src/private/Core/Editor/Editor.cpp

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ Editor* Editor::m_instance;
55
Editor::Editor() {
66
this->m_bMenuOpen = false;
77
this->m_sceneMgr = nullptr;
8+
this->m_inspectorObj = nullptr;
9+
this->m_nWidth = 0;
10+
this->m_nHeight = 0;
811
}
912

10-
void Editor::Init() {
13+
void Editor::Init(UINT nWidth, UINT nHeight) {
1114
this->m_sceneMgr = SceneManager::GetInstance();
1215

1316
ImGuiIO& io = ImGui::GetIO();
@@ -98,6 +101,9 @@ void Editor::Init() {
98101
style->GrabRounding = 3;
99102
style->LogSliderDeadzone = 4;
100103
style->TabRounding = 4;
104+
105+
this->m_nWidth = nWidth;
106+
this->m_nHeight = nHeight;
101107
}
102108

103109
void Editor::Update() {
@@ -122,14 +128,111 @@ void Editor::Update() {
122128
ImGui::EndMainMenuBar();
123129

124130
if (this->m_sceneMgr) {
131+
132+
float identityMatrix[16] = {
133+
1.0f, 0.0f, 0.0f, 0.0f,
134+
0.0f, 1.0f, 0.0f, 0.0f,
135+
0.0f, 0.0f, 1.0f, 0.0f,
136+
0.0f, 0.0f, 0.0f, 1.0f // Posición del cubo (X=1.0f, Y=1.0f, Z=0.0f)
137+
};
138+
139+
ImGuizmo::Enable(true);
140+
125141
ImGui::SetNextWindowSize(ImVec2{ 300.f, 600.f });
126142
ImGui::Begin("Scene");
127143

128144
Scene* scene = this->m_sceneMgr->GetCurrentScene();
145+
Camera* camera = scene->GetCurrentCamera();
146+
147+
Transform cameraTransform = camera->transform;
148+
149+
XMVECTOR eye = XMVectorSet(
150+
cameraTransform.location.x,
151+
cameraTransform.location.y,
152+
cameraTransform.location.z,
153+
1.0f
154+
);
155+
156+
float pitch = XMConvertToRadians(cameraTransform.rotation.x);
157+
float yaw = XMConvertToRadians(cameraTransform.rotation.y);
158+
159+
XMVECTOR forward = XMVectorSet(
160+
cosf(pitch) * sinf(yaw),
161+
-sinf(pitch),
162+
-cosf(pitch) * cosf(yaw),
163+
0.0f
164+
);
165+
166+
XMVECTOR at = XMVectorAdd(eye, forward);
167+
168+
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
169+
170+
this->m_wvp.View = (XMMatrixLookAtLH(eye, at, up));
171+
this->m_wvp.World = (XMMatrixIdentity());
172+
this->m_wvp.Projection = (XMMatrixPerspectiveFovLH(XMConvertToRadians(70.f), static_cast<float>(this->m_nWidth) / static_cast<float>(this->m_nHeight), 0.01f, 3000.f));
173+
174+
/* Convert our View Projection matrices to a length 16 array of floats */
175+
float viewMatrix[16];
176+
float projectionMatrix[16];
177+
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(viewMatrix), this->m_wvp.View);
178+
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(projectionMatrix), this->m_wvp.Projection);
179+
180+
ImGuizmo::DrawGrid(viewMatrix, projectionMatrix, identityMatrix, 100);
181+
182+
float worldMatrix[16];
183+
184+
float objectMatrix[16] = {
185+
1.0f, 0.0f, 0.0f, 0.0f,
186+
0.0f, 1.0f, 0.0f, 0.0f,
187+
0.0f, 0.0f, 1.0f, 0.0f,
188+
1.0f, 1.0f, 0.0f, 1.0f // Posición del cubo (X=1.0f, Y=1.0f, Z=0.0f)
189+
};
129190

130191
for (std::pair<std::string, GameObject*> object : scene->m_gameObjects) {
131-
if(ImGui::Button(object.second->m_name.c_str())) {}
192+
if(ImGui::Button(object.second->m_name.c_str())) {
193+
this->m_inspectorObj = object.second;
194+
}
195+
}
196+
197+
ImGui::SetNextWindowSize(ImVec2{ 300.f, 600.f });
198+
ImGui::SetNextWindowPos(ImVec2{ 1575,180 });
199+
ImGui::Begin("Inspector");
200+
if (this->m_inspectorObj) {
201+
Transform* pObjTransform = &this->m_inspectorObj->transform;
202+
203+
this->m_wvp.World *= (XMMatrixRotationX(XMConvertToRadians(pObjTransform->rotation.x)));
204+
this->m_wvp.World *= (XMMatrixRotationY(XMConvertToRadians(pObjTransform->rotation.y)));
205+
this->m_wvp.World *= (XMMatrixRotationZ(XMConvertToRadians(pObjTransform->rotation.z)));
206+
this->m_wvp.World *= (XMMatrixTranslation(
207+
pObjTransform->location.x,
208+
pObjTransform->location.y,
209+
pObjTransform->location.z));
210+
211+
this->m_wvp.World = XMMatrixTranspose(this->m_wvp.World);
212+
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(worldMatrix), this->m_wvp.World);
213+
214+
ImGuizmo::Manipulate(viewMatrix, projectionMatrix, ImGuizmo::OPERATION::TRANSLATE, ImGuizmo::MODE::WORLD, worldMatrix);
215+
216+
ImGui::Text(this->m_inspectorObj->m_name.c_str());
217+
ImGui::Text("Location");
218+
ImGui::PushItemWidth(100);
219+
220+
ImGui::InputFloat("##X", &pObjTransform->location.x, 0.1f, 1.f, "%.2f");
221+
ImGui::InputFloat("##Y", &pObjTransform->location.y, 0.1f, 1.f, "%.2f");
222+
ImGui::InputFloat("##Z", &pObjTransform->location.z, 0.1f, 1.f, "%.2f");
223+
ImGui::PopItemWidth();
224+
ImGui::Text("Rotation");
225+
ImGui::PushItemWidth(100);
226+
227+
ImGui::InputFloat("##X", &pObjTransform->rotation.x, 5.f, 10.f, "%.2f");
228+
ImGui::InputFloat("##Y", &pObjTransform->rotation.y, 5.f, 10.f, "%.2f");
229+
ImGui::InputFloat("##Z", &pObjTransform->rotation.z, 5.f, 10.f, "%.2f");
230+
ImGui::PopItemWidth();
231+
}
232+
else {
233+
ImGui::Text("Select an object...");
132234
}
235+
ImGui::End();
133236

134237
ImGui::End();
135238
}

src/private/Core/Renderer/D3D12.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void D3D12::Init(HWND hwnd) {
179179

180180
ImGui::StyleColorsDark();
181181

182-
this->m_editor->Init();
182+
this->m_editor->Init(this->m_nWidth, this->m_nHeight);
183183
}
184184

185185
void D3D12::InitDepth() {
@@ -311,6 +311,16 @@ void D3D12::Update() {
311311
ImGui_ImplWin32_NewFrame();
312312
ImGui_ImplDX12_NewFrame();
313313
ImGui::NewFrame();
314+
ImGuizmo::BeginFrame();
315+
316+
ImGuizmo::BeginFrame();
317+
ImGuizmo::SetDrawlist(ImGui::GetBackgroundDrawList());
318+
ImGuizmo::SetRect(0, 0, this->m_nWidth, this->m_nHeight);
319+
320+
// Establecer el área donde se dibujará el guizmo en la ventana
321+
ImGuizmo::SetRect(0.0f, 0.0f, static_cast<float>(this->m_nWidth), static_cast<float>(this->m_nHeight));
322+
323+
314324

315325
this->m_editor->Update();
316326

src/public/Core/Editor/Editor.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
#pragma once
22
#include <iostream>
33
#include <imgui/imgui.h>
4+
#include <ImGuizmo/ImGuizmo.h>
5+
#include <DXMath/DirectXMath.h>
46
#include "Fonts/Roboto.h"
57
#include "Core/Scene/SceneManager.h"
8+
#include "Util.h"
69

710
class Editor {
811
private:
912
static Editor* m_instance;
1013
bool m_bMenuOpen;
1114
SceneManager* m_sceneMgr;
15+
GameObject* m_inspectorObj;
16+
17+
UINT m_nWidth;
18+
UINT m_nHeight;
19+
20+
WVP m_wvp;
1221
public:
1322
Editor();
14-
void Init();
23+
void Init(UINT nWidth, UINT nHeight);
1524
void Update();
1625

1726
static Editor* GetInstance();

src/public/Core/Renderer/D3D12.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <imgui/imgui_impl_dx12.h>
1313
#include <imgui/imgui_impl_win32.h>
1414
#include "Core/Editor/Editor.h"
15+
#include <ImGuizmo/ImGuizmo.h>
1516

1617
using namespace Microsoft::WRL;
1718

0 commit comments

Comments
 (0)