Android OpenGL ES: 渐变颜色的三角形

public class MyRenderer implements Renderer 
{
FloatBuffer verticesBuffer;
private final int VERTEX_SIZE = (2 + 4) * 4;

@Override
public void onDrawFrame(GL10 gl)
{
gl.glViewport(0, 0, 320, 480);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, 320, 0, 480, 1, -1);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

verticesBuffer.position(0);
gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, verticesBuffer);
verticesBuffer.position(2);
gl.glColorPointer(4, GL10.GL_FLOAT, VERTEX_SIZE, verticesBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{

}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * 6 * 4); // 3:顶点数 6:维数2 + 颜色数4 4:一个float型占4个字节
byteBuffer.order(ByteOrder.nativeOrder());
verticesBuffer = byteBuffer.asFloatBuffer();
verticesBuffer.put(new float[] {0, 0, 1, 0, 0, 1,
320, 0, 0, 1, 0, 1,
160, 480, 0, 0, 1, 1});
verticesBuffer.flip();
}
}

 

转载于:https://www.cnblogs.com/xiaobo68688/archive/2011/12/17/2291051.html

你可能感兴趣的:(Android OpenGL ES: 渐变颜色的三角形)