Selaa lähdekoodia

Slight modifications to the draw helper

Marko Pintera 12 vuotta sitten
vanhempi
sitoutus
0fee942b2b

+ 21 - 8
BansheeEngine/Include/BsDrawHelper.h

@@ -13,6 +13,13 @@ namespace BansheeEngine
 		Normalized
 	};
 
+	enum class DrawStyle
+	{
+		Fill,
+		Border,
+		FillAndBorder
+	};
+
 	class BS_EXPORT DrawHelper : public CM::Module<DrawHelper>
 	{
 		enum class DebugDrawType
@@ -73,6 +80,7 @@ namespace BansheeEngine
 		 * @param	a				Start point of the line.
 		 * @param	b				End point of the line.
 		 * @param	width			Width of the line.
+		 * @param	borderWidth		Width of the anti-aliased border.
 		 * @param	color			Color of the line.
 		 * @param	meshData		Mesh data that will be populated by this method.
 		 * @param	vertexOffset	Offset in number of vertices from the start of the buffer to start writing at.
@@ -84,11 +92,13 @@ namespace BansheeEngine
 		 * 			  32bit index buffer
 		 *			  Enough space for 8 vertices and 30 indices
 		 */
-		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 line2D_AA(const CM::Vector2& a, const CM::Vector2& b, float width, float borderWidth, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
 
 		void drawQuad2D(const HCamera& camera, const CM::FRect& area, const CM::Color& color = CM::Color::White, CoordType coordType = CoordType::Pixel, float timeout = 0.0f);
-		void drawLine2D_Pixel(const HCamera& camera, const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color = CM::Color::White, CoordType coordType = CoordType::Pixel, float timeout = 0.0f);
-		void drawLine2D_AA(const HCamera& camera, const CM::Vector2& a, const CM::Vector2& b, float width, const CM::Color& color = CM::Color::White, CoordType coordType = CoordType::Pixel, float timeout = 0.0f);
+		void drawLine2D_Pixel(const HCamera& camera, const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color = CM::Color::White, 
+			CoordType coordType = CoordType::Pixel, float timeout = 0.0f);
+		void drawLine2D_AA(const HCamera& camera, const CM::Vector2& a, const CM::Vector2& b, float width, float borderWidth, 
+			const CM::Color& color = CM::Color::White, CoordType coordType = CoordType::Pixel, float timeout = 0.0f);
 
 		void render(const HCamera& camera, CM::RenderQueue& renderQueue);
 
@@ -97,16 +107,19 @@ namespace BansheeEngine
 
 		CM::UnorderedMap<const CM::Viewport*, CM::Vector<DebugDrawCommand>::type>::type mCommandsPerViewport;
 
-		void quad2D(const CM::FRect& area, 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, 
+		void line2D_AA(const CM::Vector2& a, const CM::Vector2& b, float width, float borderWidth, 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 borderWidth, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
+			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset);
+
+		void polygonFill2D_Pixel(const CM::Vector<CM::Vector2>::type& points, CM::UINT8* outVertices, 
 			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, 
+		void polygonBorder2D_Pixel(const CM::Vector<CM::Vector2>::type& points, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
 			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset);
 
 		CM::FRect normalizedCoordToClipSpace(const CM::FRect& area) const;

+ 73 - 54
BansheeEngine/Source/BsDrawHelper.cpp

@@ -19,32 +19,6 @@ namespace BansheeEngine
 		mMaterial2DClipSpace = BuiltinMaterialManager::instance().createDebugDraw2DClipSpaceMaterial();
 	}
 
-	void DrawHelper::quad2D(const CM::FRect& area, UINT8* outVertices, 
-		UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset)
-	{
-		outVertices += (vertexOffset * vertexStride);
-
-		Vector2* vertices = (Vector2*)outVertices;
-		(*vertices) = Vector2(area.x, area.y);
-
-		vertices = (Vector2*)(outVertices + vertexStride);
-		(*vertices) = Vector2(area.x + area.width, area.y);
-
-		vertices = (Vector2*)(outVertices + vertexStride * 2);
-		(*vertices) = Vector2(area.x, area.y + area.height);
-
-		vertices = (Vector2*)(outVertices + vertexStride * 3);
-		(*vertices) = Vector2(area.x + area.width, area.y + area.height);
-
-		outIndices += indexOffset;
-		outIndices[0] = vertexOffset + 0;
-		outIndices[1] = vertexOffset + 1;
-		outIndices[2] = vertexOffset + 2;
-		outIndices[3] = vertexOffset + 1;
-		outIndices[4] = vertexOffset + 3;
-		outIndices[5] = vertexOffset + 2;
-	}
-
 	void DrawHelper::quad2D(const CM::FRect& area, const MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
 	{
 		UINT32* indexData = meshData->getIndices32();
@@ -53,7 +27,13 @@ namespace BansheeEngine
 		assert((vertexOffset + 4) <= meshData->getNumVertices());
 		assert((indexOffset + 6) <= meshData->getNumIndices());
 
-		quad2D(area, positionData, vertexOffset, meshData->getVertexStride(), indexData, indexOffset);
+		Vector<Vector2>::type points;
+		points.push_back(Vector2(area.x, area.y));
+		points.push_back(Vector2(area.x + area.width, area.y));
+		points.push_back(Vector2(area.x + area.width, area.y + area.height));
+		points.push_back(Vector2(area.x, area.y + area.height));	
+		
+		polygonFill2D_Pixel(points, 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, 
@@ -91,38 +71,38 @@ namespace BansheeEngine
 		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, 
+	void DrawHelper::line2D_AA(const CM::Vector2& a, const CM::Vector2& b, float width, float borderWidth, 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;
-
+
+		Vector<Vector2>::type points(4);
+
+		float r = width - 1.0f;
+		r *= 0.5f;
+		if (r < 0.01f) 
+			r = 0.01f;
+
+		dir = dir * r;
+		nrm = nrm * r;
+
+		Vector2 v0 = a - dir - nrm;
+		Vector2 v1 = a - dir + nrm;
+		Vector2 v2 = b + dir + nrm;
+		Vector2 v3 = b + dir - nrm;
+
 		points[0] = v0;
 		points[1] = v1;
 		points[2] = v2;
 		points[3] = v3;
 
-		polygon2D_AA(points, width, color, outVertices, outColors, vertexOffset, vertexStride, outIndices, indexOffset);
+		polygon2D_AA(points, borderWidth, 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)
+	void DrawHelper::line2D_AA(const CM::Vector2& a, const CM::Vector2& b, float width, float borderWidth, const CM::Color& color, const MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
 	{
 		UINT32* indexData = meshData->getIndices32();
 		UINT8* positionData = meshData->getElementData(VES_POSITION);
@@ -131,16 +111,17 @@ namespace BansheeEngine
 		assert((vertexOffset + 8) <= meshData->getNumVertices());
 		assert((indexOffset + 30) <= meshData->getNumIndices());
 
-		line2D_AA(a, b, width, color, positionData, colorData, vertexOffset, meshData->getVertexStride(), indexData, indexOffset);
+		line2D_AA(a, b, width, borderWidth, color, positionData, colorData, vertexOffset, meshData->getVertexStride(), indexData, indexOffset);
 	}
 
-	void DrawHelper::polygon2D_AA(const Vector<Vector2>::type& points, float width, const CM::Color& color, 
+	void DrawHelper::polygon2D_AA(const Vector<Vector2>::type& points, float borderWidth, const CM::Color& color, 
 		UINT8* outVertices, CM::UINT8* outColors, UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset)
 	{
+		UINT32 numCoords = (UINT32)points.size();
+
 		outVertices += vertexOffset * vertexStride;
-		Vector<Vector2>::type tempNormals;
+		Vector<Vector2>::type tempNormals(numCoords);
 
-		UINT32 numCoords = (UINT32)points.size();
 		for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
 		{
 			const Vector2& v0 = points[j];
@@ -153,7 +134,7 @@ namespace BansheeEngine
 			std::swap(d.x, d.y);
 			d.y = -d.y;
 
-			tempNormals.push_back(d);
+			tempNormals[j] = d;
 
 			// Also start populating the vertex array
 			Vector2* vertices = (Vector2*)outVertices;
@@ -187,7 +168,7 @@ namespace BansheeEngine
 				avgNrm.y *= scale;
 			}
 
-			Vector2 tempCoord = points[i] + avgNrm * width;
+			Vector2 tempCoord = points[i] + avgNrm * borderWidth;
 
 			// Move it to the vertex array
 			Vector2* vertices = (Vector2*)outVertices;
@@ -223,6 +204,44 @@ namespace BansheeEngine
 		}
 	}
 
+	void DrawHelper::polygonFill2D_Pixel(const CM::Vector<CM::Vector2>::type& points, CM::UINT8* outVertices, 
+		CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
+	{
+		outVertices += (vertexOffset * vertexStride);
+
+		for(auto& point : points)
+		{
+			Vector2* vertices = (Vector2*)outVertices;
+			(*vertices) = Vector2(point.x, point.y);
+
+			outVertices += vertexStride;
+		}
+
+		outIndices += indexOffset;
+		INT32 numPoints = (INT32)points.size();
+		UINT32 idxCnt = 0;
+		for(int i = 2; i < numPoints; i++)
+		{
+			outIndices[idxCnt++] = vertexOffset;
+			outIndices[idxCnt++] = vertexOffset + i - 1;
+			outIndices[idxCnt++] = vertexOffset + i;
+		}
+	}
+
+	void DrawHelper::polygonBorder2D_Pixel(const CM::Vector<CM::Vector2>::type& points, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
+		CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
+	{
+		INT32 numPoints = (INT32)points.size();
+		UINT32 curVertOffset = vertexOffset;
+		UINT32 curIdxOffset = indexOffset;
+		for(INT32 i = 0, j = numPoints - 1; i < numPoints; j = i++)
+		{
+			line2D_Pixel(points[j], points[i], color, outVertices, outColors, curVertOffset, vertexStride, outIndices, curIdxOffset);
+			curVertOffset += 2;
+			curIdxOffset += 2;
+		}
+	}
+
 	void DrawHelper::drawQuad2D(const HCamera& camera, const CM::FRect& area, const Color& color, CoordType coordType, float timeout)
 	{
 		const Viewport* viewport = camera->getViewport().get();
@@ -333,7 +352,7 @@ namespace BansheeEngine
 		}
 	}
 
-	void DrawHelper::drawLine2D_AA(const HCamera& camera, const Vector2& a, const Vector2& b, float width, const Color& color, CoordType coordType, float timeout)
+	void DrawHelper::drawLine2D_AA(const HCamera& camera, const Vector2& a, const Vector2& b, float width, float borderWidth, const Color& color, CoordType coordType, float timeout)
 	{
 		const Viewport* viewport = camera->getViewport().get();
 
@@ -361,7 +380,7 @@ namespace BansheeEngine
 			actualB = normalizedCoordToClipSpace(b);
 		}
 
-		line2D_AA(actualA, actualB, width, color, meshData, 0, 0);
+		line2D_AA(actualA, actualB, width, borderWidth, color, meshData, 0, 0);
 
 		HMesh mesh = Mesh::create();
 

+ 1 - 1
CamelotClient/Source/BsMainEditorWindow.cpp

@@ -44,7 +44,7 @@ namespace BansheeEditor
 
 		DrawHelper::instance().drawLine2D_Pixel(sceneCamera, Vector2(0, 0), Vector2(20, 20), Color::Blue, CoordType::Pixel, 250.0f);
 
-		DrawHelper::instance().drawLine2D_AA(sceneCamera, Vector2(100, 0), Vector2(120, 20), 5.0f, Color::Blue, CoordType::Pixel, 250.0f);
+		DrawHelper::instance().drawLine2D_AA(sceneCamera, Vector2(100, 10), Vector2(120, 40), 1.0f, 1.0f, Color::Blue, CoordType::Pixel, 250.0f);
 	}
 
 	MainEditorWindow::~MainEditorWindow()

+ 0 - 8
DrawHelper.txt

@@ -19,16 +19,8 @@ Add class DrawHelper:
 
 -------------------------
 
- There's an issue with GPuParamBlockBuffer::writeData. Data is trying to be written but buffer is uninitialized! Happens with DX11
- DX9 reports a memory heap corruption
- - Both of these seems to be related to DrawHelper::render and don't seem to happen if I comment it out
- - My best guess is that Material::setFloat for ScreenSpace shader is not working properly
-   - ISsue possibly being that minimum amount of space allocated by the shader is 16 bytes, but ScreenSpace shader needs only 2
-   - This doubt is further confirmed by the crash not happening when I comment out the screen space rendering
-
 AA lines render but look a bit off.
 
-Vertex colors dont match between DX11/GL and DX9. DX9 seems to expect BGRA
 
 ---------