Browse Source

Added debug draw timeout, 2D/3D materials

Marko Pintera 12 years ago
parent
commit
bf4d036b47

+ 20 - 5
BansheeEngine/Include/BsDebugDraw.h

@@ -9,6 +9,21 @@ 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();
 
@@ -16,13 +31,13 @@ namespace BansheeEngine
 		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);
 
-		// TODO - Need a version that accepts a camera otherwise they will draw on all cameras
-		void drawQuad2D(const CM::Vector2& pos, const CM::Vector2& size, const CM::Color& color = CM::Color::White, float timeout = 0.0f);
+		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(CM::RenderQueue& renderQueue);
+		void render(const HCamera& camera, CM::RenderQueue& renderQueue);
 
 	private:
-		CM::HMaterial mMaterial;
-		CM::Vector<CM::HMesh>::type mMeshes;
+		CM::HMaterial mMaterial2D;
+
+		CM::UnorderedMap<const CM::Viewport*, CM::Vector<DebugDrawCommand>::type>::type mCommandsPerViewport;
 	};
 }

+ 3 - 3
BansheeEngine/Source/BsD3D11BuiltinMaterialFactory.cpp

@@ -249,7 +249,7 @@ namespace BansheeEngine
 
 	void D3D11BuiltinMaterialFactory::initDebugDraw3DShader()
 	{
-		String vsCode = "float4x4 viewTfrm;					\
+		String vsCode = "float4x4 matViewProj;					\
 															\
 						void vs_main(						\
 						in float3 inPos : POSITION,			\
@@ -257,7 +257,7 @@ namespace BansheeEngine
 						out float4 oPosition : SV_Position, \
 						out float4 oColor : COLOR0)			\
 						{										\
-						oPosition = mul(viewTfrm, float4(inPos.xyz, 1)); \
+						oPosition = mul(matViewProj, float4(inPos.xyz, 1)); \
 						oColor = color;						\
 						}";
 
@@ -276,7 +276,7 @@ namespace BansheeEngine
 
 		mDebugDraw3DShader = Shader::create("DebugDraw3DShader");
 
-		mDebugDraw3DShader->addParameter("viewTfrm", "viewTfrm", GPDT_MATRIX_4X4);
+		mDebugDraw3DShader->addParameter("matViewProj", "matViewProj", GPDT_MATRIX_4X4);
 
 		TechniquePtr newTechnique = mDebugDraw3DShader->addTechnique("D3D11RenderSystem", RendererManager::getCoreRendererName()); 
 		PassPtr newPass = newTechnique->addPass();

+ 3 - 3
BansheeEngine/Source/BsD3D9BuiltinMaterialFactory.cpp

@@ -247,7 +247,7 @@ namespace BansheeEngine
 
 	void D3D9BuiltinMaterialFactory::initDebugDraw3DShader()
 	{
-		String vsCode = "float4x4 viewTfrm;						\
+		String vsCode = "float4x4 matViewProj;						\
 																\
 						void vs_main(							\
 						in float3 inPos : POSITION,				\
@@ -255,7 +255,7 @@ namespace BansheeEngine
 						out float4 oPosition : POSITION,		\
 						out float4 oColor : COLOR0)				\
 						{										\
-						oPosition = mul(viewTfrm, float4(inPos.xyz, 1));		\
+						oPosition = mul(matViewProj, float4(inPos.xyz, 1));		\
 						oColor = inColor;						\
 						}										\
 						";
@@ -274,7 +274,7 @@ namespace BansheeEngine
 
 		mDebugDraw3DShader = Shader::create("DebugDraw3DShader");
 
-		mDebugDraw3DShader->addParameter("viewTfrm", "viewTfrm", GPDT_MATRIX_4X4);
+		mDebugDraw3DShader->addParameter("matViewProj", "matViewProj", GPDT_MATRIX_4X4);
 
 		TechniquePtr newTechnique = mDebugDraw3DShader->addTechnique("D3D9RenderSystem", RendererManager::getCoreRendererName()); 
 		PassPtr newPass = newTechnique->addPass();

+ 46 - 12
BansheeEngine/Source/BsDebugDraw.cpp

@@ -6,6 +6,7 @@
 #include "CmPass.h"
 #include "CmApplication.h"
 #include "CmRenderQueue.h"
+#include "BsCamera.h"
 #include "BsBuiltinMaterialManager.h"
 
 using namespace CamelotFramework;
@@ -14,7 +15,7 @@ namespace BansheeEngine
 {
 	DebugDraw::DebugDraw()
 	{
-		mMaterial = BuiltinMaterialManager::instance().createDebugDraw2DMaterial();
+		mMaterial2D = BuiltinMaterialManager::instance().createDebugDraw2DMaterial();
 	}
 
 	void DebugDraw::quad2D(const Vector2& pos, const Vector2& size, UINT8* outVertices, UINT8* outColors, 
@@ -56,9 +57,16 @@ namespace BansheeEngine
 		outIndices[5] = vertexOffset + 3;
 	}
 
-	void DebugDraw::drawQuad2D(const Vector2& pos, const Vector2& size, const CM::Color& color, float timeout)
+	void DebugDraw::drawQuad2D(const HCamera& camera, const Vector2& pos, const Vector2& size, const CM::Color& color, float timeout)
 	{
-		// TODO - DONT USE ONE MESH PER DRAW - Instead merge multiple elements into a single mesh
+		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);
 
@@ -80,22 +88,48 @@ namespace BansheeEngine
 		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
 		gMainSyncedCA().submitToCoreThread(true);
 
-		// TODO - Timeout is ignored!
-		mMeshes.push_back(mesh);
+		dbgCmd.mesh = mesh;
+		dbgCmd.material = mMaterial2D;
+		dbgCmd.worldCenter = Vector3::ZERO;
 	}
 
-	void DebugDraw::render(CM::RenderQueue& renderQueue)
+	void DebugDraw::render(const HCamera& camera, CM::RenderQueue& renderQueue)
 	{
-		if(mMaterial == nullptr || !mMaterial.isLoaded())
-			return;
+		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& mesh : mMeshes)
+		for(auto& cmd : commands)
 		{
-			if(mesh == nullptr || !mesh.isLoaded())
+			if(cmd.mesh == nullptr || !cmd.mesh.isLoaded())
+				continue;
+
+			if(cmd.material == nullptr || !cmd.material.isLoaded())
 				continue;
 
-			// TODO - I'm not setting a world position
-			renderQueue.add(mMaterial, mesh->getSubMeshData(), Vector3::ZERO);
+			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);
 	}
 }

+ 3 - 3
BansheeEngine/Source/BsGLBuiltinMaterialFactory.cpp

@@ -259,7 +259,7 @@ namespace BansheeEngine
 	{
 		String vsCode = "#version 400\n								\
 																	\
-						uniform mat4 viewTfrm;						\
+						uniform mat4 matViewProj;						\
 																	\
 						in vec3 cm_position;						\
 						in vec4 cm_color0;							\
@@ -267,7 +267,7 @@ namespace BansheeEngine
 																	\
 						void main()									\
 						{											\
-						gl_Position = viewTfrm * vec4(cm_position.xyz, 1);		\
+						gl_Position = matViewProj * vec4(cm_position.xyz, 1);		\
 						color0 = cm_color0;							\
 						}";
 
@@ -289,7 +289,7 @@ namespace BansheeEngine
 
 		mDebugDraw3DShader = Shader::create("DebugDraw3DShader");
 
-		mDebugDraw3DShader->addParameter("viewTfrm", "viewTfrm", GPDT_MATRIX_4X4);
+		mDebugDraw3DShader->addParameter("matViewProj", "matViewProj", GPDT_MATRIX_4X4);
 
 		TechniquePtr newTechnique = mDebugDraw3DShader->addTechnique("GLRenderSystem", RendererManager::getCoreRendererName()); 
 		PassPtr newPass = newTechnique->addPass();

+ 1 - 1
BansheeForwardRenderer/Source/BsForwardRenderer.cpp

@@ -156,7 +156,7 @@ namespace BansheeEngine
 		OverlayManager::instance().render(camera->getViewport(), *mRenderQueue);
 
 		// Get debug render operations
-		DebugDraw::instance().render(*mRenderQueue);
+		DebugDraw::instance().render(camera, *mRenderQueue);
 
 		// TODO - Material queue is completely ignored
 		mRenderQueue->sort();

+ 0 - 3
CamelotClient/CamelotClient.cpp

@@ -23,7 +23,6 @@
 #include "CmFontImportOptions.h"
 #include "CmCommandQueue.h"
 #include "CmBlendState.h"
-#include "BsDebugDraw.h"
 
 // Editor includes
 #include "BsEditorWindowManager.h"
@@ -271,8 +270,6 @@ int CALLBACK WinMain(
 
 	dbgCursor.reset();
 
-	DebugDraw::instance().drawQuad2D(Vector2(-1.0f, 0.0f), Vector2(100, 50), Color::White);
-
 	/************************************************************************/
 	/* 								EDITOR INIT                      		*/
 	/************************************************************************/

+ 3 - 0
CamelotClient/Source/BsMainEditorWindow.cpp

@@ -7,6 +7,7 @@
 // DEBUG ONLY
 #include "CmTestTextSprite.h"
 #include "CmDebugCamera.h"
+#include "BsDebugDraw.h"
 
 using namespace CamelotFramework;
 using namespace BansheeEngine;
@@ -36,6 +37,8 @@ namespace BansheeEditor
 		textSprite->initialize(mCamera->getViewport().get(), renderWindow.get());
 
 		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);
 	}
 
 	MainEditorWindow::~MainEditorWindow()

+ 2 - 2
CamelotCore/Include/CmMesh.h

@@ -74,8 +74,8 @@ namespace CamelotFramework
 
 		Mesh();
 
-		VertexData* mVertexData;
-		IndexData* mIndexData;
+		std::shared_ptr<VertexData> mVertexData;
+		std::shared_ptr<IndexData> mIndexData;
 
 		Vector<SubMesh>::type mSubMeshes;
 

+ 2 - 2
CamelotCore/Include/CmRenderOpMesh.h

@@ -23,8 +23,8 @@ namespace CamelotFramework
 			indexData(nullptr) 
 		{ }
 
-		VertexData *vertexData;
-		IndexData *indexData;
+		std::shared_ptr<VertexData> vertexData;
+		std::shared_ptr<IndexData> indexData;
 
 		DrawOperationType operationType;
 		bool useIndexes;

+ 2 - 14
CamelotCore/Source/CmMesh.cpp

@@ -33,12 +33,6 @@ namespace CamelotFramework
 
 		mSubMeshes.clear();
 
-		if(mVertexData != nullptr)
-			cm_delete<PoolAlloc>(mVertexData);
-
-		if(mIndexData != nullptr)
-			cm_delete<PoolAlloc>(mIndexData);
-
 		// Submeshes
 		for(UINT32 i = 0; i < meshData.getNumSubmeshes(); i++)
 		{
@@ -51,7 +45,7 @@ namespace CamelotFramework
 		}
 
 		// Indices
-		mIndexData = cm_new<IndexData, PoolAlloc>();
+		mIndexData = std::shared_ptr<IndexData>(cm_new<IndexData, PoolAlloc>());
 
 		mIndexData->indexCount = meshData.getNumIndices();
 		mIndexData->indexBuffer = HardwareBufferManager::instance().createIndexBuffer(
@@ -70,7 +64,7 @@ namespace CamelotFramework
 		mIndexData->indexBuffer->unlock();
 
 		// Vertices
-		mVertexData = cm_new<VertexData, PoolAlloc>();
+		mVertexData = std::shared_ptr<VertexData>(cm_new<VertexData, PoolAlloc>());
 
 		mVertexData->vertexCount = meshData.getNumVertices();
 		mVertexData->vertexDeclaration = meshData.createDeclaration();
@@ -247,12 +241,6 @@ namespace CamelotFramework
 	{
 		THROW_IF_NOT_CORE_THREAD;
 
-		if(mVertexData != nullptr)
-			cm_delete<PoolAlloc>(mVertexData);
-
-		if(mIndexData != nullptr)
-			cm_delete<PoolAlloc>(mIndexData);
-
 		Resource::destroy_internal();
 	}