# Why are there different ways of building projection matrices?

Matrix.PerspectiveFovLH documentation page says that this method uses the following formula to build a perspective projection matrix.

[w, 0, 0, 0]
[0, h, 0, 0]
[0, 0, zFar/(zFar - zNear), 1]
[0, 0, -zNear * zFar / (zFar - zNear), 0]


where

h = cot(fovY / 2)
w = h * aspectRatio


DirectXMath's DirectX::XMMatrixPerspectiveFovLH uses the same formula but with a difference. It calculates w as w = h / aspectRatio therefore creates a different matrix.

There is a difference between orthogonal projection matrix building too.

Why are there such differences or am i missing something?

• What makes you think that XMMatrixPerspectiveFovLH uses w = h / aspectRatio? That would be exactly opposite of the way aspect ratio is universally defined as w/h, which implies w = h * aspectRatio. And in the docs it explicitly says the aspect ratio is "X:Y" i.e. w/h. Mar 2, 2014 at 20:44
• I stepped in the function call when debugging. The line is float Width = Height / AspectHByW;. The code is in file DirectXMathMatrix.inl and can be found under C:\Program Files (x86)\Windows Kits\8.1\Include\um directory at Win8 machines.
– frkn
Mar 2, 2014 at 20:54
• Hmm, you're right. The parameter is even called AspectHByW, which is in contravention of both what the docs say and the way everyone else in the world defines aspect ratio. WTF. Mar 2, 2014 at 20:57
• To give a conclusion to anybody reading this: AspectHByW is incorrectly named, you should pass W/H and not H/W. Confirmed by the devs here: github.com/Microsoft/DirectXTK/issues/25 Apr 5, 2016 at 7:58

Simply because there are many ways to compute the projection of objects in a 3D scene onto a 2D plane. That's why you have perspective versus orthographic projections (and various flavors thereof, such as off-center projections), and that's why you can use slightly different formulae to compute the projection transformation.

Sometimes the differences are semantic in nature (such as in the case of perspective versus orthographic). Sometimes they are purely conventional in nature. Sometimes they are due to known quantities or characteristics of the inputs the system.

It's sort of similar to how you can represent your vectors as 4x1 or 1x4 matrices, and that consequently has a impact on whether you multiply your vectors on the right or the left of your transformation matrices.

• Hmm, i see. So both are correct and used for creating different projections. But i don't understand why would anyone want to use w = h * aspectRatio one. Because when i render a square it gets stretched horizontally and looks like a rectangle therefore unrealistic.
– frkn
Mar 2, 2014 at 20:04
• Perhaps that method expects a different computation for aspect ratio.
– user1430
Mar 2, 2014 at 20:11
• yes, you are right! i tried 1:1 aspect ratio and it looked as expected. thank you.
– frkn
Mar 2, 2014 at 20:29