Sign in to follow this  
browny

OpenGL Rendering Z-Buffer as texture

Recommended Posts

In OpenGL, as far as i remember, i used to render the Z-Buffer by assigning a texture as a RenderTarget as well as Z-Buffer. The texture can then later be used as a LUMINANCE texture to draw a Quad with it. How can i do a similar thing in Direct3D9. I am guessing that pDevice->StretchRect or pDevice->UpdateRect may do the trick by copying the Z-Buffer surface to a texture surface, but not sure. Another thing is, what should be the correct D3DFMT_ for the texture so that i can draw the Z-Buffer.

Share this post


Link to post
Share on other sites
You should use IDirect3DDevice9::GetDepthStencilSurface() function. Pass the IDirect3DSurface9 pointer into which you wanna store the zbuffer image. Hope this helps

Share this post


Link to post
Share on other sites
If I understand correctly, you want access to the Z buffer data. This is not easy to do in Direct3D 9. In D3D9 you can create a lockable Z buffer if the hardware supports that format. You will then be able to read its data back, though not render with it. ATI supports special texture Z formats, which can then be used for rendering as you describe. These are described in the "Radeon X1x00 Programming Guide" in the ATI SDK. I don't know if NVIDIA has anything similar.

Share this post


Link to post
Share on other sites
Now that led me asking if it is possible to do ShadowMap using Fixed Pipeline in Direct3D9. The sample in the SDK shows how to do it using Shaders, where Depth value is stored in the alpha channel. In OpenGL, ShadowMap can be done entirely in the Fixed Pipeline.

Share this post


Link to post
Share on other sites
It can be done, but with some tricks only. To lay it out nicely:

a) To my knowledge, DirectX 9 provides no way to use the ZBuffer as a texture. DX10 does, DX9 does not.
b) The graphics cards themselves do provide ways to sample from a ZBuffer. At least NVidia cards can.
c) The graphics card vendors offer some dirty tricks that you can use to bind a special ZBuffer as a texture.

To my knowledge, only NVidia does support sampling from ZBuffers natively. There's a tutorial about simple shadow mapping on nvidia.com where some code snippet is provided how to do that. If I remember correctly, if you bind this special type of texture to a sampler, sampling from it will result in proper shadow mapping even with the fixed pixel pipeline. I don't know of any way to achieve this on ATI cards, though.

Disclaimer: highly speculative knowledge presented here. No guarantee given. Hope this vague hints are of use for you.

Bye, Thomas

Share this post


Link to post
Share on other sites
Unless something has changed in recent years, NVIDIA doesn't allow Z buffer sampling, though it does allow Z textures, and sampling them with a function that compares them to the Z at the pixel. This is useful for shadows, but doesn't get you the actual Z value. It works in the fixed function pipeline, though.

As I mentioned, ATI does provide the ability to create a Z texture, which can then be sampled. It's possible that it could be used with the fixed function pipeline. However, I feel that it's rather pointless to not use shaders for this. The NVIDIA functionality was useful with FF since it existed in the age when most cards didn't have shaders.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this