Android OpenGL Es Wizard Learning Notes (Literacy-only)

Source: Internet
Author: User

Android currently supports several versions of the OpenGL ES API:
OpenGL ES 1.0 and 1.1:android 1.0 and later versions support this API specification.
OpenGL ES 2.0:android 2.2 (API 8) and later versions support this API specification.
OpenGL ES 3.0:android 4.3 (API 18) and later versions support this API specification.
OpenGL ES 3.1:android 5.0 (API 21) and later versions support this API specification.
The API that supports OpenGL ES 3.0 needs to implement the graphics pipeline provided by the device manufacturer, so a Android4.3 or later device may not support OpenGL ES 3.0.

Android provides support for OpenGL in both the framework API and the NDK


OpenGL ES Package:


1. OpenGL ES 1.0/1.1 API Pack
Android.opengl-This package provides a static interface to the OpenGL ES 1.0/1.1 containing class, which has better performance than the interface in the Javax.microedition.khronos package
      • 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
2. API classes for OpenGL ES 2.0
      • ANDROID.OPENGL.GLES20-This package provides an interface for Opengles 2.0, which can be used in Android2.2 and later versions.


3. API package for OpenGL ES 3.0/3/1
Android.opengl-This package provides the class interface for OpenGL ES 3.0/3.1.
      • GLES30
      • GLES31
      • Gles31ext (Android Extension Pack)


Map coordinates for drawing objects (Mapping coordinates for drawn Objects)


a fundamental problem with displaying graphics on Android devices is that the screen has a different size and shape, and OpenGL assumes that it has a uniform square coordinate system by default and draws those coordinates to a screen that is usually not a square, as if it were a perfect square.

Figure 1 Default OpenGL coordinate system (left) mapped to normal Android device screen (right)

The picture above realistically assumes the unified coordinate system (left) of the OpenGL frame hypothesis, and how these coordinates are mapped to a common device on a horizontal screen (right). To solve this problem, you can use OpenGL projection mode and camera view to transform coordinates so that your graphical objects have the correct proportions on any screen.
to apply the projection and camera views, create a projection matrix and camera view matrix and apply them to the OpenGL render pipeline, and the projection matrix recalculates the coordinates of the graphs so that they map correctly on the device screen. The camera view matrix creates a transform from a specific view location to the rendered object.


projection and camera view for OpenGL ES 1.0


in the ES 1.0 API, create the appropriate matrices for the projection and camera views, and then add them to the OpenGL environment.
1. Projection matrix-Creates a projection matrix using the geometry parameters of the device screen to recalculate the coordinates of the objects so that they are drawn at the correct proportions. The following example code shows how to create a projection matrix with the aspect ratio of the screen in the Onsurfacechanged () method 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
}


2. Camera Transform matrix-when you use a projection matrix to fit the coordinate system, you must apply a camera view. The following code shows a view transformation that uses Glu.glulookat () to create an analog camera position.
Public void Ondrawframe (GL10 gl) {
    ...
//Set Gl_modelview Transformation mode
Gl.glmatrixmode (Gl10.gl_modelview);
gl.glloadidentity (); //Reset the Matrix to their 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);
    ...
}


OpenGL ES 2.0 or later projection and camera views


In the ES 2.0 or 3.0 API, when applying a projection and camera view, you first add a matrix member object to the drawing object's vertex shader, and when the Matrix object is added, the projection and camera view matrix can be generated and applied to the object.


1. Add a matrix to the vertex shader-Create a variable for the view's projection matrix and multiply it by the position of the shader. The following shader code, which contains the Umvpmatrix member allows you to apply a projection and camera view matrix to the object using this shader.
private final String Vertexshadercode =

//This matrix member variable provides a hooks to manipulate
The coordinates of objects that use this vertex shader.
"Uniform mat4 Umvpmatrix; \ n "+

"attribute Vec4 vposition; \ n "+
"void Main () {\ n" +
//The matrix must is included as part of Gl_position
Note that the Umvpmatrix factor *must is first* in order
//For the matrix multiplication product to be correct.
"gl_position = Umvpmatrix * vposition; \ n "+

"} \ n";


Note: The above example defines a matrix transform member in the vertex shader, and you can apply a combination matrix of projection and camera view to the matrix. Depending on your needs, you may need to define separate projection matrices and camera view matrix members so that you can easily change them separately and freely.


2. Accessing the shader matrix-once you have created a hook in the vertex shader, you can access the variable to apply the projection and camera view matrix. The following code shows how to access the matrix variables defined in the vertex shader:
Public
void onsurfacecreated (GL10 unused, EGLConfig config) {
...
Mumvpmatrixhandle = Gles20.glgetuniformlocation (Mprogram, "Umvpmatrix");
...
}


3. Create a projection and camera view matrix-generates a projection and view matrix that can be applied to a drawing object. The following code creates a projection matrix and a camera view matrix based on the aspect ratio of the device screen.
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);
}


4. Apply the projection and camera view matrix-apply the projection and camera view transformations, multiply the two matrices, and then set them to the vertex shader. The following code shows how to apply a matrix to an OpenGL-rendered graphic object
Public void Ondrawframe (GL10 unused) {
    ...
//Combine the projection and camera view matrices
matrix.multiplymm (Mmvpmatrix, 0, Mprojmatrix, 0, Mvmatrix, 0);

//Apply the combined projection and camera view transformations
GLES20.GLUNIFORMMATRIX4FV (Mumvpmatrixhandle, 1, False, Mmvpmatrix, 0);

//Draw objects
    ...
}


OpenGL ES 1.0/1.1api is significantly different from version 2.0 or higher, the 1.x version has more convenient methods and fixed pipelines, while 2.0 and 3.0API provide more direct control over the pipeline through OpenGL shaders. You should carefully consider the need to draw graphics and choose the API that best suits your application.
The OpenGL ES 3.0 API offers more features and better performance than 2.0, while also backwards compatible. This means that you can set the version of OpenGL ES for your app to 2.0 and selectively include some valid 3.0 graphics features.


Texture Compression Support


texture compression can significantly improve OpenGL performance by reducing memory, making memory usage more efficient. The Android framework provides the ETC1 compression format as a standard feature, including the Etc1util tool class and the Etctool compression tool (located in the <sdk>/tools/directory under the Android SDK).
Most Android devices support the ETC1 format, but there is no guarantee that it will be supported. Check if the device supports ETC1 format, call the Etc1util.isetc1supported () method. The ETC1 texture compression format does not support a texture with transparency (alpha channel), and if the application requires a texture with transparency, you should select other texture compression formats supported by the target device.
when using OpenGL ES3.0, the ETC2/EAC texture compression format is guaranteed to be available, and this texture format has excellent compression ratios, high quality visuals, and also supports transparency.
in addition to the ETC format, Android devices support many other formats for texture compression, slightly different depending on the GPU chip and OpenGL implementations. You should investigate the compression formats supported by the target device, and then determine which compression formats the app supports. In order to determine which texture formats the device supports, you must query the device, check the OpenGL extension, and mark which texture compression formats are supported. Some common texture compression formats include the following:
ATITC (ATC)-ATI texture compression is supported on many devices, it supports RGB texture compression but does not contain an alpha channel, and some OpenGL extensions can represent this format, such as:
gl_amd_compressed_atc_texture
GL_ATI_TEXTURE_COMPRESSION_ATITC

PVRTC-PVRTC Texture compression is supported on many devices, supports 2-bit or 4-bit textures per pixel, and contains or does not contain an alpha channel. The following OpenGL extensions can represent this format, such as:
GL_IMG_TEXTURE_COMPRESSION_PVRTC

S3TC (DXTN/DXTC)-S3 has some formatting changes (from DXT1 to DXT5) and is not widely used. It supports RGB textures that contain 4-bit or 8-bit alpha channels. The following OpenGL extensions can represent this format, such as:
GL_OES_TEXTURE_COMPRESSION_S3TC
GL_EXT_TEXTURE_COMPRESSION_S3TC
Gl_ext_texture_compression_dxt1
GL_EXT_TEXTURE_COMPRESSION_DXT3
GL_EXT_TEXTURE_COMPRESSION_DXT5

3DC-3DC Texture compression is less used to support RGB textures that contain alpha channels, and the following OpenGL extensions can represent this format:
gl_amd_compressed_3dc_texture


These texture compression formats are not supported on all devices, different manufacturers and devices support different formats, once you decide which texture compression format to apply, be sure to use <supports-gl-texture> to declare in the manifest file, Use this statement to filter out devices that do not support these formats by Google Play.


Determining OpenGL Extensions


OpenGL implementations differ according to the extensions supported by the OpenGL ES API, which contain texture compression, but typically also include extensions to other OpenGL feature sets.

You can determine which texture compression formats and OpenGL extensions are supported on a particular device by using the following method:

1. Execute the following code on the target device to determine which texture compression format the device supports, and the results are different on various models, so you should run this code on multiple models to determine which compression formats are widely supported:
String extensions = javax.microedition.khronos.opengles.GL10.glGetString (gl10.gl_extensions);
2. Review the output of this method to determine which OpenGL extensions are supported by the device


Android Expansion Pack (AEP)
AEP ensures that the application supports a standardized set of OpenGL extensions that go beyond the core set described in the OpenGL 3.1 specification, which facilitates the consistency of functionality across devices, while allowing developers to take advantage of the latest features of their mobile GPU devices.
AEP also improves support for image, shader storage cache, and cell shader atomic counters.
when the app uses AEP, for example in manifest, the platform version must support it
<uses feature android:name= "ANDROID.HARDWARE.OPENGLES.AEP" android:required= "true"/>
Use the Hassystemfeature (String) method to verify that AEP is supported, passing feature_opengles_extension_pack as a parameter:
Boolean DEVICESUPPORTSAEP = Getpackagemanager (). Hassystemfeature (Packagemanager.feature_opengles_extension_ PACK);

Android OpenGL Es Wizard Learning Notes (Literacy-only)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.