Getting Started with Android OpenGL basics

Source: Internet
Author: User

Android has been supporting OpenGL since the 2.2 release and can be used (simulated by software) in the absence of OpenGL-enabled GPUs. The object to use OpenGL on Android is Glsurfaceview, an extension that inherits from view.

OpenGL on Android is a fixed-point shader and fragment shader that implements the loading and rendering of images via vertex Shader and Fragment Shader, which are both point-and-spot shader programs. A complete OpenGL program needs to create a fixed-point shader and fragment shader and link them together to form a complete OpenGL program.

The purpose of the vertex shader is to generate coordinates for each vertex, so each vertex runs through the vertex shader program once, and once the vertex coordinates are computed, OpenGL can use the vertices to form points, lines, and triangles. All graphics are described by these three basic elements. is the process of coordinate transformation of a vertex shader (slightly more complex):

  

This process involves the generation of eye coordinates from the original object coordinates through the Model view transformation, then the projection transformation to generate the clipping coordinates, and then through the normalization of the W coordinates into the NDC (vertex coordinates by (X,Y,Z,W), in the shader program is generally described by a four-dimensional vector vec4), The screen coordinates are eventually displayed on the screen through the viewport transform. This series of transformations are done through matrices, the conversion process and the principle is the essence of OpenGL, for the need to enter the 3D world of students need to master. In the 2D world we just need to know about NDC, which is called the OpenGL coordinates, plus the normalized because the values of these coordinates are between ( -1,1) and OpenGL coordinates are shown:

OpenGL coordinates origin in the center of the screen, the left and right coordinate range is [ -1,1], in the 2d environment, the coordinate value only exists (x, y), and the coordinate range can only be between 1 to 1, the screen Center coordinates (0,0). Therefore, if you need to specify the location of a picture, the specified coordinates need to be based on this coordinate system, and in order to ensure that the picture display scale (length-width ratio), the transition between portrait to landscape usually need to multiply by a aspectratio (width/height ) to reset the coordinate values.

Knowing the OpenGL coordinates, we'll look at the screen coordinates (the coordinate origin of the screen coordinates in the upper-left corner) and the texture coordinates (the coordinate origin of the texture coordinates in the lower-left corner):

Screen coordinate system

Texture coordinate system

The fragment shader's purpose is to generate the final rendered color for the fragment (Fragment) of each vertex of a point, line, or triangle. A fragment is a small, monochrome rectangular area that you can simply think of as a pixel on the screen.

The above basic knowledge can basically deal with OpenGL to achieve 2D image rendering and processing. Here is a simple look at the shader, on the Android platform shader programs generally appear as strings, or in the res/raw/directory in the form of *.GLSL, if it appears as a separate file, You need to define a dedicated file read-in interface to load the shader program. Here is an introduction to the form of the string Shader program, to see the following simple vertex Shader and Fragment Shader:

    Private Static Final String Vertex_shader =        "attribute vec4 a_position;\n" + "attribute vec2 a_texcoord;\n        " +        "varying vec2 v_ texcoord;\n "+        " void main () {\ n "+        "  gl_position = a_position;\n "+        "  V_texcoord = a_texcoord;\n " +        "}\n";     Private Static Final String Fragment_shader =        "precision mediump float;\n" + "        uniform sampler2d tex_sampler;\n" +        "varying VEC2 v_texcoord;\n "+        " void main () {\ n "+        "  Gl_fragcolor = texture2d (Tex_sampler, v_texcoord); \ n "+        "}\n";

First look at Vertex_shader, define two types of variables atrribute and varying.

Where the attribute variable is a variable that can be used only in vertex shader. (it cannot declare the attribute variable in fragment shader, nor can it be used in fragment shader), generally with the attribute variable to represent some vertex data, which contains vertex coordinates, normals, texture coordinates, vertex colors, and so on. The application typically uses the function glbindattriblocation () to bind the position of each attribute variable, and then assigns a value to each attribute variable using the function Glvertexattribpointer ().

Varying is called a variable, and is generally used to pass data from vertex Shader to Fragment Shader, which defines the vertexshader type of two-dimensional vector attribute in A_texcoord, and assigns the value to the two-dimensional vector v_texcoord of the varying type. In addition, vertex coordinates must be assigned to the system variable gl_position for vertex Shader in main ().

Take a look at Fragment_shader, which defines two types of variables, uniform and varying. In addition to a precision mediump float, which defines the accuracy of the data, OpenGL can set three types of precision (Lowp,medium and HIGHP), for vertex shader, OpenGL uses the default highest precision level (HIGHP) and is therefore not defined.

Uniform variables are variables that are passed to the shader of the app order (vertex and fragment). The value is assigned by the function gluniform** () function. Inside the shader program (vertex and fragment), the uniform variable is like a constant (const) in the C language, which cannot be modified by the shader program (shader can only be used and cannot be changed). If the uniform variable is declared exactly the same way between vertex and fragment, it can be used in both vertex and fragment shares. (equivalent to a global variable shared by vertex and fragment shader) the uniform variable is generally used to represent: transformation matrix, material, illumination parameters and color information.

For fragment shader It is also necessary to assign a value to Gl_fragcolor.

Now you have an idea of the basic shader and see how Android uses the shader program. First of all, the resources in OpenGL are typically referenced by a handle (handle), which is typically returned by the gl*** interface, representing a specific resource.

Using GL on Android requires a number of column interfaces, where the basic interface is listed in the General Order of invocation (no error handling is included)

1. and create shader Related: Glcompileshader, Glshadersource, Glcreateshader, and finally return a shader handle through calls to these interfaces

2. Related to creating Shader program (each Shader program must contain vertex Shader and Fragment Shader two parts): Glcreateprogram, Glattachshader Gllinkprogram, calls through these interfaces eventually return a handle to the program.

3. Get shader internal variable assignments and pass data to GL, using different interfaces for different types of data: Gles20.glgetUniformlocation (Mprogram, "Tex_sampler"), GLES20.GLgetattriblocation (Mprogram, "A_texcoord"), above two interfaces Mprogram represents Glcreateprogram return shader program handle, "A_ Texcoord "and" Tex_sampler "are variables defined in Vertex shader and Fragment shader, through which the handles of the variables are obtained, facilitating the passing of values to those variables. After getting the handle of the variable, we will pass the value to it, generally through glvertexattribpointer (), the detailed parameters are not listed here, in the instance code can be learned, After the value is passed, Glenablevertexattribarray is required to pass the data into GL, and the incoming data uses gldrawarrays ().

The basics of OpenGL are described here, and the next example describes the use of OpenGL on Android.

Getting Started with Android OpenGL basics

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.