Browse Source

Fill-mode love.graphics.circle/ellipse draws a triangle fan from the center of the ellipse, instead of from an edge.

--HG--
branch : minor
Alex Szpakowski 7 years ago
parent
commit
915e6c8eec
2 changed files with 19 additions and 5 deletions
  1. 18 4
      src/modules/graphics/Graphics.cpp
  2. 1 1
      src/modules/graphics/Graphics.h

+ 18 - 4
src/modules/graphics/Graphics.cpp

@@ -1316,7 +1316,20 @@ void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b, int po
 	float angle_shift = (two_pi / points);
 	float angle_shift = (two_pi / points);
 	float phi = .0f;
 	float phi = .0f;
 
 
-	Vector2 *coords = getScratchBuffer<Vector2>(points + 1);
+	// 1 extra point at the end for a closed loop, and 1 extra point at the
+	// start in filled mode for the vertex in the center of the ellipse.
+	int extrapoints = 1 + (mode == DRAW_FILL ? 1 : 0);
+
+	Vector2 *polygoncoords = getScratchBuffer<Vector2>(points + extrapoints);
+	Vector2 *coords = polygoncoords;
+
+	if (mode == DRAW_FILL)
+	{
+		coords[0].x = x;
+		coords[0].y = y;
+		coords++;
+	}
+
 	for (int i = 0; i < points; ++i, phi += angle_shift)
 	for (int i = 0; i < points; ++i, phi += angle_shift)
 	{
 	{
 		coords[i].x = x + a * cosf(phi);
 		coords[i].x = x + a * cosf(phi);
@@ -1325,7 +1338,8 @@ void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b, int po
 
 
 	coords[points] = coords[0];
 	coords[points] = coords[0];
 
 
-	polygon(mode, coords, points + 1);
+	// Last argument to polygon(): don't skip the last vertex in fill mode.
+	polygon(mode, polygoncoords, points + extrapoints, false);
 }
 }
 
 
 void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b)
 void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b)
@@ -1418,7 +1432,7 @@ void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float r
 	arc(drawmode, arcmode, x, y, radius, angle1, angle2, (int) (points + 0.5f));
 	arc(drawmode, arcmode, x, y, radius, angle1, angle2, (int) (points + 0.5f));
 }
 }
 
 
-void Graphics::polygon(DrawMode mode, const Vector2 *coords, size_t count)
+void Graphics::polygon(DrawMode mode, const Vector2 *coords, size_t count, bool skipLastFilledVertex)
 {
 {
 	// coords is an array of a closed loop of vertices, i.e.
 	// coords is an array of a closed loop of vertices, i.e.
 	// coords[count-1] == coords[0]
 	// coords[count-1] == coords[0]
@@ -1435,7 +1449,7 @@ void Graphics::polygon(DrawMode mode, const Vector2 *coords, size_t count)
 		cmd.formats[0] = vertex::getSinglePositionFormat(is2D);
 		cmd.formats[0] = vertex::getSinglePositionFormat(is2D);
 		cmd.formats[1] = vertex::CommonFormat::RGBAub;
 		cmd.formats[1] = vertex::CommonFormat::RGBAub;
 		cmd.indexMode = vertex::TriangleIndexMode::FAN;
 		cmd.indexMode = vertex::TriangleIndexMode::FAN;
-		cmd.vertexCount = (int)count - 1;
+		cmd.vertexCount = (int)count - (skipLastFilledVertex ? 1 : 0);
 
 
 		StreamVertexData data = requestStreamDraw(cmd);
 		StreamVertexData data = requestStreamDraw(cmd);
 
 

+ 1 - 1
src/modules/graphics/Graphics.h

@@ -717,7 +717,7 @@ public:
 	 * @param coords Vertex positions.
 	 * @param coords Vertex positions.
 	 * @param count Vertex array size.
 	 * @param count Vertex array size.
 	 **/
 	 **/
-	void polygon(DrawMode mode, const Vector2 *vertices, size_t count);
+	void polygon(DrawMode mode, const Vector2 *vertices, size_t count, bool skipLastFilledVertex = true);
 
 
 	/**
 	/**
 	 * Gets the graphics capabilities (feature support, limit values, and
 	 * Gets the graphics capabilities (feature support, limit values, and