# OpenGL Deferred depth problem

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

## 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))
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 on other sites
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 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

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633709
• Total Posts
3013481
×