Simple TriangleRendererjava

The code for the SimpleTriangleRenderer class (see Listing 13-3) is the same as the code for the corresponding class in Chapter 10 (see Listing 10-12), except that the classes inherit from a different AbstractRenderer.

Listing 13-3. SimpleTriangleRenderer Source Code //filename: SimpleTriangleRenderer.java public class SimpleTriangleRenderer extends AbstractRenderer {

//Number of points or vertices we want to use private final static int VERTS = 3;

//A raw native buffer to hold the point coordinates private FloatBuffer mFVertexBuffer;

//A raw native buffer to hold indices //allowing a reuse of points. private ShortBuffer mIndexBuffer;

public SimpleTriangleRenderer(Context context) {

ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);

vbb.order(ByteOrder.nativeOrder());

mFVertexBuffer = vbb.asFloatBuffer();

ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);

ibb.order(ByteOrder.nativeOrder());

mIndexBuffer = ibb.asShortBuffer();

-0.5f, -0.5f, 0, // (x1,y1,zl) 0.5f, -0.5f, 0, 0.0f, 0.5f, 0

for (int i = 0; i < VERTS; i++) { for(int j = 0; j < 3; j++) {

mFVertexBuffer.put(coords[i*3+j]);

mIndexBuffer.put(myIndecesArray[i]);

mFVertexBuffer.position(0); mIndexBuffer.position(0);

//overridden method protected void draw(GLl0 gl) {

gl.glVertexPointer(3, GLl0.GL_FLOAT, 0, mFVertexBuffer); gl.glDrawElements(GLl0.GL_TRIANGLES, VERTS,

GLl0.GL_UNSIGNED_SHORT, mIndexBuffer);

0 0

Post a comment