1. What is view coordinate system
The View coordinate system is the coordinate system under the camera’s perspective.
Cameras in the world coordinate system 👆
In view coordinates, the camera position is the origin position. The coordinates of all objects in the View coordinate system are relative to the camera position. And the camera is always oriented in the -Z direction of the View coordinate system (for openGL’s right hand coordinate system, Direct3D is left hand coordinate system, the camera is oriented in the same direction as the Z axis), that is, the opposite direction of the blue arrow in the figure above.
2. View transformation
Essence: multiply a transformation matrix, let the coordinates of all vertices in the world coordinate system transform to the coordinates in the View coordinate system
2.1 Derivation of view transformation matrix
First we need to define:
- The position vector of the camera in the world coordinate system, let’s say P
- The orientation of the camera (the direction of observation) is assumed to be vector G, which corresponds to the negative direction of the z-axis of the camera coordinate system, and vector D= -g is assumed to point in the positive direction of the Z-axis
- A right vector R represents the positive x direction in view coordinates
- A vector U pointing in the positive y direction in view coordinates
View transformation first moves the camera to the position (0,0,0), and the corresponding displacement transformation in the homogeneous coordinate system is:
Then begin to rotate the coordinate system: XYZ→RUD
If you define a coordinate space with three mutually perpendicular (or non-linear) axes, you can use the direction vectors of the three axes to form a matrix as shown below, which implements the transformation XYZ→RUD:
Therefore, the derivation formula of view transformation matrix can be written as follows:
âť—âť— superposition order of matrix transformations: the formulas are in order from right to left
To actually use the view transformation matrix in OPENGL, just call the GLM ::lookat() function.
The user only needs to define a camera position, a target position, and an upper vector in the world coordinate system (for deriving the direction of the right vector R) to obtain the observation matrix.
glm::mat4 view;
view = glm::lookAt(glm::vec3(0.0 f.0.0 f.3.0 f), // Camera position
glm::vec3(0.0 f.0.0 f.0.0 f), // Target position, assuming the camera is looking at the origin
glm::vec3(0.0 f.1.0 f.0.0 f));// Upper vector in the world coordinate system
Copy the code