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.

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 frurther 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, althought things are not perfect: for some reaons I see only object positioned on negative z coordinates.

This might relate to my incorrect understaind of perspective projection function.

share|improve this question
1  
because zNear and zFar specify the distances the planes are from the camera, not their position... or am I misunderstanding your question? –  da code monkey May 26 '11 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. –  Bunkai.Satori May 26 '11 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? –  Josh Petrie May 26 '11 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. –  Josh Petrie May 26 '11 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. –  Bunkai.Satori May 26 '11 at 1:21

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
+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. –  Bunkai.Satori May 26 '11 at 1:37

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.