Game Development Reference
In-Depth Information
Now look at preceding the loop again. Notice something? For each Bob, we enable the same
vertex attributes over and over again via
glEnableClientState()
. We actually only need to set
those once, as each Bob uses the same model that always uses the same vertex attributes.
The next big problem are the calls to
glXXXPointer()
for each Bob. Since those pointers are
also OpenGL ES states, we only need to set them once as well, as they will never change once
they're set. So how can we fix that? Let's rewrite the
Vertices.draw()
method a little:
public void
bind() {
GL10 gl = glGraphics.getGL();
gl.glEnableClientState(GL10.
GL_VERTEX_ARRAY
);
vertices.position(0);
gl.glVertexPointer(2, GL10.
GL_FLOAT
, vertexSize, vertices);
if
if(hasColor) {
gl.glEnableClientState(GL10.
GL_COLOR_ARRAY
);
vertices.position(2);
gl.glColorPointer(4, GL10.
GL_FLOAT
, vertexSize, vertices);
}
if
(hasTexCoords) {
gl.glEnableClientState(GL10.
GL_TEXTURE_COORD_ARRAY
);
vertices.position(hasColor?6:2);
gl.glTexCoordPointer(2, GL10.
GL_FLOAT
, vertexSize, vertices);
}
}
public void
draw(
int
primitiveType,
int
offset,
int
numVertices) {
GL10 gl = glGraphics.getGL();
if
if(indices !=
null
) {
indices.position(offset);
gl.glDrawElements(primitiveType, numVertices, GL10.
GL_UNSIGNED_SHORT
, indices);
}
else
{
gl.glDrawArrays(primitiveType, offset, numVertices);
}
}
public void
unbind() {
GL10 gl = glGraphics.getGL();
if
(hasTexCoords)
gl.glDisableClientState(GL10.
GL_TEXTURE_COORD_ARRAY
);
if
if(hasColor)
gl.glDisableClientState(GL10.
GL_COLOR_ARRAY
);
}