1 public class MyRenderer implements Renderer
2 {
3 FloatBuffer verticesBuffer;
4
5 @Override
6 public void onDrawFrame(GL10 gl)
7 {
8 gl.glViewport(0, 0, 320, 480);
9 gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
10 gl.glMatrixMode(GL10.GL_PROJECTION);
11 gl.glLoadIdentity();
12 gl.glOrthof(0, 320, 0, 480, 1, -1);
13
14 gl.glColor4f(1, 0, 0, 1);
15 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
16 gl.glVertexPointer(2, GL10.GL_FLOAT, 0, verticesBuffer);
17 gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
18 }
19
20 @Override
21 public void onSurfaceChanged(GL10 gl, int width, int height)
22 {
23
24 }
25
26 @Override
27 public void onSurfaceCreated(GL10 gl, EGLConfig config)
28 {
29 ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * 2 * 4); // 3:顶点数 2:维数,x和y 4:一个float型占4个字节
30 byteBuffer.order(ByteOrder.nativeOrder());
31 verticesBuffer = byteBuffer.asFloatBuffer();
32 verticesBuffer.put(new float[] {0, 0,
33 320, 0,
34 160, 480});
35 verticesBuffer.flip();
36 }
37 }
// Activity:
public class OpenGLTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GLSurfaceView glSurfaceView = new GLSurfaceView(this);
MyRenderer renderer = new MyRenderer();
glSurfaceView.setRenderer(renderer);
setContentView(glSurfaceView);
}
}