Game Development Reference
In-Depth Information
public void
setVertices(
float
[] vertices,
int
offset,
int
length) {
this
.vertices.clear();
int
len = offset + length;
for
(
int
i = offset, j = 0; i < len; i++, j++)
tmpBuffer[j] = Float.
floatToRawIntBits
(vertices[i]);
this
.vertices.put(tmpBuffer, 0, length);
this
.vertices.flip();
}
public void
setIndices(
short
[] indices,
int
offset,
int
length) {
this
.indices.clear();
this
.indices.put(indices, offset, length);
this
.indices.flip();
}
As you can see, the methods
setVertices()
and
setIndices()
stay the same.
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(3);
gl.glColorPointer(4, GL10.
GL_FLOAT
, vertexSize, vertices);
}
if
(hasTexCoords) {
gl.glEnableClientState(GL10.
GL_TEXTURE_COORD_ARRAY
);
vertices.position(hasColor ? 7 : 3);
gl.glTexCoordPointer(2, GL10.
GL_FLOAT
, vertexSize, vertices);
}
if
(hasNormals) {
gl.glEnableClientState(GL10.
GL_NORMAL_ARRAY
);
int
offset = 3;
if
(hasColor)
offset += 4;
if
(hasTexCoords)
offset += 2;
vertices.position(offset);
gl.glNormalPointer(GL10.
GL_FLOAT
, vertexSize, vertices);
}
}
In the
bind()
method, we do the usual
ByteBuffer
tricks, this time also incorporating normals via
the
glNormalPointer()
method. To calculate the offset for the normal pointer, we have to take
into account whether colors and texture coordinates are given.