calculate eye-space position from depth

Started by
0 comments, last by Beholder 13 years, 9 months ago
I don't understand the following function:

// calculate eye-space position from depth
float3 posEye = uvToEye(texCoord, depth);
// calculate differences
float3 ddx = getEyePos(depthTex, texCoord + float2(texelSize, 0)) -posEye;
float3 ddx2 = posEye -getEyePos(depthTex, texCoord + vec2(-texelSize, 0));
if (abs(ddx.z) > abs(ddx2.z)) {
ddx = ddx2;
}

how to get eye-space position from depth?
Advertisement
Are you asking how uvToEye works in that snippet? Assuming texCoord is the screen-space coordinate of the current pixel, then the function could work like this:

// fetch depth value for current pixel
float depth = tex2D( depthTex, texCoord ).r;

// convert texture coordinate to homogeneous space
float2 xyPos = texCoord * 2.0 - 1.0;

// construct clip-space position
float4 clipPos = float4( xyPos, depth, 1.0 );

// transform from clip space to view (eye) space
// NOTE: this assumes that you've precomputed the
// inverse of the view->clip transform matrix and
// provided it to the shader as a constant.
float4 viewPos = mul( clipPos, c_invViewClipTransform );

// Make the position homogeneous and return
return viewPos.xyz / viewPos.w;

This topic is closed to new replies.

Advertisement