Przeglądaj źródła

Added basic support for 2D line & aliased polygon drawing
Renamed DebugDraw to DrawHelper

Marko Pintera 12 lat temu
rodzic
commit
36d9a44c79

+ 2 - 2
BansheeEngine/BansheeEngine.vcxproj

@@ -227,7 +227,7 @@
   </ItemDefinitionGroup>
   <ItemGroup>
     <ClInclude Include="Include\BsApplication.h" />
-    <ClInclude Include="Include\BsDebugDraw.h" />
+    <ClInclude Include="Include\BsDrawHelper.h" />
     <ClInclude Include="Include\BsDragAndDropManager.h" />
     <ClInclude Include="Include\BsEngineGUI.h" />
     <ClInclude Include="Include\BsGUIArea.h" />
@@ -284,7 +284,7 @@
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\BsApplication.cpp" />
-    <ClCompile Include="Source\BsDebugDraw.cpp" />
+    <ClCompile Include="Source\BsDrawHelper.cpp" />
     <ClCompile Include="Source\BsDragAndDropManager.cpp" />
     <ClCompile Include="Source\BsEngineGUI.cpp" />
     <ClCompile Include="Source\BsGUIArea.cpp" />

+ 6 - 6
BansheeEngine/BansheeEngine.vcxproj.filters

@@ -117,9 +117,6 @@
     <ClInclude Include="Include\BsUpdateCallback.h">
       <Filter>Header Files\Components</Filter>
     </ClInclude>
-    <ClInclude Include="Include\BsDebugDraw.h">
-      <Filter>Header Files</Filter>
-    </ClInclude>
     <ClInclude Include="Include\BsGUIArea.h">
       <Filter>Header Files\GUI</Filter>
     </ClInclude>
@@ -201,6 +198,9 @@
     <ClInclude Include="Include\BsRendererSort.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="Include\BsDrawHelper.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\BsGUIElement.cpp">
@@ -269,9 +269,6 @@
     <ClCompile Include="Source\BsUpdateCallback.cpp">
       <Filter>Source Files\Components</Filter>
     </ClCompile>
-    <ClCompile Include="Source\BsDebugDraw.cpp">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="Source\BsGUIArea.cpp">
       <Filter>Source Files\GUI</Filter>
     </ClCompile>
@@ -344,5 +341,8 @@
     <ClCompile Include="Source\BsGUIViewport.cpp">
       <Filter>Source Files\GUI</Filter>
     </ClCompile>
+    <ClCompile Include="Source\BsDrawHelper.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 0 - 43
BansheeEngine/Include/BsDebugDraw.h

@@ -1,43 +0,0 @@
-#pragma once
-
-#include "BsPrerequisites.h"
-#include "CmModule.h"
-#include "CmColor.h"
-#include "CmAABox.h"
-
-namespace BansheeEngine
-{
-	class BS_EXPORT DebugDraw : public CM::Module<DebugDraw>
-	{
-		enum class DebugDrawType
-		{
-			ScreenSpace,
-			WorldSpace
-		};
-
-		struct DebugDrawCommand
-		{
-			CM::HMesh mesh;
-			CM::HMaterial material;
-			DebugDrawType type;
-			CM::Vector3 worldCenter;
-			float endTime;
-		};
-
-	public:
-		DebugDraw();
-
-		// TODO - Add a version that accepts 4 points
-		void quad2D(const CM::Vector2& pos, const CM::Vector2& size, CM::UINT8* outVertices, CM::UINT8* outColors, 
-			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset, const CM::Color& color = CM::Color::White);
-
-		void drawQuad2D(const HCamera& camera, const CM::Vector2& pos, const CM::Vector2& size, const CM::Color& color = CM::Color::White, float timeout = 0.0f);
-
-		void render(const HCamera& camera, CM::RenderQueue& renderQueue);
-
-	private:
-		CM::HMaterial mMaterial2D;
-
-		CM::UnorderedMap<const CM::Viewport*, CM::Vector<DebugDrawCommand>::type>::type mCommandsPerViewport;
-	};
-}

+ 99 - 0
BansheeEngine/Include/BsDrawHelper.h

@@ -0,0 +1,99 @@
+#pragma once
+
+#include "BsPrerequisites.h"
+#include "CmModule.h"
+#include "CmColor.h"
+#include "CmAABox.h"
+
+namespace BansheeEngine
+{
+	class BS_EXPORT DrawHelper : public CM::Module<DrawHelper>
+	{
+		enum class DebugDrawType
+		{
+			ScreenSpace,
+			WorldSpace
+		};
+
+		struct DebugDrawCommand
+		{
+			CM::HMesh mesh;
+			CM::HMaterial material;
+			DebugDrawType type;
+			CM::Vector3 worldCenter;
+			float endTime;
+		};
+
+	public:
+		DrawHelper();
+
+		/**
+		 * @brief	TODO
+		 *
+		 * @param	pos				TODO
+		 * @param	size			TODO
+		 * @param	meshData		Mesh data that will be populated by this method.
+		 * @param	vertexOffset	Number of vertices from buffer start to start writing at.
+		 * @param	indexOffset 	Number of indices from buffer start to start writing at.
+		 * 							
+		 * @note	Provided MeshData must have some specific elements at least:
+		 * 			  Vector2 VES_POSITION
+		 * 			  32bit index buffer
+		 */
+		void quad2D(const CM::Vector2& pos, const CM::Vector2& size, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
+
+		/**
+		 * @brief	TODO
+		 *
+		 * @param	pos				TODO
+		 * @param	size			TODO
+		 * @param	meshData		Mesh data that will be populated by this method.
+		 * @param	vertexOffset	Number of vertices from buffer start to start writing at.
+		 * @param	indexOffset 	Number of indices from buffer start to start writing at.
+		 * 							
+		 * @note	Provided MeshData must have some specific elements at least:
+		 * 			  Vector2 VES_POSITION
+		 * 			  UINT32  VES_COLOR
+		 * 			  32bit index buffer
+		 */
+		void line2D_Pixel(const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
+
+		/**
+		 * @brief	TODO
+		 *
+		 * @param	pos				TODO
+		 * @param	size			TODO
+		 * @param	meshData		Mesh data that will be populated by this method.
+		 * @param	vertexOffset	Number of vertices from buffer start to start writing at.
+		 * @param	indexOffset 	Number of indices from buffer start to start writing at.
+		 * 							
+		 * @note	Provided MeshData must have some specific elements at least:
+		 * 			  Vector2 VES_POSITION
+		 * 			  UINT32  VES_COLOR
+		 * 			  32bit index buffer
+		 */
+		void line2D_AA(const CM::Vector2& a, const CM::Vector2& b, float width, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
+
+		void drawQuad2D(const HCamera& camera, const CM::Vector2& pos, const CM::Vector2& size, const CM::Color& color = CM::Color::White, float timeout = 0.0f);
+
+		void render(const HCamera& camera, CM::RenderQueue& renderQueue);
+
+	private:
+		CM::HMaterial mMaterial2D;
+
+		CM::UnorderedMap<const CM::Viewport*, CM::Vector<DebugDrawCommand>::type>::type mCommandsPerViewport;
+
+		// TODO - Add a version that accepts 4 points
+		void quad2D(const CM::Vector2& pos, const CM::Vector2& size, CM::UINT8* outVertices, CM::UINT32 vertexOffset, 
+			CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset);
+
+		void line2D_Pixel(const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
+			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset);
+
+		void line2D_AA(const CM::Vector2& a, const CM::Vector2& b, float width, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
+			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset);
+
+		void polygon2D_AA(const CM::Vector<CM::Vector2>::type& points, float width, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
+			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset);
+	};
+}

+ 3 - 3
BansheeEngine/Source/BsApplication.cpp

@@ -2,7 +2,7 @@
 #include "BsGUIMaterialManager.h"
 #include "BsGUIManager.h"
 #include "BsOverlayManager.h"
-#include "BsDebugDraw.h"
+#include "BsDrawHelper.h"
 #include "BsBuiltinMaterialManager.h"
 #include "BsD3D9BuiltinMaterialFactory.h"
 #include "BsD3D11BuiltinMaterialFactory.h"
@@ -45,7 +45,7 @@ namespace BansheeEngine
 		BuiltinMaterialManager::instance().addFactory(cm_new<GLBuiltinMaterialFactory>());
 		BuiltinMaterialManager::instance().setActive(desc.renderSystem);
 
-		DebugDraw::startUp(cm_new<DebugDraw>());
+		DrawHelper::startUp(cm_new<DrawHelper>());
 
 		EngineGUI::startUp(new EngineGUI());
 
@@ -63,7 +63,7 @@ namespace BansheeEngine
 
 		EngineGUI::shutDown();
 
-		DebugDraw::shutDown();
+		DrawHelper::shutDown();
 
 		GUIMaterialManager::instance().forceReleaseAllMaterials();
 

+ 0 - 135
BansheeEngine/Source/BsDebugDraw.cpp

@@ -1,135 +0,0 @@
-#include "BsDebugDraw.h"
-#include "CmMesh.h"
-#include "CmTime.h"
-#include "CmVector2.h"
-#include "CmMaterial.h"
-#include "CmPass.h"
-#include "CmApplication.h"
-#include "CmRenderQueue.h"
-#include "BsCamera.h"
-#include "BsBuiltinMaterialManager.h"
-
-using namespace CamelotFramework;
-
-namespace BansheeEngine
-{
-	DebugDraw::DebugDraw()
-	{
-		mMaterial2D = BuiltinMaterialManager::instance().createDebugDraw2DMaterial();
-	}
-
-	void DebugDraw::quad2D(const Vector2& pos, const Vector2& size, UINT8* outVertices, UINT8* outColors, 
-		UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset, const Color& color)
-	{
-		outVertices += vertexOffset;
-		outColors += vertexOffset;
-
-		Vector3* vertices = (Vector3*)outVertices;
-		(*vertices) = Vector3(pos.x, pos.y, 0.0f);
-
-		vertices = (Vector3*)(outVertices + vertexStride);
-		(*vertices) = Vector3(pos.x + size.x, pos.y, 0.0f);
-
-		vertices = (Vector3*)(outVertices + vertexStride * 2);
-		(*vertices) = Vector3(pos.x, pos.y + size.y, 0.0f);
-
-		vertices = (Vector3*)(outVertices + vertexStride * 3);
-		(*vertices) = Vector3(pos.x + size.x, pos.y + size.y, 0.0f);
-
-		UINT32* colors = (UINT32*)outColors;
-		(*colors) = color.getAsRGBA();
-
-		colors = (UINT32*)(outColors + vertexStride);
-		(*colors) = color.getAsRGBA();
-		
-		colors = (UINT32*)(outColors + vertexStride * 2);
-		(*colors) = color.getAsRGBA();
-
-		colors = (UINT32*)(outColors + vertexStride * 3);
-		(*colors) = color.getAsRGBA();
-
-		outIndices += indexOffset;
-		outIndices[0] = vertexOffset + 0;
-		outIndices[1] = vertexOffset + 2;
-		outIndices[2] = vertexOffset + 1;
-		outIndices[3] = vertexOffset + 1;
-		outIndices[4] = vertexOffset + 2;
-		outIndices[5] = vertexOffset + 3;
-	}
-
-	void DebugDraw::drawQuad2D(const HCamera& camera, const Vector2& pos, const Vector2& size, const CM::Color& color, float timeout)
-	{
-		const Viewport* viewport = camera->getViewport().get();
-
-		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
-
-		commands.push_back(DebugDrawCommand());
-		DebugDrawCommand& dbgCmd = commands.back();
-		dbgCmd.type = DebugDrawType::ScreenSpace;
-		dbgCmd.endTime = gTime().getTime() + timeout;
-
-		MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(4);
-
-		meshData->beginDesc();
-
-		meshData->addSubMesh(6);
-		meshData->addVertElem(VET_FLOAT3, VES_POSITION);
-		meshData->addVertElem(VET_COLOR, VES_COLOR);
-
-		meshData->endDesc();
-
-		UINT32* indexData = meshData->getIndices32();
-		UINT8* positionData = meshData->getElementData(VES_POSITION);
-		UINT8* colorData = meshData->getElementData(VES_COLOR);
-
-		quad2D(pos, size, positionData, colorData, 0, meshData->getVertexStride(), indexData, 0, color);
-
-		HMesh mesh = Mesh::create();
-		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
-		gMainSyncedCA().submitToCoreThread(true);
-
-		dbgCmd.mesh = mesh;
-		dbgCmd.material = mMaterial2D;
-		dbgCmd.worldCenter = Vector3::ZERO;
-	}
-
-	void DebugDraw::render(const HCamera& camera, CM::RenderQueue& renderQueue)
-	{
-		const Viewport* viewport = camera->getViewport().get();
-		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
-
-		Matrix4 projMatrixCstm = camera->getProjectionMatrix();
-		Matrix4 viewMatrixCstm = camera->getViewMatrix();
-		Matrix4 viewProjMatrix = projMatrixCstm * viewMatrixCstm;
-
-		for(auto& cmd : commands)
-		{
-			if(cmd.mesh == nullptr || !cmd.mesh.isLoaded())
-				continue;
-
-			if(cmd.material == nullptr || !cmd.material.isLoaded())
-				continue;
-
-			if(cmd.type == DebugDrawType::ScreenSpace)
-			{
-				renderQueue.add(cmd.material, cmd.mesh->getSubMeshData(), cmd.worldCenter);
-			}
-			else if(cmd.type == DebugDrawType::WorldSpace)
-			{
-				cmd.material->setMat4("matViewProj", viewProjMatrix);
-
-				renderQueue.add(cmd.material, cmd.mesh->getSubMeshData(), cmd.worldCenter);
-			}
-		}
-
-		float curTime = gTime().getTime();
-		Vector<DebugDrawCommand>::type newCommands;
-		for(auto& cmd : commands)
-		{
-			if(cmd.endTime > curTime)
-				newCommands.push_back(cmd);
-		}
-
-		commands.swap(newCommands);
-	}
-}

+ 313 - 0
BansheeEngine/Source/BsDrawHelper.cpp

@@ -0,0 +1,313 @@
+#include "BsDrawHelper.h"
+#include "CmMesh.h"
+#include "CmTime.h"
+#include "CmVector2.h"
+#include "CmMaterial.h"
+#include "CmPass.h"
+#include "CmApplication.h"
+#include "CmRenderQueue.h"
+#include "BsCamera.h"
+#include "BsBuiltinMaterialManager.h"
+
+using namespace CamelotFramework;
+
+namespace BansheeEngine
+{
+	DrawHelper::DrawHelper()
+	{
+		mMaterial2D = BuiltinMaterialManager::instance().createDebugDraw2DMaterial();
+	}
+
+	void DrawHelper::quad2D(const Vector2& pos, const Vector2& size, UINT8* outVertices, 
+		UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset)
+	{
+		outVertices += (vertexOffset * vertexStride);
+
+		Vector2* vertices = (Vector2*)outVertices;
+		(*vertices) = Vector2(pos.x, pos.y);
+
+		vertices = (Vector2*)(outVertices + vertexStride);
+		(*vertices) = Vector2(pos.x + size.x, pos.y);
+
+		vertices = (Vector2*)(outVertices + vertexStride * 2);
+		(*vertices) = Vector2(pos.x, pos.y + size.y);
+
+		vertices = (Vector2*)(outVertices + vertexStride * 3);
+		(*vertices) = Vector2(pos.x + size.x, pos.y + size.y);
+
+		outIndices += indexOffset;
+		outIndices[0] = vertexOffset + 0;
+		outIndices[1] = vertexOffset + 2;
+		outIndices[2] = vertexOffset + 1;
+		outIndices[3] = vertexOffset + 1;
+		outIndices[4] = vertexOffset + 2;
+		outIndices[5] = vertexOffset + 3;
+	}
+
+	void DrawHelper::quad2D(const CM::Vector2& pos, const CM::Vector2& size, const MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
+	{
+		UINT32* indexData = meshData->getIndices32();
+		UINT8* positionData = meshData->getElementData(VES_POSITION);
+
+		assert((vertexOffset + 4) < meshData->getNumVertices());
+		assert((indexOffset + 6) < meshData->getNumIndices());
+
+		quad2D(pos, size, positionData, vertexOffset, meshData->getVertexStride(), indexData, indexOffset);
+	}
+
+	void DrawHelper::line2D_Pixel(const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
+		CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
+	{
+		outVertices += (vertexOffset * vertexStride);
+		outColors += (vertexOffset * vertexStride);
+
+		Vector2* vertices = (Vector2*)outVertices;
+		(*vertices) = a;
+
+		vertices = (Vector2*)(outVertices + vertexStride);
+		(*vertices) = b;
+
+		UINT32* colors = (UINT32*)outColors;
+		(*colors) = color.getAsRGBA();
+
+		colors = (UINT32*)(outColors + vertexStride);
+		(*colors) = color.getAsRGBA();
+
+		outIndices += indexOffset;
+		outIndices[0] = vertexOffset + 0;
+		outIndices[1] = vertexOffset + 2;
+	}
+
+	void DrawHelper::line2D_Pixel(const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color, const MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
+	{
+		UINT32* indexData = meshData->getIndices32();
+		UINT8* positionData = meshData->getElementData(VES_POSITION);
+		UINT8* colorData = meshData->getElementData(VES_COLOR);
+
+		assert((vertexOffset + 2) < meshData->getNumVertices());
+		assert((indexOffset + 2) < meshData->getNumIndices());
+
+		line2D_Pixel(a, b, color, positionData, colorData, vertexOffset, meshData->getVertexStride(), indexData, indexOffset);
+	}
+
+	void DrawHelper::line2D_AA(const CM::Vector2& a, const CM::Vector2& b, float width, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
+		CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
+	{
+		Vector2 dir = b - a;
+		dir.normalize();
+
+		Vector2 nrm(dir.y, -dir.x);
+
+		Vector<Vector2>::type points(4);
+
+		width -= 1.0f;
+		width *= 0.5f;
+		if (width < 0.01f) 
+			width = 0.01f;
+
+		dir = dir * width;
+		nrm = nrm * width;
+
+		Vector2 v0 = a - dir - nrm;
+		Vector2 v1 = a - dir + nrm;
+		Vector2 v2 = b + dir + nrm;
+		Vector2 v3 = b + dir - nrm;
+
+		points.push_back(v0);
+		points.push_back(v1);
+		points.push_back(v2);
+		points.push_back(v3);
+
+		polygon2D_AA(points, width, color, outVertices, outColors, vertexOffset, vertexStride, outIndices, indexOffset);
+	}
+
+	void DrawHelper::line2D_AA(const CM::Vector2& a, const CM::Vector2& b, float width, const CM::Color& color, const MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
+	{
+		UINT32* indexData = meshData->getIndices32();
+		UINT8* positionData = meshData->getElementData(VES_POSITION);
+		UINT8* colorData = meshData->getElementData(VES_COLOR);
+
+		assert((vertexOffset + 2) < meshData->getNumVertices());
+		assert((indexOffset + 2) < meshData->getNumIndices());
+
+		line2D_AA(a, b, width, color, positionData, colorData, vertexOffset, meshData->getVertexStride(), indexData, indexOffset);
+	}
+
+	void DrawHelper::polygon2D_AA(const Vector<Vector2>::type& points, float width, const CM::Color& color, 
+		UINT8* outVertices, CM::UINT8* outColors, UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset)
+	{
+		outVertices += vertexOffset * vertexStride;
+		Vector<Vector2>::type tempNormals;
+
+		UINT32 numCoords = (UINT32)points.size();
+		for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
+		{
+			const Vector2& v0 = points[j];
+			const Vector2& v1 = points[i];
+
+			Vector2 d = v1 - v0;
+			d.normalize();
+
+			// Rotate by 90 degrees
+			std::swap(d.x, d.y);
+			d.y = -d.y;
+
+			tempNormals.push_back(d);
+
+			// Also start populating the vertex array
+			Vector2* vertices = (Vector2*)outVertices;
+			*vertices = v1;
+
+			UINT32* colors = (UINT32*)outColors;
+			*colors = color.getAsRGBA();
+
+			outVertices += vertexStride;
+			outColors += vertexStride;
+		}
+
+		Color transparentColor = color;
+		transparentColor.a = 0.0f;
+
+		for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
+		{
+			const Vector2& n0 = tempNormals[j];
+			const Vector2& n1 = tempNormals[i];
+
+			Vector2 avgNrm = (n0 + n1) * 0.5f;
+			float magSqrd = avgNrm.squaredLength();
+
+			if (magSqrd > 0.000001f)
+			{
+				float scale = 1.0f / magSqrd;
+				if (scale > 10.0f) 
+					scale = 10.0f;
+
+				avgNrm.x *= scale;
+				avgNrm.y *= scale;
+			}
+
+			Vector2 tempCoord = points[i] + avgNrm * width;
+
+			// Move it to the vertex array
+			Vector2* vertices = (Vector2*)outVertices;
+			*vertices = tempCoord;
+
+			UINT32* colors = (UINT32*)outColors;
+			*colors = transparentColor.getAsRGBA();
+
+			outVertices += vertexStride;
+			outColors += vertexStride;
+		}
+
+		// Populate index buffer
+		outIndices += indexOffset;
+
+		UINT32 idxCnt = 0;
+		for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
+		{
+			outIndices[idxCnt++] = i;
+			outIndices[idxCnt++] = j;
+			outIndices[idxCnt++] = numCoords + j;
+
+			outIndices[idxCnt++] = numCoords + j;
+			outIndices[idxCnt++] = numCoords + i;
+			outIndices[idxCnt++] = i;
+		}
+		
+		for(UINT32 i = 2; i < numCoords; ++i)
+		{
+			outIndices[idxCnt++] = 0;
+			outIndices[idxCnt++] = i - 1;
+			outIndices[idxCnt++] = i;
+		}
+	}
+
+	void DrawHelper::drawQuad2D(const HCamera& camera, const Vector2& pos, const Vector2& size, const Color& color, float timeout)
+	{
+		const Viewport* viewport = camera->getViewport().get();
+
+		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
+
+		commands.push_back(DebugDrawCommand());
+		DebugDrawCommand& dbgCmd = commands.back();
+		dbgCmd.type = DebugDrawType::ScreenSpace;
+		dbgCmd.endTime = gTime().getTime() + timeout;
+
+		MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(4);
+
+		meshData->beginDesc();
+
+		meshData->addSubMesh(6);
+		meshData->addVertElem(VET_FLOAT3, VES_POSITION);
+		meshData->addVertElem(VET_COLOR, VES_COLOR);
+
+		meshData->endDesc();
+
+		UINT32* indexData = meshData->getIndices32();
+		UINT8* positionData = meshData->getElementData(VES_POSITION);
+		UINT8* colorData = meshData->getElementData(VES_COLOR);
+
+		quad2D(pos, size, positionData, 0, meshData->getVertexStride(), indexData, 0);
+
+		UINT32 vertexStride = meshData->getVertexStride();
+		UINT32* colors = (UINT32*)meshData->getElementData(VES_COLOR);
+		(*colors) = color.getAsRGBA();
+
+		colors = (UINT32*)(colorData + vertexStride);
+		(*colors) = color.getAsRGBA();
+
+		colors = (UINT32*)(colorData + vertexStride * 2);
+		(*colors) = color.getAsRGBA();
+
+		colors = (UINT32*)(colorData + vertexStride * 3);
+		(*colors) = color.getAsRGBA();
+
+		HMesh mesh = Mesh::create();
+		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
+		gMainSyncedCA().submitToCoreThread(true);
+
+		dbgCmd.mesh = mesh;
+		dbgCmd.material = mMaterial2D;
+		dbgCmd.worldCenter = Vector3::ZERO;
+	}
+
+	void DrawHelper::render(const HCamera& camera, CM::RenderQueue& renderQueue)
+	{
+		const Viewport* viewport = camera->getViewport().get();
+		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
+
+		Matrix4 projMatrixCstm = camera->getProjectionMatrix();
+		Matrix4 viewMatrixCstm = camera->getViewMatrix();
+		Matrix4 viewProjMatrix = projMatrixCstm * viewMatrixCstm;
+
+		for(auto& cmd : commands)
+		{
+			if(cmd.mesh == nullptr || !cmd.mesh.isLoaded())
+				continue;
+
+			if(cmd.material == nullptr || !cmd.material.isLoaded())
+				continue;
+
+			if(cmd.type == DebugDrawType::ScreenSpace)
+			{
+				renderQueue.add(cmd.material, cmd.mesh->getSubMeshData(), cmd.worldCenter);
+			}
+			else if(cmd.type == DebugDrawType::WorldSpace)
+			{
+				cmd.material->setMat4("matViewProj", viewProjMatrix);
+
+				renderQueue.add(cmd.material, cmd.mesh->getSubMeshData(), cmd.worldCenter);
+			}
+		}
+
+		float curTime = gTime().getTime();
+		Vector<DebugDrawCommand>::type newCommands;
+		for(auto& cmd : commands)
+		{
+			if(cmd.endTime > curTime)
+				newCommands.push_back(cmd);
+		}
+
+		commands.swap(newCommands);
+	}
+}

+ 2 - 2
BansheeForwardRenderer/Source/BsForwardRenderer.cpp

@@ -14,7 +14,7 @@
 #include "CmRenderOperation.h"
 #include "CmDefaultRenderQueue.h"
 #include "BsOverlayManager.h"
-#include "BsDebugDraw.h"
+#include "BsDrawHelper.h"
 #include "BsGUIManager.h"
 
 using namespace CamelotFramework;
@@ -156,7 +156,7 @@ namespace BansheeEngine
 		OverlayManager::instance().render(camera->getViewport(), *mRenderQueue);
 
 		// Get debug render operations
-		DebugDraw::instance().render(camera, *mRenderQueue);
+		DrawHelper::instance().render(camera, *mRenderQueue);
 
 		// TODO - Material queue is completely ignored
 		mRenderQueue->sort();

+ 2 - 2
CamelotClient/Source/BsMainEditorWindow.cpp

@@ -7,7 +7,7 @@
 // DEBUG ONLY
 #include "CmTestTextSprite.h"
 #include "CmDebugCamera.h"
-#include "BsDebugDraw.h"
+#include "BsDrawHelper.h"
 
 using namespace CamelotFramework;
 using namespace BansheeEngine;
@@ -38,7 +38,7 @@ namespace BansheeEditor
 
 		textSprite->init(sceneCamera, "Testing in a new row, does this work?", nullptr);
 
-		DebugDraw::instance().drawQuad2D(sceneCamera, Vector2(-1.0f, 0.0f), Vector2(100, 50), Color::White, 50.0f);
+		DrawHelper::instance().drawQuad2D(sceneCamera, Vector2(-1.0f, 0.0f), Vector2(100, 50), Color::White, 50.0f);
 	}
 
 	MainEditorWindow::~MainEditorWindow()

+ 6 - 10
DrawHelper.txt

@@ -2,12 +2,10 @@ Add class DrawHelper:
  - Line2D_AA - Creates an emulated AA line using triangles. See Recast/imguiRenderGL::drawPolygon for implementation. Line vertices and colors
       are appended in the specified buffer. No actual drawing is done.
  - Line2D_Pixel - Creates a simple line using LINE primitive topology. Line vertices and colors are appended in the specified buffer.
- - Quad2D
+ - ScreenQuad - accepts normalized viewport coordinates
+ - Quad2D - accepts pixel coordinates
  - QuadBorder2D_AA
  - QuadBorder2D_Pixel
- - Polygon2D
- - PolygonBorder2D_AA
- - PolygonBorder2D_Pixel
  - LineList2D_AA
  - LineList2D_Pixel
  - RoundedRect2D - Also copy from Recast/imguiRenderGL::drawRoundedRect
@@ -15,10 +13,8 @@ Add class DrawHelper:
   - Line, Quad, QuadBorder, Box, BoxBorder
 
  - Then I also need draw* equivalents of those methods. draw equivalents will also create the buffer and submit the draw call.
-   - draw equivalents accept a time for how long to display the element, if 0 it will only be displayed for one frame
-   - all elements should be grouped into a common vertex/index buffer (as much as possible)
-   - draw methods should combine methods above to make drawing easier. e.g. a transparent box with AA line borders should be easily drawable
 
-   IMMEDIATE:
-    Add debug draw materials for DX9 and GL
-    I need to apply view transform on non-2D elements (probably will need another shader and material)
+--------------------------
+
+ Port the 2D shader so it accepts viewport size. Because we want things to be defined in pixels (except for fullscreen quad)
+ Only one material per viewport needs to exist, no need for separate material for each element

+ 0 - 9
TODO.txt

@@ -1,9 +1,5 @@
 ----------------------- CAMELOT 2D / GUI -----------------------------------------------------------
 
-DebugDraw
- - DebugDraw support for AA lines
- - Add DX9 and GL debug draw shaders
-
 I still re-create GUIWidget mesh every frame instead of just updating it.
 
 MAJOR ISSUE: writeSubresource/readSubresoure doesn't require a shared ptr to GpuResourceData which means it could get destroyed while still in command queue. Right now it only works because I block right after I call those methods, which ensures nothing is destroyed.
@@ -16,11 +12,8 @@ GUIWidget::updateMeshes leaks. If I leave the game running I can see memory cont
    gApplication and gBansheeApp, which is non-intuitive (e.g. retrieving a window can be done on gApplication, but running main loop can happen on both
 
 Over the holidays:
- - Get release mode working
  - Profile code to see why it runs to slow in debug (AQTime probably)
- - Edit solution so that 32 bit binaries end up it x86 folder
  - Window docking/undocking
- - (More platform specific files to /PlatformSpecific subfolder)
 
 IMMEDIATE:
  - Clicking on a window to focus and immediately trying to drag/resize it, doesn't work. I first need to click, then click again to drag/resize.
@@ -64,8 +57,6 @@ Later add InputMap class in which you can bind certain actions (like move left,
   - I probably want to determine front faces based on normals
 
 Immediate TODO:
- - A way to handle onMouseOver, onMouseOut, onMouseDown, onMouseUp events
- - (optional) Implement DirectDraw for drawing the bounds so I know they're correct
  - A way to update mesh buffers without recreating vertex/index buffers (Setting data currently does exactly that)
 
 ------------------------------------------------------------------------------------------------