Skip to content

Commit

Permalink
add wireframe mode
Browse files Browse the repository at this point in the history
  • Loading branch information
T-rvw committed Sep 15, 2023
1 parent 0c49ac5 commit 8ab459b
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 72 deletions.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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/BloomRenderer.h"
Expand Down Expand Up @@ -169,7 +169,7 @@ 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->SetAABBRenderer(m_pAABBRenderer);
m_pEditorImGuiContext->AddDynamicLayer(cd::MoveTemp(pSceneView));

Expand Down Expand Up @@ -319,6 +319,7 @@ void EditorApp::InitSkyEntity()
meshComponent.SetMeshData(&optMesh.value());
meshComponent.SetRequiredVertexFormat(&vertexFormat);
meshComponent.Build();
meshComponent.Submit();
}

void EditorApp::InitShaderVariantCollectionEntity()
Expand Down Expand Up @@ -403,11 +404,11 @@ void EditorApp::InitEngineRenderers()
pAnimationRenderer->SetSceneWorld(m_pSceneWorld.get());
AddEngineRenderer(cd::MoveTemp(pAnimationRenderer));

auto pDebugRenderer = std::make_unique<engine::DebugRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
m_pDebugRenderer = pDebugRenderer.get();
pDebugRenderer->SetSceneWorld(m_pSceneWorld.get());
pDebugRenderer->SetEnable(false);
AddEngineRenderer(cd::MoveTemp(pDebugRenderer));
auto pWhiteModelRenderer = std::make_unique<engine::WhiteModelRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
m_pWhiteModelRenderer = pWhiteModelRenderer.get();
pWhiteModelRenderer->SetSceneWorld(m_pSceneWorld.get());
pWhiteModelRenderer->SetEnable(false);
AddEngineRenderer(cd::MoveTemp(pWhiteModelRenderer));

auto pAABBRenderer = std::make_unique<engine::AABBRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
m_pAABBRenderer = pAABBRenderer.get();
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Editor/EditorApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class EditorApp final : public engine::IApplication
std::unique_ptr<engine::SceneWorld> 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_pPBRSkyRenderer = nullptr;
engine::Renderer* m_pIBLSkyRenderer = nullptr;
engine::Renderer* m_pTerrainRenderer = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions Engine/Source/Editor/UILayers/EntityList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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()));
Expand Down
13 changes: 9 additions & 4 deletions Engine/Source/Editor/UILayers/SceneView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,19 @@ void SceneView::UpdateDebugCombo()
{
if (DebugModeType::NoDebug == m_debugMode)
{
m_pSceneRenderer->SetEnable(true);
m_pDebugRenderer->SetEnable(false);
m_pWhiteModelRenderer->SetEnable(false);
}
else if (DebugModeType::WhiteModel == m_debugMode)
{
m_pSceneRenderer->SetEnable(false);
m_pDebugRenderer->SetEnable(true);
m_pWhiteModelRenderer->SetEnable(true);
}
else if (DebugModeType::Wireframe == m_debugMode)
{

}

// white model
m_pSceneRenderer->SetEnable(!m_pWhiteModelRenderer->IsEnable());
}
ImGui::PushItemWidth(130);
}
Expand Down
9 changes: 5 additions & 4 deletions Engine/Source/Editor/UILayers/SceneView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <imgui/imgui.h>
#define IMGUI_DEFINE_MATH_OPERATORS
Expand All @@ -27,14 +27,15 @@ enum class DebugModeType
{
NoDebug,
WhiteModel,
Wireframe,

Count,
};

constexpr const char* DebugModes[] = {
"NoDebug",
"WhiteModel",
//"WireFrame"
"Wireframe",
};

static_assert(static_cast<int>(DebugModeType::Count) == sizeof(DebugModes) / sizeof(char*),
Expand Down Expand Up @@ -76,7 +77,7 @@ class SceneView : public engine::ImGuiBaseLayer
AABBModeType GetAABBMode() const { return m_AABBMode; }

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 SetAABBRenderer(engine::Renderer* pAABBRenderer) { m_pAABBRenderer = pAABBRenderer; }

bool IsTerrainEditMode() const { return m_isTerrainEditMode; }
Expand Down Expand Up @@ -106,7 +107,7 @@ class SceneView : public engine::ImGuiBaseLayer
AABBModeType m_AABBMode = AABBModeType::NoAABB;

engine::Renderer* m_pSceneRenderer = nullptr;
engine::Renderer* m_pDebugRenderer = nullptr;
engine::Renderer* m_pWhiteModelRenderer = nullptr;
engine::Renderer* m_pAABBRenderer = nullptr;

engine::RenderTarget* m_pRenderTarget = nullptr;
Expand Down
12 changes: 6 additions & 6 deletions Engine/Source/Game/GameApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -251,11 +251,11 @@ void GameApp::InitEngineRenderers()
pAnimationRenderer->SetSceneWorld(m_pSceneWorld.get());
AddEngineRenderer(cd::MoveTemp(pAnimationRenderer));

auto pDebugRenderer = std::make_unique<engine::DebugRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
m_pDebugRenderer = pDebugRenderer.get();
pDebugRenderer->SetSceneWorld(m_pSceneWorld.get());
pDebugRenderer->SetEnable(false);
AddEngineRenderer(cd::MoveTemp(pDebugRenderer));
auto pWhiteModelRenderer = std::make_unique<engine::WhiteModelRenderer>(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<engine::DDGIRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
Expand Down
74 changes: 50 additions & 24 deletions Engine/Source/Runtime/ECWorld/StaticMeshComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -50,13 +55,12 @@ void StaticMeshComponent::Build()

m_vertexBuffer.resize(vertexCount * vertexFormatStride);

uint32_t currentDataSize = 0U;
auto currentDataPtr = m_vertexBuffer.data();

auto FillVertexBuffer = [&currentDataPtr, &currentDataSize](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(&currentDataPtr[currentDataSize], pData, dataSize);
currentDataSize += dataSize;
std::memcpy(&vbDataPtr[vbDataSize], pData, dataSize);
vbDataSize += dataSize;
};

for (uint32_t vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
Expand Down Expand Up @@ -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<uint32_t>(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<uint32_t>(std::numeric_limits<uint16_t>::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 = [&currentDataPtr, &currentDataSize](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(&currentDataPtr[currentDataSize], pData, dataSize);
currentDataSize += dataSize;
std::memcpy(&ibDataPtr[ibDataSize], pData, dataSize);
ibDataSize += dataSize;
};

for (const auto& polygon : m_pMeshData->GetPolygons())
Expand All @@ -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<uint32_t>(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<uint32_t>(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<uint32_t>(std::numeric_limits<uint16_t>::max()) + 1U;
auto SubmitIndexBuffer = [&useU16Index](const std::vector<std::byte>& indexBuffer) -> uint16_t
{
const bgfx::Memory* pIndexBufferRef = bgfx::makeRef(indexBuffer.data(), static_cast<uint32_t>(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<uint32_t>(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
}

}
9 changes: 9 additions & 0 deletions Engine/Source/Runtime/ECWorld/StaticMeshComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,6 +61,11 @@ class StaticMeshComponent final
std::vector<std::byte> m_indexBuffer;
uint16_t m_vertexBufferHandle = UINT16_MAX;
uint16_t m_indexBufferHandle = UINT16_MAX;

#ifdef EDITOR_MODE
std::vector<std::byte> m_wireframeIndexBuffer;
uint16_t m_wireframeIndexBufferHandle = UINT16_MAX;
#endif
};

}
10 changes: 2 additions & 8 deletions Engine/Source/Runtime/Rendering/AABBRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@ void AABBRenderer::UpdateView(const float* pViewMatrix, const float* pProjection

void AABBRenderer::RenderAll(float deltaTime)
{
for (Entity entity : m_pCurrentSceneWorld->GetStaticMeshEntities())
for (Entity entity : m_pCurrentSceneWorld->GetCollisionMeshEntities())
{
auto* pCollisionMesh = m_pCurrentSceneWorld->GetCollisionMeshComponent(entity);
if (!pCollisionMesh)
{
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() });

Expand All @@ -47,7 +42,6 @@ void AABBRenderer::RenderAll(float deltaTime)

constexpr StringCrc AABBAllProgram("AABBProgram");
bgfx::submit(GetViewID(), GetRenderContext()->GetProgram(AABBAllProgram));

}
}

Expand Down
Loading

0 comments on commit 8ab459b

Please sign in to comment.