Class ShapeRenderer
- java.lang.Object
-
- com.badlogic.gdx.graphics.glutils.ShapeRenderer
-
- All Implemented Interfaces:
Disposable
public class ShapeRenderer extends java.lang.Object implements Disposable
Renders points, lines, shape outlines and filled shapes.By default a 2D orthographic projection with the origin in the lower left corner is used and units are specified in screen pixels. This can be changed by configuring the projection matrix, usually using the
Camera.combinedmatrix. If the screen resolution changes, the projection matrix may need to be updated.Shapes are rendered in batches to increase performance. Standard usage pattern looks as follows:
ShapeRenderer has a second matrix called the transformation matrix which is used to rotate, scale and translate shapes in a more flexible manner. The following example shows how to rotate a rectangle around its center using the z-axis as the rotation axis and placing it's center at (20, 12, 2):camera.update(); shapeRenderer.setProjectionMatrix(camera.combined); shapeRenderer.begin(ShapeType.Line); shapeRenderer.setColor(1, 1, 0, 1); shapeRenderer.line(x, y, x2, y2); shapeRenderer.rect(x, y, width, height); shapeRenderer.circle(x, y, radius); shapeRenderer.end(); shapeRenderer.begin(ShapeType.Filled); shapeRenderer.setColor(0, 1, 0, 1); shapeRenderer.rect(x, y, width, height); shapeRenderer.circle(x, y, radius); shapeRenderer.end();shapeRenderer.begin(ShapeType.Line); shapeRenderer.identity(); shapeRenderer.translate(20, 12, 2); shapeRenderer.rotate(0, 0, 1, 90); shapeRenderer.rect(-width / 2, -height / 2, width, height); shapeRenderer.end();
Matrix operations all use postmultiplication and work just like glTranslate, glScale and glRotate. The last transformation specified will be the first that is applied to a shape (rotate then translate in the above example).The projection and transformation matrices are a state of the ShapeRenderer, just like the color, and will be applied to all shapes until they are changed.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classShapeRenderer.ShapeTypeShape types to be used withbegin(ShapeType).
-
Constructor Summary
Constructors Constructor Description ShapeRenderer()ShapeRenderer(int maxVertices)ShapeRenderer(int maxVertices, ShaderProgram defaultShader)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidarc(float x, float y, float radius, float start, float degrees)Callsarc(float, float, float, float, float, int)by estimating the number of segments needed for a smooth arc.voidarc(float x, float y, float radius, float start, float degrees, int segments)Draws an arc usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidbegin()Begins a new batch without specifying a shape type.voidbegin(ShapeRenderer.ShapeType type)Starts a new batch of shapes.voidbox(float x, float y, float z, float width, float height, float depth)Draws a cube usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.protected voidcheck(ShapeRenderer.ShapeType preferred, ShapeRenderer.ShapeType other, int newVertices)Checks whether the correct ShapeType was set.voidcircle(float x, float y, float radius)Callscircle(float, float, float, int)by estimating the number of segments needed for a smooth circle.voidcircle(float x, float y, float radius, int segments)Draws a circle usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidcone(float x, float y, float z, float radius, float height)Callscone(float, float, float, float, float, int)by estimating the number of segments needed for a smooth circular base.voidcone(float x, float y, float z, float radius, float height, int segments)Draws a cone usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidcurve(float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x2, float y2, int segments)Draws a curve usingShapeRenderer.ShapeType.Line.voiddispose()Releases all resources of this object.voidellipse(float x, float y, float width, float height)Callsellipse(float, float, float, float, int)by estimating the number of segments needed for a smooth ellipse.voidellipse(float x, float y, float width, float height, float rotation)Callsellipse(float, float, float, float, float, int)by estimating the number of segments needed for a smooth ellipse.voidellipse(float x, float y, float width, float height, float rotation, int segments)Draws an ellipse usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidellipse(float x, float y, float width, float height, int segments)Draws an ellipse usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidend()Finishes the batch of shapes and ensures they get rendered.voidflush()ColorgetColor()ShapeRenderer.ShapeTypegetCurrentType()Returns the current shape type.Matrix4getProjectionMatrix()If the matrix is modified,updateMatrices()must be called.ImmediateModeRenderergetRenderer()Matrix4getTransformMatrix()If the matrix is modified,updateMatrices()must be called.voididentity()Sets the transformation matrix to identity.booleanisDrawing()voidline(float x, float y, float x2, float y2)voidline(float x, float y, float z, float x2, float y2, float z2)Draws a line usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidline(float x, float y, float z, float x2, float y2, float z2, Color c1, Color c2)Draws a line usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidline(float x, float y, float x2, float y2, Color c1, Color c2)voidline(Vector2 v0, Vector2 v1)voidline(Vector3 v0, Vector3 v1)voidpoint(float x, float y, float z)Draws a point usingShapeRenderer.ShapeType.Point,ShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidpolygon(float[] vertices)voidpolygon(float[] vertices, int offset, int count)Draws a polygon in the x/y plane usingShapeRenderer.ShapeType.Line.voidpolyline(float[] vertices)voidpolyline(float[] vertices, int offset, int count)Draws a polyline in the x/y plane usingShapeRenderer.ShapeType.Line.voidrect(float x, float y, float width, float height)Draws a rectangle in the x/y plane usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidrect(float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float degrees)Draws a rectangle in the x/y plane usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidrect(float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float degrees, Color col1, Color col2, Color col3, Color col4)Draws a rectangle in the x/y plane usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidrect(float x, float y, float width, float height, Color col1, Color col2, Color col3, Color col4)Draws a rectangle in the x/y plane usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidrectLine(float x1, float y1, float x2, float y2, float width)Draws a line using a rotated rectangle, where with one edge is centered at x1, y1 and the opposite edge centered at x2, y2.voidrectLine(float x1, float y1, float x2, float y2, float width, Color c1, Color c2)Draws a line using a rotated rectangle, where with one edge is centered at x1, y1 and the opposite edge centered at x2, y2.voidrectLine(Vector2 p1, Vector2 p2, float width)voidrotate(float axisX, float axisY, float axisZ, float degrees)Multiplies the current transformation matrix by a rotation matrix.voidscale(float scaleX, float scaleY, float scaleZ)Multiplies the current transformation matrix by a scale matrix.voidset(ShapeRenderer.ShapeType type)voidsetAutoShapeType(boolean autoShapeType)If true, when drawing a shape cannot be performed with the current shape type, the batch is flushed and the shape type is changed automatically.voidsetColor(float r, float g, float b, float a)Sets the color to be used by the next shapes drawn.voidsetColor(Color color)Sets the color to be used by the next shapes drawn.voidsetProjectionMatrix(Matrix4 matrix)Sets the projection matrix to be used for rendering.voidsetTransformMatrix(Matrix4 matrix)voidtranslate(float x, float y, float z)Multiplies the current transformation matrix by a translation matrix.voidtriangle(float x1, float y1, float x2, float y2, float x3, float y3)Draws a triangle in x/y plane usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidtriangle(float x1, float y1, float x2, float y2, float x3, float y3, Color col1, Color col2, Color col3)Draws a triangle in x/y plane with colored corners usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidupdateMatrices()voidx(float x, float y, float size)Draws two crossed lines usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.voidx(Vector2 p, float size)
-
-
-
Constructor Detail
-
ShapeRenderer
public ShapeRenderer()
-
ShapeRenderer
public ShapeRenderer(int maxVertices)
-
ShapeRenderer
public ShapeRenderer(int maxVertices, ShaderProgram defaultShader)
-
-
Method Detail
-
setColor
public void setColor(Color color)
Sets the color to be used by the next shapes drawn.
-
setColor
public void setColor(float r, float g, float b, float a)Sets the color to be used by the next shapes drawn.
-
getColor
public Color getColor()
-
updateMatrices
public void updateMatrices()
-
setProjectionMatrix
public void setProjectionMatrix(Matrix4 matrix)
Sets the projection matrix to be used for rendering. Usually this will be set toCamera.combined.- Parameters:
matrix-
-
getProjectionMatrix
public Matrix4 getProjectionMatrix()
If the matrix is modified,updateMatrices()must be called.
-
setTransformMatrix
public void setTransformMatrix(Matrix4 matrix)
-
getTransformMatrix
public Matrix4 getTransformMatrix()
If the matrix is modified,updateMatrices()must be called.
-
identity
public void identity()
Sets the transformation matrix to identity.
-
translate
public void translate(float x, float y, float z)Multiplies the current transformation matrix by a translation matrix.
-
rotate
public void rotate(float axisX, float axisY, float axisZ, float degrees)Multiplies the current transformation matrix by a rotation matrix.
-
scale
public void scale(float scaleX, float scaleY, float scaleZ)Multiplies the current transformation matrix by a scale matrix.
-
setAutoShapeType
public void setAutoShapeType(boolean autoShapeType)
If true, when drawing a shape cannot be performed with the current shape type, the batch is flushed and the shape type is changed automatically. This can increase the number of batch flushes if care is not taken to draw the same type of shapes together. Default is false.
-
begin
public void begin()
Begins a new batch without specifying a shape type.- Throws:
java.lang.IllegalStateException- ifautoShapeTypeis false.
-
begin
public void begin(ShapeRenderer.ShapeType type)
Starts a new batch of shapes. Shapes drawn within the batch will attempt to use the type specified. The call to this method must be paired with a call toend().- See Also:
setAutoShapeType(boolean)
-
set
public void set(ShapeRenderer.ShapeType type)
-
point
public void point(float x, float y, float z)Draws a point usingShapeRenderer.ShapeType.Point,ShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.
-
line
public final void line(float x, float y, float z, float x2, float y2, float z2)Draws a line usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.
-
line
public final void line(float x, float y, float x2, float y2)
-
line
public void line(float x, float y, float z, float x2, float y2, float z2, Color c1, Color c2)Draws a line usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled. The line is drawn with two colors interpolated between the start and end points.
-
curve
public void curve(float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x2, float y2, int segments)Draws a curve usingShapeRenderer.ShapeType.Line.
-
triangle
public void triangle(float x1, float y1, float x2, float y2, float x3, float y3)Draws a triangle in x/y plane usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.
-
triangle
public void triangle(float x1, float y1, float x2, float y2, float x3, float y3, Color col1, Color col2, Color col3)Draws a triangle in x/y plane with colored corners usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.
-
rect
public void rect(float x, float y, float width, float height)Draws a rectangle in the x/y plane usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.
-
rect
public void rect(float x, float y, float width, float height, Color col1, Color col2, Color col3, Color col4)Draws a rectangle in the x/y plane usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled. The x and y specify the lower left corner.- Parameters:
col1- The color at (x, y).col2- The color at (x + width, y).col3- The color at (x + width, y + height).col4- The color at (x, y + height).
-
rect
public void rect(float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float degrees)Draws a rectangle in the x/y plane usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled. The x and y specify the lower left corner. The originX and originY specify the point about which to rotate the rectangle.
-
rect
public void rect(float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float degrees, Color col1, Color col2, Color col3, Color col4)Draws a rectangle in the x/y plane usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled. The x and y specify the lower left corner. The originX and originY specify the point about which to rotate the rectangle.- Parameters:
col1- The color at (x, y)col2- The color at (x + width, y)col3- The color at (x + width, y + height)col4- The color at (x, y + height)
-
rectLine
public void rectLine(float x1, float y1, float x2, float y2, float width)Draws a line using a rotated rectangle, where with one edge is centered at x1, y1 and the opposite edge centered at x2, y2.
-
rectLine
public void rectLine(float x1, float y1, float x2, float y2, float width, Color c1, Color c2)Draws a line using a rotated rectangle, where with one edge is centered at x1, y1 and the opposite edge centered at x2, y2.
-
box
public void box(float x, float y, float z, float width, float height, float depth)Draws a cube usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled. The x, y and z specify the bottom, left, front corner of the rectangle.
-
x
public void x(float x, float y, float size)Draws two crossed lines usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.
-
x
public void x(Vector2 p, float size)
- See Also:
x(float, float, float)
-
arc
public void arc(float x, float y, float radius, float start, float degrees)Callsarc(float, float, float, float, float, int)by estimating the number of segments needed for a smooth arc.
-
arc
public void arc(float x, float y, float radius, float start, float degrees, int segments)Draws an arc usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.
-
circle
public void circle(float x, float y, float radius)Callscircle(float, float, float, int)by estimating the number of segments needed for a smooth circle.
-
circle
public void circle(float x, float y, float radius, int segments)Draws a circle usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.
-
ellipse
public void ellipse(float x, float y, float width, float height)Callsellipse(float, float, float, float, int)by estimating the number of segments needed for a smooth ellipse.
-
ellipse
public void ellipse(float x, float y, float width, float height, int segments)Draws an ellipse usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.
-
ellipse
public void ellipse(float x, float y, float width, float height, float rotation)Callsellipse(float, float, float, float, float, int)by estimating the number of segments needed for a smooth ellipse.
-
ellipse
public void ellipse(float x, float y, float width, float height, float rotation, int segments)Draws an ellipse usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.
-
cone
public void cone(float x, float y, float z, float radius, float height)Callscone(float, float, float, float, float, int)by estimating the number of segments needed for a smooth circular base.
-
cone
public void cone(float x, float y, float z, float radius, float height, int segments)Draws a cone usingShapeRenderer.ShapeType.LineorShapeRenderer.ShapeType.Filled.
-
polygon
public void polygon(float[] vertices, int offset, int count)Draws a polygon in the x/y plane usingShapeRenderer.ShapeType.Line. The vertices must contain at least 3 points (6 floats x,y).
-
polygon
public void polygon(float[] vertices)
- See Also:
polygon(float[], int, int)
-
polyline
public void polyline(float[] vertices, int offset, int count)Draws a polyline in the x/y plane usingShapeRenderer.ShapeType.Line. The vertices must contain at least 2 points (4 floats x,y).
-
polyline
public void polyline(float[] vertices)
- See Also:
polyline(float[], int, int)
-
check
protected final void check(ShapeRenderer.ShapeType preferred, ShapeRenderer.ShapeType other, int newVertices)
Checks whether the correct ShapeType was set. If not and autoShapeType is enabled, it flushes the batch and changes the shape type. The batch is also flushed, when the matrix has been changed or not enough vertices remain.- Parameters:
preferred- usually ShapeType.Lineother- usually ShapeType.Filled. May be null.newVertices- vertices count of geometric figure you want to draw
-
end
public void end()
Finishes the batch of shapes and ensures they get rendered.
-
flush
public void flush()
-
getCurrentType
public ShapeRenderer.ShapeType getCurrentType()
Returns the current shape type.
-
getRenderer
public ImmediateModeRenderer getRenderer()
-
isDrawing
public boolean isDrawing()
- Returns:
- true if currently between begin and end.
-
dispose
public void dispose()
Description copied from interface:DisposableReleases all resources of this object.- Specified by:
disposein interfaceDisposable
-
-