Analysis of the principle of transforming three-dimensional coordinates of the game world into screen coordinates: trigonometric function transformation and matrix transformation

After obtaining the xyz coordinates of the game character enemy in a 3D game, it cannot be directly drawn on the screen. A series of conversion algorithms are required. The current mainstream algorithms include trigonometric function conversion and matrix conversion. The trigonometric function conversion method is outdated and cumbersome. Recommended to use, this article mainly talks about the principle of matrix transformation :

 

1. The conversion relationship of multiple coordinate matrices in the game

CE can be found by the enemy game of world space coordinates , i.e. the game world space coordinates XYZ, the transform matrix Clip Space coordinate (coordinate shear), obtained by a segmentation algorithm perspective NDC coordinates , the coordinates of the window and finally the mapping matrix NDC i.e. Get screen coordinates .

 

Second, the matrix storage form in D3D and opengl

 Direct3D uses Row major storage

“Effect matrix parameters and HLSL matrix variables can define whether the value is a row-major or column-major matrix; however, the DirectX APIs always treat D3DMATRIX and D3DXMATRIX as row-major.”

OpenGL uses Colume major storage

“The m parameter points to a 4x4 matrix of single- or double-precision floating-point values stored in column-major order. That is, the matrix is stored as follows”

  The storage order explains how the matrix in linear algebra is stored in a linear memory array, d3d stores each row in the array by row, and OpenGL stores each column in each row of the array:

  Therefore, for the same matrix in thread algebra, there are different representations in d3d and OpenGL:

                  Line substitution matrix: a11, a12, a13, a14 d3d save: a11, a12, a13, a14 OpenGL save: a11, a21, a31, a41 
                       a21, a22, a23, a24 a21, a22, a23, a24 a12, a22, a32 ,a42 
                       a31,a32,a33,a34 a31,a32,a33,a34 a13,a23,a33,a43 
                       a41,a42,a43,a44 a41,a42,a43,a44 a14,a24,a34,a44

 

3. Algorithm implementation process (take D3D as an example)

1. Suppose the enemy's world coordinates are X, Y, Z, W; among them, W is a secondary item, which is generally set to 1 (this is the case in the D3D specification, this does not matter)

2. Calculate the shear coordinates:

Cutting coordinate X = a11*X + a12*Y + a13*Z + a14

Shear coordinate Y = a21*X + a22*Y + a23*Z + a24

Shear coordinate Z = a31*X + a32*Y + a33*Z + a34

Cutting coordinate W = a41*X + a42*Y + a43*Z + a44

(Pay attention to setting the filter conditions here. Only the cutting coordinates w>0 can continue the following operations, and the ones less than 0 can be ignored, because it is not in your perspective, PS: students who do not believe can try the difference between adding and not adding last )

3. Calculate the NDC coordinates: (perspective segmentation algorithm: divide the shear coordinates XYZ by W)

NDC coordinate X = shear coordinate X / shear coordinate W

NDC coordinate Y = shear coordinate Y / shear coordinate W

NDC coordinate Z = shear coordinate Z / shear coordinate W

4. Convert NDC coordinates to screen coordinates:

The conversion formula is as follows:

Screen coordinates. x = window width ÷ 2 × NDC coordinates. x + NDC coordinates. x + window width ÷ 2
screen coordinates. y =-(window height ÷ 2 × NDC coordinates. y) + NDC coordinates. y + window height ÷ 2

So far, the algorithm for converting world coordinates to screen coordinates has been described.

 

 

 

If you are interested in NDC to screen coordinates, you can continue to look down:

Four, NDC to screen formula analysis

 

As shown in the figure, we can get the above formula by matrix multiplication of the viewport transformation matrix ViewPort and NDC coordinates, but the formula for the screen coordinate Y cannot be derived from this matrix multiplication, pay attention to the symbol in front of the formula: screen coordinate.y = (window height ÷ 2 × NDC coordinate.y) + NDC coordinate.y + window height ÷ 2,

According to the reasoning of matrix multiplication, there should be no negative sign, but there is one here for the following reasons:

First popularize the difference between the normal screen coordinate system and the screen viewport coordinate system:

From this we can know that in matrix multiplication with VIewPort, Y should be the opposite of the original, so in

[x,

and;

from,

1] Add the symbol to y in the vertical matrix, and then do matrix multiplication, you can get the above correct algorithm formula.

 

 

 

references:

https://www.bilibili.com/video/BV1UK4y1D781/?spm_id_from=trigger_reload

https://www.cnblogs.com/icmzn/p/13531265.html

https://space.bilibili.com/442168898/channel/detail?cid=123750

https://www.bilibili.com/video/av286268338/

 

 

 

 

Guess you like

Origin blog.csdn.net/THMAIL/article/details/114462827