XNA Creators Club Online
Page 1 of 1 (1 items)
Sort Posts: Previous Next

[SOLVED] Restoring world space positions from linear depth buffer

Last post 11/17/2009 10:55 PM by Kjetil. 0 replies.
  • 11/17/2009 10:55 PM

    [SOLVED] Restoring world space positions from linear depth buffer

    Hi!
    I tried posting this problem on Gamedev earlier, though it didn't get a lot of attention, so I hope it's not bad karma to post it here as well. The more readers the merrier :)

    So I'm trying to reconstruct the world space postion from a linear depth buffer (depths between [0,1] in view space) in my deferred renderer for a bounding volume (namely a light sphere). I know this topic has been covered before in a few threads, though these have focused on the problem when using fullscreen quads, and not bounding volumes. Also, MJP has posted a nice technique in his blog here, but I don't really understand it and can't get it working. MJP proposes this method to get the view space position from the depth buffer:
     
    float3 VSPositionFromDepth(float2 vTexCoord, float3 vPositionVS) {    
    // Calculate the frustum ray using the view-space position.   
    // farCip is the distance to the camera's far clipping plane.    
    // Negating the Z component only necessary for right-handed coordinates    
    float3 vFrustumRayVS = vPositionVS.xyz * (farClip/-vPositionVS.z);    
    return tex2D(depthSampler, vTexCoord).x * vFrustumRayVS;
    }

    So I create my texcoords, which after a litte cpu-testing/debugging seems to work just fine, like this: 
    input.ssPos.xy /= input.ssPos.w;
    //Setup texture coordinates, transforming from [-1,1]->[1,-1] to [0,1]->[1,0]
    float2 tex = (0.5f * (float2(input.ssPos.x, -input.ssPos.y) + 1)) - halfPixel;  

    where ssPos is output from the vertex shader computed normally as: 
    output.ssPos = mul(input.pos, WorldViewProj);   

    Okay...so after having run: 

    //View space position
    float3 wsPos = VSPositionFromDepth(tex, input.vsPos); 
    //Transform to world space
    float3 wsPos = mul(wsPos, InvertView);   

    the shader still produces incorrect results. I don't see why wsPos is incorrect, which is mysterious to me. Any thoughts on how to approach the problem?
Page 1 of 1 (1 items) Previous Next