1
\$\begingroup\$

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?

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 2, 2014 at 20:44
  • \$\begingroup\$ 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. \$\endgroup\$
    – frkn
    Mar 2, 2014 at 20:54
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 2, 2014 at 20:57
  • \$\begingroup\$ 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 \$\endgroup\$ Apr 5, 2016 at 7:58

1 Answer 1

4
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – frkn
    Mar 2, 2014 at 20:04
  • \$\begingroup\$ Perhaps that method expects a different computation for aspect ratio. \$\endgroup\$
    – user1430
    Mar 2, 2014 at 20:11
  • \$\begingroup\$ yes, you are right! i tried 1:1 aspect ratio and it looked as expected. thank you. \$\endgroup\$
    – frkn
    Mar 2, 2014 at 20:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .