7
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ because zNear and zFar specify the distances the planes are from the camera, not their position... or am I misunderstanding your question? \$\endgroup\$ – jeffythedragonslayer May 26 '11 at 0:18
  • \$\begingroup\$ @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. \$\endgroup\$ – Bunkai.Satori May 26 '11 at 0:34
  • \$\begingroup\$ 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? \$\endgroup\$ – Josh May 26 '11 at 1:20
  • \$\begingroup\$ Also do not that it simply by convention that things "deeper" have a "more negative" depth. You can arrange it however you like. \$\endgroup\$ – Josh May 26 '11 at 1:21
  • \$\begingroup\$ @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. \$\endgroup\$ – Bunkai.Satori May 26 '11 at 1:21
5
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ +1 for helpful answer. Hi Josh, and thanks for the clarification. It was my misinterpretation of the text. By the way, this knowledge was key to solve unpredicatble behavior of my OpenGL graphics. As there is nothing more to add, let me mark this as the Accepted Answer. \$\endgroup\$ – Bunkai.Satori May 26 '11 at 1:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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