APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.graphics.glutils

public Class ShapeRenderer

extends Object
implements Disposable
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.utils.Disposable
Imports
com.badlogic.gdx.Gdx, com.badlogic.gdx.graphics.Camera, .Color, .GL20, com.badlogic.gdx.math.MathUtils, .Matrix4, .Vector2, .Vector3, com.badlogic.gdx.utils.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#combined matrix. 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:

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 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):
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.

Authors
mzechner, stbachmann, Nathan Sweet

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
ShapeRenderer.ShapeType

Shape types to be used with begin(ShapeType).

Field Summary

Modifier and TypeField and Description
private boolean
private final Color
private final Matrix4
private float
private boolean
private final Matrix4
private final ImmediateModeRenderer
private ShapeRenderer.ShapeType
private final Vector2
private final Matrix4

Constructor Summary

AccessConstructor and Description
public
public
ShapeRenderer(int maxVertices)

public
ShapeRenderer(int maxVertices, ShaderProgram defaultShader)

Method Summary

Modifier and TypeMethod and Description
public void
arc(float x, float y, float radius, float start, float degrees)

Calls arc(float, float, float, float, float, int) by estimating the number of segments needed for a smooth arc.

public void
arc(float x, float y, float radius, float start, float degrees, int segments)

Draws an arc using ShapeType#Line or ShapeType#Filled.

public void
begin()

Begins a new batch without specifying a shape type.

public void
begin(ShapeRenderer.ShapeType type)

Starts a new batch of shapes.

public void
box(float x, float y, float z, float width, float height, float depth)

Draws a cube using ShapeType#Line or ShapeType#Filled.

protected final void
check(ShapeRenderer.ShapeType
usually ShapeType.Line
preferred
,
ShapeRenderer.ShapeType
usually ShapeType.Filled. May be null.
other
,
int
vertices count of geometric figure you want to draw
newVertices
)

Checks whether the correct ShapeType was set.

public void
circle(float x, float y, float radius)

Calls circle(float, float, float, int) by estimating the number of segments needed for a smooth circle.

public void
circle(float x, float y, float radius, int segments)

Draws a circle using ShapeType#Line or ShapeType#Filled.

public void
cone(float x, float y, float z, float radius, float height)

Calls cone(float, float, float, float, float, int) by estimating the number of segments needed for a smooth circular base.

public void
cone(float x, float y, float z, float radius, float height, int segments)

Draws a cone using ShapeType#Line or ShapeType#Filled.

public void
curve(float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x2, float y2, int segments)

Draws a curve using ShapeType#Line.

public void
dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Releases all resources of this object.

public void
ellipse(float x, float y, float width, float height)

Calls ellipse(float, float, float, float, int) by estimating the number of segments needed for a smooth ellipse.

public void
ellipse(float x, float y, float width, float height, int segments)

Draws an ellipse using ShapeType#Line or ShapeType#Filled.

public void
ellipse(float x, float y, float width, float height, float rotation)

Calls ellipse(float, float, float, float, float, int) by estimating the number of segments needed for a smooth ellipse.

public void
ellipse(float x, float y, float width, float height, float rotation, int segments)

Draws an ellipse using ShapeType#Line or ShapeType#Filled.

public void
end()

Finishes the batch of shapes and ensures they get rendered.

public void
public Color
public ShapeRenderer.ShapeType
getCurrentType()

Returns the current shape type.

public Matrix4
getProjectionMatrix()

If the matrix is modified, updateMatrices() must be called.

public ImmediateModeRenderer
public Matrix4
getTransformMatrix()

If the matrix is modified, updateMatrices() must be called.

public void
identity()

Sets the transformation matrix to identity.

public boolean
isDrawing()

Returns: true if currently between begin and end.

public final void
line(float x, float y, float z, float x2, float y2, float z2)

Draws a line using ShapeType#Line or ShapeType#Filled.

public final void
public final void
line(float x, float y, float x2, float y2)

public final void
public final void
line(float x, float y, float x2, float y2, Color c1, Color c2)

public void
line(float x, float y, float z, float x2, float y2, float z2, Color c1, Color c2)

Draws a line using ShapeType#Line or ShapeType#Filled.

public void
point(float x, float y, float z)

Draws a point using ShapeType#Point, ShapeType#Line or ShapeType#Filled.

public void
polygon(float[] vertices, int offset, int count)

Draws a polygon in the x/y plane using ShapeType#Line.

public void
polygon(float[] vertices)

public void
polyline(float[] vertices, int offset, int count)

Draws a polyline in the x/y plane using ShapeType#Line.

public void
polyline(float[] vertices)

public void
rect(float x, float y, float width, float height)

Draws a rectangle in the x/y plane using ShapeType#Line or ShapeType#Filled.

public void
rect(float x, float y, float width, float height, Color
The color at (x, y).
col1
,
Color
The color at (x + width, y).
col2
,
Color
The color at (x + width, y + height).
col3
,
Color
The color at (x, y + height).
col4
)

Draws a rectangle in the x/y plane using ShapeType#Line or ShapeType#Filled.

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 using ShapeType#Line or ShapeType#Filled.

public void
rect(float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float degrees, Color
The color at (x, y)
col1
,
Color
The color at (x + width, y)
col2
,
Color
The color at (x + width, y + height)
col3
,
Color
The color at (x, y + height)
col4
)

Draws a rectangle in the x/y plane using ShapeType#Line or ShapeType#Filled.

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.

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.

public void
rectLine(Vector2 p1, Vector2 p2, float width)

public void
rotate(float axisX, float axisY, float axisZ, float degrees)

Multiplies the current transformation matrix by a rotation matrix.

public void
scale(float scaleX, float scaleY, float scaleZ)

Multiplies the current transformation matrix by a scale matrix.

public void
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.

public void
setColor(Color color)

Sets the color to be used by the next shapes drawn.

public void
setColor(float r, float g, float b, float a)

Sets the color to be used by the next shapes drawn.

public void
setProjectionMatrix(Matrix4 matrix)

Sets the projection matrix to be used for rendering.

public void
public void
translate(float x, float y, float z)

Multiplies the current transformation matrix by a translation matrix.

public void
triangle(float x1, float y1, float x2, float y2, float x3, float y3)

Draws a triangle in x/y plane using ShapeType#Line or ShapeType#Filled.

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 using ShapeType#Line or ShapeType#Filled.

public void
public void
x(float x, float y, float size)

Draws two crossed lines using ShapeType#Line or ShapeType#Filled.

public void
x(Vector2 p, float size)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

autoShapeTypeback to summary
private boolean autoShapeType
colorback to summary
private final Color color
combinedMatrixback to summary
private final Matrix4 combinedMatrix
defaultRectLineWidthback to summary
private float defaultRectLineWidth
matrixDirtyback to summary
private boolean matrixDirty
projectionMatrixback to summary
private final Matrix4 projectionMatrix
rendererback to summary
private final ImmediateModeRenderer renderer
shapeTypeback to summary
private ShapeRenderer.ShapeType shapeType
tmpback to summary
private final Vector2 tmp
transformMatrixback to summary
private final Matrix4 transformMatrix

Constructor Detail

ShapeRendererback to summary
public ShapeRenderer()
ShapeRendererback to summary
public ShapeRenderer(int maxVertices)
ShapeRendererback to summary
public ShapeRenderer(int maxVertices, ShaderProgram defaultShader)

Method Detail

arcback to summary
public void arc(float x, float y, float radius, float start, float degrees)

Calls arc(float, float, float, float, float, int) by estimating the number of segments needed for a smooth arc.

arcback to summary
public void arc(float x, float y, float radius, float start, float degrees, int segments)

Draws an arc using ShapeType#Line or ShapeType#Filled.

beginback to summary
public void begin()

Begins a new batch without specifying a shape type.

Exceptions
IllegalStateException:
if autoShapeType is false.
beginback to summary
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 to end().

See Also
setAutoShapeType(boolean)
boxback to summary
public void box(float x, float y, float z, float width, float height, float depth)

Draws a cube using ShapeType#Line or ShapeType#Filled. The x, y and z specify the bottom, left, front corner of the rectangle.

checkback to summary
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:ShapeRenderer.ShapeType

usually ShapeType.Line

other:ShapeRenderer.ShapeType

usually ShapeType.Filled. May be null.

newVertices:int

vertices count of geometric figure you want to draw

circleback to summary
public void circle(float x, float y, float radius)

Calls circle(float, float, float, int) by estimating the number of segments needed for a smooth circle.

circleback to summary
public void circle(float x, float y, float radius, int segments)

Draws a circle using ShapeType#Line or ShapeType#Filled.

coneback to summary
public void cone(float x, float y, float z, float radius, float height)

Calls cone(float, float, float, float, float, int) by estimating the number of segments needed for a smooth circular base.

coneback to summary
public void cone(float x, float y, float z, float radius, float height, int segments)

Draws a cone using ShapeType#Line or ShapeType#Filled.

curveback to summary
public void curve(float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x2, float y2, int segments)

Draws a curve using ShapeType#Line.

disposeback to summary
public void dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Doc from com.badlogic.gdx.utils.Disposable.dispose.

Releases all resources of this object.

ellipseback to summary
public void ellipse(float x, float y, float width, float height)

Calls ellipse(float, float, float, float, int) by estimating the number of segments needed for a smooth ellipse.

ellipseback to summary
public void ellipse(float x, float y, float width, float height, int segments)

Draws an ellipse using ShapeType#Line or ShapeType#Filled.

ellipseback to summary
public void ellipse(float x, float y, float width, float height, float rotation)

Calls ellipse(float, float, float, float, float, int) by estimating the number of segments needed for a smooth ellipse.

ellipseback to summary
public void ellipse(float x, float y, float width, float height, float rotation, int segments)

Draws an ellipse using ShapeType#Line or ShapeType#Filled.

endback to summary
public void end()

Finishes the batch of shapes and ensures they get rendered.

flushback to summary
public void flush()
getColorback to summary
public Color getColor()
getCurrentTypeback to summary
public ShapeRenderer.ShapeType getCurrentType()

Returns the current shape type.

getProjectionMatrixback to summary
public Matrix4 getProjectionMatrix()

If the matrix is modified, updateMatrices() must be called.

getRendererback to summary
public ImmediateModeRenderer getRenderer()
getTransformMatrixback to summary
public Matrix4 getTransformMatrix()

If the matrix is modified, updateMatrices() must be called.

identityback to summary
public void identity()

Sets the transformation matrix to identity.

isDrawingback to summary
public boolean isDrawing()
Returns:boolean

true if currently between begin and end.

lineback to summary
public final void line(float x, float y, float z, float x2, float y2, float z2)

Draws a line using ShapeType#Line or ShapeType#Filled.

lineback to summary
public final void line(Vector3 v0, Vector3 v1)
See Also
line(float, float, float, float, float, float)
lineback to summary
public final void line(float x, float y, float x2, float y2)
See Also
line(float, float, float, float, float, float)
lineback to summary
public final void line(Vector2 v0, Vector2 v1)
See Also
line(float, float, float, float, float, float)
lineback to summary
public final void line(float x, float y, float x2, float y2, Color c1, Color c2)
See Also
line(float, float, float, float, float, float, Color, Color)
lineback to summary
public void line(float x, float y, float z, float x2, float y2, float z2, Color c1, Color c2)

Draws a line using ShapeType#Line or ShapeType#Filled. The line is drawn with two colors interpolated between the start and end points.

pointback to summary
public void point(float x, float y, float z)

Draws a point using ShapeType#Point, ShapeType#Line or ShapeType#Filled.

polygonback to summary
public void polygon(float[] vertices, int offset, int count)

Draws a polygon in the x/y plane using ShapeType#Line. The vertices must contain at least 3 points (6 floats x,y).

polygonback to summary
public void polygon(float[] vertices)
See Also
polygon(float[], int, int)
polylineback to summary
public void polyline(float[] vertices, int offset, int count)

Draws a polyline in the x/y plane using ShapeType#Line. The vertices must contain at least 2 points (4 floats x,y).

polylineback to summary
public void polyline(float[] vertices)
See Also
polyline(float[], int, int)
rectback to summary
public void rect(float x, float y, float width, float height)

Draws a rectangle in the x/y plane using ShapeType#Line or ShapeType#Filled.

rectback to summary
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 using ShapeType#Line or ShapeType#Filled. The x and y specify the lower left corner.

Parameters
col1:Color

The color at (x, y).

col2:Color

The color at (x + width, y).

col3:Color

The color at (x + width, y + height).

col4:Color

The color at (x, y + height).

rectback to summary
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 using ShapeType#Line or ShapeType#Filled. The x and y specify the lower left corner. The originX and originY specify the point about which to rotate the rectangle.

rectback to summary
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 using ShapeType#Line or 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:Color

The color at (x, y)

col2:Color

The color at (x + width, y)

col3:Color

The color at (x + width, y + height)

col4:Color

The color at (x, y + height)

rectLineback to summary
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.

rectLineback to summary
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.

rectLineback to summary
public void rectLine(Vector2 p1, Vector2 p2, float width)
See Also
rectLine(float, float, float, float, float)
rotateback to summary
public void rotate(float axisX, float axisY, float axisZ, float degrees)

Multiplies the current transformation matrix by a rotation matrix.

scaleback to summary
public void scale(float scaleX, float scaleY, float scaleZ)

Multiplies the current transformation matrix by a scale matrix.

setback to summary
public void set(ShapeRenderer.ShapeType type)
setAutoShapeTypeback to summary
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.

setColorback to summary
public void setColor(Color color)

Sets the color to be used by the next shapes drawn.

setColorback to summary
public void setColor(float r, float g, float b, float a)

Sets the color to be used by the next shapes drawn.

setProjectionMatrixback to summary
public void setProjectionMatrix(Matrix4 matrix)

Sets the projection matrix to be used for rendering. Usually this will be set to Camera#combined.

setTransformMatrixback to summary
public void setTransformMatrix(Matrix4 matrix)
translateback to summary
public void translate(float x, float y, float z)

Multiplies the current transformation matrix by a translation matrix.

triangleback to summary
public void triangle(float x1, float y1, float x2, float y2, float x3, float y3)

Draws a triangle in x/y plane using ShapeType#Line or ShapeType#Filled.

triangleback to summary
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 using ShapeType#Line or ShapeType#Filled.

updateMatricesback to summary
public void updateMatrices()
xback to summary
public void x(float x, float y, float size)

Draws two crossed lines using ShapeType#Line or ShapeType#Filled.

xback to summary
public void x(Vector2 p, float size)
See Also
x(float, float, float)
com.badlogic.gdx.graphics.glutils back to summary

public final Enum ShapeRenderer.ShapeType

extends Enum<ShapeRenderer.ShapeType>
Class Inheritance

Shape types to be used with begin(ShapeType).
Author
mzechner, stbachmann

Field Summary

Modifier and TypeField and Description
public static final ShapeRenderer.ShapeType
private final int
public static final ShapeRenderer.ShapeType
public static final ShapeRenderer.ShapeType

Constructor Summary

AccessConstructor and Description
private
ShapeType(int glType)

Method Summary

Modifier and TypeMethod and Description
public int
public static ShapeRenderer.ShapeType
public static ShapeRenderer.ShapeType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

Filledback to summary
public static final ShapeRenderer.ShapeType Filled
glTypeback to summary
private final int glType
Lineback to summary
public static final ShapeRenderer.ShapeType Line
Pointback to summary
public static final ShapeRenderer.ShapeType Point

Constructor Detail

ShapeTypeback to summary
private ShapeType(int glType)

Method Detail

getGlTypeback to summary
public int getGlType()
valueOfback to summary
public static ShapeRenderer.ShapeType valueOf(String name)
valuesback to summary
public static ShapeRenderer.ShapeType[] values()