MVP matrix transformation of opengl

If there is a triangle, its three vertices are in the virtual space. If you need to rotate, scale, and translate the triangle, you need to use the model matrix operation. The model matrix only operates on the current triangle, not affect other objects. Before performing matrix operations on an object, the matrix needs to be normalized.

 (1) Translation of the matrix

       private final float[] modelMatrix = new float[16]; //Get a model matrix

       Matrix.setIdentityM(modelMatrix, 0); //Using the Matrix unitization matrix that comes with android
       Matrix.translateM(modelMatrix, 0, 0, -1.2f, 0); //Move the object to the Y axis by -1.2 units

 

(2) Rotation of the matrix: When using the model matrix to rotate the object, it can be rotated around the X, Y, Z axes,

Matrix.rotateM(modelMatrix, 0, rotateAngle, 1f, 0f, 0f);//Rotate rotateAngle around the X axis

Matrix.rotateM(modelMatrix, 0, rotateAngle, 0, 1f, 0f);//Rotate rotateAngle around the Y axis

Matrix.rotateM(modelMatrix, 0, rotateAngle, 0f, 0f, 1f);//Rotate rotateAngle around the Z axis

 

(2.1) But what if you want to make the object rotate around a certain point? The answer is compound operations on matrices.

For example, an object has a vertex (1.1f, -1.2f, 0), and I want the object to rotate around this vertex.

1. Move the object to the position of the origin, which is the origin of the virtual space. Matrix.translateM(modelMatrix, 0, -1.1f, 1.2f, 0);

2. Rotate the object. Matrix.rotateM(modelMatrix, 0, rotateAngle, 1f, 0f, 0f);

3. Move the object to (1.1f, -1.2f, 0). Matrix.translateM(modelMatrix, 0, 1.1f, -1.2f, 0);

The idea is basically this idea, but there is a trap that is easy to fall into, the order of matrix operations. If you follow the steps 1, 2, and 3 just now, you won't get the desired result. Because this is related to matrix multiplication, the first matrix is ​​multiplied last, and the last matrix is ​​multiplied first. Friends who are interested in this mathematical relationship can refer to Chapter 6.4 of the book "[Game Programming Mathematics and Physics Fundamentals]. (Sdale)", which is more exciting. So the correct code order should be:

        Matrix.translateM(modelMatrix, 0, 1.1f, -1.2f, 0);
        Matrix.rotateM(modelMatrix, 0, rotateAngle, 1f, 0f, 0f);
        Matrix.translateM(modelMatrix, 0, -1.1f, 1.2f, 0);

This makes an object rotate around the point (1.1f, -1.2f, 0) on the X axis of x=1.1f.

 

(3) Next, let's talk about scaling. The scaling is relative to the origin of the virtual space.

 Matrix.scaleM(modelMatrix, 0, 30f, 30f, 1);

The above code zooms the object by a factor of 30 along the X and Y axes, but not along the object's own center point.

(3.1) Let the object take a specified point as the center point of scaling. This is actually a composite operation of a matrix. The steps are similar to the composite operation of rotation. You can refer to the above explanation.

 

View Matrix: Scale, rotate, and move all objects in the virtual space. The difference between the view matrix and the model matrix is ​​that the model matrix acts on one object, and the view matrix acts on all objects in the space.

        private float[] viewMatrix = new float[16]; 

        Matrix.setLookAtM(viewMatrix, 0,
                0f, 0f, 1f, // eye position
                0f, 0f, -1f, // eye direction
                0f, 1f, 0f // normal line of sight

         ); //Get a view matrix
        Matrix.translateM(viewMatrix, 0, 0, 0, -2); //Translate all objects in the space to the Z-axis direction by -2 units.

 

Finally, let me talk about the perspective matrix. Displaying 3D graphics effects on a 2D screen relies on such a matrix. After using the projection matrix, a view cone will be generated. In layman's terms, things in the distance can be seen more, but they are smaller; things nearer can be seen less, but they are larger. One of the most important tasks of perspective projection is to calculate the value of W, and use this value to prepare for the next step of perspective division.

        float fov = 120;// The field of view based on the Y axis
        float aspect = ((float) width / (float) height);//The aspect ratio of the screen
        float near = 0.1f; // Near plane
        float far = 200f; / /Far plane
        Matrix.perspectiveM(projectionMatrix, 0, fov, aspect, near, far);//Generate perspective projection matrix

 

How the vertices change after perspective projection, I will explain in the next article.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324414948&siteId=291194637