diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index c724b09b..167ec41c 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -43,6 +43,7 @@ struct LaunchSettings std::string projectPath; }; + std::vector ParseArguments(int argc, char* argv[]) { std::vector args; @@ -53,6 +54,7 @@ std::vector ParseArguments(int argc, char* argv[]) return args; } + LaunchSettings ParseLaunchSettings(const std::vector& arguments) { LaunchSettings launchSettings; @@ -170,8 +172,9 @@ int ApplicationMain(int argc, char* argv[]) if (currentScene) { camera = currentScene->m_EditorCamera; + //currentScene->m_SceneRenderer->GetGBuffer().GetTexture(GL_COLOR_ATTACHMENT3)->Unbind(); + //glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, currentScene->m_SceneRenderer->GetGBuffer().GetTexture(GL_COLOR_ATTACHMENT3)->GetID(), 0); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, currentScene->m_SceneRenderer->GetGBuffer().GetTexture(GL_DEPTH_ATTACHMENT)->GetID(), 0); - //glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, currentScene->m_SceneRenderer->GetGBuffer().GetTexture(GL_COLOR_ATTACHMENT3)->GetID(), 0); } if (currentScene && !Nuake::Engine::IsPlayMode()) @@ -194,11 +197,9 @@ int ApplicationMain(int argc, char* argv[]) } //glDepthMask(true); - } sceneFramebuffer->Unbind(); - - + // Update & Draw editor editor.Draw(); editor.Update(Nuake::Engine::GetTimestep()); diff --git a/Editor/src/Actions/EditorSelection.h b/Editor/src/Actions/EditorSelection.h index 0cdfcf69..ce465ccd 100644 --- a/Editor/src/Actions/EditorSelection.h +++ b/Editor/src/Actions/EditorSelection.h @@ -1,11 +1,14 @@ #pragma once #include "src/Core/Core.h" #include "src/Scene/Entities/Entity.h" +#include "src/Rendering/SceneRenderer.h" #include "src/Resource/Resource.h" #include "Engine.h" -#include "src/Rendering/SceneRenderer.h" -enum EditorSelectionType { + + +enum EditorSelectionType +{ None, Entity, File, @@ -13,16 +16,19 @@ enum EditorSelectionType { Directory }; -class EditorSelection { -public: +class EditorSelection +{ + +public: EditorSelectionType Type = EditorSelectionType::None; - Nuake::Entity Entity; Ref File; Ref Directory; + Nuake::Entity Entity; Nuake::Resource Resource; +public: EditorSelection() { Type = None; diff --git a/Editor/src/Misc/GizmoDrawer.cpp b/Editor/src/Misc/GizmoDrawer.cpp index 0d42b6ea..f87e7145 100644 --- a/Editor/src/Misc/GizmoDrawer.cpp +++ b/Editor/src/Misc/GizmoDrawer.cpp @@ -383,7 +383,7 @@ void GizmoDrawer::DrawGizmos(Ref scene, bool occluded) for (auto e : camView) { gizmoShader->SetUniformTex("gizmo_texture", TextureManager::Get()->GetTexture("Resources/Gizmos/camera.png").get()); - gizmoShader->SetUniform1i("u_EntityID", (uint32_t)e + 1); + gizmoShader->SetUniform1i("u_EntityID", ((int32_t)(uint32_t)(e)) + 1); auto [transform, camera] = scene->m_Registry.get(e); auto initialTransform = transform.GetGlobalTransform(); @@ -399,14 +399,13 @@ void GizmoDrawer::DrawGizmos(Ref scene, bool occluded) renderList.AddToRenderList(Renderer::QuadMesh, particleTransform); renderList.Flush(gizmoShader, true); } - // Lights auto lightView = scene->m_Registry.view(); for (auto e : lightView) { gizmoShader->SetUniformTex("gizmo_texture", TextureManager::Get()->GetTexture("Resources/Gizmos/light.png").get()); - gizmoShader->SetUniform1i("u_EntityID", (uint32_t)e + 1); + gizmoShader->SetUniform1i("u_EntityID", ((int32_t)(uint32_t)(e)) + 1); auto [transform, light] = scene->m_Registry.get(e); auto initialTransform = transform.GetGlobalTransform(); @@ -423,13 +422,12 @@ void GizmoDrawer::DrawGizmos(Ref scene, bool occluded) renderList.Flush(gizmoShader, true); } - // Player auto characterControllerView = scene->m_Registry.view(); for (auto e : characterControllerView) { gizmoShader->SetUniformTex("gizmo_texture", TextureManager::Get()->GetTexture("Resources/Gizmos/player.png").get()); - gizmoShader->SetUniform1i("u_EntityID", static_cast(e) + 1); + gizmoShader->SetUniform1i("u_EntityID", ((int32_t)(uint32_t)(e)) + 1); auto [transform, characterControllerComponent] = scene->m_Registry.get(e); auto initialTransform = transform.GetGlobalTransform(); @@ -447,13 +445,12 @@ void GizmoDrawer::DrawGizmos(Ref scene, bool occluded) } - // Bones auto boneView = scene->m_Registry.view(); for (auto e : boneView) { gizmoShader->SetUniformTex("gizmo_texture", TextureManager::Get()->GetTexture("Resources/Gizmos/bone.png").get()); - gizmoShader->SetUniform1i("u_EntityID", static_cast(e) + 1); + gizmoShader->SetUniform1i("u_EntityID", ((int32_t)(uint32_t)(e)) + 1); auto [transform, boneComponent] = scene->m_Registry.get(e); auto initialTransform = transform.GetGlobalTransform(); @@ -475,7 +472,7 @@ void GizmoDrawer::DrawGizmos(Ref scene, bool occluded) for (auto e : audioView) { gizmoShader->SetUniformTex("gizmo_texture", TextureManager::Get()->GetTexture("Resources/Gizmos/speaker.png").get()); - gizmoShader->SetUniform1i("u_EntityID", static_cast(e) + 1); + gizmoShader->SetUniform1i("u_EntityID", ((int32_t)(uint32_t)(e)) + 1); auto [transformComponent, audioEmitterComponent] = scene->m_Registry.get(e); auto initialTransform = transformComponent.GetGlobalTransform(); diff --git a/Editor/src/Misc/ThumbnailManager.cpp b/Editor/src/Misc/ThumbnailManager.cpp index 986f3d8a..1dfd48ea 100644 --- a/Editor/src/Misc/ThumbnailManager.cpp +++ b/Editor/src/Misc/ThumbnailManager.cpp @@ -71,7 +71,7 @@ Ref ThumbnailManager::GenerateThumbnail(const std::string& path, // Gbuffer pass m_Framebuffer->Bind(); { - RenderCommand::SetClearColor({ 0.2, 0.2, 0.2, 0.0f }); + RenderCommand::SetClearColor({ 0.0f, 0.0f, 0.0f, 0.0f }); m_Framebuffer->Clear(); RenderCommand::Disable(RendererEnum::BLENDING); diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 15173aa6..dedebd00 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -270,27 +270,43 @@ namespace Nuake { if (ImGui::GetIO().WantCaptureMouse && m_IsHoveringViewport && Input::IsMouseButtonPressed(GLFW_MOUSE_BUTTON_1) && !ImGuizmo::IsUsing() && m_IsViewportFocused) { const auto windowPosNuake = Vector2(windowPos.x, windowPos.y); + auto& gbuffer = Engine::GetCurrentScene()->m_SceneRenderer->GetGBuffer(); auto pixelPos = Input::GetMousePosition() - windowPosNuake; pixelPos.y = gbuffer.GetSize().y - pixelPos.y; // imgui coords are inverted on the Y axis - gbuffer.Bind(); - - if (const int result = gbuffer.ReadPixel(3, pixelPos); result > 0) + auto gizmoBuffer = Engine::GetCurrentWindow()->GetFrameBuffer(); + gizmoBuffer->Bind(); + bool foundSomethingToSelect = false; + if (const int result = gizmoBuffer->ReadPixel(3, pixelPos); result > 0) { auto ent = Entity{ (entt::entity)(result - 1), Engine::GetCurrentScene().get() }; if (ent.IsValid()) { Selection = EditorSelection(ent); + foundSomethingToSelect = true; } } - else + gizmoBuffer->Unbind(); + + if(!foundSomethingToSelect) { - Selection = EditorSelection(); // None + gbuffer.Bind(); + if (const int result = gbuffer.ReadPixel(3, pixelPos); result > 0) + { + auto ent = Entity{ (entt::entity)(result - 1), Engine::GetCurrentScene().get() }; + if (ent.IsValid()) + { + Selection = EditorSelection(ent); + } + } + else + { + Selection = EditorSelection(); // None + } + gbuffer.Unbind(); } - - gbuffer.Unbind(); } } else diff --git a/Nuake/src/Resource/StaticResources.cpp b/Nuake/src/Resource/StaticResources.cpp index 4f6b807b..5c35e0a1 100644 --- a/Nuake/src/Resource/StaticResources.cpp +++ b/Nuake/src/Resource/StaticResources.cpp @@ -235175,7 +235175,7 @@ const std::string Resources_Shaders_gizmo_shader_path = R"(Resources/Shaders/giz 0x20, 0x6f, 0x75, 0x74, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0d, 0x0a, 0x6c, 0x61, 0x79, 0x6f, 0x75, 0x74, 0x28, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, - 0x6e, 0x20, 0x3d, 0x20, 0x31, 0x29, 0x20, 0x6f, 0x75, 0x74, 0x20, 0x69, + 0x6e, 0x20, 0x3d, 0x20, 0x33, 0x29, 0x20, 0x6f, 0x75, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x67, 0x45, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x49, 0x44, 0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x61, 0x5f, 0x55, 0x56, 0x3b, 0x0d, 0x0a, 0x69, 0x6e, 0x20, 0x6d, @@ -235206,13 +235206,13 @@ const std::string Resources_Shaders_gizmo_shader_path = R"(Resources/Shaders/giz 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x2a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28, 0x31, 0x2c, 0x20, 0x31, 0x2c, 0x20, 0x31, 0x2c, 0x20, 0x75, 0x5f, 0x4f, 0x70, 0x61, 0x63, 0x69, 0x74, 0x79, 0x29, 0x3b, 0x0d, 0x0a, - 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, - 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, - 0x0d, 0x0a, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x67, 0x45, 0x6e, 0x74, - 0x69, 0x74, 0x79, 0x49, 0x44, 0x20, 0x3d, 0x20, 0x69, 0x6e, 0x74, 0x28, - 0x30, 0x29, 0x3b, 0x0d, 0x0a, 0x7d + 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x67, 0x45, 0x6e, 0x74, 0x69, 0x74, + 0x79, 0x49, 0x44, 0x20, 0x3d, 0x20, 0x75, 0x5f, 0x45, 0x6e, 0x74, 0x69, + 0x74, 0x79, 0x49, 0x44, 0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, + 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0d, 0x0a, 0x7d }; - unsigned int Resources_Shaders_gizmo_shader_len = 990; + unsigned int Resources_Shaders_gizmo_shader_len = 994; // Data for file: Resources_Shaders_line_shader_path const std::string Resources_Shaders_line_shader_path = R"(Resources/Shaders/line.shader)"; diff --git a/Nuake/src/Scene/Components/TransformComponent.cpp b/Nuake/src/Scene/Components/TransformComponent.cpp index c83c19e5..dac22795 100644 --- a/Nuake/src/Scene/Components/TransformComponent.cpp +++ b/Nuake/src/Scene/Components/TransformComponent.cpp @@ -37,6 +37,7 @@ namespace Nuake void TransformComponent::SetGlobalRotation(const Quat& rotation) { GlobalRotation = rotation; + Dirty = true; } Vector3 TransformComponent::GetLocalPosition() const diff --git a/Nuake/src/Scene/Components/TransformComponent.h b/Nuake/src/Scene/Components/TransformComponent.h index de35568b..2c027866 100644 --- a/Nuake/src/Scene/Components/TransformComponent.h +++ b/Nuake/src/Scene/Components/TransformComponent.h @@ -18,6 +18,8 @@ namespace Nuake Matrix4 GlobalTransform; public: bool Dirty = true; + bool GlobalDirty = true; + TransformComponent(); Matrix4 GetGlobalTransform() const; diff --git a/Nuake/src/Scene/Systems/AnimationSystem.cpp b/Nuake/src/Scene/Systems/AnimationSystem.cpp index c1783fd1..7a6b5710 100644 --- a/Nuake/src/Scene/Systems/AnimationSystem.cpp +++ b/Nuake/src/Scene/Systems/AnimationSystem.cpp @@ -72,7 +72,6 @@ namespace Nuake transformComponent.SetLocalRotation(localRotation); transformComponent.SetLocalScale(localScale); transformComponent.SetLocalTransform(finalTransform); - transformComponent.Dirty = false; } } diff --git a/Nuake/src/Scene/Systems/TransformSystem.cpp b/Nuake/src/Scene/Systems/TransformSystem.cpp index e9ee86f3..61c84c91 100644 --- a/Nuake/src/Scene/Systems/TransformSystem.cpp +++ b/Nuake/src/Scene/Systems/TransformSystem.cpp @@ -41,6 +41,8 @@ namespace Nuake for (auto tv : localTransformView) { TransformComponent& transform = localTransformView.get(tv); + Entity currentEntity = { tv, m_Scene }; + ParentComponent& parentComponent = currentEntity.GetComponent(); if (transform.Dirty) { const Vector3& localTranslate = transform.GetLocalPosition(); @@ -51,6 +53,9 @@ namespace Nuake const Matrix4& scaleMatrix = glm::scale(Matrix4(1.0f), localScale); const Matrix4& newLocalTransform = translationMatrix * rotationMatrix * scaleMatrix; + transform.GlobalDirty = true; + UpdateDirtyFlagRecursive(currentEntity); + transform.SetLocalTransform(newLocalTransform); transform.Dirty = false; } @@ -77,7 +82,7 @@ namespace Nuake Vector3 globalPosition = transform.GetLocalPosition(); Quat globalOrientation = transform.GetLocalRotation(); Vector3 globalScale = transform.GetLocalScale(); - + ParentComponent parentComponent = currentParent.GetComponent(); #define FRAME_PERFECT_TRANSFORM #ifndef FRAME_PERFECT_TRANSFORM @@ -92,15 +97,16 @@ namespace Nuake globalTransform = transformComponent.GetGlobalTransform() * globalTransform; } #else + bool exitEarly = false; while (parentComponent.HasParent) { TransformComponent& transformComponent = parentComponent.Parent.GetComponent(); globalPosition = transformComponent.GetLocalPosition() + (globalPosition); - globalScale *= transformComponent.GetLocalScale(); globalOrientation = transformComponent.GetLocalRotation() * globalOrientation; globalTransform = transformComponent.GetLocalTransform() * globalTransform; + transformComponent.GlobalDirty = false; NameComponent& nameComponent = parentComponent.Parent.GetComponent(); parentComponent = parentComponent.Parent.GetComponent(); @@ -134,4 +140,41 @@ namespace Nuake ; camera.CameraInstance->SetTransform(glm::inverse(translationMatrix * rotationMatrix)); } } + + void TransformSystem::UpdateDirtyFlagRecursive(Entity& entity) + { + auto& parentComponent = entity.GetComponent(); + + for (auto& c : parentComponent.Children) + { + + auto& childParentComponent = c.GetComponent(); + childParentComponent.GlobalDirty = true; + + UpdateDirtyFlagRecursive(c); + } + } + + void TransformSystem::CalculateGlobalTransform(Entity& entity) + { + auto& parentComponent = entity.GetComponent(); + auto& transformComponent = entity.GetComponent(); + auto& parentTransformComponent = parentComponent.Parent.GetComponent(); + + Vector3 globalPosition = parentTransformComponent.GetGlobalPosition() + Vector3(transformComponent.GetLocalPosition()); + Quat globalRotation = parentTransformComponent.GetGlobalRotation() * transformComponent.GetLocalRotation(); + Vector3 globalScale = parentTransformComponent.GetGlobalScale() * transformComponent.GetGlobalScale(); + auto globalTransform = transformComponent.GetGlobalTransform() * parentTransformComponent.GetGlobalTransform(); + + transformComponent.SetGlobalPosition(Vector3(globalPosition)); + transformComponent.SetGlobalRotation(globalRotation); + transformComponent.SetGlobalScale(globalScale); + transformComponent.SetGlobalTransform(globalTransform); + transformComponent.GlobalDirty = false; + + for (auto& c : parentComponent.Children) + { + CalculateGlobalTransform(c); + } + } } diff --git a/Nuake/src/Scene/Systems/TransformSystem.h b/Nuake/src/Scene/Systems/TransformSystem.h index a6f88596..3fce1ef4 100644 --- a/Nuake/src/Scene/Systems/TransformSystem.h +++ b/Nuake/src/Scene/Systems/TransformSystem.h @@ -2,7 +2,9 @@ #include "System.h" namespace Nuake { + class Scene; + class Entity; class TransformSystem : public System { @@ -14,6 +16,9 @@ namespace Nuake { void FixedUpdate(Timestep ts) override; void Exit() override; + private: void UpdateTransform(); + void UpdateDirtyFlagRecursive(Entity& entity); + void CalculateGlobalTransform(Entity& entity); }; } diff --git a/Nuake/src/Window.cpp b/Nuake/src/Window.cpp index f610012e..ff97117c 100644 --- a/Nuake/src/Window.cpp +++ b/Nuake/src/Window.cpp @@ -68,7 +68,7 @@ namespace Nuake SetWindowIcon("resources/Images/nuake-logo.png"); glfwMakeContextCurrent(m_Window); - SetVSync(true); + SetVSync(false); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { @@ -99,8 +99,10 @@ namespace Nuake //glEnable(GL_CULL_FACE); // Create viewports - m_Framebuffer = CreateRef(true, glm::vec2(1920, 1080)); - m_Framebuffer->SetTexture(CreateRef(glm::vec2(1920, 1080), GL_RGB)); + const Vector2 defaultResolution = Vector2(1, 1); + m_Framebuffer = CreateRef(true, defaultResolution); + m_Framebuffer->SetTexture(CreateRef(defaultResolution, GL_RGB)); + m_Framebuffer->SetTexture(CreateRef(defaultResolution, GL_RED_INTEGER, GL_R32I, GL_INT), GL_COLOR_ATTACHMENT3); // Entity ID InitImgui(); diff --git a/Resources/Shaders/gizmo.shader b/Resources/Shaders/gizmo.shader index 241dce06..ffc694fe 100644 --- a/Resources/Shaders/gizmo.shader +++ b/Resources/Shaders/gizmo.shader @@ -24,7 +24,7 @@ void main() #version 440 core layout(location = 0) out vec4 FragColor; -layout(location = 1) out int gEntityID; +layout(location = 3) out int gEntityID; in vec2 a_UV; in mat4 o_Projection; @@ -42,7 +42,7 @@ void main() vec4 px_color = texture(gizmo_texture, a_UV).rgba; color = px_color * vec4(1, 1, 1, u_Opacity); - FragColor = color; + gEntityID = u_EntityID; - gEntityID = int(0); + FragColor = color; } \ No newline at end of file