Game Development Reference
In-Depth Information
public void
setVertices(
float[]
vertices,
int
offset,
int
length) {
this
.vertices.clear();
JniUtils.copy(
this
.vertices, vertices, offset, length);
this
.vertices.position(length * 4);
}
public void
bind() {
GL10 gl = glGraphics.getGL();
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
vertices.position(0);
gl.glVertexPointer(3, GL10.GL_FLOAT, vertexSize, vertices);
if
(hasColor) {
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
vertices.position(12);
gl.glColorPointer(4, GL10.GL_FLOAT, vertexSize, vertices);
}
if
(hasTexCoords) {
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
vertices.position(hasColor ? 28 : 12);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, vertexSize, vertices);
}
if
(hasNormals) {
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
int
offset = 12;
if
(hasColor)
offset += 16;
if
(hasTexCoords)
offset += 8;
vertices.position(offset);
gl.glNormalPointer(GL10.GL_FLOAT, vertexSize, vertices);
}
}
// rest as before
The big changes are in
setVertices()
and
bind()
. In
setVertices()
, we now use the
JniUtils
class to copy the
float[]
array to the direct
ByteBuffer
instance. Note that we set the limit and
position of the buffer manually. This is necessary because our JNI method doesn't manipulate
the
position
and
limit
fields of the buffer. The OpenGL ES methods we pass that buffer to
might use that information, though.
In
bind()
, we have to modify the position offsets because we are now counting in bytes instead
of ints.
Note
If you paid attention, you can see that we could do the same for the indices
ByteBuffer
.
This would require adding a new native method to
JniUtils
that takes a short array. Give it a try!