概述:OpenGL 是Open Graphics Library的简称,ES是Embedded Systems的简称,连起来就是针对嵌入式系统的开源图形库。
看下OpenGL ES 的api版本和安卓系统版本的对照:
OpenGL ES 1.0 and 1.1 - This API specification is supported by Android 1.0 and higher.
OpenGL ES 2.0 - This API specification is supported by Android 2.2 (API level 8) and higher.
OpenGL ES 3.0 - This API specification is supported by Android 4.3 (API level 18) and higher.
OpenGL ES 3.1 - This API specification is supported by Android 5.0 (API level 21) and higher.
讲解:Android框架中有两个基础类,可让您使用OpenGL ES API创建和操作图形:GLSurfaceView(https://developer.android.com/reference/android/opengl/GLSurfaceView.html)和GLSurfaceView.Renderer(https://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer.html)。
1.GLSurfaceView这个类是一个View(https://developer.android.com/reference/android/view/View.html)可以使用OpenGL API调用绘制和操作对象的类,在功能上类似于SurfaceView(https://developer.android.com/reference/android/view/SurfaceView.html)
2.GLSurfaceView.Renderer(https://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer.html)
此接口定义了在a中绘制图形所需的方法GLSurfaceView(https://developer.android.com/reference/android/opengl/GLSurfaceView.html)。您必须将此接口的实现作为单独的类提供,并GLSurfaceView(https://developer.android.com/reference/android/opengl/GLSurfaceView.html)使用它将其附加到您的实例 GLSurfaceView.setRenderer()(https://developer.android.com/reference/android/opengl/GLSurfaceView.html#setRenderer(android.opengl.GLSurfaceView.Renderer))`。
GLSurfaceView.Renderer(https://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer.html)接口要求您实现以下方法:
- onSurfaceCreated():系统在创建时调用此方法一次GLSurfaceView使用此方法执行仅需要发生一次的操作,例如设置OpenGL环境参数或初始化OpenGL图形对象。
- onDrawFrame():系统在每次重绘时调用此方法GLSurfaceView。使用此方法作为绘制(和重新绘制)图形对象的主要执行点。
- onSurfaceChanged():系统在GLSurfaceView几何图形更改时调用此方法,包括更GLSurfaceView设备屏幕的大小或方向。例如,当设备从纵向更改为横向时,系统会调用此方法。使用此方法响应GLSurfaceView容器中的更改。
使用前,需在清单文件里声明,这里我们使用ES2.0,如下:
OpenGL的坐标系以屏幕中心点为原点
简单示例:
public class OpenGLES20Activity 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.
mGLView = new MyGLSurfaceView(this);
setContentView(mGLView);
}
}
class MyGLSurfaceView extends GLSurfaceView {
private final MyGLRenderer mRenderer;
public MyGLSurfaceView(Context context){
super(context);
// Create an OpenGL ES 2.0 context
setEGLContextClientVersion(2);
mRenderer = new MyGLRenderer();
// Set the Renderer for drawing on the GLSurfaceView
setRenderer(mRenderer);
}
}
public class MyGLRenderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
public void onDrawFrame(GL10 unused) {
// Redraw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
}
上面三段代码准备好,开始写关键部分图形绘制的bean
画一个形状
使用OpenGL ES 2.0绘制定义的形状需要大量代码,因为您必须向图形渲染管道提供大量细节。具体而言,您必须定义以下内容:
- 顶点着色器Vertex Shader - 用于渲染形状顶点的OpenGL ES图形代码。
- Fragment Shader - OpenGL ES代码,用于渲染具有颜色或纹理的形状的面。
- 程序 Program - 一个OpenGL ES对象,包含要用于绘制一个或多个形状的着色器。
至少一个顶点着色器来绘制形状,并使用一个片段着色器为该形状着色。必须编译这些着色器,然后将其添加到OpenGL ES程序中,然后使用该程序绘制形状。以下是如何定义可用于在Triangle类中绘制形状的基本着色器的示例 :
public class Triangle {
private int mPositionHandle;
private int mColorHandle;
private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
private final int mProgram;
private FloatBuffer vertexBuffer;
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;" +
"}";
// 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
};
// 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(
// (number of coordinate values * 4 bytes per float)
triangleCoords.length * 4);
// 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);
int vertexShader = MyGLSurfaceView.MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
vertexShaderCode);
int fragmentShader = MyGLSurfaceView.MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);
// create empty OpenGL ES Program
mProgram = GLES20.glCreateProgram();
// add the vertex shader to program
GLES20.glAttachShader(mProgram, vertexShader);
// add the fragment shader to program
GLES20.glAttachShader(mProgram, fragmentShader);
// creates OpenGL ES program executables
GLES20.glLinkProgram(mProgram);
}
public void draw() {
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
// 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");
// 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);
}
}
定义好图形,我们在GlSurfaceView里面初始化,在onDrawFrame方法里面调用onDraw()
public class MyGLRenderer implements GLSurfaceView.Renderer {
.
private Triangle mTriangle;
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
mTriangle = new Triangle();
}
public void onDrawFrame(GL10 unused) {
mTriangle.draw();
}
}
运行起来就得到下面的图像:
此代码示例存在一些问题。首先,它不会打动你的朋友。其次,当您更改设备的屏幕方向时,三角形会被压扁并改变形状。形状偏斜的原因是由于对象的顶点尚未针对GLSurfaceView (https://developer.android.com/reference/android/opengl/GLSurfaceView.html)`
显示的屏幕区域的比例进行校正 。有时间再写下使用投影和相机视图解决该问题。
最后,三角形是静止的,这有点无聊。在“ 添加运动”课程中,您可以使此形状旋转并更有趣地使用OpenGL ES图形管道。
感谢阅读!