OpenGL tutorial (6) Translation Transformation

Source: Internet
Author: User

Original post address: http://ogldev.atspace.co.uk/www/tutorial06/tutorial06.html

 

In this tutorial, we start to change the position of a 3D object, such as translation, rotation, scaling, and so on. The operations of object location changes are usually implemented through matrices. Each change is represented by a matrix. If an object performs multiple location operations, the corresponding matrices can be multiplied, multiply the coordinates of the vertex to obtain the coordinates of the vertex after the object position changes.

First, let's look at the translation operation. Translation indicates moving an object from one position to another along a vector of any length and direction. For example, I want to move the triangle on the left to the right.

The first method is to provide an offset vector. In the medium offset vector is (), this vector is transmitted to the shader as an uniform variable. In each vertex shader operation, add the vector to the vertex position.

The second method is to use a matrix to represent the offset. Then, use the matrix as the uniform variable and transmit it to the shader. Then, multiply the matrix by the vertex coordinate to obtain the vertex coordinate after the offset.

Next, let's take a look at how to obtain the translation matrix? For example, if a matrix is multiplied by a vertex () and a vertex () is obtained, the first two-dimensional matrix cannot do this, similarly, you cannot obtain (, 1) from the vertex (, 0) through a 3-dimensional matrix ).

Generally, given matrix m, a vertex P (x, y, z), and vector V (V1, V2, V3 ), M * P = p1 (x + V1, Y + V2, Z + V3) means that matrix m translates vertex P to position P1, from the results of P1, we can see that each of its components is the sum of the product of each component of P and the corresponding component of V.

For a 3-dimensional vector, the final matrix m is like the left matrix, and the 3-dimensional vector (x, y, z) is also extended into a four-dimensional vector (X, Y, Z, 1 ), in this case, M * P = p1 is implemented. The four-dimensional vector is called W, which is the homogeneous coordinate. Generally, W = 1 for a point and W = 0 for a vector, so the point can be translated, but the vector cannot.

The code of the program is as follows:

struct Matrix4f {
float m[4][4];
};

We add a 4x4 matrix structure in math_3d.h to define the coordinate matrix of objects.

GLuint gWorldLocation;

We use this OpenGL handle to access the world variation matrix uniform variable in the shader. It is called the World variation matrix because our translation operations are performed in the world coordinate system.

Matrix4f World;
World.m[0][0] = 1.0f; World.m[0][1] = 0.0f; World.m[0][2] = 0.0f; World.m[0][3] = sinf(Scale);
World.m[1][0] = 0.0f; World.m[1][1] = 1.0f; World.m[1][2] = 0.0f; World.m[1][3] = 0.0f;
World.m[2][0] = 0.0f; World.m[2][1] = 0.0f; World.m[2][2] = 1.0f; World.m[2][3] = 0.0f;
World.m[3][0] = 0.0f; World.m[3][1] = 0.0f; World.m[3][2] = 0.0f; World.m[3][3] = 1.0f;

In the rendering function, we define the world matrix and set V2 and V3 to 0. In this way, the coordinates of the object on the Y axis and the Z axis remain unchanged and V1 is set as the result of the sinf function, when its value changes between [-], the X coordinate of the object changes constantly.

glUniformMatrix4fv(gWorldLocation, 1, GL_TRUE, &World.m[0][0]);

This is to assign a value to the second uniform variable, which is a 4x4 matrix. The first parameter is the location index of the shader uniform variable, and the second parameter is the number of matrices to be updated. We only have one matrix, so its value is 1. Through this parameter, we can assign values to multiple matrices at the same time. The third parameter specifies whether the matrix is in the primary order of the row or in the Primary Order of the column. The primary order of the row indicates that the matrix is stored by one row in the array, the column primary order indicates that the matrix is stored in one column and one column in the array, and C ++ stores the row primary order by default. For example, the following two-dimensional array:

int a[2][3];
a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
a[1][1] = 5;
a[1][2] = 6;

It represents the following matrix:
1 2 3
4 5 6
In-memory storage: 1 2 3 4 5 6

The third parameter of gluniformmatrix4fv () is gl_true, because we use the primary sequence of rows. You can also use the primary column order, which indicates the transpose matrix. The starting address of the fourth parameter matrix in the memory.

The following is the code in the shader:

uniform mat4 gWorld;

It represents a 4x4 matrix. mat2 and mat3 can also be used in the shader.

gl_Position = gWorld * vec4(Position, 1.0);

The position attribute of the triangle vertex is a three-dimensional vector, but we use four-dimensional homogeneous coordinates in the shader, so we will expand the location vector and set w = 1.0.

After the program is executed, each frame will input a translation world matrix. We use it to change the vertex position so that the X coordinate of the object moves.

After the program runs, the interface is as follows:

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.