Jump to content
  • Advertisement
Sign in to follow this  

reconstruction 3d pixel position from depth

This topic is 3898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, I want to reconstruct a pixel's 3D position from its depth value. I already read this thread but somehow my reconstruction doesn't yet work. could anyone take a look at my code please? I added as many comments as possible to make reading easier. I guess the crucial part is the deferred shader where I apply my reconstruction.
// PASS 1

DepthVertexOutput DepthViewVertexShader( appdata IN )
    DepthVertexOutput OUT = ( DepthVertexOutput ) 0;
    float4 Po = float4( IN.Position.xyz, 1 );
    float4 Pw = MUL ( WorldXf, Po );
    OUT.HPosition = MUL( WvpXf, Po );     
    OUT.Position.xy = IN.UV; // store (s,t) in TEXCOORD
    OUT.Position.zw = OUT.HPosition.zw; // store depth and w-value in TEXCOORD

    return OUT;

float4 DepthViewPixelShader( DepthVertexOutput IN ) : COLOR 
		// Do parallax occlusion mapping, compute new depth
    float3	view = normalize( IN.Eye );
    float3	texCoord = traceUnroll( IN.Position.xy, view );    

		// this is all POM stuff and not my actual problem
    float dist = length(float3(1.0,1.0,depthRange)
                        *(float3(IN.Position.xy,1.0) - texCoord))
               * 0.5*(length(ddx(IN.Position.xy))+length(ddy(IN.Position.xy)))
               * SHADOW_SIZE;    
    IN.Position.z -= dist;
    // red+green = (s,t), blue = z / w, alpha = 1 / w
    return float4( texCoord.xy, IN.Position.z / IN.Position.w, 1.0 / IN.Position.w );


float4 DeferredPixelShader( vertexOutput IN ) : COLOR
    float3 light = normalize( IN.LightVec );
    float3 view = normalize( IN.Eye );    
    float4 texCoord1 = MUL( matTexAdj, IN.Position );
    float4 texCoord2 = IN.LProj;    
    float4 viewPixel = tex2Dproj ( ColorShadSampler, texCoord1 );
     // 3D reconstruction  
    float4 per_pix = float4( IN.Position.x, IN.Position.y, viewPixel.b, 1.0 );
    per_pix /= viewPixel.w;
    float4 pos4d = MUL( WvpITXf, per_pix ); // multiply with inverse ViewProjection
    pos4d.xyz /= pos4d.w;
    pos4d = MUL( WorldXf, pos4d ); // point is now in object space: transform to world space again
    //float4 posL = MUL( LampViewXf, pos4d );
    float4 posL = MUL( ShadowViewProjXf, pos4d ); // transform reconstructed point to light space 
 		posL.xyz / posL.w; // this shall be the 3d position, BUT ISN'T :-(


here are two pics of what my code actually produces: thank you very much, Nick!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!