Android Official Development Document Training series: Application projection and camera view of OpenGL drawing

Source: Internet
Author: User

Original address: http://android.xsoftlab.net/training/graphics/opengl/projection.html# #transform

In the OpenGL ES environment, the projected camera view simulates the drawing as a physical trait seen in reality. This physical simulation is achieved by changing the numerical coordinates of the object:

    • Projection-This is achieved based on the Glsurfaceview's high-width coordinate transformation. If this calculation is not used, the objects drawn will be distorted by the unequal proportions of the window. The projection transformation is calculated only if the scale of the OpenGL view is determined or if the onsurfacechanged () method of the renderer changes. For more OpenGL coordinate mapping relationships, see mapping coordinates for drawn Objects.
    • Camera View-This is based on the location shift of the virtual camera. It is important to note that OpenGL does not define a real camera object, but rather provides a way to simulate a camera by converting a graphical object. A camera view transformation is only started when Glsurfaceview is determined, or is dynamically changed based on the user's behavior or the functionality of the application.

This lesson describes how to create a projected camera view and apply it to the drawing of a drawing.

Defining projections

The data for the projection transformation computes the onsurfacechanged () method located in Glsurfaceview.renderer. The following code first obtains the height and width of the glsurfaceview and then populates the data into the matrix of the projection transformation using the Matrix.frustumm () method.

//Mmvpmatrix is a abbreviation for "Model View Projection Matrix"Private Final float[] Mmvpmatrix =New float[ -];Private Final float[] Mprojectionmatrix =New float[ -];Private Final float[] Mviewmatrix =New float[ -];@Override Public void onsurfacechanged(GL10 unused,intWidthintHeight) {Gles20.glviewport (0,0, width, height);floatRatio = (float) Width/height;//This projection matrix was applied to object coordinates    //In the Ondrawframe () methodMatrix.frustumm (Mprojectionmatrix,0,-ratio, ratio,-1,1,3,7);}

The code fills the projection matrix: Mprojectionmatrix, which can be used to integrate with the camera view transformation.

Note: It is generally meaningless to apply the projection transformation only to the object to be drawn. In general, you must also apply the camera view transform as well. This allows the object to be rendered on the screen.

Defining the Camera View

Completing the conversion of the camera view is also part of the image processing process. In the following code, the Camera view transformation is calculated using the Matrix.setlookatm () method, and the results are then integrated into the previously computed matrix. The final integrated matrix is then used to draw the graph.

@Override Public void Ondrawframe(GL10 unused) {    ...//Set the camera position (View matrix)MATRIX.SETLOOKATM (Mviewmatrix,0,0,0, -3,0F0F0F0F1.0F0.0f);//Calculate the projection and view transformationMatrix.multiplymm (Mmvpmatrix,0, Mprojectionmatrix,0, Mviewmatrix,0);//Draw shapeMtriangle.draw (Mmvpmatrix);}
Application of projection transformation and camera conversion

In order to be able to use the matrix that is integrated above, you first need to add the matrix variable to the vertex renderer defined in the previous lesson:

 Public  class Triangle {    Private FinalString Vertexshadercode =//This matrix member variable provides a hooks to manipulate        //The coordinates of the objects that use this vertex shader        "Uniform mat4 umvpmatrix;"+"attribute Vec4 vposition;"+"void Main () {"+//The matrix must be included as a modifier 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;"+"}";//Use to access and set the view transformation    Private intMmvpmatrixhandle; ...}

Next, modify the Draw () method of the drawing object so that you can receive the consolidated matrix and then apply it to the drawing:

 Public void Draw(float[] Mvpmatrix) {//Pass in the calculated transformation matrix...//Get handle to shape ' s transformation matrixMmvpmatrixhandle = Gles20.glgetuniformlocation (Mprogram,"Umvpmatrix");//Pass the projection and view transformation to the shaderGLES20.GLUNIFORMMATRIX4FV (Mmvpmatrixhandle,1,false, Mvpmatrix,0);//Draw the triangleGles20.gldrawarrays (Gles20.gl_triangles,0, Vertexcount);//Disable vertex arrayGles20.gldisablevertexattribarray (Mpositionhandle);}

If there is a correct calculation and the correct use of the integrated matrix, then the graph will be drawn in the correct proportions, the final look should be the effect of such a child:

Now the program can normally display the normal proportion of the graph, it is time for the graphics to add action.

Android Official Development Document Training series: Application projection and camera view of OpenGL drawing

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.