Android graphics-OpenGL (4)

Source: Internet
Author: User

This article translated from: http://developer.android.com/guide/topics/graphics/opengl.html

Projection and photography views in OpenGL ES 2.0

In the ES 2.0 API, you must first add a matrix member to the vertex shaders at the top of the graphic object to use the projection and photography views. Then, the added matrix member can generate and apply the projection and photo views to the graphic objects.

1.Add a matrix to the vertex shaders--- Create a valid projection matrix for the view and use it as the multiplier for the coloring position. In the following code, the umvpmatrix member can apply the projection and photographic views matrix to the coordinates of the graphic objects using the coloring tool:

privatefinalString vertexShaderCode =
 
        // This matrix member variable provides a hook 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 be included as part of gl_Position
        " gl_Position = uMVPMatrix * vPosition; \n"+
 


"} \ N ";

Note: The vertex shader in the above example defines a single transformation matrix. You can combine the projection matrix and the photographic view matrix into this transformation matrix. Based on the needs of the application, you can also
Defines the projection matrix and the photographic view matrix respectively, so that they can be changed separately.

2.Access the shader Matrix--- After a callback is created in the top-level shadow (veterx shader) for projection and photography views, the variables applied to the projection matrix and photo view matrix can be accessed. The following code shows how to edit the glsurfaceview. Renderer class onsurfacecreated () method to access the matrix variables defined in vertex shader.

publicvoid onSurfaceCreated(GL10 unused,EGLConfig config){
        ...
        muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram,"uMVPMatrix");
        ...
    }

3.Create a projection and photo view Matrix--- Generate projection and view matrices for applications and graphic objects. The following code shows how to edit the onsurfacecreated () and onsurfacechanged () Methods of the glsurfaceview. Renderer class to create a camera view matrix and projection matrix based on the screen appearance ratio of devices:

publicvoid 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);
    }
 
    publicvoid 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.Application projection and photo view matrix ---To apply projection and photo view transformations, we need to set them together to Verter shader. The following code shows how to edit glsurfaceview. the ondrawframe () method of the render class is used to synthesize the projection matrix created in the above Code and the photo view. Then, it is applied to the graphics objects presented by OpenGL.

publicvoid 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
        ...
    }

For a complete example of how to use OpenGL ES 2.0, see the OpenGL ES 2.0 Guide (http://developer.android.com/training/graphics/opengl/index.html)

 

 

Related Article

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.