Difference between revisions of "Depth Test"

From OpenGL.org
Jump to: navigation, search
(Briefly filled in the details. It's more API-oriented than these should be, but it's accurate and complete.)
m (Steps: Punctuation.)
Line 8: Line 8:
 
After clipping and division by w, depth coordinates range from -1 to 1, corresponding to the near and far clipping planes. Each viewport has an independent depth range specified as a linear mapping of the normalized depth coordinates in this range to window depth coordinates. Regardless of the actual depth buffer implementation, window coordinate depth values are treated as though they range from 0 through 1 (like color components). The depth values are mapped from window coordinates to normalized device coordinates using {{apifunc|glDepthRange}}(GLdouble {{param|nearVal}}, GLdouble {{param|farVal}}), which can be retrieved as a pair using {{apifunc|glGet}}...({{enum|GL_DEPTH_RANGE}}); the initial values are 0 and 1 respectively. When there are multiple viewports, {{apifunc|glDepthRangeIndexed}}(GLuint {{param|index}}, GLdouble {{param|nearVal}}, GLdouble {{param|farVal}}) and {{apifunc|glDepthRangeArray}}(GLuint {{param|first}}, GLsizei {{param|count}}, const GLdouble *{{param|v}}) are used.
 
After clipping and division by w, depth coordinates range from -1 to 1, corresponding to the near and far clipping planes. Each viewport has an independent depth range specified as a linear mapping of the normalized depth coordinates in this range to window depth coordinates. Regardless of the actual depth buffer implementation, window coordinate depth values are treated as though they range from 0 through 1 (like color components). The depth values are mapped from window coordinates to normalized device coordinates using {{apifunc|glDepthRange}}(GLdouble {{param|nearVal}}, GLdouble {{param|farVal}}), which can be retrieved as a pair using {{apifunc|glGet}}...({{enum|GL_DEPTH_RANGE}}); the initial values are 0 and 1 respectively. When there are multiple viewports, {{apifunc|glDepthRangeIndexed}}(GLuint {{param|index}}, GLdouble {{param|nearVal}}, GLdouble {{param|farVal}}) and {{apifunc|glDepthRangeArray}}(GLuint {{param|first}}, GLsizei {{param|count}}, const GLdouble *{{param|v}}) are used.
  
The depth test does not occur unless if {{enum|GL_DEPTH_TEST}} is enabled; the initial value is <i>false</i>. The depth of the sample being written to (<i>s</i>) is compared to the value in the depth buffer (<i>d</i>) using the function specified by {{apifunc|glDepthFunc}}(GLenum {{param|func}}) (which is retrieved with {{apifunc|glGet}}...({{enum|GL_DEPTH_FUNC}}). This can be {{enum|GL_NEVER}} (<i>false</i>), {{enum|GL_LESS}} (<i>s</i> &lt; <i>d</i>), {{enum|GL_EQUAL}} (<i>s</i> = <i>d</i>), {{enum|GL_LEQUAL}} (<i>s</i> ≤ <i>d</i>), {{enum|GL_GREATER}} (<i>s</i> &gt; <i>d</i>), {{enum|GL_NOTEQUAL}} (<i>s</i> ≠ <i>d</i>), {{enum|GL_GEQUAL}} (<i>s</i> ≥ <i>d</i>), or {{enum|GL_ALWAYS}} (<i>true</i>). The initial value is {{enum|GL_LESS}}.
+
The depth test does not occur unless if {{enum|GL_DEPTH_TEST}} is enabled; the initial value is <i>false</i>. The depth of the sample being written to (<i>s</i>) is compared to the value in the depth buffer (<i>d</i>) using the function specified by {{apifunc|glDepthFunc}}(GLenum {{param|func}}), which is retrieved with {{apifunc|glGet}}...({{enum|GL_DEPTH_FUNC}}). This can be {{enum|GL_NEVER}} (<i>false</i>), {{enum|GL_LESS}} (<i>s</i> &lt; <i>d</i>), {{enum|GL_EQUAL}} (<i>s</i> = <i>d</i>), {{enum|GL_LEQUAL}} (<i>s</i> ≤ <i>d</i>), {{enum|GL_GREATER}} (<i>s</i> &gt; <i>d</i>), {{enum|GL_NOTEQUAL}} (<i>s</i> ≠ <i>d</i>), {{enum|GL_GEQUAL}} (<i>s</i> ≥ <i>d</i>), or {{enum|GL_ALWAYS}} (<i>true</i>). The initial value is {{enum|GL_LESS}}.
  
 
If the test passes or is disabled, the depth value may then be written to the depth buffer. To control whether to write to the depth buffer, call {{apifunc|glDepthMask}}(GLboolean {{param|flag}}) or retrieve the current setting with {{apifunc|glGet}}...({{enum|GL_DEPTH_WRITEMASK​}}). The default is <i>true</i>.
 
If the test passes or is disabled, the depth value may then be written to the depth buffer. To control whether to write to the depth buffer, call {{apifunc|glDepthMask}}(GLboolean {{param|flag}}) or retrieve the current setting with {{apifunc|glGet}}...({{enum|GL_DEPTH_WRITEMASK​}}). The default is <i>true</i>.

Revision as of 20:55, 19 November 2013

The Depth Test is a per-sample operation performed conceptually after the Fragment Shader. The output depth may be tested against the depth of the sample being written to. If the test fails, the fragment is discarded. If the test passes, the depth buffer may be written to.

Steps

If the depth buffer exists, the value it is cleared to can be assigned with glClearDepth or retrieved with glGet...(GL_DEPTH_CLEAR_VALUE), and then the buffer itself is cleared using glClear(GL_DEPTH_BUFFER_BIT).

After clipping and division by w, depth coordinates range from -1 to 1, corresponding to the near and far clipping planes. Each viewport has an independent depth range specified as a linear mapping of the normalized depth coordinates in this range to window depth coordinates. Regardless of the actual depth buffer implementation, window coordinate depth values are treated as though they range from 0 through 1 (like color components). The depth values are mapped from window coordinates to normalized device coordinates using glDepthRange(GLdouble nearVal​, GLdouble farVal​), which can be retrieved as a pair using glGet...(GL_DEPTH_RANGE); the initial values are 0 and 1 respectively. When there are multiple viewports, glDepthRangeIndexed(GLuint index​, GLdouble nearVal​, GLdouble farVal​) and glDepthRangeArray(GLuint first​, GLsizei count​, const GLdouble *v​) are used.

The depth test does not occur unless if GL_DEPTH_TEST is enabled; the initial value is false. The depth of the sample being written to (s) is compared to the value in the depth buffer (d) using the function specified by glDepthFunc(GLenum func​), which is retrieved with glGet...(GL_DEPTH_FUNC). This can be GL_NEVER (false), GL_LESS (s < d), GL_EQUAL (s = d), GL_LEQUAL (sd), GL_GREATER (s > d), GL_NOTEQUAL (sd), GL_GEQUAL (sd), or GL_ALWAYS (true). The initial value is GL_LESS.

If the test passes or is disabled, the depth value may then be written to the depth buffer. To control whether to write to the depth buffer, call glDepthMask(GLboolean flag​) or retrieve the current setting with glGet...(GL_DEPTH_WRITEMASK​). The default is true.

Associated Gets

glGet with argument GL_DEPTH_CLEAR_VALUE, GL_DEPTH_FUNC, GL_DEPTH_RANGE, GL_DEPTH_WRITEMASK​

glIsEnabled with argument GL_DEPTH_TEST

See Also

glClear, glClearDepth, glDepthFunc, glDepthMask, glDepthRange, glDepthRangeArray, glDepthRangeIndexed