Zbuffer rules for gl_Position

Here are my settings for opengl, if they matter:

glViewport(0,0,w,h); //w=800, h=600 glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 

For a while, I thought that clipping planes would apply to everything for which gl_Position.z is outside -1 to 1. But even when I force gl_Position.z to a constant number in the shader:

 gl_Position.z = -0.01; 

There is still proper z-buffering and clipping based on gl_Position.w! So what are the rules?

+4
source share
2 answers

The value of gl_Position is that OpenGL calls "clip space". This is a homogeneous coordinate system . In particular, the XYZ extents of the clip space are [-W, + W]. Therefore, each vertex is in its own space of the clip.

I do not quite understand what you mean by "what rules." The gl_Position clip space is tied to the viewport in a uniform space. Therefore, they are attached to the region [-W, + W] for each vertex. It is difficult to imagine that in a projective homogeneous space, but mathematics is obtained, therefore it does not matter.

After that, the trimmed vertices are converted to the normalized device coordinate space (NDC). This means dividing the XYZ space of the clip by W. All these vertices are in the range [-1, 1].

From there, the viewport is converted. The glViewport parameters scale vertices from the [-1, 1] NDC space to the viewport area. This converts dots to window space; it is in the window space that they are rasterized.

XY window space is controlled by glViewport . Z values ​​for the window space are controlled using glDepthRange . The default depth range is a close value of 0 and a far value of 1 (WARNING: the perspective near / far matrix has nothing to do with the depth range. Do not confuse the two). Thus, it displays [-1, 1] Z in the NDC space in [near, far] in the window.

These are the "rules."

+9
source

You are using glClearDepth (1.0f); glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);

The depth buffer has a value from 0 to 1 (maximum depth), use GL_LEQUAL, which means that everything will be drawn on the screen, since the depth value will be less than or equal to 1.

For clarity, visit: http://www.zeuscmd.com/tutorials/opengl/11-Depth.php

0
source

All Articles