Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Nathan Reed Mar 2 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 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. –  Nathan Reed Mar 2 at 20:57
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
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 at 20:04
    
Perhaps that method expects a different computation for aspect ratio. –  Josh Petrie Mar 2 at 20:11
    
yes, you are right! i tried 1:1 aspect ratio and it looked as expected. thank you. –  frkn Mar 2 at 20:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.