add ImGuizmo

This commit is contained in:
2025-11-29 12:09:40 +08:00
parent 788081479a
commit 5b99111bc4
11 changed files with 126 additions and 13 deletions

View File

@ -4,6 +4,9 @@
#include "EditorLayer.h"
#include "ImGuizmo.h"
#include "Prism/Core/KeyCodes.h"
namespace Prism
{
enum class PropertyFlag
@ -201,6 +204,9 @@ namespace Prism
m_Light.Direction = {-0.5f, -0.5f, 1.0f};
m_Light.Radiance = {1.0f, 1.0f, 1.0f};
m_Transform = glm::scale(glm::mat4(1.0f), glm::vec3(m_MeshScale));
}
void EditorLayer::OnDetach()
@ -305,7 +311,7 @@ namespace Prism
{
if (m_Mesh)
{
m_Mesh->Render(deltaTime, scale(mat4(1.0f), vec3(m_MeshScale)), m_MeshMaterial);
m_Mesh->Render(deltaTime, m_Transform, m_MeshMaterial);
}
}
@ -678,9 +684,20 @@ namespace Prism
auto viewportSize = ImGui::GetContentRegionAvail();
m_Framebuffer->Resize((uint32_t)viewportSize.x, (uint32_t)viewportSize.y);
m_FinalPresentBuffer->Resize((uint32_t)viewportSize.x, (uint32_t)viewportSize.y);
m_Camera.SetProjectionMatrix(glm::perspectiveFov(glm::radians(45.0f), viewportSize.x, viewportSize.y, 0.1f,
10000.0f));
m_Camera.SetProjectionMatrix(glm::perspectiveFov(glm::radians(45.0f), viewportSize.x, viewportSize.y, 0.1f, 10000.0f));
ImGui::Image((ImTextureRef)m_FinalPresentBuffer->GetColorAttachmentRendererID(), viewportSize, {0, 1}, {1, 0});
// ImGuizmo
if (m_GizmoType != -1)
{
const float rw = (float)ImGui::GetWindowWidth();
const float rh = (float)ImGui::GetWindowHeight();
ImGuizmo::SetOrthographic(false);
ImGuizmo::SetDrawlist();
ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, rw, rh);
ImGuizmo::Manipulate(glm::value_ptr(m_Camera.GetViewMatrix()), glm::value_ptr(m_Camera.GetProjectionMatrix()), (ImGuizmo::OPERATION)m_GizmoType, ImGuizmo::LOCAL, glm::value_ptr(m_Transform));
}
ImGui::End();
ImGui::PopStyleVar();
@ -702,6 +719,27 @@ namespace Prism
void EditorLayer::OnEvent(Event& e)
{
Layer::OnEvent(e);
EventDispatcher dispatcher(e);
dispatcher.Dispatch<KeyPressedEvent>(HZ_BIND_EVENT_FN(EditorLayer::OnKeyPressedEvent));
}
bool EditorLayer::OnKeyPressedEvent(KeyPressedEvent& e)
{
switch (e.GetKeyCode())
{
case PM_KEY_Q:
m_GizmoType = -1;
break;
case PM_KEY_W:
m_GizmoType = ImGuizmo::OPERATION::TRANSLATE;
break;
case PM_KEY_E:
m_GizmoType = ImGuizmo::OPERATION::ROTATE;
break;
case PM_KEY_R:
m_GizmoType = ImGuizmo::OPERATION::SCALE;
break;
}
return false;
}
}

View File

@ -21,6 +21,9 @@ public:
virtual void OnImGuiRender() override;
virtual void OnEvent(Event& e) override;
private:
bool OnKeyPressedEvent(KeyPressedEvent& e);
private:
float m_ClearColor[4];
@ -54,6 +57,10 @@ private:
Ref<Shader> m_Shader;
Ref<Shader> m_PBRShader;
// Imguizmo
int m_GizmoType = -1; // -1 = no gizmo
glm::mat4 m_Transform;
struct AlbedoInput
{