Browse Source

Add draw debug triangle in DebugRenderer.

aster2013 11 years ago
parent
commit
b9f106ce9d

+ 83 - 3
Source/Engine/Graphics/DebugRenderer.cpp

@@ -44,6 +44,8 @@ extern const char* SUBSYSTEM_CATEGORY;
 
 // Cap the amount of lines to prevent crash when eg. debug rendering large heightfields
 static const unsigned MAX_LINES = 1000000;
+// Cap the amount of triangles to prevent crash.
+static const unsigned MAX_TRIANGLES = 100000;
 
 DebugRenderer::DebugRenderer(Context* context) :
     Component(context)
@@ -88,6 +90,22 @@ void DebugRenderer::AddLine(const Vector3& start, const Vector3& end, unsigned c
         noDepthLines_.Push(DebugLine(start, end, color));
 }
 
+void DebugRenderer::AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3,  const Color& color, bool depthTest)
+{
+    AddTriangle(v1, v2, v3, color.ToUInt(), depthTest);
+}
+
+void DebugRenderer::AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color, bool depthTest)
+{
+    if (triangles_.Size() + noDepthTriangles_.Size() >= MAX_TRIANGLES)
+        return;
+
+    if (depthTest)
+        triangles_.Push(DebugTriangle(v1, v2, v3, color));
+    else
+        noDepthTriangles_.Push(DebugTriangle(v1, v2, v3, color));
+}
+
 void DebugRenderer::AddNode(Node* node, float scale, bool depthTest)
 {
     if (!node)
@@ -305,7 +323,7 @@ void DebugRenderer::AddTriangleMesh(const void* vertexData, unsigned vertexSize,
 
 void DebugRenderer::Render()
 {
-    if (lines_.Empty() && noDepthLines_.Empty())
+    if (lines_.Empty() && noDepthLines_.Empty() && triangles_.Empty() && noDepthTriangles_.Empty())
         return;
 
     Graphics* graphics = GetSubsystem<Graphics>();
@@ -351,6 +369,38 @@ void DebugRenderer::Render()
         dest += 8;
     }
 
+    for (unsigned i = 0; i < triangles_.Size(); ++i)
+    {
+        const DebugTriangle& triangle = triangles_[i];
+
+        dest[0] = triangle.v1_.x_; dest[1] = triangle.v1_.y_; dest[2] = triangle.v1_.z_;
+        ((unsigned&)dest[3]) = triangle.color_;
+
+        dest[4] = triangle.v2_.x_; dest[5] = triangle.v2_.y_; dest[6] = triangle.v2_.z_;
+        ((unsigned&)dest[7]) = triangle.color_;
+        
+        dest[8] = triangle.v3_.x_; dest[9] = triangle.v3_.y_; dest[10] = triangle.v3_.z_;
+        ((unsigned&)dest[11]) = triangle.color_;
+
+        dest += 12;
+    }
+
+    for (unsigned i = 0; i < noDepthTriangles_.Size(); ++i)
+    {
+        const DebugTriangle& triangle = noDepthTriangles_[i];
+
+        dest[0] = triangle.v1_.x_; dest[1] = triangle.v1_.y_; dest[2] = triangle.v1_.z_;
+        ((unsigned&)dest[3]) = triangle.color_;
+
+        dest[4] = triangle.v2_.x_; dest[5] = triangle.v2_.y_; dest[6] = triangle.v2_.z_;
+        ((unsigned&)dest[7]) = triangle.color_;
+
+        dest[8] = triangle.v3_.x_; dest[9] = triangle.v3_.y_; dest[10] = triangle.v3_.z_;
+        ((unsigned&)dest[11]) = triangle.color_;
+
+        dest += 12;
+    }
+
     vertexBuffer_->Unlock();
 
     graphics->SetBlendMode(BLEND_REPLACE);
@@ -365,15 +415,37 @@ void DebugRenderer::Render()
     graphics->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
     graphics->SetVertexBuffer(vertexBuffer_);
 
+    unsigned start = 0;
+    unsigned count = 0;
     if (lines_.Size())
     {
+        count = lines_.Size() * 2;
         graphics->SetDepthTest(CMP_LESSEQUAL);
-        graphics->Draw(LINE_LIST, 0, lines_.Size() * 2);
+        graphics->Draw(LINE_LIST, start, count);
+        start += count;
     }
     if (noDepthLines_.Size())
     {
+        count = noDepthLines_.Size() * 2;
+        graphics->SetDepthTest(CMP_ALWAYS);
+        graphics->Draw(LINE_LIST, start, count);
+        start += count;
+    }
+    
+    graphics->SetBlendMode(BLEND_ALPHA);
+    
+    if (triangles_.Size())
+    {
+        count = triangles_.Size() * 3;
+        graphics->SetDepthTest(CMP_LESSEQUAL);
+        graphics->Draw(TRIANGLE_LIST, start, count);
+        start += count;
+    }
+    if (noDepthTriangles_.Size())
+    {
+        count = noDepthTriangles_.Size() * 3;
         graphics->SetDepthTest(CMP_ALWAYS);
-        graphics->Draw(LINE_LIST, lines_.Size() * 2, noDepthLines_.Size() * 2);
+        graphics->Draw(TRIANGLE_LIST, start, count);
     }
 }
 
@@ -387,14 +459,22 @@ void DebugRenderer::HandleEndFrame(StringHash eventType, VariantMap& eventData)
     // When the amount of debug geometry is reduced, release memory
     unsigned linesSize = lines_.Size();
     unsigned noDepthLinesSize = noDepthLines_.Size();
+    unsigned trianglesSize = triangles_.Size();
+    unsigned noDepthTrianglesSize = noDepthTriangles_.Size();
 
     lines_.Clear();
     noDepthLines_.Clear();
+    triangles_.Clear();
+    noDepthTriangles_.Clear();
 
     if (lines_.Capacity() > linesSize * 2)
         lines_.Reserve(linesSize);
     if (noDepthLines_.Capacity() > noDepthLinesSize * 2)
         noDepthLines_.Reserve(noDepthLinesSize);
+    if (triangles_.Capacity() > trianglesSize * 2)
+        triangles_.Reserve(trianglesSize);
+    if (noDepthTriangles_.Capacity() > noDepthTrianglesSize * 2)
+        noDepthTriangles_.Reserve(noDepthTrianglesSize);
 }
 
 }

+ 35 - 0
Source/Engine/Graphics/DebugRenderer.h

@@ -64,6 +64,33 @@ struct DebugLine
     unsigned color_;
 };
 
+/// Debug render triangle.
+struct DebugTriangle
+{
+    /// Construct undefined.
+    DebugTriangle()
+    {
+
+    }
+    /// Construct with start and end positions and color.
+    DebugTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color) :
+        v1_(v1),
+        v2_(v2),
+        v3_(v3),
+        color_(color)
+    {
+    }
+
+    /// Vertex a.
+    Vector3 v1_;
+    /// Vertex b.
+    Vector3 v2_;
+    /// Vertex c.
+    Vector3 v3_;
+    /// Color.
+    unsigned color_;
+};
+
 /// Debug geometry rendering component. Should be added only to the root scene node.
 class URHO3D_API DebugRenderer : public Component
 {
@@ -83,6 +110,10 @@ public:
     void AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest = true);
     /// Add a line with color already converted to unsigned.
     void AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest = true);
+    /// Add a triangle.
+    void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Color& color, bool depthTest = true);
+    /// Add a triangle with color already converted to unsigned.
+    void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color, bool depthTest = true);
     /// Add a scene node represented as its coordinate axes.
     void AddNode(Node* node, float scale = 1.0f, bool depthTest = true);
     /// Add a bounding box.
@@ -119,6 +150,10 @@ private:
     PODVector<DebugLine> lines_;
     /// Lines rendered without depth test.
     PODVector<DebugLine> noDepthLines_;
+    /// Triangles rendered with depth test.
+    PODVector<DebugTriangle> triangles_;
+    /// Triangles rendered without depth test.
+    PODVector<DebugTriangle> noDepthTriangles_;
     /// View transform.
     Matrix3x4 view_;
     /// Projection transform.

+ 2 - 0
Source/Engine/LuaScript/pkgs/Graphics/DebugRenderer.pkg

@@ -5,6 +5,8 @@ class DebugRenderer : public Component
     void SetView(Camera* camera);
     void AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest = true);
     void AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest = true);
+    void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Color& color, bool depthTest = true);
+    void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color, bool depthTest = true);
     void AddNode(Node* node, float scale = 1.0f, bool depthTest = true);
     void AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest = true);
     void AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest = true);

+ 1 - 0
Source/Engine/Script/GraphicsAPI.cpp

@@ -1368,6 +1368,7 @@ static void RegisterDebugRenderer(asIScriptEngine* engine)
 {
     RegisterComponent<DebugRenderer>(engine, "DebugRenderer", true, false);
     engine->RegisterObjectMethod("DebugRenderer", "void AddLine(const Vector3&in, const Vector3&in, const Color&in, bool depthTest = true)", asMETHODPR(DebugRenderer, AddLine, (const Vector3&, const Vector3&, const Color&, bool), void), asCALL_THISCALL);
+    engine->RegisterObjectMethod("DebugRenderer", "void AddTriangle(const Vector3&in, const Vector3&in, const Vector3&in, const Color&in, bool depthTest = true)", asMETHODPR(DebugRenderer, AddTriangle, (const Vector3&, const Vector3&, const Vector3&, const Color&, bool), void), asCALL_THISCALL);
     engine->RegisterObjectMethod("DebugRenderer", "void AddNode(Node@+, float scale = 1.0, bool depthTest = true)", asMETHOD(DebugRenderer, AddNode), asCALL_THISCALL);
     engine->RegisterObjectMethod("DebugRenderer", "void AddBoundingBox(const BoundingBox&in, const Color&in, bool depthTest = true)", asMETHODPR(DebugRenderer, AddBoundingBox, (const BoundingBox&, const Color&, bool), void), asCALL_THISCALL);
     engine->RegisterObjectMethod("DebugRenderer", "void AddFrustum(const Frustum&in, const Color&in, bool depthTest = true)", asMETHOD(DebugRenderer, AddFrustum), asCALL_THISCALL);