There are a few things I don’t understand when writing code:

Problem a:

When we do matrix manipulation, we find that the amount of translation and the size of some figures are basically a decimal. What does this decimal mean?

Problem two:

CameraFrame. MoveForward (10); In which direction is it moving? What does this 10 mean?

I believe that some beginner friends will find the same mistake, this is due to the coordinate system knowledge is not solid enough. So let’s try to refine this.


World coordinate system

First of all, it needs to be clear:

  1. inOpenGLThe world coordinate system is based on the center of the screen(0, 0, 0)And remains the same
  2. Window range according to this unit is exactly(1, 1)to(1, 1)
    The bottom left corner of the screen (-1,-1), the top right corner of the screen (1,1)Copy the code
  3. The world coordinate system is a right-handed coordinate system and has the following characteristics
    A. The right direction is + X. B. The top direction is + Y. CCopy the code

Question 1:

By displaying Windows and world coordinates, we can see that a screen will go beyond the screen if we pan it or set the graphics size to be large


GLFrameuseforward

The GLFrame default constructor initializes the object as:

  1. Position in(0, 0)point
  2. At the top of the screen+yThe direction of
  3. forwardfor(0, 0, 1)
    -z towards the world means that when you're going in, you're going in.Copy the code
  4. To the left of the screen+xThe direction of

In camera coordinates: The camera is at the origin (0, 0, 0)

A. The vertical direction is' +y '. B. The camera is' +z 'cCopy the code

The above is the default construct for GLFrame, which is good for the camera, but not for the model. So when setting up the model, we sometimes set the following:

modelFrame.SetForwardVector(0, 0, 1);
Copy the code

Problem 2 analysis

If cameraFram keeps its initialization, then it moves forward (10), which is 10 units in the -z direction of the screen.

If you use the following configuration.

# is the same as changing the positive direction from -z to +z
cameraFrame.SetForwardVector(0, 0, 1);
Copy the code

So call moveForward(10); It’s going to go 10 out of the screen.

Note that the orientation is out of the screen and you may not see the values we drew inside the screen, so you need to rotate the camera again180You can see the direction of degrees.

From this, we found that for the camera, it is best to keep it in the original direction:

So forward is going into the screen, which is minus z in the world coordinate system, and it’s also easy to visualize.


Divergent backtracking —OpenGLCoordinate system

Looking at the above analysis, I have a dilemma. If different coordinate systems (object, world, inertia, camera) are not the same, some are left-handed, some are right-handed, then it is not a pit.

At last, I found that the original negligence at the beginning led to a series of misunderstandings in the follow-up.

The knowledge points are as follows:

  • OpenGLCoordinate systems (object, world, camera, inertia) belong to the right-handed coordinate system
  • The device coordinate system belongs to the left hand coordinate system

The illustration is as follows:


Divergent extension – coordinate system transformation process

The coordinate system in which objects are drawn. When the program is initialized, the world coordinate system and the current drawing coordinate system overlap. Only after translation, expansion and transformation of the current drawing coordinate system, the world coordinate system and the current drawing coordinate system no longer coincide.

The initial vertex coordinates of the object are local coordinates, which are later converted to world coordinates, observer coordinates, clipping coordinates, and so on.

Refer to the following figure for the conversion process:


conclusion

If the coordinate system knowledge is confused, there will be many misunderstandings in the subsequent understanding of the direction. For this use, nothing more than a lack of understanding of the coordinate system knowledge, and finally back to the most fundamental position: OpenGL coordinates are right-handed.

Reference:

OpenGL custom camera and model: GLFrame

OpenGL – coordinate system