Progress towards gizmo mouse picking

This commit is contained in:
Antoine Pilote
2023-10-10 00:06:21 -04:00
parent 84bd2f3399
commit 90732fca50
13 changed files with 113 additions and 41 deletions

View File

@@ -43,6 +43,7 @@ struct LaunchSettings
std::string projectPath;
};
std::vector<std::string> ParseArguments(int argc, char* argv[])
{
std::vector<std::string> args;
@@ -53,6 +54,7 @@ std::vector<std::string> ParseArguments(int argc, char* argv[])
return args;
}
LaunchSettings ParseLaunchSettings(const std::vector<std::string>& 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());

View File

@@ -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<Nuake::File> File;
Ref<Nuake::Directory> Directory;
Nuake::Entity Entity;
Nuake::Resource Resource;
public:
EditorSelection()
{
Type = None;

View File

@@ -383,7 +383,7 @@ void GizmoDrawer::DrawGizmos(Ref<Scene> 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<TransformComponent, CameraComponent>(e);
auto initialTransform = transform.GetGlobalTransform();
@@ -399,14 +399,13 @@ void GizmoDrawer::DrawGizmos(Ref<Scene> scene, bool occluded)
renderList.AddToRenderList(Renderer::QuadMesh, particleTransform);
renderList.Flush(gizmoShader, true);
}
// Lights
auto lightView = scene->m_Registry.view<TransformComponent, LightComponent>();
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<TransformComponent, LightComponent>(e);
auto initialTransform = transform.GetGlobalTransform();
@@ -423,13 +422,12 @@ void GizmoDrawer::DrawGizmos(Ref<Scene> scene, bool occluded)
renderList.Flush(gizmoShader, true);
}
// Player
auto characterControllerView = scene->m_Registry.view<TransformComponent, CharacterControllerComponent>();
for (auto e : characterControllerView)
{
gizmoShader->SetUniformTex("gizmo_texture", TextureManager::Get()->GetTexture("Resources/Gizmos/player.png").get());
gizmoShader->SetUniform1i("u_EntityID", static_cast<uint32_t>(e) + 1);
gizmoShader->SetUniform1i("u_EntityID", ((int32_t)(uint32_t)(e)) + 1);
auto [transform, characterControllerComponent] = scene->m_Registry.get<TransformComponent, CharacterControllerComponent>(e);
auto initialTransform = transform.GetGlobalTransform();
@@ -447,13 +445,12 @@ void GizmoDrawer::DrawGizmos(Ref<Scene> scene, bool occluded)
}
// Bones
auto boneView = scene->m_Registry.view<TransformComponent, BoneComponent>();
for (auto e : boneView)
{
gizmoShader->SetUniformTex("gizmo_texture", TextureManager::Get()->GetTexture("Resources/Gizmos/bone.png").get());
gizmoShader->SetUniform1i("u_EntityID", static_cast<uint32_t>(e) + 1);
gizmoShader->SetUniform1i("u_EntityID", ((int32_t)(uint32_t)(e)) + 1);
auto [transform, boneComponent] = scene->m_Registry.get<TransformComponent, BoneComponent>(e);
auto initialTransform = transform.GetGlobalTransform();
@@ -475,7 +472,7 @@ void GizmoDrawer::DrawGizmos(Ref<Scene> 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<uint32_t>(e) + 1);
gizmoShader->SetUniform1i("u_EntityID", ((int32_t)(uint32_t)(e)) + 1);
auto [transformComponent, audioEmitterComponent] = scene->m_Registry.get<TransformComponent, AudioEmitterComponent>(e);
auto initialTransform = transformComponent.GetGlobalTransform();

View File

@@ -71,7 +71,7 @@ Ref<Nuake::Texture> 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);

View File

@@ -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

View File

@@ -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)";

View File

@@ -37,6 +37,7 @@ namespace Nuake
void TransformComponent::SetGlobalRotation(const Quat& rotation)
{
GlobalRotation = rotation;
Dirty = true;
}
Vector3 TransformComponent::GetLocalPosition() const

View File

@@ -18,6 +18,8 @@ namespace Nuake
Matrix4 GlobalTransform;
public:
bool Dirty = true;
bool GlobalDirty = true;
TransformComponent();
Matrix4 GetGlobalTransform() const;

View File

@@ -72,7 +72,6 @@ namespace Nuake
transformComponent.SetLocalRotation(localRotation);
transformComponent.SetLocalScale(localScale);
transformComponent.SetLocalTransform(finalTransform);
transformComponent.Dirty = false;
}
}

View File

@@ -41,6 +41,8 @@ namespace Nuake
for (auto tv : localTransformView)
{
TransformComponent& transform = localTransformView.get<TransformComponent>(tv);
Entity currentEntity = { tv, m_Scene };
ParentComponent& parentComponent = currentEntity.GetComponent<ParentComponent>();
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<ParentComponent>();
#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<TransformComponent>();
globalPosition = transformComponent.GetLocalPosition() + (globalPosition);
globalScale *= transformComponent.GetLocalScale();
globalOrientation = transformComponent.GetLocalRotation() * globalOrientation;
globalTransform = transformComponent.GetLocalTransform() * globalTransform;
transformComponent.GlobalDirty = false;
NameComponent& nameComponent = parentComponent.Parent.GetComponent<NameComponent>();
parentComponent = parentComponent.Parent.GetComponent<ParentComponent>();
@@ -134,4 +140,41 @@ namespace Nuake
; camera.CameraInstance->SetTransform(glm::inverse(translationMatrix * rotationMatrix));
}
}
void TransformSystem::UpdateDirtyFlagRecursive(Entity& entity)
{
auto& parentComponent = entity.GetComponent<ParentComponent>();
for (auto& c : parentComponent.Children)
{
auto& childParentComponent = c.GetComponent<TransformComponent>();
childParentComponent.GlobalDirty = true;
UpdateDirtyFlagRecursive(c);
}
}
void TransformSystem::CalculateGlobalTransform(Entity& entity)
{
auto& parentComponent = entity.GetComponent<ParentComponent>();
auto& transformComponent = entity.GetComponent<TransformComponent>();
auto& parentTransformComponent = parentComponent.Parent.GetComponent<TransformComponent>();
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);
}
}
}

View File

@@ -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);
};
}

View File

@@ -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<FrameBuffer>(true, glm::vec2(1920, 1080));
m_Framebuffer->SetTexture(CreateRef<Texture>(glm::vec2(1920, 1080), GL_RGB));
const Vector2 defaultResolution = Vector2(1, 1);
m_Framebuffer = CreateRef<FrameBuffer>(true, defaultResolution);
m_Framebuffer->SetTexture(CreateRef<Texture>(defaultResolution, GL_RGB));
m_Framebuffer->SetTexture(CreateRef<Texture>(defaultResolution, GL_RED_INTEGER, GL_R32I, GL_INT), GL_COLOR_ATTACHMENT3); // Entity ID
InitImgui();

View File

@@ -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;
}