Renderer: Add DebugDrawer

This commit is contained in:
SirLynix
2022-08-17 20:11:06 +02:00
parent 099528758c
commit 4a5f866754
5 changed files with 423 additions and 31 deletions

View File

@@ -9,53 +9,91 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Color.hpp>
#include <Nazara/Math/BoundingVolume.hpp>
#include <Nazara/Math/Box.hpp>
#include <Nazara/Math/Frustum.hpp>
#include <Nazara/Math/OrientedBox.hpp>
#include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Renderer/Config.hpp>
#include <Nazara/Renderer/UploadPool.hpp>
#include <Nazara/Utility/VertexStruct.hpp>
#include <memory>
#include <vector>
namespace Nz
{
class CommandBufferBuilder;
class RenderBuffer;
class RenderFrame;
class RenderDevice;
class RenderPipeline;
class RenderPipelineLayout;
class ShaderBinding;
class Skeleton;
class StaticMesh;
class NAZARA_RENDERER_API DebugDrawer
{
public:
static void Draw(const BoundingVolumef& volume);
static void Draw(const Boxf& box);
static void Draw(const Boxi& box);
static void Draw(const Boxui& box);
static void Draw(const Frustumf& frustum);
static void Draw(const OrientedBoxf& orientedBox);
static void Draw(const Skeleton* skeleton);
static void Draw(const Vector3f& position, float size = 0.1f);
static void DrawAxes(const Vector3f& position = Vector3f::Zero(), float size = 1.f);
static void DrawBinormals(const StaticMesh* subMesh);
static void DrawCone(const Vector3f& origin, const Quaternionf& rotation, float angle, float length);
static void DrawLine(const Vector3f& p1, const Vector3f& p2);
static void DrawPoints(const Vector3f* ptr, unsigned int pointCount);
static void DrawNormals(const StaticMesh* subMesh, float normalLength = 0.01f);
static void DrawTangents(const StaticMesh* subMesh);
DebugDrawer(RenderDevice& renderDevice, std::size_t maxVertexPerDraw = DefaultVertexBlockSize);
DebugDrawer(const DebugDrawer&) = delete;
DebugDrawer(DebugDrawer&&) = delete;
~DebugDrawer();
static void EnableDepthBuffer(bool depthBuffer);
void Draw(CommandBufferBuilder& builder);
static float GetLineWidth();
static float GetPointSize();
static Color GetPrimaryColor();
static Color GetSecondaryColor();
inline void DrawBox(const Boxf& box, const Color& color);
inline void DrawLine(const Vector3f& start, const Vector3f& end, const Color& color);
inline void DrawLine(const Vector3f& start, const Vector3f& end, const Color& startColor, const Color& endColor);
void DrawSkeleton(const Skeleton& skeleton, const Color& color);
static bool Initialize();
static bool IsDepthBufferEnabled();
void Prepare(RenderFrame& renderFrame);
static void SetLineWidth(float width);
static void SetPointSize(float size);
static void SetPrimaryColor(const Color& color);
static void SetSecondaryColor(const Color& color);
void Reset(RenderFrame& renderFrame);
static void Uninitialize();
void SetViewerData(const Matrix4f& viewProjMatrix);
DebugDrawer& operator=(const DebugDrawer&) = delete;
DebugDrawer& operator=(DebugDrawer&&) = delete;
static constexpr std::size_t DefaultVertexBlockSize = 4096;
private:
struct ViewerData
{
std::shared_ptr<RenderBuffer> buffer;
std::shared_ptr<ShaderBinding> binding;
};
struct DataPool
{
std::vector<std::shared_ptr<RenderBuffer>> vertexBuffers;
std::vector<ViewerData> viewerData;
};
struct DrawCall
{
std::shared_ptr<RenderBuffer> vertexBuffer;
std::uint64_t vertexCount;
};
struct PendingUpload
{
UploadPool::Allocation* allocation;
RenderBuffer* vertexBuffer;
};
std::shared_ptr<DataPool> m_dataPool;
std::shared_ptr<RenderPipeline> m_renderPipeline;
std::shared_ptr<RenderPipelineLayout> m_renderPipelineLayout;
std::size_t m_vertexPerBlock;
std::vector<DrawCall> m_drawCalls;
std::vector<PendingUpload> m_pendingUploads;
std::vector<UInt8> m_viewerData;
std::vector<VertexStruct_XYZ_Color> m_lineVertices;
RenderDevice& m_renderDevice;
ViewerData m_currentViewerData;
bool m_viewerDataUpdated;
};
}
#include <Nazara/Renderer/DebugDrawer.inl>
#endif // NAZARA_RENDERER_DEBUGDRAWER_HPP

View File

@@ -0,0 +1,46 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/DebugDrawer.hpp>
#include <Nazara/Renderer/Debug.hpp>
namespace Nz
{
inline void DebugDrawer::DrawBox(const Boxf& box, const Color& color)
{
Vector3f max = box.GetMaximum();
Vector3f min = box.GetMinimum();
DrawLine({ min.x, min.y, min.z }, { max.x, min.y, min.z }, color);
DrawLine({ min.x, min.y, min.z }, { min.x, max.y, min.z }, color);
DrawLine({ min.x, min.y, min.z }, { min.x, min.y, max.z }, color);
DrawLine({ max.x, max.y, max.z }, { min.x, max.y, max.z }, color);
DrawLine({ max.x, max.y, max.z }, { max.x, min.y, max.z }, color);
DrawLine({ max.x, max.y, max.z }, { max.x, max.y, min.z }, color);
DrawLine({ min.x, min.y, max.z }, { max.x, min.y, max.z }, color);
DrawLine({ min.x, min.y, max.z }, { min.x, max.y, max.z }, color);
DrawLine({ min.x, max.y, min.z }, { max.x, max.y, min.z }, color);
DrawLine({ min.x, max.y, min.z }, { min.x, max.y, max.z }, color);
DrawLine({ max.x, min.y, min.z }, { max.x, max.y, min.z }, color);
DrawLine({ max.x, min.y, min.z }, { max.x, min.y, max.z }, color);
}
inline void DebugDrawer::DrawLine(const Vector3f& start, const Vector3f& end, const Color& color)
{
return DrawLine(start, end, color, color);
}
inline void DebugDrawer::DrawLine(const Vector3f& start, const Vector3f& end, const Color& startColor, const Color& endColor)
{
auto& startVertex = m_lineVertices.emplace_back();
startVertex.color = startColor;
startVertex.position = start;
auto& endVertex = m_lineVertices.emplace_back();
endVertex.color = endColor;
endVertex.position = end;
}
}
#include <Nazara/Renderer/DebugOff.hpp>