Jump to content
  • Advertisement
Sign in to follow this  
keym

OpenGL Deferred depth problem

This topic is 1881 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 guys,

I'm struggling again with my deferred renderer. This time I'm rewriting it from OpenGL/Cg to DX11/HLSL. I have almost everything working but (I think) my reconstruction from depth is now working correctly (I compared normals and they look the same as in OGL renderer, so I guess they are fine). The result I get is somewhat correct but bad things start to happen when I move away from the lit object - It becomes darker and darker and eventually gets all black. The thing is that I use almost the same code as when dealing with Cg/OGL and perhaps I've overlooked something with conversion. Fun fact - walls that are closer to the camera gets darker quicker than walls that are further away - maybe this will give some hint.

 

Posting pics

 

 

    //compute view space position
    #define ZNEAR zNearZFar.x
    #define ZFAR zNearZFar.y
    #define A (ZNEAR + ZFAR)
    #define B (ZNEAR - ZFAR)
    #define C (2.0 * ZNEAR * ZFAR)
    #define D (ndcPos.z * B)
    #define ZEYE (-C / (A + D))
    float depth2 = shaderTexture2.Sample(SampleType, texCoord);
    float3 ndcPos = (float3(texCoord.x, 1 - texCoord.y, depth2) - 0.5) * 2.0; //<<<Here I had to flip Y coord when compared to the OGL/Cg shader
    float4 clipPos;
    clipPos.w = -ZEYE;
    clipPos.xyz = ndcPos * clipPos.w;
    float3 vsPos = mul(invProj, clipPos).xyz;

 

Share this post


Link to post
Share on other sites
Advertisement
Direct3D (any version) uses a different convention for the clip space normalized device coordinates: z goes from 0 to 1, not -1 to 1. (Off the top of my head: C = ZNEAR * ZFAR, but please check the formula yourself)
 
Best thing to do: Output position to a debug g-buffer (full float) and compare it with your reconstructed one. Edited by unbird

Share this post


Link to post
Share on other sites

So I guees I have to also do this:

 

 

from:

//From [0,1] to [-1,-1] in xyz
float3 ndcPos = (float3(texCoord.x, 1 - texCoord.y, depth2) - 0.5) * 2.0;

 

to:

//From [0,1] to [-1,1] only in xy, z stays in [0,1]
float3 ndcPos = float3(texCoord.x, 1 - texCoord.y, depth2);
ndcPos.xy = (ndcPos.xy - 0.5) * 2;

 

But this ^ and changing C to zNear*zFar sadly doesn't produce correct results. : /

 

Edit: For generating projection matrix I use D3DXMatrixPerspectiveFovRH (right handed coordinate system).
 

Edited by keym

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!