Related Contents:

  1. OpenGL– An introduction to the graphics API
  2. OpenGL– Related terms explained
  3. OpenGL– Environment configuration
  4. OpenGL- Case 1- Draw a triangle
  5. OpenGL– Case 2 — Draw a square
  6. OpenGL– Image rip
  7. OpenGL–3D mathematics related (Vectors and Matrices)
  8. OpenGL– Matrix transformations and matrix stacks

Matrix transformation of OpenGL

The process of OpenGL rendering 3D objects on the screen is actually similar to the process of taking photos with our ordinary cameras. The steps are roughly as follows:

  1. Attach the camera to the tripod and align it with the scene (View transformation)
  2. Reposition objects in the scene (model transformation)
  3. Select the camera lens and adjust the magnification (projection transform)
  4. Determine the size of the final photo (viewport transform)

Note: View transformations must take place before model transformations, other transformations can take place at any time.

Let’s take a look at each of these steps separately:

View transformation

  • View transformation, set the camera position. Is the first transformation applied to a scene to determine a vantage point in the scene. By default, perspective projections are at the origin (0, 0, 0) and are viewed along the negative z-axis.

  • View transformations can place the viewer anywhere you want and allow viewing of the scene in any direction. Determining a view transformation is like placing an observer in a scene and pointing it in a certain direction.

  • In the grand scheme of things, view transformations must be applied before any other model transformations can be applied. This is done because, for the visual coordinate system, the view transformation moves the coordinate system of the current work; Subsequent changes will be based on the newly adjusted coordinate system.

Model transformation

  • Set the position and orientation of the object, including: rotate, move, scale, and pay attention to the order in which they are used in combination, such as pan then rotate and then pan they behave differently.

  • Sometimes, instead of moving the camera (view transformation), you can move the object (model transformation). Both methods can achieve the same effect, and sometimes it is more convenient to use one transformation than the other. This is why both view transformation and model transformation are unified into the model view matrix.

  • It can also be seen from the above figure that the same object, after rotation or translation, will get different results at last

Projection transformation

  • Set up the viewport and project the 3d coordinates to the 2d screen coordinates. Anything outside the viewport will be cropped out.
  • It is divided into two kinds of projection: orthographic projection and perspective projection. Orthographic projection is a parallel projection. Objects look the same near or far. Perspective projection, on the other hand, is the real effect of near and far.

Viewport transformation

It basically specifies which area of the screen the final image will be displayed on, usually as large as the screen.

Model view matrix

View duality

In fact, the above view transformations and model transformations are the same in terms of their internal effects and the final appearance of the scene. The distinction between the two is purely for programmer convenience; there is no visual difference between moving an object backwards and moving a frame of reference forward.

Like model transformation, visual transformation is applied in the whole scene, and the objects in the scene are often transformed separately after view transformation. The term “model view” refers to the combination of these two transformations in the transformation pipeline into a single matrix, the model view matrix.

Model view matrix

The model view matrix is a 4×4 matrix that represents the transformed coordinate system that we can use to set and Orient objects. The vertices supplied to the primitives are used as a single column matrix and multiplied by a model view matrix to obtain a new transformed coordinate relative to the visual coordinates.

We can call the following functions in the Math3D library to perform matrix transformations:

// 1
void m3dLoadIdentity44(M3DMatrix44f m);

// create a translation matrix
// A translation matrix simply translates our vertices along one or more of the three axes.
void m3dTranslationMatrix44(M3DMatrix44f m,float x,float y,float z);

// create rotation matrix
// Rotate around a vector specified by the x, y, and z variables. The Angle of rotation is calculated in radians counterclockwise, specified by the variable Angle. It can be rotated about an axis or about any vector specified by the x, y, and z variables.
void m3dRotationMatrix44(M3DMatrix44f m,float angle,float x,float y,float z);


// Create a scaling matrix
// The scale matrix can be scaled up or down by a specified factor along three axes to change the size of the object.
voidM3dScaleMatrix44 (M3DMatrix44f m,float xScale,float yScale,floatZScale)// 5
// Add the two transformations together and you get the synthesis, just multiply the two matrices
void m3dMatrixMultiply44(M3DMatrix44f product,const M3DMatrix44f a,const M3DMatrix b);
Copy the code

The matrix stack

Matrix related

matrix

Matrix is a powerful mathematical tool, which greatly simplifies the process of solving equations or equations with complex relationships between variables. Matrix is widely used in coordinate transformation.

For example, if we have A point A in space defined by the x,y and z axes, and we rotate it by A certain Angle around any point in any direction, we need to use A matrix if we want to know the current position of point A. Because the x-coordinate of point A now depends not only on the original x-coordinate position, but also on the rotation parameters and the values of the y and z coordinates, solving this kind of problem is what matrices are good at.

Projection matrix

Projection matrix is divided into orthographic projection and perspective projection:

  • Orthographic projection produces a parallel projection, which is useful for drawing specific objects viewed from a distance without any shortening of perspective.
// Set orthographic projection with this function
GLFrustum::SetOrthographic(GLfloat xMin, GLfloat xMax,   GLfloat yMin, GLfloat yMax, GLfloat zMin, GLfloat zMax);
Copy the code
  • Perspective projection has a 3D effect that looks more realistic, and is often used when rendering 3D graphics
// Use this function to set perspective projection
GLFrustum::SetPerspective(float fFov, float fAspect, float fNear, float fFar);
Copy the code

The matrix stack

What is a matrix stack?

OpenGL’s matrix stack refers to a special area of memory dedicated to storing matrix data. In general, matrix stacks are used to construct inherited models (complex models made up of simple objects). Matrix stack is very beneficial to the connection and independence of multiple transformation operations in the process of complex model movement.

Why use a matrix stack?

After drawing a graph, there may be a variety of changes and complex scene switching, these transformations are generally implemented through the matrix to operate. The transformation in OpenGL generally includes view transformation, model transformation, projection transformation, etc. After each transformation, OpenGL will present a new state (state machine).

But sometimes after a transformation we want to go back to where we were before, and that’s why we use the matrix stack. He gives us two functions, PushMatrix() and PopMatrix(), which help us to return to the previous state without affecting the previous state. Let’s take a look at how the matrix stack is used and how the previous two functions work.

Use of matrix stack

1. Use

/ / type
GLMatrixStack::GLMatrixStack(int iStackDepth = 64);

// Load a cell matrix at the top of the stack
void GLMatrixStack::LoadIdentity(void);

// Load any matrix at the top of the stack with arguments :4*4 matrix
void GLMatrixStack::LoadMatrix(const M3DMatrix44f m);

// Multiply the matrix by the matrix at the top of the stack and store the result at the top of the stack
void GLMatrixStack::MultMatrix(const M3DMatrix44f);

// Get the GetMatrix function at the top of the matrix stack
// To accommodate the use of GLShaderMananger, or get a copy of the top matrix
const M3DMatrix44f & GLMatrixStack::GetMatrix(void);
void GLMatrixStack::GetMatrix(M3DMatrix44f mMatrix);
Copy the code

2. Push and push the stack

// Push the current matrix onto the stack (copy the top matrix onto the stack)
 void GLMatrixStack::PushMatrix(void);

// Push the M3DMatrix44f matrix object onto the current matrix stack
void PushMatrix(const M3DMatrix44f mMatrix);

// Press the GLFame object into the matrix object
void PushMatrix(GLFame &frame);

// Unstack (unstack refers to removing the top matrix object)
void GLMatrixStack::PopMatrix(void);
Copy the code

Here is a picture of the process of pushing and unloading the stack (PS: picture from the network):