Shapepublic abstract class Shape extends Object An abstract superclass for various three-dimensional objects to be drawn
using OpenGL ES. Each subclass is responsible for setting up NIO buffers
containing vertices, texture coordinates, colors, normals, and indices.
The {@link #draw(GL10)} method draws the object to the given OpenGL context. |
Fields Summary |
---|
public static final int | INT_BYTES | public static final int | SHORT_BYTES | public static final float | DEGREES_TO_RADIANS | public static final float | PI | public static final float | TWO_PI | public static final float | PI_OVER_TWO | protected int | mPrimitive | protected int | mIndexDatatype | protected boolean | mEmitTextureCoordinates | protected boolean | mEmitNormals | protected boolean | mEmitColors | protected IntBuffer | mVertexBuffer | protected IntBuffer | mTexcoordBuffer | protected IntBuffer | mColorBuffer | protected IntBuffer | mNormalBuffer | protected Buffer | mIndexBuffer | protected int | mNumIndices |
Constructors Summary |
---|
protected Shape(int primitive, int indexDatatype, boolean emitTextureCoordinates, boolean emitNormals, boolean emitColors)Constructs a Shape.
mPrimitive = primitive;
mIndexDatatype = indexDatatype;
mEmitTextureCoordinates = emitTextureCoordinates;
mEmitNormals = emitNormals;
mEmitColors = emitColors;
|
Methods Summary |
---|
private void | allocate(int[] vertices, int[] texcoords, int[] normals, int[] colors)Allocate the vertex, texture coordinate, normal, and color buffer.
ByteBuffer vbb =
ByteBuffer.allocateDirect(vertices.length * INT_BYTES);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asIntBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
if ((texcoords != null) && mEmitTextureCoordinates) {
ByteBuffer tbb =
ByteBuffer.allocateDirect(texcoords.length * INT_BYTES);
tbb.order(ByteOrder.nativeOrder());
mTexcoordBuffer = tbb.asIntBuffer();
mTexcoordBuffer.put(texcoords);
mTexcoordBuffer.position(0);
}
if ((normals != null) && mEmitNormals) {
ByteBuffer nbb =
ByteBuffer.allocateDirect(normals.length * INT_BYTES);
nbb.order(ByteOrder.nativeOrder());
mNormalBuffer = nbb.asIntBuffer();
mNormalBuffer.put(normals);
mNormalBuffer.position(0);
}
if ((colors != null) && mEmitColors) {
ByteBuffer cbb =
ByteBuffer.allocateDirect(colors.length * INT_BYTES);
cbb.order(ByteOrder.nativeOrder());
mColorBuffer = cbb.asIntBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);
}
| public void | allocateBuffers(int[] vertices, int[] texcoords, int[] normals, int[] colors, int[] indices)Copies the given data into the instance
variables mVertexBuffer, mTexcoordBuffer, mNormalBuffer, mColorBuffer,
and mIndexBuffer.
allocate(vertices, texcoords, normals, colors);
ByteBuffer ibb =
ByteBuffer.allocateDirect(indices.length * INT_BYTES);
ibb.order(ByteOrder.nativeOrder());
IntBuffer intIndexBuffer = ibb.asIntBuffer();
intIndexBuffer.put(indices);
intIndexBuffer.position(0);
this.mIndexBuffer = intIndexBuffer;
| public void | allocateBuffers(int[] vertices, int[] texcoords, int[] normals, int[] colors, short[] indices)Copies the given data into the instance
variables mVertexBuffer, mTexcoordBuffer, mNormalBuffer, mColorBuffer,
and mIndexBuffer.
allocate(vertices, texcoords, normals, colors);
ByteBuffer ibb =
ByteBuffer.allocateDirect(indices.length * SHORT_BYTES);
ibb.order(ByteOrder.nativeOrder());
ShortBuffer shortIndexBuffer = ibb.asShortBuffer();
shortIndexBuffer.put(indices);
shortIndexBuffer.position(0);
this.mIndexBuffer = shortIndexBuffer;
| public static void | cross(float[] p, float[] q, float[] out)Computes the cross-product of two vectors p and q and places
the result in out.
out[0] = p[1] * q[2] - p[2] * q[1];
out[1] = p[2] * q[0] - p[0] * q[2];
out[2] = p[0] * q[1] - p[1] * q[0];
| public void | draw(javax.microedition.khronos.opengles.GL10 gl)Draws the shape to the given OpenGL ES 1.0 context. Texture coordinates,
normals, and colors are emitted according the the preferences set for
this shape.
gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
if (mEmitTextureCoordinates) {
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, mTexcoordBuffer);
gl.glEnable(GL10.GL_TEXTURE_2D);
} else {
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisable(GL10.GL_TEXTURE_2D);
}
if (mEmitNormals) {
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glNormalPointer(GL10.GL_FIXED, 0, mNormalBuffer);
} else {
gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
}
if (mEmitColors) {
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FIXED, 0, mColorBuffer);
} else {
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
gl.glDrawElements(mPrimitive,
mNumIndices > 0 ? mNumIndices : mIndexBuffer.capacity(),
mIndexDatatype,
mIndexBuffer);
| public int | getNumTriangles()Returns the number of triangles associated with this shape.
if (mPrimitive == GL10.GL_TRIANGLES) {
return mIndexBuffer.capacity() / 3;
} else if (mPrimitive == GL10.GL_TRIANGLE_STRIP) {
return mIndexBuffer.capacity() - 2;
}
return 0;
| public static float | length(float vx, float vy, float vz)Returns the length of a vector, given as three floats.
return (float) Math.sqrt(vx * vx + vy * vy + vz * vz);
| public static float | length(float[] v)Returns the length of a vector, given as an array of three floats.
return length(v[0], v[1], v[2]);
| public static void | normalize(float[] v)Normalizes the given vector of three floats to have length == 1.0.
Vectors with length zero are unaffected.
float length = length(v);
if (length != 0.0f) {
float norm = 1.0f / length;
v[0] *= norm;
v[1] *= norm;
v[2] *= norm;
}
| public static int | toFixed(float x)Converts the given floating-point value to fixed-point.
return (int) (x * 65536.0);
| public static float | toFloat(int x)Converts the given fixed-point value to floating-point.
return (float) (x / 65536.0);
|
|