처음 지연 렌더링 Deferred Rendering 을 구현 했을때 깊이 버퍼를 Z/W 형태로 저장했습니다. 그러다 오즈라엘님의 블로그MJP 블로그를 통해 Z / FarClip 방식 통해서서도 구현이 가능하다는 것을 알고 한번 구현해보기로 했습니다.

Z / W로 부터 위치값 구하기 ( None-Linear )
// In VS
float4 ViewPosition = mul( In.Position, g_WorldViewProj );
float Depth = In.ViewPosition.z / In.ViewPosition.w;

// In PS
float Depth = tex2D( DepthSampler, In.TexCoord );
float4 H = float4( In.TexCoord.x * 2 - 1, ( 1 - In.TexCoord.y ) * 2 - 1, Depth, 1 );
float4 D = mul( H, g_ProjInv );  
float4 ViewPosition = D / D.w;

그리고 Z / FarClip 으로 부터 위치 값을 구하는 방법은 프러스텀 코너 위치를 이용해 구하는 방법등이 있는데, 이 방법은 셰이더로 프러스텀 값이랑 뭐 이거저거 넘겨주고 할게 많아서 ( 사실은 귀찮아서 ) 보류 할까 하고 있었는데, MJP의 후속 포스팅에서 보다 쉬운 방법이 있길래 그걸로 적용 해봤습니다.

Z / FarClip 으로 부터 위치 값 구하기 ( Linear )
// In VS
float4 ViewPosition = mul( In.Position, g_WorldView );
float Depth = In.ViewPosition.z / g_FarClip;

// In Ps
float Depth = tex2D( DepthSampler, In.TexCoord );
float3 vFrustumRayVS = In.CamViewPos.xyz * ( g_FarClip / In.CamViewPos.z );
float3 ViewPosition = Depth * vFrustumRayVS;

.
.
.

최종 결과물

음... 제대로 조명도 받고 잘 나오는 것 같군요.
근데... 이걸 어따 써먹지...

+ Recent posts