I am rendering an object to a texture using the following setup code which I copied from Ogre's rendering to texture tutorial:

Ogre::RenderTexture *renderTexture = rtt_texture->getBuffer()->getRenderTarget();

renderTexture->addViewport(myViewPoint); //the original code uses mCamera, but I want
//to render the scene from a different view point.

Is there any way I can get the depth value at runtime as seen from myViewPoint?

I tried to use:

Ogre::GLDepthBuffer *depthBuf = renderTexture->getDepthBuffer();

but it didn't compile and the compiler says GLDepthBuffer is not a member of Ogre.

  • \$\begingroup\$ Do you need the depth values in C++ code or for the use in a shader? \$\endgroup\$ Nov 20, 2014 at 11:40
  • \$\begingroup\$ @PhilipAllgaier I need them in C++ code. \$\endgroup\$
    – shapeare
    Nov 20, 2014 at 11:52
  • \$\begingroup\$ For OpenGL or D3D? \$\endgroup\$ Nov 21, 2014 at 22:05
  • \$\begingroup\$ @PhilipAllgaier For OpenGL! \$\endgroup\$
    – shapeare
    Nov 22, 2014 at 2:26
  • \$\begingroup\$ The question as asked can be answered, but it may be useful to know why you think you need the depth value. I don't know your situation of course, but from experience I'm willing to bet that you don't need the depth value at all and you would be much better served by a ray test in your physics world. Remember that accessing the GPU's depth buffer from CPU code is slooooooow and that the depth buffer doesn't even necessarily model the data that you want (esp. when translucent object rendering is added to the mix). \$\endgroup\$ Sep 20, 2015 at 23:42

1 Answer 1


To use the class "GLDepthBuffer", you will have to manually include (and link) the OpenGL RenderSystem into your application (since that class is part of that Ogre plugin):

#include <RenderSystems/GL/OgreGLDepthBuffer.h>

However, Ogre currently does not provide a direct way to retrieve the depth buffer. The following code will help you to get the GLDepthBuffer instance:

Ogre::DepthBuffer* pDepthBuffer = OgreFramework::getSingletonPtr()->m_pViewport->getTarget()->getDepthBuffer();

if(OgreFramework::getSingletonPtr()->m_pRoot->getRenderSystem()->getName().find("OpenGL") != Ogre::String::npos)
    Ogre::GLDepthBuffer* pGlDepthBuffer = static_cast<Ogre::GLDepthBuffer*>(pDepthBuffer);
    Ogre::GLRenderBuffer* pRenderBuffer = pGlDepthBuffer->getDepthBuffer(); 

Unfortunately, the last line will not yield the expected result, since that will only return a null pointer as described in the API.

Bottom-line: You will have to find a way to access the depth buffer in OpenGL via the context (Ogre::GLContext and its subclasses for various platforms) or the FBOs. As a starting point, search the following method in the Ogre code base (part of multiple classes):

getCustomAttribute( const String& name, void* pData )

Alternatively you can also retrieve the context by adding the following lines below the previous code snippet (on Windows; other platforms have similar ways):

Ogre::GLContext* pGLContext = pGlDepthBuffer->getGLContext();

    Ogre::Win32Context* pWinContext = static_cast<Ogre::Win32Context*>(pGLContext);

If you found a way, please let us know either on the forums or even better, create a pull-request for our repository on BitBucket and we might merge it in.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .