Renders points, lines, rectangles, filled rectangles and boxes. This class works with OpenGL ES 1.x and 2.0. In its base configuration a 2D orthographic projection with the origin in the lower left corner is used. Units are given in screen pixels. To change the projection properties use the {@link #setProjectionMatrix(Matrix4)} method. Usually the {@link Camera#combined}matrix is set via this method. If the screen orientation or resolution changes, the projection matrix might have to be adapted as well. Shapes are rendered in batches to increase performance. The standard use-pattern looks as follows:
{@code 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();}
The class has a second matrix called the transformation matrix which is used to rotate, scale and translate shapes in a more flexible manner. This mechanism works much like matrix operations in OpenGL ES 1.x. 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.
@author mzechner
@author stbachmann
@author Nathan Sweet