一、创建Activity /* 为OpenGLES Graphics创建一个Activity,这个Activity与其它类型应用的Activity并无不同,要说不同,也仅仅是放到Activity的layout的View不一样,你需要放入的是一个GLSurfaceView。 */ public class OpenGLES20Basic extends Activity { private GLSurfaceView mGLView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create a GLSurfaceView instance and set it as the ContentView for this Activity /* 为了能在你的Android应用中使用OpenGLES绘画,你必须创建一个view作为容器。而最直接的方式就是从GLSurfaceView和 GLSurfaceView.Renderer分别派生一个类 。GLSurfaceView作为OpenGL绘制所在的容器,而实际的绘图动作都是在GLSurfaceView.Renderer里面发生的。 MyGLSurfaceView 继承自GLSurfaceView,作为承载绘 制内容的容器 */ mGLView = new MyGLSurfaceView(this); setContentView(mGLView); } @Override protected void onPause() { super.onPause(); // The following call pauses the rendering thread. // If your OpenGL application is memory intensive, // you should consider de-allocating objects that // consume significant memory here. mGLView.onPause(); } @Override protected void onResume() { super.onResume(); // The following call resumes a paused rendering thread. // If you de-allocated graphic objects for onPause() // this is a good place to re-allocate them. mGLView.onResume(); } } 二、创建MySurfaceView /* 构键一个GLSurfaceView对象,GLSurfaceView中其实不需要做太多工作,实际的绘制任务都在GLSurfaceView.Renderer中了。所以GLSurfaceView中的代码也非常少,你甚至可以直接使用GLSurfaceView,但是,然而,别这样做。因为可以扩展这个类来响应触摸事件等。 */ class MyGLSurfaceView extends GLSurfaceView { public MyGLSurfaceView(Context context) { super(context); // Create an OpenGL ES 2.0 context. setEGLContextClientVersion(2); // Set the Renderer for drawing on the GLSurfaceView setRenderer(new MyGLRenderer()); /* Render the view only when there is a change in the drawing data,这个设置是可选的操作,在RENDERMODE_WHEN_DIRTY模式下,只在绘制数据发生改变时才绘 制view,此设置会阻止绘制GLSurfaceView的帧,直到你调用了requestRender(),这样会非常高效。*/ setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); } } 二、创建MyGLRenderer /* 此类控制向GLSurfaceView的绘制工作。它有三个方法被Android系统调用来计算在GLSurfaceView上画什么以及如何画。 onSurfaceCreated()- 仅调用一次,用于设置view的OpenGLES环境。 onDrawFrame()- 每次View被重绘时被调用。 onSurfaceChanged()- 如果view的几和形状发生变化了就调用,例如当竖屏变为横屏时。 */ public class MyGLRenderer implements GLSurfaceView.Renderer { private Triangle mTriangle; @Override public void onSurfaceCreated(GL10 unused, EGLConfig config) { // Set the background frame color,设置背景颜色 GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //定义需要绘制的三角形 mTriangle = new Triangle(); } @Override public void onDrawFrame(GL10 unused) { // Draw background color,绘制背景颜色 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); // Draw triangle,执行绘制操作 mTriangle.draw(); } @Override public void onSurfaceChanged(GL10 unused, int width, int height) { // Adjust the viewport based on geometry changes, // such as screen rotation GLES20.glViewport(0, 0, width, height); } /*下载并编译shader代码*/ public static int loadShader(int type, String shaderCode){ // create a vertex shader type (GLES20.GL_VERTEX_SHADER) // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER) int shader = GLES20.glCreateShader(type); // add the source code to the shader and compile it GLES20.glShaderSource(shader, shaderCode); GLES20.glCompileShader(shader); return shader; } } class Triangle { //顶点着色器 private final String vertexShaderCode = "attribute vec4 vPosition;" + "void main() {" + " gl_Position = vPosition;" + "}"; //片段着色器 private final String fragmentShaderCode = "precision mediump float;" + "uniform vec4 vColor;" + "void main() {" + " gl_FragColor = vColor;" + "}"; private final FloatBuffer vertexBuffer;//顶点buffer private final int mProgram; //程序对象 private int mPositionHandle; //顶点数据在buffer中的位置 private int mColorHandle; //颜色数据在buffer中的位置 // number of coordinates per vertex in this array static final int COORDS_PER_VERTEX = 3; static float triangleCoords[] = { // in counterclockwise order: 0.0f, 0.622008459f, 0.0f, // top -0.5f, -0.311004243f, 0.0f, // bottom left 0.5f, -0.311004243f, 0.0f // bottom right }; private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX; private final int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex // Set color with red, green, blue and alpha (opacity) values float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f }; public Triangle() { // initialize vertex byte buffer for shape coordinates ByteBuffer bb = ByteBuffer.allocateDirect( triangleCoords.length * 4);// (number of coordinate values * 4 bytes per float // use the device hardware's native byte order bb.order(ByteOrder.nativeOrder()); // create a floating point buffer from the ByteBuffer vertexBuffer = bb.asFloatBuffer(); // add the coordinates to the FloatBuffer vertexBuffer.put(triangleCoords); // set the buffer to read the first coordinate vertexBuffer.position(0); // prepare shaders and OpenGL program int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode); int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program GLES20.glLinkProgram(mProgram); // create OpenGL program executable } public void draw() { // Add program to OpenGL environment GLES20.glUseProgram(mProgram); // get handle to vertex shader's vPosition member mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");//着色器中定义的变量vPosition对应mPositionHandle // Enable a handle to the triangle vertices GLES20.glEnableVertexAttribArray(mPositionHandle); // Prepare the triangle coordinate data GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); // get handle to fragment shader's vColor member mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");//着色器中定义的变量vColor对应mColorHandle // Set color for drawing the triangle GLES20.glUniform4fv(mColorHandle, 1, color, 0); // Draw the triangle GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); // Disable vertex array GLES20.glDisableVertexAttribArray(mPositionHandle); } } 也可以参考http://blog.csdn.net/niu_gao/article/details/8533126,一步一步学习OpenGL ES 2.0系列1-6