android-OpenGL ES

来源:互联网 发布:软件开发需求调研 编辑:程序博客网 时间:2024/06/06 19:42

Android includes support for high performance 2D and 3D graphics with the Open Graphics Library (OpenGL®), specifically, the OpenGL ES API. OpenGL is a cross-platform graphics API that specifies a standard software interface for 3D graphics processing hardware. OpenGL ES is a flavor of the OpenGL specification intended for embedded devices. Android supports several versions of the 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 supports OpenGL both through its framework API and the Native Development Kit (NDK). This topic focuses on the Android framework interfaces.

There are two foundational classes in the Android framework that let you create and manipulate graphics with the OpenGL ES API: GLSurfaceView and GLSurfaceView.Renderer. If your goal is to use OpenGL in your Android application, understanding how to implement these classes in an activity should be your first objective.

The GLSurfaceView.Renderer interface requires that you implement the following methods:onSurfaceCreated();onDrawFrame();onSurfaceChanged();

<!-- Tell the system this app requires OpenGL ES 2.0. --><uses-feature android:glEsVersion="0x00020000" android:required="true" />

    For OpenGL ES 3.0:

    <!-- Tell the system this app requires OpenGL ES 3.0. --><uses-feature android:glEsVersion="0x00030000" android:required="true" />

    For OpenGL ES 3.1:

    <!-- Tell the system this app requires OpenGL ES 3.1. --><uses-feature android:glEsVersion="0x00030001" android:required="true" />
 The following example code demonstrates how to modify the onSurfaceChanged() method of a GLSurfaceView.Renderer implementation to create a projection matrix based on the screen's aspect ratio and apply it to the OpenGL rendering environment.
public void onSurfaceChanged(GL10 gl, int width, int height) {    gl.glViewport(0, 0, width, height);    // make adjustments for screen ratio    float ratio = (float) width / height;    gl.glMatrixMode(GL10.GL_PROJECTION);        // set matrix to projection mode    gl.glLoadIdentity();                        // reset the matrix to its default state    gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);  // apply the projection matrix}

 The following example code shows how to modify the onDrawFrame()method of a GLSurfaceView.Renderer implementation to apply a model view and use theGLU.gluLookAt() utility to create a viewing tranformation which simulates a camera position.

public void onDrawFrame(GL10 gl) {    ...    // Set GL_MODELVIEW transformation mode    gl.glMatrixMode(GL10.GL_MODELVIEW);    gl.glLoadIdentity();                      // reset the matrix to its default state    // When using GL_MODELVIEW, you must set the camera view    GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);    ...}

The following example code shows how to modify the onSurfaceCreated() andonSurfaceChanged() methods of a GLSurfaceView.Renderer implementation to create camera view matrix and a projection matrix based on the screen aspect ratio of the device.

public void onSurfaceCreated(GL10 unused, EGLConfig config) {    ...    // Create a camera view matrix    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);}public void onSurfaceChanged(GL10 unused, int width, int height) {    GLES20.glViewport(0, 0, width, height);    float ratio = (float) width / height;    // create a projection matrix from device screen geometry    Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);}
Declare the AEP requirement in the manifest as follows:
<uses feature android:name="android.hardware.opengles.aep"              android:required="true" />

Before using OpenGL ES features from a version higher than the minimum required in your application manifest, your application should check the version of the API available on the device. You can do this in one of two ways:

  1. Attempt to create the higher-level OpenGL ES context (EGLContext) and check the result.
  2. Create a minimum-supported OpenGL ES context and check the version value.

The following example code demonstrates how to check the available OpenGL ES version by creating anEGLContext and checking the result. This example shows how to check for OpenGL ES 3.0 version:

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;  }}
0 0
原创粉丝点击