Game Development Reference
In-Depth Information
Now, this is pretty close to how we worked with Canvas . We have a lot more flexibility as well,
since we are not limited to axis-aligned rectangles anymore.
This example has covered all we need to know about vertices for now. We saw that every vertex
must have at least a position, and can have additional attributes, such as a color, given as four
RGBA float values and texture coordinates. We also saw that we can reuse vertices via indexing
in case we want to avoid duplication. This gives us a little performance boost, since OpenGL
ES does not have to multiply more vertices by the projection and model-view matrices than
absolutely necessary (which, again, is not entirely correct, but let's stick to this interpretation).
A Vertices Class
Let's make our code easier to write by creating a Vertices class that can hold a maximum
number of vertices and, optionally, indices to be used for rendering. It should also take care of
enabling all the states needed for rendering, as well as cleaning up the states after rendering has
finished, so that other code can rely on a clean set of OpenGL ES states. Listing 7-10 shows our
easy-to-use Vertices class.
Listing 7-10. Vertices.java; Encapsulating (Indexed) Vertices
package com.badlogic.androidgames.framework.gl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL10;
import com.badlogic.androidgames.framework.impl.GLGraphics;
public class Vertices {
final GLGraphics glGraphics;
final boolean hasColor;
final boolean hasTexCoords;
final int vertexSize;
final FloatBuffer vertices;
final ShortBuffer indices;
The Vertices class has a reference to the GLGraphics instance, so we can get ahold of the
GL10 instance when we need it. We also store whether the vertices have colors and texture
coordinates. This gives us great flexibility, as we can choose the minimal set of attributes
we need for rendering. Additionally, we store a FloatBuffer that holds our vertices and a
ShortBuffer that holds the optional indices.
public Vertices(GLGraphics glGraphics, int maxVertices, int maxIndices, boolean hasColor,
boolean hasTexCoords) {
this .glGraphics = glGraphics;
this .hasColor = hasColor;
 
Search WWH ::




Custom Search