Rendering depth

Started by
2 comments, last by elurahu 15 years, 2 months ago
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.
Advertisement
Anything from the Debug Runtimes? I suspect that D3DFMT_D24S8 isn't a valid renderable format (What should it appear as? Grey? Red? Some other weird format?)

I suspect you'll have to output the depth as a colour value in a second pass.
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.
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);}

This topic is closed to new replies.

Advertisement