Game Development Reference
In-Depth Information
public
Vertices3(GLGraphics glGraphics,
int
maxVertices,
int
maxIndices,
boolean
hasColor,
boolean
hasTexCoords) {
this
.glGraphics = glGraphics;
this
.hasColor = hasColor;
this
.hasTexCoords = hasTexCoords;
this
.vertexSize = (3 + (hasColor ? 4 : 0) + (hasTexCoords ? 2 : 0)) * 4;
this
.tmpBuffer =
new int
[maxVertices * vertexSize / 4];
ByteBuffer buffer = ByteBuffer.
allocateDirect
(maxVertices * vertexSize);
buffer.order(ByteOrder.
nativeOrder
());
vertices = buffer.asIntBuffer();
if
(maxIndices > 0) {
buffer = ByteBuffer.
allocateDirect
(maxIndices * Short.
SIZE
/ 8);
buffer.order(ByteOrder.
nativeOrder
());
indices = buffer.asShortBuffer();
}
else
{
indices =
null
;
}
}
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();
}
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);
}