2
\$\begingroup\$

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.
renderTexture->getViewport(0)->setClearEveryFrame(true);
renderTexture->getViewport(0)->setBackgroundColour(Ogre::ColourValue::Black);
renderTexture->getViewport(0)->setOverlaysEnabled(false);

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.

\$\endgroup\$
5
  • \$\begingroup\$ Do you need the depth values in C++ code or for the use in a shader? \$\endgroup\$ – Philip Allgaier Nov 20 '14 at 11:40
  • \$\begingroup\$ @PhilipAllgaier I need them in C++ code. \$\endgroup\$ – shapeare Nov 20 '14 at 11:52
  • \$\begingroup\$ For OpenGL or D3D? \$\endgroup\$ – Philip Allgaier Nov 21 '14 at 22:05
  • \$\begingroup\$ @PhilipAllgaier For OpenGL! \$\endgroup\$ – shapeare Nov 22 '14 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\$ – Sean Middleditch Sep 20 '15 at 23:42
1
\$\begingroup\$

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();

if(WIN32)
{
    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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.