Android graphics --- OpenGL (4)

Source: Internet
Author: User

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 encoded 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 define the projection matrix and the photographic view matrix in the top-level coloring machine (Vertex Shader), so that they can be changed separately.

2. access the shadow matrix-after a callback is created in the top-layer shadow (Veterx Shader) for projection and photography views, you can access the variables applied to the projection matrix and photo view matrix. 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 = glesjavasglgetuniformlocation (mProgram, "uMVPMatrix ");...} 3. create projection and photographic view matrices: 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. setLookAtM (mVMatrix, 0, 0,-3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);} publicvoid onSurfaceChanged (GL10 unused, int width, int height) {GLES20.glViewport (, width, height); float ratio = (float) width/height; // create a projection matrix from device screen geometry Matrix. frustumM (mProjMatrix, 0,-ratio, ratio,-1, 1 ,);} 4. Apply the projection and photographic view matrix-to apply the projection and photographic view transformations, 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 values (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 (www.2cto.com)

 

By FireOfStar
 

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.