diff --git a/Engine/BuiltInShaders/shaders/fs_debug.sc b/Engine/BuiltInShaders/shaders/fs_whiteModel.sc similarity index 100% rename from Engine/BuiltInShaders/shaders/fs_debug.sc rename to Engine/BuiltInShaders/shaders/fs_whiteModel.sc 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_debug.sc b/Engine/BuiltInShaders/shaders/vs_whiteModel.sc similarity index 100% rename from Engine/BuiltInShaders/shaders/vs_debug.sc rename to Engine/BuiltInShaders/shaders/vs_whiteModel.sc 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/ECWorld/ECWorldConsumer.cpp b/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp index c6db5f13..36dfcae2 100644 --- a/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp +++ b/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp @@ -199,6 +199,7 @@ void ECWorldConsumer::AddStaticMesh(engine::Entity entity, const cd::Mesh& mesh, staticMeshComponent.SetMeshData(&mesh); staticMeshComponent.SetRequiredVertexFormat(&vertexFormat); staticMeshComponent.Build(); + staticMeshComponent.Submit(); } void ECWorldConsumer::AddSkinMesh(engine::Entity entity, const cd::Mesh& mesh, const cd::VertexFormat& vertexFormat) diff --git a/Engine/Source/Editor/EditorApp.cpp b/Engine/Source/Editor/EditorApp.cpp index bba08d91..e0abcd8c 100644 --- a/Engine/Source/Editor/EditorApp.cpp +++ b/Engine/Source/Editor/EditorApp.cpp @@ -16,7 +16,8 @@ #ifdef ENABLE_DDGI #include "Rendering/DDGIRenderer.h" #endif -#include "Rendering/DebugRenderer.h" +#include "Rendering/WhiteModelRenderer.h" +#include "Rendering/WireframeRenderer.h" #include "Rendering/ImGuiRenderer.h" #include "Rendering/PBRSkyRenderer.h" #include "Rendering/BloomRenderer.h" @@ -169,7 +170,8 @@ void EditorApp::InitEditorUILayers() m_pSceneView = pSceneView.get(); pSceneView->SetCameraController(m_pViewportCameraController.get()); pSceneView->SetSceneRenderer(m_pSceneRenderer); - pSceneView->SetDebugRenderer(m_pDebugRenderer); + pSceneView->SetWhiteModelRenderer(m_pWhiteModelRenderer); + pSceneView->SetWireframeRenderer(m_pWireframeRenderer); pSceneView->SetAABBRenderer(m_pAABBRenderer); m_pEditorImGuiContext->AddDynamicLayer(cd::MoveTemp(pSceneView)); @@ -319,6 +321,7 @@ void EditorApp::InitSkyEntity() meshComponent.SetMeshData(&optMesh.value()); meshComponent.SetRequiredVertexFormat(&vertexFormat); meshComponent.Build(); + meshComponent.Submit(); } void EditorApp::InitShaderVariantCollectionEntity() @@ -399,36 +402,40 @@ 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)); - auto pDebugRenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); - m_pDebugRenderer = pDebugRenderer.get(); - pDebugRenderer->SetSceneWorld(m_pSceneWorld.get()); - pDebugRenderer->SetEnable(false); - AddEngineRenderer(cd::MoveTemp(pDebugRenderer)); - - 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 pWhiteModelRenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); + m_pWhiteModelRenderer = pWhiteModelRenderer.get(); + pWhiteModelRenderer->SetSceneWorld(m_pSceneWorld.get()); + pWhiteModelRenderer->SetEnable(false); + AddEngineRenderer(cd::MoveTemp(pWhiteModelRenderer)); 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 d28f2754..cb71de73 100644 --- a/Engine/Source/Editor/EditorApp.h +++ b/Engine/Source/Editor/EditorApp.h @@ -92,7 +92,8 @@ class EditorApp final : public engine::IApplication std::unique_ptr m_pSceneWorld; editor::SceneView* m_pSceneView = nullptr; engine::Renderer* m_pSceneRenderer = nullptr; - engine::Renderer* m_pDebugRenderer = 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/EntityList.cpp b/Engine/Source/Editor/UILayers/EntityList.cpp index 33fa7598..aa502d59 100644 --- a/Engine/Source/Editor/UILayers/EntityList.cpp +++ b/Engine/Source/Editor/UILayers/EntityList.cpp @@ -51,6 +51,7 @@ void EntityList::AddEntity(engine::SceneWorld* pSceneWorld) staticMeshComponent.SetMeshData(&mesh); staticMeshComponent.SetRequiredVertexFormat(&pMaterialType->GetRequiredVertexFormat()); staticMeshComponent.Build(); + staticMeshComponent.Submit(); mesh.SetName(pSceneWorld->GetNameComponent(entity)->GetName()); mesh.SetID(cd::MeshID(pSceneDatabase->GetMeshCount())); @@ -113,6 +114,7 @@ void EntityList::AddEntity(engine::SceneWorld* pSceneWorld) meshComponent.SetMeshData(&mesh); meshComponent.SetRequiredVertexFormat(&pTerrainMaterialType->GetRequiredVertexFormat());//to do : modify vertexFormat meshComponent.Build(); + meshComponent.Submit(); mesh.SetName(pSceneWorld->GetNameComponent(entity)->GetName()); mesh.SetID(cd::MeshID(pSceneDatabase->GetMeshCount())); diff --git a/Engine/Source/Editor/UILayers/Inspector.cpp b/Engine/Source/Editor/UILayers/Inspector.cpp index 661b8572..edbbbd19 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,12 +586,13 @@ 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); diff --git a/Engine/Source/Editor/UILayers/SceneView.cpp b/Engine/Source/Editor/UILayers/SceneView.cpp index 423f6e25..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,44 +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_pSceneRenderer->SetEnable(true); - m_pDebugRenderer->SetEnable(false); - } - else if (DebugModeType::WhiteModel == m_debugMode) - { - m_pSceneRenderer->SetEnable(false); - m_pDebugRenderer->SetEnable(true); - } + m_pSceneRenderer->SetEnable(RenderModeType::Rendered == m_renderMode); + m_pWhiteModelRenderer->SetEnable(RenderModeType::Solid == m_renderMode); + + auto* pWireframeRenderer = static_cast(m_pWireframeRenderer); + pWireframeRenderer->SetEnableGlobalWireframe(RenderModeType::Wireframe == m_renderMode); } ImGui::PushItemWidth(130); } @@ -193,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) @@ -251,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 689c37af..8067f55a 100644 --- a/Engine/Source/Editor/UILayers/SceneView.h +++ b/Engine/Source/Editor/UILayers/SceneView.h @@ -3,7 +3,7 @@ #include "ECWorld/Entity.h" #include "ImGui/ImGuiBaseLayer.h" #include "Rendering/AABBRenderer.h" -#include "Rendering/DebugRenderer.h" +#include "Rendering/WhiteModelRenderer.h" #include #define IMGUI_DEFINE_MATH_OPERATORS @@ -23,41 +23,24 @@ class RenderTarget; namespace editor { -enum class DebugModeType +enum class RenderModeType { - NoDebug, - WhiteModel, + Rendered, + Solid, + Wireframe, Count, }; -constexpr const char* DebugModes[] = { - "NoDebug", - "WhiteModel", - //"WireFrame" +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: @@ -72,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 SetDebugRenderer(engine::Renderer* pDebugRenderer) { m_pDebugRenderer = pDebugRenderer; } + 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; } @@ -90,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; @@ -102,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_pDebugRenderer = 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 3ec84f97..48e0b30d 100644 --- a/Engine/Source/Game/GameApp.cpp +++ b/Engine/Source/Game/GameApp.cpp @@ -13,7 +13,7 @@ #ifdef ENABLE_DDGI #include "Rendering/DDGIRenderer.h" #endif -#include "Rendering/DebugRenderer.h" +#include "Rendering/WhiteModelRenderer.h" #include "Rendering/ImGuiRenderer.h" #include "Rendering/PBRSkyRenderer.h" #include "Rendering/PostProcessRenderer.h" @@ -251,12 +251,6 @@ void GameApp::InitEngineRenderers() pAnimationRenderer->SetSceneWorld(m_pSceneWorld.get()); AddEngineRenderer(cd::MoveTemp(pAnimationRenderer)); - auto pDebugRenderer = std::make_unique(m_pRenderContext->CreateView(), pSceneRenderTarget); - m_pDebugRenderer = pDebugRenderer.get(); - pDebugRenderer->SetSceneWorld(m_pSceneWorld.get()); - pDebugRenderer->SetEnable(false); - AddEngineRenderer(cd::MoveTemp(pDebugRenderer)); - #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/ECWorld/StaticMeshComponent.cpp b/Engine/Source/Runtime/ECWorld/StaticMeshComponent.cpp index e4694f83..bee07247 100644 --- a/Engine/Source/Runtime/ECWorld/StaticMeshComponent.cpp +++ b/Engine/Source/Runtime/ECWorld/StaticMeshComponent.cpp @@ -22,6 +22,11 @@ void StaticMeshComponent::Reset() m_indexBuffer.clear(); m_indexBufferHandle = UINT16_MAX; + +#ifdef EDITOR_MODE + m_wireframeIndexBuffer.clear(); + m_wireframeIndexBufferHandle = UINT16_MAX; +#endif } void StaticMeshComponent::Build() @@ -50,13 +55,12 @@ void StaticMeshComponent::Build() m_vertexBuffer.resize(vertexCount * vertexFormatStride); - uint32_t currentDataSize = 0U; - auto currentDataPtr = m_vertexBuffer.data(); - - auto FillVertexBuffer = [¤tDataPtr, ¤tDataSize](const void* pData, uint32_t dataSize) + uint32_t vbDataSize = 0U; + auto vbDataPtr = m_vertexBuffer.data(); + auto FillVertexBuffer = [&vbDataPtr, &vbDataSize](const void* pData, uint32_t dataSize) { - std::memcpy(¤tDataPtr[currentDataSize], pData, dataSize); - currentDataSize += dataSize; + std::memcpy(&vbDataPtr[vbDataSize], pData, dataSize); + vbDataSize += dataSize; }; for (uint32_t vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex) @@ -128,25 +132,18 @@ void StaticMeshComponent::Build() } } - // Create vertex buffer. - bgfx::VertexLayout vertexLayout; - VertexLayoutUtility::CreateVertexLayout(vertexLayout, m_pRequiredVertexFormat->GetVertexLayout()); - const bgfx::Memory* pVertexBufferRef = bgfx::makeRef(m_vertexBuffer.data(), static_cast(m_vertexBuffer.size())); - bgfx::VertexBufferHandle vertexBufferHandle = bgfx::createVertexBuffer(pVertexBufferRef, vertexLayout); - assert(bgfx::isValid(vertexBufferHandle)); - m_vertexBufferHandle = vertexBufferHandle.idx; - // Fill index buffer data. bool useU16Index = vertexCount <= static_cast(std::numeric_limits::max()) + 1U; uint32_t indexTypeSize = useU16Index ? sizeof(uint16_t) : sizeof(uint32_t); - m_indexBuffer.resize(m_pMeshData->GetPolygonCount() * 3 * indexTypeSize); + uint32_t indicesCount = m_pMeshData->GetPolygonCount() * 3U; + m_indexBuffer.resize(indicesCount* indexTypeSize); - currentDataSize = 0U; - currentDataPtr = m_indexBuffer.data(); - auto FillIndexBuffer = [¤tDataPtr, ¤tDataSize](const void* pData, uint32_t dataSize) + uint32_t ibDataSize = 0U; + auto ibDataPtr = m_indexBuffer.data(); + auto FillIndexBuffer = [&ibDataPtr, &ibDataSize](const void* pData, uint32_t dataSize) { - std::memcpy(¤tDataPtr[currentDataSize], pData, dataSize); - currentDataSize += dataSize; + std::memcpy(&ibDataPtr[ibDataSize], pData, dataSize); + ibDataSize += dataSize; }; for (const auto& polygon : m_pMeshData->GetPolygons()) @@ -166,11 +163,40 @@ void StaticMeshComponent::Build() } } +#ifdef EDITOR_MODE + uint32_t wireframeIndicesCount = bgfx::topologyConvert(bgfx::TopologyConvert::TriListToLineList, nullptr, 0U, + m_indexBuffer.data(), indicesCount, !useU16Index); + m_wireframeIndexBuffer.resize(indicesCount * indexTypeSize); + bgfx::topologyConvert(bgfx::TopologyConvert::TriListToLineList, m_wireframeIndexBuffer.data(), static_cast(m_wireframeIndexBuffer.size()), + m_indexBuffer.data(), indicesCount, !useU16Index); +#endif +} + +void StaticMeshComponent::Submit() +{ + // Create vertex buffer. + bgfx::VertexLayout vertexLayout; + VertexLayoutUtility::CreateVertexLayout(vertexLayout, m_pRequiredVertexFormat->GetVertexLayout()); + const bgfx::Memory* pVertexBufferRef = bgfx::makeRef(m_vertexBuffer.data(), static_cast(m_vertexBuffer.size())); + bgfx::VertexBufferHandle vertexBufferHandle = bgfx::createVertexBuffer(pVertexBufferRef, vertexLayout); + assert(bgfx::isValid(vertexBufferHandle)); + m_vertexBufferHandle = vertexBufferHandle.idx; + + bool useU16Index = m_pMeshData->GetVertexCount() <= static_cast(std::numeric_limits::max()) + 1U; + auto SubmitIndexBuffer = [&useU16Index](const std::vector& indexBuffer) -> uint16_t + { + const bgfx::Memory* pIndexBufferRef = bgfx::makeRef(indexBuffer.data(), static_cast(indexBuffer.size())); + bgfx::IndexBufferHandle indexBufferHandle = bgfx::createIndexBuffer(pIndexBufferRef, useU16Index ? 0U : BGFX_BUFFER_INDEX32); + assert(bgfx::isValid(indexBufferHandle)); + return indexBufferHandle.idx; + }; + // Create index buffer. - const bgfx::Memory* pIndexBufferRef = bgfx::makeRef(m_indexBuffer.data(), static_cast(m_indexBuffer.size())); - bgfx::IndexBufferHandle indexBufferHandle = bgfx::createIndexBuffer(pIndexBufferRef, useU16Index ? 0U : BGFX_BUFFER_INDEX32); - assert(bgfx::isValid(indexBufferHandle)); - m_indexBufferHandle = indexBufferHandle.idx; + m_indexBufferHandle = SubmitIndexBuffer(m_indexBuffer); + +#ifdef EDITOR_MODE + m_wireframeIndexBufferHandle = SubmitIndexBuffer(m_wireframeIndexBuffer); +#endif } } \ No newline at end of file diff --git a/Engine/Source/Runtime/ECWorld/StaticMeshComponent.h b/Engine/Source/Runtime/ECWorld/StaticMeshComponent.h index 4460a7db..bd18c3cf 100644 --- a/Engine/Source/Runtime/ECWorld/StaticMeshComponent.h +++ b/Engine/Source/Runtime/ECWorld/StaticMeshComponent.h @@ -43,9 +43,13 @@ class StaticMeshComponent final uint16_t GetVertexBuffer() const { return m_vertexBufferHandle; } uint16_t GetIndexBuffer() const { return m_indexBufferHandle; } +#ifdef EDITOR_MODE + uint16_t GetWireframeIndexBuffer() const { return m_wireframeIndexBufferHandle; } +#endif void Reset(); void Build(); + void Submit(); private: // Input @@ -57,6 +61,11 @@ class StaticMeshComponent final std::vector m_indexBuffer; uint16_t m_vertexBufferHandle = UINT16_MAX; uint16_t m_indexBufferHandle = UINT16_MAX; + +#ifdef EDITOR_MODE + std::vector m_wireframeIndexBuffer; + uint16_t m_wireframeIndexBufferHandle = UINT16_MAX; +#endif }; } \ No newline at end of file diff --git a/Engine/Source/Runtime/Rendering/AABBRenderer.cpp b/Engine/Source/Runtime/Rendering/AABBRenderer.cpp index d1e26a18..4be83abd 100644 --- a/Engine/Source/Runtime/Rendering/AABBRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/AABBRenderer.cpp @@ -22,12 +22,12 @@ 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->GetStaticMeshEntities()) + for (Entity entity : m_pCurrentSceneWorld->GetCollisionMeshEntities()) { auto* pCollisionMesh = m_pCurrentSceneWorld->GetCollisionMeshComponent(entity); - if (!pCollisionMesh) + if (!m_enableGlobalAABB && !pCollisionMesh->IsDebugDrawEnable()) { continue; } @@ -47,45 +47,6 @@ void AABBRenderer::RenderAll(float deltaTime) constexpr StringCrc AABBAllProgram("AABBProgram"); bgfx::submit(GetViewID(), GetRenderContext()->GetProgram(AABBAllProgram)); - - } -} - -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); } } 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/DebugRenderer.cpp b/Engine/Source/Runtime/Rendering/WhiteModelRenderer.cpp similarity index 57% rename from Engine/Source/Runtime/Rendering/DebugRenderer.cpp rename to Engine/Source/Runtime/Rendering/WhiteModelRenderer.cpp index 0635143a..eaca198e 100644 --- a/Engine/Source/Runtime/Rendering/DebugRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/WhiteModelRenderer.cpp @@ -1,4 +1,4 @@ -#include "DebugRenderer.h" +#include "WhiteModelRenderer.h" #include "Core/StringCrc.h" #include "ECWorld/SceneWorld.h" @@ -10,19 +10,19 @@ namespace engine { -void DebugRenderer::Init() +void WhiteModelRenderer::Init() { - GetRenderContext()->CreateProgram("DebugProgram", "vs_debug.bin", "fs_debug.bin"); - bgfx::setViewName(GetViewID(), "DebugRenderer"); + GetRenderContext()->CreateProgram("WhiteModelProgram", "vs_whiteModel.bin", "fs_whiteModel.bin"); + bgfx::setViewName(GetViewID(), "WhiteModelRenderer"); } -void DebugRenderer::UpdateView(const float* pViewMatrix, const float* pProjectionMatrix) +void WhiteModelRenderer::UpdateView(const float* pViewMatrix, const float* pProjectionMatrix) { UpdateViewRenderTarget(); bgfx::setViewTransform(GetViewID(), pViewMatrix, pProjectionMatrix); } -void DebugRenderer::Render(float deltaTime) +void WhiteModelRenderer::Render(float deltaTime) { for (Entity entity : m_pCurrentSceneWorld->GetStaticMeshEntities()) { @@ -37,27 +37,22 @@ void DebugRenderer::Render(float deltaTime) continue; } - StaticMeshComponent* pMeshComponent = m_pCurrentSceneWorld->GetStaticMeshComponent(entity); - if (!pMeshComponent) - { - continue; - } - if (TransformComponent* pTransformComponent = m_pCurrentSceneWorld->GetTransformComponent(entity)) { pTransformComponent->Build(); bgfx::setTransform(pTransformComponent->GetWorldMatrix().Begin()); } - bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pMeshComponent->GetVertexBuffer() }); - bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pMeshComponent->GetIndexBuffer() }); + StaticMeshComponent* pStaticMesh = m_pCurrentSceneWorld->GetStaticMeshComponent(entity); + bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pStaticMesh->GetVertexBuffer() }); + bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pStaticMesh->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::setState(state); - constexpr StringCrc debugProgram("DebugProgram"); - bgfx::submit(GetViewID(), GetRenderContext()->GetProgram(debugProgram)); + constexpr StringCrc whiteModelProgram("WhiteModelProgram"); + bgfx::submit(GetViewID(), GetRenderContext()->GetProgram(whiteModelProgram)); } } diff --git a/Engine/Source/Runtime/Rendering/DebugRenderer.h b/Engine/Source/Runtime/Rendering/WhiteModelRenderer.h similarity index 90% rename from Engine/Source/Runtime/Rendering/DebugRenderer.h rename to Engine/Source/Runtime/Rendering/WhiteModelRenderer.h index c13a14f0..62325dec 100644 --- a/Engine/Source/Runtime/Rendering/DebugRenderer.h +++ b/Engine/Source/Runtime/Rendering/WhiteModelRenderer.h @@ -9,7 +9,7 @@ namespace engine class SceneWorld; -class DebugRenderer final : public Renderer +class WhiteModelRenderer final : public Renderer { public: using Renderer::Renderer; 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