Difference between revisions of "FAQ/Depth Buffer"

From OpenGL Wiki
< FAQ
Jump to: navigation, search
(Start on cleanup.)
(This page needs work. Adding some more details and also link to common mistakes page.)
Line 4: Line 4:
  
 
* The current framebuffer, whether an [[Framebuffer Object|FBO]] or the [[Default Framebuffer|default framebuffer]], must have a depth buffer.
 
* The current framebuffer, whether an [[Framebuffer Object|FBO]] or the [[Default Framebuffer|default framebuffer]], must have a depth buffer.
* Depth testing must be enabled. This is done by calling {{code|glEnable(GL_DEPTH_TEST)}}.
+
* Depth testing must be enabled. This is done by calling {{code|glEnable(GL_DEPTH_TEST)}}. By default, depth testing is disable when you create the GL context.
* The depth range zNear and zFar must be set properly. Use {{code|glDepthRange}} to set these values.
+
* The depth function should be set with {{code|glDepthFunc}}. We recommend glDepthFunc(GL_LEQUAL) where LEQUAL means less or equal.
* The depth buffer must be cleared when appropriate. This can be done by using glClear with the GL_DEPTH_BUFFER_BIT (along with glClearDepth to set the value that it will be cleared to). glClearBuffer can also be used to clear specific buffers.
+
* The depth mask is set with {{code|glDepthMask}}. By default, this is GL_TRUE which means writing to the depth buffer is enabled. Use it as you see fit but don't forget that if you set it to GL_FALSE, glClear(GL_DEPTH_BUFFER_BIT) won't have any effect.
 +
* The depth remap is set with {{code|glDepthRange}}. By default, this is glDepthRange(0.0, 1.0) and you don't need to touch it.
 +
* The depth buffer must be cleared when appropriate. This can be done by using glClear with the GL_DEPTH_BUFFER_BIT (along with glClearDepth to set the value that it will be cleared to). Typically, people use glClearDepth(1.0) to have the maximum. glClearBuffer can also be used to clear specific buffers (requires GL 3.0).
  
 
== Depth buffer precision ==
 
== Depth buffer precision ==
Line 15: Line 17:
 
; How do I turn off the zNear clipping plane? : See [http://www.opengl.org/resources/faq/technical/clipping.htm#0050 this question] in the Clipping section.
 
; How do I turn off the zNear clipping plane? : See [http://www.opengl.org/resources/faq/technical/clipping.htm#0050 this question] in the Clipping section.
 
; An article about depth buffer precision http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html
 
; An article about depth buffer precision http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html
 
+
; Common Mistakes http://www.opengl.org/wiki/Common_Mistakes#Depth_Buffer_Precision
 
{{stub}}
 
{{stub}}
  
 
[[Category:Framebuffer]]
 
[[Category:Framebuffer]]
 
[[Category:Depth Buffering]]
 
[[Category:Depth Buffering]]

Revision as of 05:12, 27 April 2011

Enabling depth buffering

In order to use depth buffering, the following must occur:.

  • The current framebuffer, whether an FBO or the default framebuffer, must have a depth buffer.
  • Depth testing must be enabled. This is done by calling glEnable(GL_DEPTH_TEST). By default, depth testing is disable when you create the GL context.
  • The depth function should be set with glDepthFunc. We recommend glDepthFunc(GL_LEQUAL) where LEQUAL means less or equal.
  • The depth mask is set with glDepthMask. By default, this is GL_TRUE which means writing to the depth buffer is enabled. Use it as you see fit but don't forget that if you set it to GL_FALSE, glClear(GL_DEPTH_BUFFER_BIT) won't have any effect.
  • The depth remap is set with glDepthRange. By default, this is glDepthRange(0.0, 1.0) and you don't need to touch it.
  • The depth buffer must be cleared when appropriate. This can be done by using glClear with the GL_DEPTH_BUFFER_BIT (along with glClearDepth to set the value that it will be cleared to). Typically, people use glClearDepth(1.0) to have the maximum. glClearBuffer can also be used to clear specific buffers (requires GL 3.0).

Depth buffer precision

Depth Buffer and Perspective Rendering 
Depth buffering doesn't work in my perspective rendering. What's going on?
Write Image To Depth Buffer 
How do I write a previously stored depth image to the depth buffer?
How do I turn off the zNear clipping plane? 
See this question in the Clipping section.
An article about depth buffer precision http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html
Common Mistakes http://www.opengl.org/wiki/Common_Mistakes#Depth_Buffer_Precision