Game Development Reference
In-Depth Information
from position 0. That would have been bad, as that's where the x coordinate of our first vertex is.
Figure 7-8 shows from where OpenGL ES will read our vertex attributes, and how it jumps from
one vertex to the next for each attribute.
Figure 7-8. Our FloatBuffer holding the vertices, start addresses for OpenGL ES from which to read position/color, and stride
used to jump to the next position/color
To draw our triangle, we again call glDrawElements() , which tells OpenGL ES to draw a triangle
using the first three vertices of our FloatBuffer :
gl.glDrawElements(GL10.GL_TRIANGLES, 0, 3);
Since we enabled the GL10.GL_VERTEX_ARRAY and GL10.GL_COLOR_ARRAY , OpenGL ES knows that
it should use the attributes specified by glVertexPointer() and glColorPointer() . It will ignore
the default color, as we provide our own per-vertex colors.
Note The way we just specified our vertices' positions and colors is called interleaving .
This means that we pack the attributes of a vertex in one continuous memory block. There's
another way we could have achieved this: noninterleaved vertex arrays . We could have used two
FloatBuffer s, one for the positions and one for the colors. However, interleaving performs much
better due to memory locality, so we won't discuss noninterleaved vertex arrays here.
Putting it all together into a new GLGame and Screen implementation should be a breeze. Listing 7-6
shows an excerpt from the file ColoredTriangleTest.java . We left out the boilerplate code.
Listing 7-6. Excerpt from ColoredTriangleTest.java; Interleaving Position and Color Attributes
class ColoredTriangleScreen extends Screen {
final int VERTEX_SIZE = (2 + 4) * 4;
GLGraphics glGraphics;
FloatBuffer vertices;
public ColoredTriangleScreen(Game game) {
super (game);
glGraphics = ((GLGame) game).getGLGraphics();
 
Search WWH ::




Custom Search