Sign in to follow this  
Asheh

Rendering depth

Recommended Posts

I have been trying to render depth to a texture, using the following:
//create the texture with 24bit depth buffer and 8bit stencil
                  hr = m_pDevice->CreateTexture             ( 800, 600,        1, D3DUSAGE_DEPTHSTENCIL, D3DFMT_D24S8, D3DPOOL_DEFAULT, &m_pShadowMapTexture,            NULL);



//RENDERING
//(1st render pass) rendering the whole scene for depth
//grab surface from shadow map and set it is depth/stencil
        IDirect3DSurface9 *pSurface;
        m_pShadowMapTexture->GetSurfaceLevel   (0, &pSurface);
        hr = m_pDevice->SetDepthStencilSurface (pSurface);

        //DISABLE THE COLOUR BUFFER
        DXRenderer.Device()->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
        DXRenderer.Device()->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_ZERO);
        DXRenderer.Device()->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE);

        //disable lighting
        DXRenderer.Device()->SetRenderState( D3DRS_LIGHTING, false);

        //enable zwriting/nick As
        DXRenderer.Device()->SetRenderState( D3DRS_ZENABLE,  true);
        DXRenderer.Device()->SetRenderState( D3DRS_ZWRITEENABLE,  true);

        //disable stencil buffer
        DXRenderer.Device()->SetRenderState( D3DRS_STENCILENABLE, false);


Then.. set the texture and render it to a quad (so i can see its working)
m_pDevice->SetTexture			 ( 0, m_pShadowMapTexture ); //HDR scene texture

But it just appears white. I have also tried normalising the depth between 0.9 and 1.0 in the shader and rendering the texture through that.
    float col = tex2D( TexShadowMap, fin.tex0.xy ).r;    
    col = (1.0 - 0.9)/(col - 0.9);    
    return float4(col, col, col, col);

Anyone? cheers.

Share this post


Link to post
Share on other sites
There are ways to bind a depth buffer as a texture for use in shadow-mapping, but they're techniques specific to Nvidia and ATI. Using standard D3D9 you can't just set a depth-stencil surface as a texture, nor can you set a regular texture as a depth-stencil surface.

Share this post


Link to post
Share on other sites
As MJP told you - You can't render directly to the depth-stencil buffer you'll need to render to a seperate texture. Atleast in DX9.

I take it you're directly rendering the clip-space (post-projective transformation) coordinates to your shadowmap. Then because of the non-linear nature of the results you can't linearly rescale your output like that. True you might be able to see some garbage but it will not be the slick grayscale image you see in shadowmap articles. If you want one of those I advise you to render the scene in with view-space coordinates rescaled to fit within the 0.0f - 1.0f range.

Something like this.


struct VSOUTPUT
{
float4 Pos : POSITION;
float ViewZ : TEXCOORD0;
};

VSOUTPUT VSMain(float4 inPos: POSITION)
{
VSOUTPUT Output = (VSOUTPUT)0;
Output.Pos = mul(inPos, WorldViewProj);
Output.ViewZ = abs(mul(inPos, WorldView).z) / g_fZPassFarPlane;

return Output;
}

float4 PSMain(VSOUTPUT Input) : COLOR0
{
return float4(Input.ViewZ, 0.0f, 0.0f, 1.0f);
}


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