当前位置:网站首页>Detailed derivation of perspective projection transformation and related applications
Detailed derivation of perspective projection transformation and related applications
2022-06-10 17:06:00 【Hua Weiyun】
because GAMES101 And LearnOpenGL In the course , Transform perspective projection 、 Viewport transform 、 Normal transformation is relatively simple , So this paper summarizes a lot of data , Hand pushed the relevant formula , It can be said that this is the most detailed information that can be seen on the Internet 、 Rigorous derivation process One of ( modesty ).
The process of projecting a model onto the screen is as follows :
One 、 Derivation of perspective projection process
1.1 Derivation of orthogonal projection matrix
The main process of orthogonal projection is to translate the center point to the origin , Rescale to
Orthogonal projection itself is relatively less used , The purpose of derivation here is to prepare for the subsequent perspective projection transformation .
1.1.1 The derivation process of pure right-handed coordinate system

explain :
- The unification here is in the right-hand coordinate system , namely z The shaft always faces outward .
- here n、f All in z Coordinate value on the negative half axis of the axis , All are negative .
- For the convenience of writing, we call this kind of
Standard right hand coordinate system,GAMES101 Which is derived in this case .
among , The translation transformation matrix is :
Scaling transformation matrix ( Note that the side length here is 2) by :
therefore , Orthogonal projection matrix :
If the visual vertebral body is up and down 、 Right and left symmetry (r=-l, t=-b, r+l=0, t+b=0) Of , be The matrix can be reduced to :
1.1.2 OpenGL Derivation of orthogonal projection transformation in

stay OpenGL China needs Particular attention :
- The observation space coordinate system is the right-hand system , and NDC in +z Axially inward , For the left hand system . Therefore, when scaling, it is no longer the case that n Mapping to 1,f Mapping to -1 It is [f,n] Mapping to NDC Medium [1,-1]
- stay OpenGL in n、f They are defined as
Near planeandFar planeThe distance value of , All positive numbers. , Therefore, we need to add a minus sign before substituting into the matrix to indicate the correct coordinate value . - For the convenience of writing, this situation is collectively referred to as
OpenGL Coordinate system.
At this point, the translation transformation matrix becomes :
Scale the transformation matrix to :
Note that there f、n There is a flip , by Flip to , The final result is as follows :
It can also be understood here in a more mathematical form as z The axis has been turned once , It is equivalent to the left multiplication of a flip matrix in the transformation process
therefore ,OpenGL Orthogonal projection matrix :
A simplified version is also available OpenGL Orthogonal projection coordinate system
1.2 Perspective projection matrix derivation
For perspective projection , There are two steps :
First ,“ Squash ” Look at the cone as a cuboid (n->n,f->f)();
Be careful : Here it is. Right hand coordinate system The following is the derivation , No longer consider the problem of coordinate transformation , But according to n、f Positive and negative conditions of There are still two derivations .

then , Use the orthogonal projection matrix above to get the final result .
Here mainly with n、f Is a positive number , use -n、-f In the case of substituting coordinates , The process is as follows :
In the process of compressing the visual cone into a cuboid , We have three principles :
The coordinates of all points in the near plane remain unchanged
Coordinates of all points in the far plane z The value remains the same All are -f
The coordinate value of the center point of the far plane remains unchanged by (0,0,-f)
The following figure shows the points in the observation space Is projected to the near plane On the situation ,

From the top view of the viewing cone (Top View of Frustum) see , Observe the of space Mapping to , Using the ratio of similar triangles, we can get :
Similarly, through the side view (Side View of Frustum) You can get :
So for any point in the optic vertebra , Its coordinates after the visual vertebral body is compressed into a cuboid should be , That is, we need a matrix Let the following formula hold :
hypothesis The first behavior of the matrix A,B,C,D. You can get the equation : , This equation is not easy to solve , If you allow A = , Others are equal to 0, You can really get results , But the value of the matrix should be constant , and It's a variable .
Similarly, for the second row of a matrix, find You will also encounter the above problems .
therefore , Here we need to use the properties of the secondary coordinates to solve . It is known that These two points are completely equivalent , So here we uniformly multiply the left side of the above equation by It can be rewritten as follows :
At this point, it is easy to launch :
The first line of the matrix :Ax+By+Cz+D = nx, Find out A=n,B=C=D=0
The second line :Ex+Fy+Gz+H = ny, Find out F=n,E=G=H=0
In the fourth row :Mx+Ny+Oz+P = z, Find out O=-1,M=N=P=0
thus , Only the third line element is still unknown , At this point, I will go back to the three principles stipulated above :
First principle : The coordinates of all points in the near plane remain unchanged
That is to say Through the matrix After transformation , It should still be equal to . Substitute it into the equation to get :
For the first 、 Two 、 Four lines , We write the derivation equation :
nx+0y+0n+0*1=x
0x+ny+0n+0*1=y
0x+0y+n+0*1=1
here ,n It should be any constant , But now only in n be equal to 1 when , One 、 Two 、 Four lines of operation , This is not a reasonable solution .
So here again Homogeneous coordinate system The left side of the equation is multiplied by n, obtain :
At this point, the first 、 Two 、 Four lines , Write the derivation equation :
nx+0y+0n+0*1=nx,
0x+ny+0n+0*1=ny,
0x+0y+n+0*1=n
It is found that the left and right sides of the above three equations are equal , And right n No restrictions , At this point, let the four numbers in the third row be A、B、C、D Available :Ax+By-Cn+D = -n^2,
It is not difficult to find the right side of the equation x,y It has nothing to do with the result , Therefore, we can find :A=0, B=0, And :
- Third principle : The coordinate value of the center point of the far plane remains unchanged by (0,0,-f)
Also, in order to ensure that the matrix I 、 Two 、 The four elements are established , Here also need to use Homogeneous coordinate system The nature of (0,0,-f,1) It's written in (0,0,-f*f,f), We get the following equation :
union (1) (2) You can easily find
C = n + f
D = nf
thus , We finally successfully deduced matrix :
Be careful : If the above is set at the beginning n、f A negative value means :, The following derivation is completely consistent with the above process , The final result is :
At this point, the final form of the perspective projection transformation matrix can be obtained
Here by looking up GLM In the library glm::perspective Find out , The source code is completely consistent with the results we pushed .
// glm/gtc/matrix_transform.inl:222// The storage method is column first Result[0][0] = (static_cast<T>(2) * nearVal) / (right - left);Result[1][1] = (static_cast<T>(2) * nearVal) / (top - bottom);Result[2][0] = (right + left) / (right - left);Result[2][1] = (top + bottom) / (top - bottom);Result[2][2] = -(farVal + nearVal) / (farVal - nearVal);Result[2][3] = static_cast<T>(-1);Result[3][2] = -(static_cast<T>(2) * farVal * nearVal) / (farVal - nearVal);1.3 Perspective division and NDC Space
Description of symbolic terms :
Express
Clipping space (clip space) coordinateExpress
Observe the space (eye\view space) coordinateExpress
NDC Spatial coordinates
The relationship between cropping coordinate system and observation coordinate system :
According to the definition of homogeneous coordinates, we know (x,y,z,w) And (x/w,y/w,z/w,1) It is equivalent. , That is to say, the (x,y,z,w) Divide by w, This step we call Perspective division (Perspective Divide).
From the above derivation, we can see that , The perspective projection transformation will compress the visual cone into a standard cube , So the apex of the visual vertebra (x/w,y/w,z/w,1), Its value range is :
So any vertex in the clipping space (x,y,z,w) If not satisfied (-w<x<w && -w<y<w && -w<z<w) That is, it is not in the optic vertebra , Need to be cropped .
The remaining vertices after clipping , Let's do a perspective division , You can transform them from crop space to NDC Space .
1.4 Zfighting problem
From perspective projection matrix and 1.3 The coordinate transformation relationship in section :
From above The formula can draw the following coordinate mapping diagram , You can see , The value changes greatly near the near plane , Good accuracy ; And near the far plane , Within a certain distance , Almost flat , Poor accuracy .
When you increase the range of the near and far clipping planes , As shown in the right figure below , We can see that near the far plane , Coordinate values are projected to The values after the coordinates are almost the same , The phenomenon of low accuracy is more obvious , The problem caused by the accuracy of this depth is called zFighting.
So try to minimize [-n,-f] The scope of the , To lessen zFighting problem .

1.5 Use FOV Derive perspective projection
The other one is often used The way is through perspective (Fov), Aspect ratio (Aspect) To specify the perspective projection .
It specifies fovy Specify the perspective ,aspect Specify the aspect ratio ,zNear and zFar Specify the clipping plane .fovy As shown in the figure below :

These parameters specify a symmetrical visible body , As shown in the figure below :

By these parameters , Substitute the above derived OpenGL Perspective projection matrix ( Simplified edition ) You can get :
By substituting the perspective transformation matrix above, we can get Fov Perspective projection matrix by :
Two 、 Viewport transformation process derivation
Viewport transform Yes, it will NDC The process of converting coordinates to display screen coordinates , As shown in the figure below :

explain :
- Express NDC Space coordinate value
- Represents the screen space coordinate value
- Represents the near plane of screen space 、 The position of the far plane
- Indicates the starting pixel of the screen
- Indicates the pixel width of the screen 、 high
Then the viewport transformation matrix obtained from the above formula is :
stay OpenGL in , In general , therefore viewPort Can be simplified as :
3、 ... and 、 The transformation relationship between linear and nonlinear depth
3.1 from NDC The value of spatial depth reverses the linear depth under the observation space
Known in 1.4 Section has been derived The transformation relationship between :
Therefore, it is easy to reverse deduce :
here Represents the observation space ( Right hand coordinate system ) Coordinate value under , It's a negative number ;
The concept of depth is aimed at screen space ( Left handed coordinate system ) To define the , Here to Take the opposite , Can be obtained from NDC Space anti push Observe the linear depth in space :
3.2 The nonlinear depth value in screen space is derived from the linear depth value in observation space
according to 5.2 The viewport transformation matrix introduced in can be easily obtained :
z_s = 0.5z_n + 0.5 \\ Plug in z_n = z_c/w_c Available : \\ \begin{align*} z_s &= \frac{1}{2}(z_n + 1) \\ &= \frac{1}{2}(\frac{f+n}{f-n} + \frac{2fn}{z_e(f-n)} + 1)\\ &= \frac{f - \frac{nf}{z_e}}{f - n} \\ &= \frac{\frac{1}{-z_e} - \frac{1}{n}}{\frac{1}{f} - \frac{1}{n}} \end{align*}Same as above , here Represents the observation space ( Right hand coordinate system ) Coordinate value under
But in the depth test article , Defined z Yes, it represents near 、 Distance value between far planes , They're all positive numbers , Therefore, we should also be right here Take the opposite .
Finally, it can be deduced from the observation space Nonlinear depth value of screen space by :
3.3 Restore position and normal information through depth information
Reference material :
https://zhuanlan.zhihu.com/p/367257314
https://mynameismjp.wordpress.com/2010/09/05/position-from-depth-3
The reasoning process of the above two materials is more detailed , No more details here .
Four 、 Description of normal transformation
Reference material :https://blog.csdn.net/u012419410/article/details/42174839

It can be seen from the above two figures , When there are scale transformations in the model , The normal vector will be destroyed , So it's not easy to use Matrix to transform the normal vector .
The derivation process of normal transformation matrix is as follows :
hypothesis Model space One of the tangent vectors in is T, The normal vector is N.
So if they are vertical, we can get :
Suppose they switch to Eye space The middle and the rear are and . Then they should still be perpendicular to each other :
Suppose the tangent vector T And normal vectors N The transformation matrix of is M、G. Then there are :
Further launch :
because , So we guess
Finally get :
namely : The transformation matrix applied to the normal vector is the inverse transpose matrix of the vertex transformation matrix .
边栏推荐
- Then, we will cooperate with impulse online and Feiteng to complete partner certification and jointly create a private computing ecosystem for Xinchuang
- 象形动态图图像化表意数据
- mysql数据库实现设置字段长度
- 再联合 冲量在线与飞腾完成合作伙伴认证,携手打造信创隐私计算生态圈
- 【无标题】
- 如何用Pygame制作简单的贪吃蛇游戏
- 打造隐私计算领先方案 冲量在线数据互联平台获得鲲鹏Validated认证
- Palm detection and finger counting based on OpenCV
- Numpy learning notes
- 亦舒作品经典语句
猜你喜欢

毕业季:致走向辽远未知的你

rmq 2333

Smart Scenic Spot Video Monitoring 5G Smart lamp Gateway Network Integrated pole

Postman common assertions

Fiddler创建AutoResponder

Carry forward the past and forge ahead into the future, multiple residuals | densenet (II)

NumPy 学习笔记

IDEA的Swing可视化插件JFormDesigner

Build a leading privacy computing scheme impulse online data interconnection platform and obtain Kunpeng validated certification

互联网企业与芯片
随机推荐
善用产业链招商,打造产业集群效应,实现产业协同发展
ahk函数命令大全
ahk常用函数
MM主要的表和主要字段_SAP刘梦_
Pictographic dynamic graphic ideographic data
新思科技在《 2022 年 Gartner 应用安全测试关键能力报告》中表现优异 在五个常见用例中荣获最高分
谁在使用我的服务器?在干什么?什么时候?
只有真正找到落地和实践元宇宙的正确方式,才能保证发展的行稳致远
线程面试相关问题
MySQL如何修改字段类型和字段长度
Build a leading privacy computing scheme impulse online data interconnection platform and obtain Kunpeng validated certification
Rethinking atlas revolution for semantic image segmentation (deeplobv3) personal summary
NumPy 学习笔记
VBA divides strings, stores them in an array, writes them to a file, and saves them
Fiddler模拟低速网络环境
Intelligent scenic spot video monitoring 5g intelligent light pole gateway networking integrated pole
VBA将字符串分割、存入数组、写入文件并保存
Shit, jialichuang's price is reduced again
Fabric.js 精简输出的JSON
Fiddler过滤会话
