Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to render depth with HLSL

Started by gsamour Jul 4, 2009 at 3:30 AM 2 replies 1k views
Original Post
gsamour
gsamour
Hi, I am following the "Soft-Edged Shadows" tutorial by Anirudh S Shastry: http://www.gamedev.net/reference/articles/article2193.asp When it gets to the part about rendering depth, I tried doing that on my backbuffer instead of on a render target texture. All the meshes I render show up completely white. So it's like all depth values are 1.0f. But they're not. Each mesh drawn is farther away from the camera. Here's my effect file: uniform float4x4 wvp:VIEWPROJECTION; // Shadow generation vertex shader struct VSOUTPUT_SHADOW { float4 vPosition : POSITION; float fDepth : TEXCOORD0; }; VSOUTPUT_SHADOW VS_Shadow( float4 inPosition : POSITION ) { // Output struct VSOUTPUT_SHADOW OUT = (VSOUTPUT_SHADOW)0; // Output the transformed position OUT.vPosition = mul( inPosition, wvp ); // Output the scene depth OUT.fDepth = OUT.vPosition.z; return OUT; } float4 PS_Shadow( VSOUTPUT_SHADOW IN ):COLOR0 { // Output the scene depth return float4( IN.fDepth, IN.fDepth, IN.fDepth, 1.0f ); } technique MyTechnique { pass P0 { VertexShader = compile vs_2_0 VS_Shadow(); PixelShader = compile ps_2_0 PS_Shadow(); } } Am I doing something wrong? (most likely) Is the tutorial wrong? (not so likely)
RDragon1
RDragon1
Why don't you 'output depth' by writing to the depth buffer, instead of a color buffer?
Erik Rufelt
Erik Rufelt
Color can only be between 0.0 and 1.0, while depth can often be much greater. Try dividing IN.fDepth by 100 or 1000 or what matches your scale. If you want to see all depth, divide by your far-plane distance.
For viewing the depth that is, if you want to actually use the depth then it's ok that it's greater than 1.0, but when drawn everything above 1.0 will be white.
gsamour
gsamour
Thanks for the replies! Now I see what I expected. I figured the depth was bigger than 1.0f duh, but I hadn't thought of dividing it by the far plane. It's 4am, that's probably why. Thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.