Current location - Training Enrollment Network - Mathematics courses - OpenGL: three-dimensional mathematical basic coordinate system, vector, matrix.
OpenGL: three-dimensional mathematical basic coordinate system, vector, matrix.
First of all, computer graphics

Computer graphics is a science that uses mathematical algorithms to convert two-dimensional or three-dimensional graphics into the grid form of computer monitors. Widely used in games, animation, simulation, virtual reality (VR), augmented reality (AR) and other fields.

In mathematics, the field of studying natural numbers and integers is called discrete mathematics, and the field of studying real numbers is called continuous mathematics.

In computer graphics, the key to choose the measurement unit of virtual world is to choose discrete precision. A wrong view is that short and int are discrete, while float and double are continuous. In fact, these data types are discrete. Therefore, computer graphics has the following standards:

The first rule of computer graphics: approximation principle-what seems right is right.

Second, Cartesian coordinate system

The 2D Cartesian coordinate system is a framework for accurately locating points. The standard representation of 2D coordinates is (x, y), which I believe everyone learned in junior high school. The general standard Cartesian coordinate system is that the X axis is right and the Y axis is up. In computer graphics, screen coordinates are usually X axis to the right and Y axis to the down. As shown in figure 1.

The 3D Cartesian coordinate system is similar to this, except that the third dimension, the Z axis, is added. Three-dimensional coordinate system is divided into two completely different coordinate systems, left-handed coordinate system and right-handed coordinate system. The judgment method is the left-hand coordinate system: extend the left hand so that the thumb and forefinger are L-shaped, the thumb is to the right, the forefinger is up, and the other fingers point forward. At this time, the thumb, forefinger and other three fingers represent the positive directions of the X, Y and Z axes respectively. The coordinate system of the right hand is the same, except that the left hand is changed to the right hand. As shown in figure 2.

Figure 2: Left-handed coordinate system and right-handed coordinate system

Among them, the left-handed coordinate system is widely used in computer graphics and D3D, and the right-handed coordinate system is widely used in OpenGL, linear algebra and 3DSMax.

Third, multi-coordinate system

Any 3D coordinate system can extend infinitely, and it can contain all points in space. Therefore, only one coordinate system is needed to describe all points. But it is more convenient to use different coordinate systems in different situations.

World coordinate system.

World coordinate system is a special kind of coordinate system, which describes the reference system needed by other coordinate systems. It is the largest and outermost coordinate system in a coordinate system. The concepts of "East" and "South" only exist in the world coordinate system.

2. Object coordinate system

An object coordinate system is a coordinate system related to a specific object. Every object has an independent coordinate system. The concepts of "front", "back", "left" and "right" are meaningful only in the workpiece coordinate system.

3. Camera coordinate system

The camera coordinate system is closely related to the observer. It is a special object coordinate system, which is defined in the visible area of the camera screen. In the camera coordinate system, the camera is at the origin, the X axis is to the right, the Z axis is forward (towards the screen or camera), and the Y axis is upward (not above the world, but above the camera itself).

4. Inertial coordinate system

Inertial coordinate system simplifies the transformation from world coordinate system to workpiece coordinate system. Its origin coincides with the object coordinate system, and the coordinate axis is parallel to the world coordinate system.

The significance of introducing inertial coordinate system is that the transformation from workpiece coordinate system to inertial coordinate system only needs rotation and the transformation from inertial coordinate system to world coordinate system only needs translation.

Fourth, the vector

For Cheng, a vector is an array. The number of "numbers" contained in an array is the dimension of a vector. Generally speaking, vectors in computer graphics mainly discuss two-dimensional, three-dimensional and four-dimensional vectors. The first two are usually used to represent positions and displacements in 2-D and 3-D spaces, and 4-D vectors are usually used for colors (RGB and transparent alpha).

Any point can be represented by a vector starting from the origin.

The following is one of the key points of this chapter, vector algorithm (examples are all three-dimensional vectors):

1. negative vector

Geometric meaning: when a vector becomes negative, it will get a vector with the same size and opposite direction.

2. Modulus of vectors

In the above formula, sqrt stands for square root.

Geometric meaning: the length of a vector

3. Multiplication of scalar and vector

Geometric meaning: If k, scale the length of the vector by a factor |k|.

4. Addition and subtraction of vectors

Geometric meaning: The geometric interpretation of the addition of vectors A and B is: translate the vector, make the head of vector A connect the tail of vector B, and then draw a vector from the tail of A to the head of b, which is the "triangle rule" of vector addition. Subtraction is similar.

5. Vector point multiplication

The word "dot multiplication" comes from the dot sign in notation A B, and the dot multiplication sign in dot multiplication cannot be omitted. It takes precedence over addition and subtraction.

Geometric meaning: The larger the point multiplication result, the closer the two vectors are.

a b = | | a | | | | b | | cosθ

θ is the angle between two vectors.

6. Vector cross product

The term "cross product" comes from the cross symbol in the symbol aXb. The cross sign cannot be omitted. Cross multiplication takes precedence over point multiplication.

The cross product does not satisfy the associative law. Satisfy the anti-commutative law: aXb = -(bXa)

Geometric meaning: aXb is perpendicular to A and B, pointing directly above the plane where A and B are located, and its size is the area of a parallelogram with A and B on both sides, which is ||||||||||| B ||||||| sin θ.

Verb (abbreviation for verb) [number] matrix

For Cheng, the vector is a one-dimensional array and the matrix is a two-dimensional array. A vector is an array of scalars, and a matrix is an array of vectors.

The algorithm of the matrix is as follows:

1. Scalar times matrix

2. Matrix multiplication

Only when certain conditions are met can two matrices be multiplied. A matrix of rXn can be multiplied by a matrix of nXc, and the result is a matrix of rXc, labeled AB. Matrix multiplication satisfies the associative law, but not the commutative law.

Multiplication of three-dimensional matrix;

Geometric meaning of matrix: Matrix is abstract. Generally speaking, a square matrix (a matrix with the same number of rows and columns) can describe any linear transformation. The formulas of matrix and linear transformation will be described in detail below.

Matrix of intransitive verbs and linear transformation

1. radial

zoom

The scaling matrix with unit vector n as the scaling direction and k as the factor is:

3. Orthogonal projection

The projection matrix of the plane perpendicular to the unit vector n is:

4. Mirror image

The mirror image transformation matrix of the plane passing through the origin and perpendicular to n is:

5. Combination of transformations

Transformations and combinations are common in rendering. Imagine that there is an object in any direction and any position in the world, and we have to render it to the camera in any direction and any position. In order to do this, we must transform all vertices of an object from the object coordinate system to the world coordinate system, and then from the world coordinate system to the camera coordinate system.

The mathematical transformation is as follows:

In this way, all matrices can be combined outside the loop of rendering, so that only one matrix can be multiplied when matrix multiplication is carried out in the loop (one matrix multiplication is saved, and the efficiency can be improved a lot).

The mathematical and geometric meanings and formulas of coordinate system, vector and matrix in three-dimensional graphics stop here. This paper covers most of the first eight chapters of "3D Mathematics Foundation+Graphics and Game Development". Pure theoretical knowledge is boring, but the three-dimensional virtual world is rich and colorful. Readers who want to read this article will take it as a note of the basic knowledge of three-dimensional graphics.