From eb06eebc324e32c885f9de59ddd9371611b3d10f Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 15 Dec 2022 20:30:20 +0800 Subject: [PATCH] Added ability to debug draw circles --- .../MiddleEnd/Interface/SHDebugDrawSystem.cpp | 20 +++++++ .../MiddleEnd/Interface/SHDebugDrawSystem.h | 3 ++ .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 4 +- .../MiddleEnd/Interface/SHGraphicsSystem.h | 5 +- .../MiddleEnd/Meshes/SHPrimitiveGenerator.cpp | 53 ++++++++++++++++++- .../MiddleEnd/Meshes/SHPrimitiveGenerator.h | 41 ++++++++++++++ SHADE_Engine/src/Tools/SHDebugDraw.cpp | 10 ++++ SHADE_Engine/src/Tools/SHDebugDraw.h | 2 + 8 files changed, 134 insertions(+), 4 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp index f3527ed2..d77fbeb0 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -182,6 +182,11 @@ namespace SHADE DrawLineLoop({ p1, p2, p3, p4 }, color, depthTested); } + void SHDebugDrawSystem::DrawCircle(const SHMatrix& matrix, const SHColour& color /*= SHColour::WHITE*/, bool depthTested /*= false*/) + { + drawCircle(getMeshBatch(false, depthTested), matrix, color); + } + void SHDebugDrawSystem::DrawWireCube(const SHMatrix& matrix, const SHColour& color, bool depthTested) { drawWireCube(getMeshBatch(false, depthTested), matrix, color); @@ -229,6 +234,12 @@ namespace SHADE markPersistentDrawsDirty(); } + void SHDebugDrawSystem::DrawPersistentCircle(const SHMatrix& matrix, const SHColour& color, bool depthTested) + { + drawCircle(getPersistentMeshBatch(false, depthTested), matrix, color); + markPersistentDrawsDirty(); + } + void SHDebugDrawSystem::DrawPersistentWireCube(const SHMatrix& matrix, const SHColour& color, bool depthTested) { drawWireCube(getPersistentMeshBatch(false, depthTested), matrix, color); @@ -328,6 +339,15 @@ namespace SHADE ); } + void SHDebugDrawSystem::drawCircle(MeshBatch& batch, const SHMatrix& transformMatrix, const SHColour& color) + { + drawMesh + ( + gfxSystem->GetMeshPrimitive(PrimitiveType::LineCircle), + batch, transformMatrix, color + ); + } + /*-----------------------------------------------------------------------------------*/ /* Helper Batch Functions - Lines */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h index 99b74855..f62a099e 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h @@ -101,6 +101,7 @@ namespace SHADE void DrawLineLoop(IterType pointListBegin, IterType pointListEnd, const SHColour& color = SHColour::WHITE, bool depthTested = false); void DrawTri(const SHVec3& p1, const SHVec3& p2, const SHVec3& p3, const SHColour& color = SHColour::WHITE, bool depthTested = false); void DrawQuad(const SHVec3& p1, const SHVec3& p2, const SHVec3& p3, const SHVec3& p4, const SHColour& color = SHColour::WHITE, bool depthTested = false); + void DrawCircle(const SHMatrix& matrix, const SHColour& color = SHColour::WHITE, bool depthTested = false); void DrawWireCube(const SHMatrix& matrix, const SHColour& color = SHColour::WHITE, bool depthTested = false); void DrawWireSphere(const SHMatrix& matrix, const SHColour& color = SHColour::WHITE, bool depthTested = false); void DrawCube(const SHMatrix& matrix, const SHColour& color = SHColour::WHITE, bool depthTested = false); @@ -115,6 +116,7 @@ namespace SHADE void DrawPersistentLineLoop(IterType pointListBegin, IterType pointListEnd, const SHColour& color = SHColour::WHITE, bool depthTested = false); void DrawPersistentTri(const SHVec3& p1, const SHVec3& p2, const SHVec3& p3, const SHColour& color = SHColour::WHITE, bool depthTested = false); void DrawPersistentQuad(const SHVec3& p1, const SHVec3& p2, const SHVec3& p3, const SHVec3& p4, const SHColour& color = SHColour::WHITE, bool depthTested = false); + void DrawPersistentCircle(const SHMatrix& matrix, const SHColour& color = SHColour::WHITE, bool depthTested = false); void DrawPersistentWireCube(const SHMatrix& matrix, const SHColour& color = SHColour::WHITE, bool depthTested = false); void DrawPersistentWireSphere(const SHMatrix& matrix, const SHColour& color = SHColour::WHITE, bool depthTested = false); void DrawPersistentCube(const SHMatrix& matrix, const SHColour& color = SHColour::WHITE, bool depthTested = false); @@ -216,6 +218,7 @@ namespace SHADE void drawWireSphere(MeshBatch& batch, const SHMatrix& transformMatrix, const SHColour& color); void drawCube(MeshBatch& batch, const SHMatrix& transformMatrix, const SHColour& color); void drawSphere(MeshBatch& batch, const SHMatrix& transformMatrix, const SHColour& color); + void drawCircle(MeshBatch& batch, const SHMatrix& transformMatrix, const SHColour& color); /*---------------------------------------------------------------------------------*/ /* Helper Batch Functions - Lines */ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 8b18d4d3..9a599a07 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -413,8 +413,9 @@ namespace SHADE // Create default meshes primitiveMeshes[static_cast(PrimitiveType::Cube)] = SHPrimitiveGenerator::Cube(meshLibrary); - primitiveMeshes[static_cast(PrimitiveType::LineCube)] = SHPrimitiveGenerator::LineCube(meshLibrary); primitiveMeshes[static_cast(PrimitiveType::Sphere)] = SHPrimitiveGenerator::Sphere(meshLibrary); + primitiveMeshes[static_cast(PrimitiveType::LineCube)] = SHPrimitiveGenerator::LineCube(meshLibrary); + primitiveMeshes[static_cast(PrimitiveType::LineCircle)] = SHPrimitiveGenerator::LineCircle(meshLibrary); BuildMeshBuffers(); // Create default materials @@ -840,6 +841,7 @@ namespace SHADE case PrimitiveType::Cube: case PrimitiveType::Sphere: case PrimitiveType::LineCube: + case PrimitiveType::LineCircle: return primitiveMeshes[static_cast(type)]; default: return {}; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 609ff3df..75b48c9b 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -71,9 +71,10 @@ namespace SHADE { Cube, Sphere, - LineCube + LineCube, + LineCircle }; - static constexpr int MAX_PRIMITIVE_TYPES = 3; + static constexpr int MAX_PRIMITIVE_TYPES = 4; enum class DebugDrawPipelineType { LineNoDepthTest, diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp index 93e4be1a..444a6630 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp @@ -10,9 +10,13 @@ Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited. *//*************************************************************************************/ +// Precompiled Header #include "SHpch.h" +// Primary Includes #include "SHPrimitiveGenerator.h" - +// STL Includes +#include +// Project Includes #include "Graphics/MiddleEnd/Interface/SHGraphicsSystem.h" #include "Graphics/MiddleEnd/Interface/SHMeshLibrary.h" @@ -24,6 +28,7 @@ namespace SHADE SHMeshData SHPrimitiveGenerator::cubeMesh; SHMeshData SHPrimitiveGenerator::sphereMesh; SHMeshData SHPrimitiveGenerator::lineCubeMesh; + SHMeshData SHPrimitiveGenerator::lineCircleMesh; /*-----------------------------------------------------------------------------------*/ /* Primitive Generation Functions */ @@ -341,6 +346,52 @@ namespace SHADE return addMeshDataTo(lineCubeMesh, gfxSystem); } + SHMeshData SHPrimitiveGenerator::LineCircle() noexcept + { + SHMeshData mesh; + + // Generate points of the circle + static constexpr int SPLITS = 36; + static constexpr float ANGLE_INCREMENTS = (std::numbers::pi_v * 2.0f) / static_cast(SPLITS); + for (int i = 0; i < SPLITS; ++i) + { + const float ANGLE = ANGLE_INCREMENTS * i; + mesh.VertexPositions.emplace_back(cos(ANGLE) * 0.5f, sin(ANGLE) * 0.5f, 0.0f); + } + + // Generate lines of the circle + for (int i = 1; i < SPLITS; ++i) + { + mesh.Indices.emplace_back(static_cast(i - 1)); + mesh.Indices.emplace_back(static_cast(i)); + } + // Last line to complete the circle + mesh.Indices.emplace_back(static_cast(SPLITS - 1)); + mesh.Indices.emplace_back(static_cast(0)); + + mesh.VertexNormals.resize(mesh.VertexPositions.size()); + mesh.VertexTangents.resize(mesh.VertexPositions.size()); + mesh.VertexTexCoords.resize(mesh.VertexPositions.size()); + + return mesh; + } + + Handle SHPrimitiveGenerator::LineCircle(SHMeshLibrary& meshLibrary) noexcept + { + if (lineCircleMesh.VertexPositions.empty()) + lineCircleMesh = LineCircle(); + + return addMeshDataTo(lineCircleMesh, meshLibrary); + } + + Handle SHPrimitiveGenerator::LineCircle(SHGraphicsSystem& gfxSystem) noexcept + { + if (lineCircleMesh.VertexPositions.empty()) + lineCircleMesh = LineCircle(); + + return addMeshDataTo(lineCircleMesh, gfxSystem); + } + /*-----------------------------------------------------------------------------------*/ /* Helper Functions */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h index 8586b480..9bcd2f3c 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h @@ -154,6 +154,46 @@ namespace SHADE */ /***********************************************************************************/ [[nodiscard]] static Handle LineCube(SHGraphicsSystem& gfxSystem) noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a circle that is comprised only of lines with no diagonal lines and + store the data in a SHMeshData object. + + \return + SHMeshData object containing vertex data for the line circle. + */ + /***********************************************************************************/ + [[nodiscard]] static SHMeshData LineCircle() noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a line circle and constructs a SHMesh using the SHGraphicsSystem + provided. + + \param meshLibrary + Reference to the SHMeshLibrary to produce and store a line circle mesh in. + + \return + SHMesh object that points to the generated line circle mesh in the SHMeshLibrary. + */ + /***********************************************************************************/ + [[nodiscard]] static Handle LineCircle(SHMeshLibrary& meshLibrary) noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a line circle and constructs a SHMesh using the SHGraphicsSystem + provided. + + \param gfxSystem + Reference to the SHGraphicsSystem to produce and store a line circle mesh in. + + \return + SHMesh object that points to the generated line circle mesh in the + SHGraphicsSystem. + */ + /***********************************************************************************/ + [[nodiscard]] static Handle LineCircle(SHGraphicsSystem& gfxSystem) noexcept; private: /*---------------------------------------------------------------------------------*/ @@ -168,5 +208,6 @@ namespace SHADE static SHMeshData cubeMesh; static SHMeshData sphereMesh; static SHMeshData lineCubeMesh; + static SHMeshData lineCircleMesh; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Tools/SHDebugDraw.cpp b/SHADE_Engine/src/Tools/SHDebugDraw.cpp index 3b64eabf..15a4bedd 100644 --- a/SHADE_Engine/src/Tools/SHDebugDraw.cpp +++ b/SHADE_Engine/src/Tools/SHDebugDraw.cpp @@ -57,6 +57,11 @@ namespace SHADE dbgDrawSys->DrawQuad(pt1, pt2, pt3, pt4, color); } + void SHDebugDraw::Circle(const SHMatrix& mat, const SHVec4& color) + { + dbgDrawSys->DrawCircle(mat, color); + } + void SHDebugDraw::LineLoop(const SHVec4& color, std::initializer_list pointList) { dbgDrawSys->DrawLineLoop(pointList, color); @@ -110,6 +115,11 @@ namespace SHADE dbgDrawSys->DrawPersistentQuad(pt1, pt2, pt3, pt4, color); } + void SHDebugDraw::PersistentCircle(const SHMatrix& mat, const SHVec4& color) + { + dbgDrawSys->DrawPersistentCircle(mat, color); + } + void SHDebugDraw::PersistentLineLoop(const SHVec4& color, std::initializer_list pointList) { dbgDrawSys->DrawPersistentLineLoop(pointList, color); diff --git a/SHADE_Engine/src/Tools/SHDebugDraw.h b/SHADE_Engine/src/Tools/SHDebugDraw.h index e213dc45..5438d8f3 100644 --- a/SHADE_Engine/src/Tools/SHDebugDraw.h +++ b/SHADE_Engine/src/Tools/SHDebugDraw.h @@ -72,6 +72,7 @@ namespace SHADE /// Third point of the quadrilateral. /// Third point of the quadrilateral. static void Quad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4); + static void Circle(const SHMatrix& mat,const SHVec4& color); /// /// Renders a polygon indicated by the specified set of points in world space. /// @@ -115,6 +116,7 @@ namespace SHADE /// Third point of the quadrilateral. /// Third point of the quadrilateral. static void PersistentQuad(const SHVec4& color, const SHVec3& pt1, const SHVec3& pt2, const SHVec3& pt3, const SHVec3& pt4); + static void PersistentCircle(const SHMatrix& mat,const SHVec4& color); /// /// Renders a polygon indicated by the specified set of points in world space that /// will persist until ClearPersistentDraws() is called.