Game Development Reference
In-Depth Information
Listing 11-6. Vertices3.java, the Final Version with Support for Normals
package
com.badlogic.androidgames.framework.gl;
import
java.nio.ByteBuffer;
import
java.nio.ByteOrder;
import
java.nio.IntBuffer;
import
java.nio.ShortBuffer;
import
javax.microedition.khronos.opengles.GL10;
import
com.badlogic.androidgames.framework.impl.GLGraphics;
public class
Vertices3 {
final
GLGraphics glGraphics;
final boolean
hasColor;
final boolean
hasTexCoords;
final boolean
hasNormals;
final int
vertexSize;
final
IntBuffer vertices;
final int
[] tmpBuffer;
final
ShortBuffer indices;
Among the members, the only new addition is the
hasNormals
Boolean, which keeps track of
whether the vertices have normals or not.
public
Vertices3(GLGraphics glGraphics,
int
maxVertices,
int
maxIndices,
boolean
hasColor,
boolean
hasTexCoords,
boolean
hasNormals) {
this
.glGraphics = glGraphics;
this
.hasColor = hasColor;
this
.hasTexCoords = hasTexCoords;
this
.hasNormals = hasNormals;
this
.vertexSize = (3 + (hasColor ? 4 : 0) + (hasTexCoords ? 2 : 0) +
(hasNormals ? 3 : 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
;
}
}
In the constructor, we now also take a
hasNormals
parameter. We have to modify the calculation
of the
vertexSize
member as well, adding three floats per vertex if normals are available.