⢠ Projection - this transformation adjusts the coordinates of the painted objects based on the width and height of the glsurfaceview they display. Without this computational transformation, shapes drawn through OpenGL will deform in different display windows. This projection change is usually only determined in the scale of OpenGL view or calculated in your renderer's onsurfacechanged () method. For more information about projection and coordinate mapping, see drawing object coordinate mapping.
⢠ Camera view - this switch adjusts the coordinates of the drawing object based on the position of the virtual camera. It should be noted that OpenGL es does not define a real camera object, but provides a practical method to simulate a camera by transforming the display of the drawn object. The camera view transformation may only be calculated when your glsurfaceview is determined, or it may be changed dynamically based on user actions or the functions of your application.
This course describes how to create projection and camera views and apply them to the painted objects of your glsurfaceview.
Define projection
The projection change data is calculated in the onsurfacechanged () method of your glsurfaceview. Renderer class. The following example code is to obtain the height and width of glsurfaceview and fill them into the projection transformation matrix through the matrix. Frustumm() method.
// mMVPMatrix is an abbreviaTIon for "Model View ProjecTIon Matrix" private final float[] mMVPMatrix = new float[16]; private final float[] mProjecTIonMatrix = new float[16]; private final float[] mViewMatrix = new float[16]; @ Override public void onSurfaceChanged(GL10 unused, int width, int height) GLES20.glViewport(0, 0, width, height); float raTIo = (float) width / height; // this projection matrix is applied to object coordinates // in the onDrawFrame() method Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
The above code is filled with a projection matrix mprojectionmatrix, which can be combined with the camera view in the next part in the onframedraw() method.
Note: if you only apply the projection matrix to the object you draw, you will usually get a very empty display. In general, you must also apply camera views for anything you want to display on the screen.
Define camera view
Complete the transformation process of your drawn image by adding camera view transformation in your renderer as part of your drawing process. In the following code, the camera view transformation is calculated by the matrix. Setlookatm() method, and then combined with the previously calculated projection matrix. The merged matrix is then passed to the drawing.
@Override public void onDrawFrame(GL10 unused) ... // Set the camera position (View matrix) Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); // Calculate the projection and view transformation Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0); // Draw shape mTriangle.draw(mMVPMatrix);
Apply projection and camera transformations
In order to use the combined matrix of projection and camera view transformation shown in the previous section, first add a matrix variable to the fixed-point shader code defined in the triangle class:
public class Triangle private final String vertexShaderCode = // This matrix member variable provides a hook 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 be first* in order // for the matrix multiplication product to be correct. " gl_Position = uMVPMatrix * vPosition;" ""; // Use to access and set the view transformation private int mMVPMatrixHandle; ...
Next, modify the draw () method of your graphic object to receive the joint transformation matrix and apply them to the graph:
public void draw(float[] mvpMatrix) // pass in the calculated transformation matrix ... // get handle to shape's transformation matrix mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); // Pass the projection and view transformation to the shader GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0) ; // Draw the triangle GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); // Disable vertex array GLES20.glDisableVertexAttribArray(mPositionHandle);
Once you correctly calculate and apply projection and camera view transformation, your drawing object will be drawn at the correct scale. It should look like the following:
Now you have an application that can display graphics at the correct scale. In the following chapters, we can learn how to add motion to your graphics.