package com.example.openGLDem; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.ShortBuffer; import javax.microedition.khronos.opengles.GL10; import android.content.Context; //filename: SimpleTriangleRenderer.java public class CarreRenderer extends AbstractRenderer { //Number of points or vertices we want to use private final static int VERTS = 4; //A raw native buffer to hold the point coordinates private FloatBuffer mFVertexBuffer; private FloatBuffer mFColorBuffer; //A raw native buffer to hold indices //allowing a reuse of points. private ShortBuffer mIndexBuffer; public CarreRenderer(Context context) { float[] vertices = { -0.5f, 0.5f, 0.0f, -0.5f,-0.5f,0.0f, 0.5f,-0.5f,0.0f, 0.5f,0.5f,0.0f }; float colors[] = { 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1f, 1f, 1.0f, 1f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f }; short[] indices = { 0,1,2, 0,2,3}; ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); //1 float= 4 octets vbb.order(ByteOrder.nativeOrder()); mFVertexBuffer = vbb.asFloatBuffer(); mFVertexBuffer.put(vertices); mFVertexBuffer.position(0); ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4); //1 float= 4 octets cbb.order(ByteOrder.nativeOrder()); mFColorBuffer = cbb.asFloatBuffer(); mFColorBuffer.put(colors); mFColorBuffer.position(0); ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length* 2); ibb.order(ByteOrder.nativeOrder()); mIndexBuffer = ibb.asShortBuffer(); mIndexBuffer.put(indices); mIndexBuffer.position(0); } //overriden method protected void draw(GL10 gl) { gl.glColorPointer(4, GL10.GL_FLOAT, 0, mFColorBuffer); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFVertexBuffer); gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, mIndexBuffer); } }