Perspective projection matrix in D3D11
-
2012年6月30日 11:38
I am learning the Direct3D tutorial (http://code.msdn.microsoft.com/windowsapps/Direct3D-Tutorial-Sample-08667fb0). In the file 'Main.cpp' of the third example, titled "Lession3.cubes", the code of the lines 596-601 assigns the perspective projection matrix
m_constantBufferData.projection = float4x4( xScale, 0.0f, 0.0f, 0.0f, 0.0f, yScale, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, -0.01f, 0.0f, 0.0f, -1.0f, 0.0f );However, according to MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/bb205350(v=vs.85).aspx), the matrix should be written as
m_constantBufferData.projection = float4x4( xScale, 0.0f, 0.0f, 0.0f, 0.0f, yScale, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, -0.01f, 0.0f );according to the formula of perspective projection matrix
xScale 0 0 0
0 yScale 0 0
0 0 zf/(zf-zn) 1
0 0 -zn*zf/(zf-zn) 0
全部回复
-
2012年6月30日 11:40Of course the example is correct but I still fail to figure out why the perspective projection matrix is written as that in the example.
-
2012年7月2日 11:09版主
Hello,
You can take a look this document
http://msdn.microsoft.com/en-us/library/windows/desktop/bb147302(v=vs.85).aspx
Best regards,
Jesse
Jesse Jiang [MSFT]
MSDN Community Support | Feedback to us
-
2012年7月2日 15:55Jesse, the document you referred agrees with my proposition, and differs from the example code.
-
2012年7月2日 19:25
Part of the confusion is "row-major" vs. "column-major" matrices. I assume the flipped signs end up the third column end up reversing each other.
You may also consider just using DirectXMath instead of hand-coding these things. XMMatrixPerspectiveLH in particular.
- 已编辑 Chuck Walbourn - MSFT 2012年7月2日 19:26
-
2012年7月3日 14:46Hello Chuck, I do some math today and am surprised to find that D3D11 coordinate system (CS) is in sharp contrast to D3D10 CS, and in fact identical to OpenGL CS. It's a right-hand CS and has its camera looking at negative z-axis. The D3D11 vector is column-vector. Whereas in D3D10, everything is opposite. I wonder why does Microsoft make such a big change without documenting it explicitly.
-
2012年7月3日 23:00
When we moved from the 'fixed-function' pipeline of Direct3D to the 'programmable shader' pipeline model, the actual choice of right-hand vs. left-hand -and- row-major vs. column-major was no longer fixed by the API. It's just a matter of preference and consistency with your shaders, constant buffers, and computations.
Most Direct3D applications still use Left-Handed Coordiantes and Row-Major forms but you can transpose them to column-major when setting them into the constant buffers since the matrix multiply can be done slightly more efficently in HLSL. The actual choice of what the HLSL shader expects depends on the /Zpr vs. /Zpc command-line options (or the D3DCOMPILE_PACK_MATRIX_ROW_MAJOR vs. D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR compile flags), the #pragma pack_matrix setting, and the use of the row_major / column_major keywords See Matrix Ordering. Remember that RH vs. LH and row vs. column-major are in fact independant choices.
You'll note that DirectXMath actually has both LH and RH implementation of 'handed' matrix functions. XMMATRIX, XMFLOAT3X3, XMFLOAT4X4, and XMFLOAT4X3 are always 'row-major'. This is covered in the DirectXMath Programmer's Guide and Reference.
- 已编辑 Chuck Walbourn - MSFT 2012年7月3日 23:08
- 已标记为答案 Leonard 2012年7月5日 17:01
-
2012年7月5日 17:01Thank you Chuck.
-
2012年7月8日 1:43
check this link to understand the concept of precpective projection
http://www.mathdisk.com/pub/safi/worksheets/Perspective_Projection

