From e715fb1dd5b485367cdfba0c201039c75158c5d9 Mon Sep 17 00:00:00 2001 From: t-rvw <429601557@qq.com> Date: Sat, 16 Sep 2023 01:31:20 +0800 Subject: [PATCH] refact aabb modes --- .../shaders/fs_wireframe_line.sc | 6 ++ .../shaders/vs_wireframe_line.sc | 8 +++ Engine/Source/Editor/EditorApp.cpp | 26 +++++--- Engine/Source/Editor/EditorApp.h | 1 + Engine/Source/Editor/UILayers/Inspector.cpp | 27 +++++++- Engine/Source/Editor/UILayers/SceneView.cpp | 54 ++++------------ Engine/Source/Editor/UILayers/SceneView.h | 43 ++++--------- Engine/Source/Game/GameApp.cpp | 6 -- .../Runtime/ECWorld/CollisionMeshComponent.h | 10 +++ .../Source/Runtime/Rendering/AABBRenderer.cpp | 47 ++------------ .../Source/Runtime/Rendering/AABBRenderer.h | 8 +-- .../Runtime/Rendering/WireframeRenderer.cpp | 64 +++++++++++++++++++ .../Runtime/Rendering/WireframeRenderer.h | 29 +++++++++ 13 files changed, 193 insertions(+), 136 deletions(-) create mode 100644 Engine/BuiltInShaders/shaders/fs_wireframe_line.sc create mode 100644 Engine/BuiltInShaders/shaders/vs_wireframe_line.sc create mode 100644 Engine/Source/Runtime/Rendering/WireframeRenderer.cpp create mode 100644 Engine/Source/Runtime/Rendering/WireframeRenderer.h diff --git a/Engine/BuiltInShaders/shaders/fs_wireframe_line.sc b/Engine/BuiltInShaders/shaders/fs_wireframe_line.sc new file mode 100644 index 00000000..267de793 --- /dev/null +++ b/Engine/BuiltInShaders/shaders/fs_wireframe_line.sc @@ -0,0 +1,6 @@ +#include "../common/common.sh" + +void main() +{ + gl_FragColor = vec4(0.5, 1.0, 1.0, 1.0); +} \ No newline at end of file diff --git a/Engine/BuiltInShaders/shaders/vs_wireframe_line.sc b/Engine/BuiltInShaders/shaders/vs_wireframe_line.sc new file mode 100644 index 00000000..c5a18b1e --- /dev/null +++ b/Engine/BuiltInShaders/shaders/vs_wireframe_line.sc @@ -0,0 +1,8 @@ +$input a_position//, a_color1 + +#include "../common/common.sh" + +void main() +{ + gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0)); +} \ No newline at end of file diff --git a/Engine/Source/Editor/EditorApp.cpp b/Engine/Source/Editor/EditorApp.cpp index c09098bd..e0abcd8c 100644 --- a/Engine/Source/Editor/EditorApp.cpp +++ b/Engine/Source/Editor/EditorApp.cpp @@ -17,6 +17,7 @@ #include "Rendering/DDGIRenderer.h" #endif #include "Rendering/WhiteModelRenderer.h" +#include "Rendering/WireframeRenderer.h" #include "Rendering/ImGuiRenderer.h" #include "Rendering/PBRSkyRenderer.h" #include "Rendering/BloomRenderer.h" @@ -170,6 +171,7 @@ void EditorApp::InitEditorUILayers() pSceneView->SetCameraController(m_pViewportCameraController.get()); pSceneView->SetSceneRenderer(m_pSceneRenderer); pSceneView->SetWhiteModelRenderer(m_pWhiteModelRenderer); + pSceneView->SetWireframeRenderer(m_pWireframeRenderer); pSceneView->SetAABBRenderer(m_pAABBRenderer); m_pEditorImGuiContext->AddDynamicLayer(cd::MoveTemp(pSceneView)); @@ -400,6 +402,10 @@ void EditorApp::InitEngineRenderers() pTerrainRenderer->SetSceneWorld(m_pSceneWorld.get()); AddEngineRenderer(cd::MoveTemp(pTerrainRenderer)); + auto pSkeletonRenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); + pSkeletonRenderer->SetSceneWorld(m_pSceneWorld.get()); + AddEngineRenderer(cd::MoveTemp(pSkeletonRenderer)); + auto pAnimationRenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); pAnimationRenderer->SetSceneWorld(m_pSceneWorld.get()); AddEngineRenderer(cd::MoveTemp(pAnimationRenderer)); @@ -410,26 +416,26 @@ void EditorApp::InitEngineRenderers() pWhiteModelRenderer->SetEnable(false); AddEngineRenderer(cd::MoveTemp(pWhiteModelRenderer)); - auto pAABBRenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); - m_pAABBRenderer = pAABBRenderer.get(); - pAABBRenderer->SetSceneWorld(m_pSceneWorld.get()); - pAABBRenderer->SetEnable(false); - AddEngineRenderer(cd::MoveTemp(pAABBRenderer)); - auto pParticlerenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); pParticlerenderer->SetSceneWorld(m_pSceneWorld.get()); AddEngineRenderer(cd::MoveTemp(pParticlerenderer)); - auto pSkeletonRenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); - pSkeletonRenderer->SetSceneWorld(m_pSceneWorld.get()); - AddEngineRenderer(cd::MoveTemp(pSkeletonRenderer)); - #ifdef ENABLE_DDGI auto pDDGIRenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); pDDGIRenderer->SetSceneWorld(m_pSceneWorld.get()); AddEngineRenderer(cd::MoveTemp(pDDGIRenderer)); #endif + auto pAABBRenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); + m_pAABBRenderer = pAABBRenderer.get(); + pAABBRenderer->SetSceneWorld(m_pSceneWorld.get()); + AddEngineRenderer(cd::MoveTemp(pAABBRenderer)); + + auto pWireframeRenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); + m_pWireframeRenderer = pWireframeRenderer.get(); + pWireframeRenderer->SetSceneWorld(m_pSceneWorld.get()); + AddEngineRenderer(cd::MoveTemp(pWireframeRenderer)); + auto pBlitRTRenderPass = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); AddEngineRenderer(cd::MoveTemp(pBlitRTRenderPass)); diff --git a/Engine/Source/Editor/EditorApp.h b/Engine/Source/Editor/EditorApp.h index 88cb5a76..cb71de73 100644 --- a/Engine/Source/Editor/EditorApp.h +++ b/Engine/Source/Editor/EditorApp.h @@ -93,6 +93,7 @@ class EditorApp final : public engine::IApplication editor::SceneView* m_pSceneView = nullptr; engine::Renderer* m_pSceneRenderer = nullptr; engine::Renderer* m_pWhiteModelRenderer = nullptr; + engine::Renderer* m_pWireframeRenderer = nullptr; engine::Renderer* m_pPBRSkyRenderer = nullptr; engine::Renderer* m_pIBLSkyRenderer = nullptr; engine::Renderer* m_pTerrainRenderer = nullptr; diff --git a/Engine/Source/Editor/UILayers/Inspector.cpp b/Engine/Source/Editor/UILayers/Inspector.cpp index 6a798860..586245a7 100644 --- a/Engine/Source/Editor/UILayers/Inspector.cpp +++ b/Engine/Source/Editor/UILayers/Inspector.cpp @@ -13,6 +13,28 @@ void UpdateComponentWidget(engine::SceneWorld* pSceneWorld, engine::Entity entit { } +template<> +void UpdateComponentWidget(engine::SceneWorld* pSceneWorld, engine::Entity entity) +{ + auto* pCollisionMesh = pSceneWorld->GetCollisionMeshComponent(entity); + if (!pCollisionMesh) + { + return; + } + + bool isHeaderOpen = ImGui::CollapsingHeader("Collision Mesh Component", ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_DefaultOpen); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2)); + ImGui::Separator(); + + if (isHeaderOpen) + { + ImGuiUtils::ImGuiBoolProperty("Debug Draw", pCollisionMesh->IsDebugDrawEnable()); + } + + ImGui::Separator(); + ImGui::PopStyleVar(); +} + template<> void UpdateComponentWidget(engine::SceneWorld* pSceneWorld, engine::Entity entity) { @@ -564,13 +586,14 @@ void Inspector::Update() details::UpdateComponentWidget(pSceneWorld, selectedEntity); details::UpdateComponentWidget(pSceneWorld, selectedEntity); - details::UpdateComponentWidget(pSceneWorld, selectedEntity); - details::UpdateComponentWidget(pSceneWorld, selectedEntity); details::UpdateComponentWidget(pSceneWorld, selectedEntity); details::UpdateComponentWidget(pSceneWorld, selectedEntity); details::UpdateComponentWidget(pSceneWorld, selectedEntity); details::UpdateComponentWidget(pSceneWorld, selectedEntity); + details::UpdateComponentWidget(pSceneWorld, selectedEntity); details::UpdateComponentWidget(pSceneWorld, selectedEntity); + details::UpdateComponentWidget(pSceneWorld, selectedEntity); + details::UpdateComponentWidget(pSceneWorld, selectedEntity); details::UpdateComponentWidget(pSceneWorld, selectedEntity); #ifdef ENABLE_DDGI diff --git a/Engine/Source/Editor/UILayers/SceneView.cpp b/Engine/Source/Editor/UILayers/SceneView.cpp index a541b3e8..9555e2c2 100644 --- a/Engine/Source/Editor/UILayers/SceneView.cpp +++ b/Engine/Source/Editor/UILayers/SceneView.cpp @@ -10,6 +10,8 @@ #include "Log/Log.h" #include "Material/ShaderSchema.h" #include "Math/Ray.hpp" +#include "Rendering/AABBRenderer.h" +#include "Rendering/WireframeRenderer.h" #include "Rendering/RenderContext.h" #include "Rendering/Renderer.h" #include "Rendering/RenderTarget.h" @@ -104,49 +106,16 @@ void SceneView::UpdateOperationButtons() } } -void SceneView::UpdateAABBCombo() -{ - ImGui::SetNextItemWidth(150); - if(ImGui::Combo("##AABBCombo", reinterpret_cast(&m_AABBMode), AABBModes, IM_ARRAYSIZE(AABBModes))) - { - if (AABBModeType::NoAABB == m_AABBMode) - { - m_pAABBRenderer->SetEnable(false); - } - else if (AABBModeType::AABBSelected == m_AABBMode) - { - m_pAABBRenderer->SetEnable(true); - static_cast(m_pAABBRenderer)->SetIsRenderSelected(true); - } - else if (AABBModeType::AABBAll == m_AABBMode) - { - m_pAABBRenderer->SetEnable(true); - static_cast(m_pAABBRenderer)->SetIsRenderSelected(false); - } - } - ImGui::PushItemWidth(150); -} - -void SceneView::UpdateDebugCombo() +void SceneView::UpdateRenderModeCombo() { ImGui::SetNextItemWidth(130); - if (ImGui::Combo("##DebugCombo", reinterpret_cast(&m_debugMode), DebugModes, IM_ARRAYSIZE(DebugModes))) + if (ImGui::Combo("##DebugCombo", reinterpret_cast(&m_renderMode), RenderModes, IM_ARRAYSIZE(RenderModes))) { - if (DebugModeType::NoDebug == m_debugMode) - { - m_pWhiteModelRenderer->SetEnable(false); - } - else if (DebugModeType::WhiteModel == m_debugMode) - { - m_pWhiteModelRenderer->SetEnable(true); - } - else if (DebugModeType::Wireframe == m_debugMode) - { - - } + m_pSceneRenderer->SetEnable(RenderModeType::Rendered == m_renderMode); + m_pWhiteModelRenderer->SetEnable(RenderModeType::Solid == m_renderMode); - // white model - m_pSceneRenderer->SetEnable(!m_pWhiteModelRenderer->IsEnable()); + auto* pWireframeRenderer = static_cast(m_pWireframeRenderer); + pWireframeRenderer->SetEnableGlobalWireframe(RenderModeType::Wireframe == m_renderMode); } ImGui::PushItemWidth(130); } @@ -198,7 +167,8 @@ void SceneView::UpdateSwitchAABBButton() if (ImGui::Button(reinterpret_cast(ICON_MDI_CUBE " AABB"))) { - GetRenderContext(); + auto* pAABBRenderer = static_cast(m_pAABBRenderer); + pAABBRenderer->SetEnableGlobalAABB(!pAABBRenderer->IsGlobalAABBEnable()); } if (isAABBActive) @@ -256,10 +226,10 @@ void SceneView::UpdateToolMenuButtons() //ImGui::SameLine(); ImGui::SameLine(); - UpdateAABBCombo(); + UpdateRenderModeCombo(); ImGui::SameLine(); - UpdateDebugCombo(); + UpdateSwitchAABBButton(); ImGui::SameLine(); UpdateSwitchTerrainButton(); diff --git a/Engine/Source/Editor/UILayers/SceneView.h b/Engine/Source/Editor/UILayers/SceneView.h index 957e7bbb..8067f55a 100644 --- a/Engine/Source/Editor/UILayers/SceneView.h +++ b/Engine/Source/Editor/UILayers/SceneView.h @@ -23,42 +23,24 @@ class RenderTarget; namespace editor { -enum class DebugModeType +enum class RenderModeType { - NoDebug, - WhiteModel, + Rendered, + Solid, Wireframe, Count, }; -constexpr const char* DebugModes[] = { - "NoDebug", - "WhiteModel", +constexpr const char* RenderModes[] = { + "Rendered", + "Solid", "Wireframe", }; -static_assert(static_cast(DebugModeType::Count) == sizeof(DebugModes) / sizeof(char*), +static_assert(static_cast(RenderModeType::Count) == sizeof(RenderModes) / sizeof(char*), "Debug mode type and names mismatch."); -enum class AABBModeType -{ - NoAABB, - AABBSelected, - AABBAll, - - Count, -}; - -constexpr const char* AABBModes[] = { - "NoAABB", - "AABBSelected", - "AABBAll" -}; - -static_assert(static_cast(AABBModeType::Count) == sizeof(AABBModes) / sizeof(char*), - "AABB mode type and names mismatch."); - class SceneView : public engine::ImGuiBaseLayer { public: @@ -73,11 +55,11 @@ class SceneView : public engine::ImGuiBaseLayer void PickSceneMesh(float regionWidth, float regionHeight); ImGuizmo::OPERATION GetImGuizmoOperation() const { return m_currentOperation; } - DebugModeType GetDebugMode() const { return m_debugMode; } - AABBModeType GetAABBMode() const { return m_AABBMode; } + RenderModeType GetRenderMode() const { return m_renderMode; } void SetSceneRenderer(engine::Renderer* pSceneRenderer) { m_pSceneRenderer = pSceneRenderer; } void SetWhiteModelRenderer(engine::Renderer* pWhiteModelRenderer) { m_pWhiteModelRenderer = pWhiteModelRenderer; } + void SetWireframeRenderer(engine::Renderer* pWireframeRenderer) { m_pWireframeRenderer = pWireframeRenderer; } void SetAABBRenderer(engine::Renderer* pAABBRenderer) { m_pAABBRenderer = pAABBRenderer; } bool IsTerrainEditMode() const { return m_isTerrainEditMode; } @@ -91,8 +73,7 @@ class SceneView : public engine::ImGuiBaseLayer void UpdateSwitchAABBButton(); void UpdateSwitchTerrainButton(); void UpdateOperationButtons(); - void UpdateDebugCombo(); - void UpdateAABBCombo(); + void UpdateRenderModeCombo(); private: uint16_t m_lastContentWidth = 0; @@ -103,11 +84,11 @@ class SceneView : public engine::ImGuiBaseLayer bool m_is3DMode = true; bool m_isIBLActive = false; bool m_isTerrainEditMode = false; - DebugModeType m_debugMode = DebugModeType::NoDebug; - AABBModeType m_AABBMode = AABBModeType::NoAABB; + RenderModeType m_renderMode = RenderModeType::Rendered; engine::Renderer* m_pSceneRenderer = nullptr; engine::Renderer* m_pWhiteModelRenderer = nullptr; + engine::Renderer* m_pWireframeRenderer = nullptr; engine::Renderer* m_pAABBRenderer = nullptr; engine::RenderTarget* m_pRenderTarget = nullptr; diff --git a/Engine/Source/Game/GameApp.cpp b/Engine/Source/Game/GameApp.cpp index 48fe7e29..48e0b30d 100644 --- a/Engine/Source/Game/GameApp.cpp +++ b/Engine/Source/Game/GameApp.cpp @@ -251,12 +251,6 @@ void GameApp::InitEngineRenderers() pAnimationRenderer->SetSceneWorld(m_pSceneWorld.get()); AddEngineRenderer(cd::MoveTemp(pAnimationRenderer)); - auto pWhiteModelRenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); - m_pWhiteModelRenderer = pWhiteModelRenderer.get(); - pWhiteModelRenderer->SetSceneWorld(m_pSceneWorld.get()); - pWhiteModelRenderer->SetEnable(false); - AddEngineRenderer(cd::MoveTemp(pWhiteModelRenderer)); - #ifdef ENABLE_DDGI auto pDDGIRenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); pDDGIRenderer->SetSceneWorld(m_pSceneWorld.get()); diff --git a/Engine/Source/Runtime/ECWorld/CollisionMeshComponent.h b/Engine/Source/Runtime/ECWorld/CollisionMeshComponent.h index 9178bd86..8df3c2bc 100644 --- a/Engine/Source/Runtime/ECWorld/CollisionMeshComponent.h +++ b/Engine/Source/Runtime/ECWorld/CollisionMeshComponent.h @@ -43,6 +43,12 @@ class CollisionMeshComponent final void Reset(); void Build(); +#ifdef EDITOR_MODE + bool SetEnableDebugDraw(bool enable) { m_enableDebugDraw = enable; } + bool& IsDebugDrawEnable() { return m_enableDebugDraw; } + bool IsDebugDrawEnable() const { return m_enableDebugDraw; } +#endif + private: CollisonMeshType m_collisionType = CollisonMeshType::AABB; cd::AABB m_aabb; @@ -51,6 +57,10 @@ class CollisionMeshComponent final std::vector m_aabbIndexBuffer; uint16_t m_aabbVBH = UINT16_MAX; uint16_t m_aabbIBH = UINT16_MAX; + +#ifdef EDITOR_MODE + bool m_enableDebugDraw = false; +#endif }; } \ No newline at end of file diff --git a/Engine/Source/Runtime/Rendering/AABBRenderer.cpp b/Engine/Source/Runtime/Rendering/AABBRenderer.cpp index 3a61ddc0..58d79204 100644 --- a/Engine/Source/Runtime/Rendering/AABBRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/AABBRenderer.cpp @@ -22,17 +22,22 @@ void AABBRenderer::UpdateView(const float* pViewMatrix, const float* pProjection bgfx::setViewTransform(GetViewID(), pViewMatrix, pProjectionMatrix); } -void AABBRenderer::RenderAll(float deltaTime) +void AABBRenderer::Render(float deltaTime) { for (Entity entity : m_pCurrentSceneWorld->GetCollisionMeshEntities()) { + auto* pCollisionMesh = m_pCurrentSceneWorld->GetCollisionMeshComponent(entity); + if (!m_enableGlobalAABB && !pCollisionMesh->IsDebugDrawEnable()) + { + continue; + } + if (auto* pTransformComponent = m_pCurrentSceneWorld->GetTransformComponent(entity)) { pTransformComponent->Build(); bgfx::setTransform(pTransformComponent->GetWorldMatrix().Begin()); } - auto* pCollisionMesh = m_pCurrentSceneWorld->GetCollisionMeshComponent(entity); bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pCollisionMesh->GetVertexBuffer() }); bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pCollisionMesh->GetIndexBuffer() }); @@ -45,42 +50,4 @@ void AABBRenderer::RenderAll(float deltaTime) } } -void AABBRenderer::RenderSelected(float deltaTime) -{ - Entity entity = m_pCurrentSceneWorld->GetSelectedEntity(); - auto* pCollisionMesh = m_pCurrentSceneWorld->GetCollisionMeshComponent(entity); - if (!pCollisionMesh) - { - return; - } - - if (TransformComponent* pTransformComponent = m_pCurrentSceneWorld->GetTransformComponent(entity)) - { - pTransformComponent->Build(); - bgfx::setTransform(pTransformComponent->GetWorldMatrix().Begin()); - } - - bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pCollisionMesh->GetVertexBuffer() }); - bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pCollisionMesh->GetIndexBuffer() }); - - constexpr uint64_t state = BGFX_STATE_WRITE_MASK | BGFX_STATE_MSAA | BGFX_STATE_DEPTH_TEST_LESS | - BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA) | BGFX_STATE_PT_LINES; - bgfx::setState(state); - - constexpr StringCrc AABBProgram("AABBProgram"); - bgfx::submit(GetViewID(), GetRenderContext()->GetProgram(AABBProgram)); -} - -void AABBRenderer::Render(float deltaTime) -{ - if (m_isRenderSelected) - { - RenderSelected(deltaTime); - } - else - { - RenderAll(deltaTime); - } -} - } \ No newline at end of file diff --git a/Engine/Source/Runtime/Rendering/AABBRenderer.h b/Engine/Source/Runtime/Rendering/AABBRenderer.h index d295e78b..32145ce5 100644 --- a/Engine/Source/Runtime/Rendering/AABBRenderer.h +++ b/Engine/Source/Runtime/Rendering/AABBRenderer.h @@ -19,14 +19,12 @@ class AABBRenderer final : public Renderer virtual void Render(float deltaTime) override; void SetSceneWorld(SceneWorld* pSceneWorld) { m_pCurrentSceneWorld = pSceneWorld; } - void SetIsRenderSelected(bool isRenderSelected) { m_isRenderSelected = isRenderSelected; } + void SetEnableGlobalAABB(bool enable) { m_enableGlobalAABB = enable; } + bool IsGlobalAABBEnable() const { return m_enableGlobalAABB; } private: - void RenderSelected(float deltaTime); - void RenderAll(float deltaTime); - SceneWorld* m_pCurrentSceneWorld = nullptr; - bool m_isRenderSelected = true; // false : all , true : selected + bool m_enableGlobalAABB = false; }; } \ No newline at end of file diff --git a/Engine/Source/Runtime/Rendering/WireframeRenderer.cpp b/Engine/Source/Runtime/Rendering/WireframeRenderer.cpp new file mode 100644 index 00000000..9bd13579 --- /dev/null +++ b/Engine/Source/Runtime/Rendering/WireframeRenderer.cpp @@ -0,0 +1,64 @@ +#include "WireframeRenderer.h" + +#include "Core/StringCrc.h" +#include "ECWorld/SceneWorld.h" +#include "ECWorld/StaticMeshComponent.h" +#include "ECWorld/TransformComponent.h" +#include "RenderContext.h" +#include "Scene/Texture.h" + +namespace engine +{ + +void WireframeRenderer::Init() +{ + GetRenderContext()->CreateProgram("WireframeLineProgram", "vs_wireframe_line.bin", "fs_wireframe_line.bin"); + bgfx::setViewName(GetViewID(), "WireframeRenderer"); +} + +void WireframeRenderer::UpdateView(const float* pViewMatrix, const float* pProjectionMatrix) +{ + UpdateViewRenderTarget(); + bgfx::setViewTransform(GetViewID(), pViewMatrix, pProjectionMatrix); +} + +void WireframeRenderer::Render(float deltaTime) +{ + for (Entity entity : m_pCurrentSceneWorld->GetStaticMeshEntities()) + { + if (!m_enableGlobalWireframe && m_pCurrentSceneWorld->GetSelectedEntity() != entity) + { + continue; + } + + if (m_pCurrentSceneWorld->GetSkyEntity() == entity) + { + continue; + } + + TerrainComponent* pTerrainComponent = m_pCurrentSceneWorld->GetTerrainComponent(entity); + if (pTerrainComponent) + { + continue; + } + + if (TransformComponent* pTransformComponent = m_pCurrentSceneWorld->GetTransformComponent(entity)) + { + pTransformComponent->Build(); + bgfx::setTransform(pTransformComponent->GetWorldMatrix().Begin()); + } + + StaticMeshComponent* pStaticMesh = m_pCurrentSceneWorld->GetStaticMeshComponent(entity); + bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pStaticMesh->GetVertexBuffer() }); + bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pStaticMesh->GetWireframeIndexBuffer() }); + + constexpr uint64_t state = BGFX_STATE_WRITE_MASK | BGFX_STATE_MSAA | BGFX_STATE_DEPTH_TEST_LEQUAL | + BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA) | BGFX_STATE_PT_LINES; + bgfx::setState(state); + + constexpr StringCrc whiteModelProgram("WireframeLineProgram"); + bgfx::submit(GetViewID(), GetRenderContext()->GetProgram(whiteModelProgram)); + } +} + +} \ No newline at end of file diff --git a/Engine/Source/Runtime/Rendering/WireframeRenderer.h b/Engine/Source/Runtime/Rendering/WireframeRenderer.h new file mode 100644 index 00000000..209befd1 --- /dev/null +++ b/Engine/Source/Runtime/Rendering/WireframeRenderer.h @@ -0,0 +1,29 @@ +#pragma once + +#include "Renderer.h" + +#include + +namespace engine +{ + +class SceneWorld; + +class WireframeRenderer final : public Renderer +{ +public: + using Renderer::Renderer; + + virtual void Init() override; + virtual void UpdateView(const float* pViewMatrix, const float* pProjectionMatrix) override; + virtual void Render(float deltaTime) override; + + void SetSceneWorld(SceneWorld* pSceneWorld) { m_pCurrentSceneWorld = pSceneWorld; } + void SetEnableGlobalWireframe(bool enable) { m_enableGlobalWireframe = enable; } + +private: + SceneWorld* m_pCurrentSceneWorld = nullptr; + bool m_enableGlobalWireframe = false; +}; + +} \ No newline at end of file