LIBGDX: How to draw a filled polygon using shaperenderer?

I defined the shape using an array of vertices:

float[] points = new float[]{50,60,50,70,60,70, 60,60,50,60}; 

And I draw it here:

 shapeRenderer.polygon(floatNew); 

It just gives the outline of the form.
How to fill it with color?
Thanks

+8
java libgdx
source share
4 answers

ShapeRenderer currently supports drawing polygons (line by line), but does not fill.

This code cuts the polygon on the triangles, and then draws each triangle separately.

Modify ShapeRenderer.java as follows:

 EarClippingTriangulator ear = new EarClippingTriangulator(); public void polygon(float[] vertices, int offset, int count) { if (shapeType != ShapeType.Filled && shapeType != ShapeType.Line) throw new GdxRuntimeException("Must call begin(ShapeType.Filled) or begin(ShapeType.Line)"); if (count < 6) throw new IllegalArgumentException("Polygons must contain at least 3 points."); if (count % 2 != 0) throw new IllegalArgumentException("Polygons must have an even number of vertices."); check(shapeType, null, count); final float firstX = vertices[0]; final float firstY = vertices[1]; if (shapeType == ShapeType.Line) { for (int i = offset, n = offset + count; i < n; i += 2) { final float x1 = vertices[i]; final float y1 = vertices[i + 1]; final float x2; final float y2; if (i + 2 >= count) { x2 = firstX; y2 = firstY; } else { x2 = vertices[i + 2]; y2 = vertices[i + 3]; } renderer.color(color); renderer.vertex(x1, y1, 0); renderer.color(color); renderer.vertex(x2, y2, 0); } } else { ShortArray arrRes = ear.computeTriangles(vertices); for (int i = 0; i < arrRes.size - 2; i = i + 3) { float x1 = vertices[arrRes.get(i) * 2]; float y1 = vertices[(arrRes.get(i) * 2) + 1]; float x2 = vertices[(arrRes.get(i + 1)) * 2]; float y2 = vertices[(arrRes.get(i + 1) * 2) + 1]; float x3 = vertices[arrRes.get(i + 2) * 2]; float y3 = vertices[(arrRes.get(i + 2) * 2) + 1]; this.triangle(x1, y1, x2, y2, x3, y3); } } } 
+6
source share

You cannot draw a filled polygon using shaperender. take a look at this from bugtracker

You can also read this in the API.

public void polygon (float [] vertices)
Draws a polygon in the x / y plane. The vertices must contain at least 3 points (6 x, y floats). ShapeRenderer.ShapeType passed to start for ShapeRenderer.ShapeType.Line.


ShapeRender API
Of course, if its with ShapeType.Line you just get the outlines.
In this case, you need to do this yourself using triangles. It should be possible to fill at least triangles.
Maybe take a look at this from Stackoverflow: drawing-filled-polygon-with-libgdx

+2
source share

For this you should use mesh . Take a look at the link. Alternatively, you can check out PolygonSpriteBatch .

+1
source share

Modify the ShapeRenderer.java class by replacing the polygon () method with the following code:

 public void polygon(float[] vertices, int offset, int count) { if (currType != ShapeType.Filled && currType != ShapeType.Line) throw new GdxRuntimeException( "Must call begin(ShapeType.Filled) or begin(ShapeType.Line)"); if (count < 6) throw new IllegalArgumentException( "Polygons must contain at least 3 points."); if (count % 2 != 0) throw new IllegalArgumentException( "Polygons must have an even number of vertices."); checkDirty(); checkFlush(count); final float firstX = vertices[0]; final float firstY = vertices[1]; if (currType == ShapeType.Line) { for (int i = offset, n = offset + count; i < n; i += 2) { final float x1 = vertices[i]; final float y1 = vertices[i + 1]; final float x2; final float y2; if (i + 2 >= count) { x2 = firstX; y2 = firstY; } else { x2 = vertices[i + 2]; y2 = vertices[i + 3]; } renderer.color(color); renderer.vertex(x1, y1, 0); renderer.color(color); renderer.vertex(x2, y2, 0); } } else { for (int i = offset, n = offset + count; i < n; i += 4) { final float x1 = vertices[i]; final float y1 = vertices[i + 1]; if (i + 2 >= count) { break; } final float x2 = vertices[i + 2]; final float y2 = vertices[i + 3]; final float x3; final float y3; if (i + 4 >= count) { x3 = firstX; y3 = firstY; } else { x3 = vertices[i + 4]; y3 = vertices[i + 5]; } renderer.color(color); renderer.vertex(x1, y1, 0); renderer.color(color); renderer.vertex(x2, y2, 0); renderer.color(color); renderer.vertex(x3, y3, 0); } } } 

Using:

  gdx_shape_renderer.begin(ShapeType.Filled); gdx_shape_renderer.setColor(fill_r, fill_g, fill_b, fill_a); gdx_shape_renderer.polygon(vertices); gdx_shape_renderer.end(); gdx_shape_renderer.begin(ShapeType.Line); gdx_shape_renderer.setColor(border_r, border_g, border_b, border_a); gdx_shape_renderer.polygon(vertices); gdx_shape_renderer.end(); 
-2
source share

All Articles