FileDocCategorySizeDatePackage
Shape.javaAPI DocAndroid 1.5 API9451Wed May 06 22:41:08 BST 2009com.android.globaltime

Shape

public 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.

param
primitive a GL primitive type understood by glDrawElements, such as GL10.GL_TRIANGLES
param
indexDatatype the GL datatype for the index buffer, such as GL10.GL_UNSIGNED_SHORT
param
emitTextureCoordinates true to enable use of the texture coordinate buffer
param
emitNormals true to enable use of the normal buffer
param
emitColors true to enable use of the color buffer


                                                                     
      
         
         
         
          
        mPrimitive = primitive;
        mIndexDatatype = indexDatatype;
        mEmitTextureCoordinates = emitTextureCoordinates;
        mEmitNormals = emitNormals;
        mEmitColors = emitColors;
    
Methods Summary
private voidallocate(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 voidallocateBuffers(int[] vertices, int[] texcoords, int[] normals, int[] colors, int[] indices)
Copies the given data into the instance variables mVertexBuffer, mTexcoordBuffer, mNormalBuffer, mColorBuffer, and mIndexBuffer.

param
vertices an array of fixed-point vertex coordinates
param
texcoords an array of fixed-point texture coordinates
param
normals an array of fixed-point normal vector coordinates
param
colors an array of fixed-point color channel values
param
indices an array of int indices

        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 voidallocateBuffers(int[] vertices, int[] texcoords, int[] normals, int[] colors, short[] indices)
Copies the given data into the instance variables mVertexBuffer, mTexcoordBuffer, mNormalBuffer, mColorBuffer, and mIndexBuffer.

param
vertices an array of fixed-point vertex coordinates
param
texcoords an array of fixed-point texture coordinates
param
normals an array of fixed-point normal vector coordinates
param
colors an array of fixed-point color channel values
param
indices an array of short indices

        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 voidcross(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 voiddraw(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 intgetNumTriangles()
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 floatlength(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 floatlength(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 voidnormalize(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 inttoFixed(float x)
Converts the given floating-point value to fixed-point.

        return (int) (x * 65536.0);
    
public static floattoFloat(int x)
Converts the given fixed-point value to floating-point.

        return (float) (x / 65536.0);