Why gluPerspective gives a result that does not match the formula in the documentation?

Question:

Return of gluPerspective differ from formula in documentation

Formula for gluPerspective described in IBM-doc and Khronos doc is

f/aspect 0 0 0
0 f 0 0
0 0 (far + near) / (near – far) (2 * far + near) / (near-far)
0 0 -1 0

but when I check gluPerspective matrix with python:

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(fov_h, aspect_ratio, near, far)
projection_matrix = glGetFloatv(GL_PROJECTION_MATRIX)

result seems too look like:

f/aspect 0 0 0
0 f 0 0
0 0 (far + near) / (near – far) -1
0 0 (2 * far + near) / (near-far) 0

For e.g. parameters gluPerspective(45.0, 4/3, 0.1, 100) returns:

[[ 1.8106601 0. 0. 0. ]

[ 0. 2.4142137 0. 0. ]

[ 0. 0. -1.002002 -1. ]

[ 0. 0. -0.2002002 0. ]]

So:

  • why?
  • which formula is correct?
Asked By: Maciej_Adamski

||

Answers:

I think the formula is correct and the problem here is your interpretation of the output. My guess is that the output is shown in so-called column major order. You are assuming that the output is row major order, but that isn’t the case. See this wikepedia article for an explanation of the difference.

Answered By: Code-Apprentice