There are two important Projection transformations in OpenGL: Orthographic Projection and Perspective Projection, each of which has its corresponding transformation matrix. It’s hard for beginners to understand where these two matrices came from. In this paper, two projection matrices are derived from the mathematical point of view.

Thought of derivation

The purpose of both orthogonal and perspective projections is to map user coordinates to the viewable region of OpenGL. If we can deduce from the transformation matrices of these two that the range of coordinates that are mapped actually happens to be within the viewable region of OpenGL, then we can derive these two projection matrices backwards.

The coordinate range of OpenGL’s viewable region is a cube with sides of length 2. The size on each dimension is 2 and the range is [-1,+1]. Coordinates beyond the range of [-1,+1] after various transformations will not be displayed on the screen.

Orthogonal projection

Effect of transformation

Orthogonal projection in OpenGL is used to adjust the screen aspect ratio and convert the actual defined coordinates to the corresponding coordinates in the range of [-1,+1].

Matrix is defined

The following figure is an orthogonal projection matrix.

Considering only X-axis and Y-axis, then:

When defining the coordinates of the object, the coordinate range is:


By using the matrix above, you can convert to the corresponding coordinates in the range [-1,+1]. Let’s prove it.

Mathematical deduction

(x,y,z,1), where the range of x is [left, right], the range of y is [top, bottom], and the range of z is [near, far].

So, the matrix star vector


That is,





② Considering the existence of perspective divide, w=1, so:




② First prove that the X-axis does fall in the range of [-1, +1].

Obviously, x1 is a linear function of one variable with respect to x.


So f of x is maximum when x is right, and minimum when x is left.

Substitute into the equation and get:


(3) so


Similarly, the range of y1 and z1 is [-1, +1].

End of proof.

summary

Orthogonal transformation is converting the coordinates of the object into OpenGL coordinates.

The range before transformation is:


The range after transformation is:


Perspective projection

Effect of transformation

When displaying a 3D scene on a 2D screen, there is a feeling of being closer and smaller. OpenGL also uses this principle to achieve 3D effects on 2D screens. Fluoroscopic projection will form an optic vertebra in which coordinates can be drawn on the screen, that is, the range of coordinates on the optic vertebra will be adjusted to the range of [-1, +1].

Matrix is defined

The parameters are described as follows:

The perspective matrix is somewhat special and does not specify the range of x and y, which is derived below.

Mathematical deduction

① Suppose one of the coordinates on the object is (x,y,z,1).

Then, the result of matrix * vector is:


That is,





(2) Considering the existence of perspective divide, we can get:




When the result falls in the range of [-1, +1], what is the range of x?

Obviously, x2 is a linear function of one variable with respect to x.


Let’s calculate the range of x when the range of x2 is [-1, +1]


So the range of x is zero


And notice, by convention, z is always going to be negative, so the range up here is fine, same thing.

④ Find: when the result falls in the range of [-1, +1], what is the range of y?

Because,


Find the values of y when y1 is 1 and negative 1.


So the range of y is zero


⑤ Find: when the result falls in the range [-1, +1], what is the range of z?

Because,


make


There are,


So if I solve the equation,


So,


That is, the coordinates before transformation must be within the z-axis of the flat-cut vertebra to be finally displayed on the screen.

End of proof.

summary

Perspective transform is to convert object coordinates to OpenGL coordinates.

The range before transformation is:


The range after transformation is:


Attached is a diagram of the vertebral body through fluoroscopy:

conclusion

Matrix transformation plays a very important role in OpenGL coordinate transformation. Orthogonal transformation is generally used in 2d image display, while perspective transformation is used in 3D image display. It’s important for us to understand what these two transformations do.