# Understanding the perspective projection matrix in OpenGL

Setting the Perspective projection matrix in Open GL (including OpenGL ES 2.0) has the following general format:

glm::mat4 perspective(float fovy, float aspect, float zNear, float zFar);


Notice the last two parameters; First one specifies zNear plane, which is the plane closer to the camera. The second one specifies zFar plane which is far from the camera.

It is common knowledge that, in OpenGL, things that are further, in other words "deeper into the screen", have negative Z axis. Why then all the OpenGL examples, that set perspective projection matrix (taken from reliable sources like OpenGL SuperBible and simmilar) look like the two above mentioned parameters (zNear, zFar) of Perspective projection funcion are swapped?

This example is taken from GLM(OpenGL Math) webiste:

glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.f);


You can see, that the zNear parameter is actually further from the camera, than zFar parameter, which is 100 points closer to the camera/viewer.

Can you provide easy to understand explanation, please? I it is not just one but all the examples, which pass zFar as higher positive value than zNear. Thanks.

## My Example:

In my code, when I set:

glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 1000.0f, -500.0f);


I assume my frustum being 1000 points long in z-positive direction and 500 points long in z-negative direction.

When I set things like this, I can see at least some object, although things are not perfect: for some reasons I see only objects positioned on negative z coordinates.

This might relate to my incorrect understanding of the perspective projection function.

• because zNear and zFar specify the distances the planes are from the camera, not their position... or am I misunderstanding your question? May 26, 2011 at 0:18
• @da code monkey: +1, nope you are not misunderstanding at all. So you say, the two above mentioned parameters do not contain physical plane coordinates, but they represent offset values from the camera. If the camera is moved, the frustum follows it. This should have been emphasized in OpenGL books. May 26, 2011 at 0:34
• It is pretty explicitly spelled out in most of those books; do you have specific examples of passages that indicate that the values are absolutes?
– user1430
May 26, 2011 at 1:20
• Also do not that it simply by convention that things "deeper" have a "more negative" depth. You can arrange it however you like.
– user1430
May 26, 2011 at 1:21
• @da code monkey: If you wisth, retype your answer as a regular answer into this thread. Then, I will be able to mark it as the Accepted Answer. Thanks for helping, I had a lot of headchache from this. May 26, 2011 at 1:21

The method in question (glm::perspective) and its ilk are essentially wrappers or re-implementations of gluPerspective. As you can see from the documentation of that function, the near/far values are distances from the view plane, and are always positive.