OpenGL ES API
ES表示Embedded System,是OpenGL針對嵌入式設(shè)備的專用版本。
OpenGL ES 1.0 & 1.1 -> Android 1.0 and heigher
OpenGL ES 2.0 -> Android 2.2 (API Level 8) and heigher
OpenGL ES 3.0 -> Android 4.3 (API level 18) and heigher
OpenGL ES 3.0 需要設(shè)備廠商支持,即使是4.3的android也有可能不支持 OpenGL ES 3.0 。
private static double glVersion = 3.0;
private static class ContextFactory implements GLSurfaceView.EGLContextFactory {
private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
public EGLContext createContext(
EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {
Log.w(TAG, "creating OpenGL ES " + glVersion + " context");
int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, (int) glVersion,
EGL10.EGL_NONE };
// attempt to create a OpenGL ES 3.0 context
EGLContext context = egl.eglCreateContext(
display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
return context; // returns null if 3.0 is not supported;
}
}
如果上述方法返回null,那么只能使用OpenGL ES 2.0 相關(guān)API。
// Create a minimum supported OpenGL ES context, then check:
String version = javax.microedition.khronos.opengles.GL10.glGetString(
GL10.GL_VERSION);
Log.w(TAG, "Version: " + version );
// The version format is displayed as: "OpenGL ES <major>.<minor>"
// followed by optional content provided by the implementation.
上面的代碼創(chuàng)建了最低版本的Context,之后檢查支持的版本。
OpenGL ES 基礎(chǔ)
有兩種方式使用:framework API 和 Native Development Kit
Android Framework API
GLSurfaceView
可以使用OpenGL ESAPI的View對象。與SurfaceView相似,可以創(chuàng)建這個對象的實(shí)例,之后添加到Render中。但是這個View不支持觸摸事件,如果需要,則需要擴(kuò)展這個累自己實(shí)現(xiàn)。
添加touch事件
GLSurfaceView.Render
GLSurfaceView 中繪圖所需要的方法。
通過GLSurfaceView.setRenderer()方法設(shè)置。
GLSurfaceView.Render需要實(shí)現(xiàn)的方法
- onSurfaceCreated()
創(chuàng)建GLSurfaceView的時候調(diào)用一次,通常用來設(shè)置OpenGL參數(shù)或初始化OpenGL圖形對象。 - onDrawFrame()
每次重畫都會調(diào)用這個方法。 - onSurfaceChanged()
幾何變化時調(diào)用這個方法,例如改變大小,設(shè)備橫豎屏變化,
OpenGL ES 軟件包
OpenGL ES 1.0/1.1 API Packages
android.opengl - This package provides a static interface to the OpenGL ES 1.0/1.1 classes and better performance than the javax.microedition.khronos package interfaces.
- GLES10
- GLES10Ext
- GLES11
- GLES11Ext
javax.microedition.khronos.opengles - This package provides the standard implementation of OpenGL ES 1.0/1.1.
- GL10
- GL10Ext
- GL11
- GL11Ext
- GL11ExtensionPack
OpenGL ES 2.0 API Class
android.opengl.GLES20 - This package provides the interface to OpenGL ES 2.0 and is available starting with Android 2.2 (API level 8).
OpenGL ES 3.0 API Class
android.opengl.GLES30 - This package provides the interface to OpenGL ES 3.0 and is available starting with Android 4.3 (API level 18).
聲明OpenGL 需求
- 版本
2.0
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
3.0
<uses-feature android:glEsVersion="0x00030000" android:required="true" />
這個聲明會導(dǎo)致不支持該版本的設(shè)備無法安裝。
- 文本壓縮
<supports-gl-texture>
同樣會使不支持的設(shè)備無法安裝