OGRE: Reversed Depth

| OGRE 14.5 Object-Oriented Graphics Rendering Engine

Loading...
Searching...
No Matches
Reversed Depth

By default Ogre is using the standard depth setup, which results in a hyperbolical depth value distribution. This means that there is a high depth resolution close to the near plane, while objects far from the near-plane are likely to experience z-fighting.

This typically occurs if you try to render large outdoor scenes, where you have objects very close to the camera, like grass-leaves, as well as objects very far away that all have their separate depth.

To mitigate this problem, Ogre allows you to use a reversed floating-point Z-Buffer, that results in an approximately linear depth value distribution. To use this, enable the "Reversed Z-Buffer" RenderSystem option.

Note
  currently this is only supported by the D3D11 and GL3Plus RenderSystems

This will make Ogre use the [1; 0] range for depth values instead of the standard [0; 1] range.

However, we also have to use a floating-point depth buffer to get any benefit from that. This is a little bit tricky, as e.g. OpenGL is very restrictive on the main depth-buffer therefore unlikely to allow you to using a floating point buffer there.

Therefore, we will use an off-screen texture for rendering, where we can easily use a floating-point depth buffer and only copy the results to the screen.

For this you can use the following Compositor script:

// a simple material that only applies the texture
material copy
{
technique
{
pass
{
lighting off
texture_unit
{
filtering none
}
}
}
}
compositor OffscreenRender
{
technique
{
// this intermediate texture allows OGRE to attach a float depth buffer
texture result target_width target_height PF_BYTE_RGBA
target result
{
// this will just render the scene as-is
input previous
}
target_output
{
// for the output we only have to copy the "result" texture to screen
pass render_quad
{
material copy
input 0 result
}
}
}
}
Ogre::PF_BYTE_RGBA
@ PF_BYTE_RGBA
32-bit pixel format, 8 bits for red, green, blue and alpha.
Definition OgrePixelFormat.h:293

If reversed depth is enabled, Ogre will automatically assign a floating point buffer here.

See Applying a Compositor, for how to set that compositor on your main window.

Note
  if you already use some compositor effects, make sure that OffscreenRender is the first compositor in the Ogre::CompositorChain.

As we only render a full-screen quad to our main window, we should tell Ogre that we do not need a depth buffer for it. We do this as:

Ogre::RenderWindow* rwin = getRenderWindow();
rwin->setDepthBufferPool(Ogre::RBP_NONE);
Ogre::RenderTarget::setDepthBufferPool
void setDepthBufferPool(uint16 poolId)
Sets the pool ID this RenderTarget should query from.
Ogre::RenderWindow
Manages the target rendering window.
Definition OgreRenderWindow.h:61
Ogre::RBP_NONE
@ RBP_NONE
No automatic allocation. Use this when the buffer is not needed, or when it will be managed explicitl...
Definition OgreCommon.h:136

Note
  If you are reading depth values in your shader, you can test for the OGRE_REVERSED_Z define, to discover whether reversed depth is enabled.