Retrieving a Depth Buffer

Download Report

Transcript Retrieving a Depth Buffer

Depth Buffer
Depth Buffer
깊이 버퍼


z-buffer , w-buffer 로도 불림.
깊이 정보를 저장.
Z-buffer




각 pixel의 z값이 저장
zero로 초기화
저장될 수 있는 가장 큰 값은
front clipping plane의 z값
대개 0.0과 1.0 사이로 지정
2
The z-buffer Algorithm
Z buffer Algorithm의 개략적 순서



(1) frame buffer와 z buffer를 초기화
(2) 임의의 순서로 각 polygon을 scan한다.
(3) polygon의 각 pixel(x, y)에 해당하는 깊이 z(x, y)를 계산하고,
그 위치의 z buffer에 저장된 값 Z(x,y)와 비교한다.
 (4) if (z(x, y) > Z(x,y))
Z(x, y)를 z(x, y)로 갱신한다.
Otherwise
No action
3
The z-buffer Algorithm
The addition of two polygon to an image (example)
(a) 빈 z buffer에 일정한 z의 polygon을 추가함.
(b) 또 다른 polygon을 추가하여 처음의 것과 교차 시킴.
4
The z-buffer Algorithm
Depth coherence


polygon이 planar라는 사실을 이용하여 scan line상의 각 point에
대해서 z의 계산을 단순화 할 수 있다.
To calculate z, 평면 방정식 Ax+By+Cz+D=0 에 대하여
 평면 방정식을 변수 z에 대해서 푼다.
 (x + x, y)에서의 z의 값은
 x는 1이므로,
 D  Ax  By
z1 
C
A
z 2  z1  (x)
C
z 2  z1 
A
C
5
The z-buffer Algorithm
장점


알고리즘이 간단하다.
구현이 쉽다.
단점



z buffer를 위한 많은 기억장소의 요구
Aliasing 문제(z buffer가 image precision algorithm이므로)
the generated z values in a z-buffer tend not to be distributed evenly
across the z-buffer range
W-buffer


more evenly distributed between the near and far clip planes than a
z-buffer.
하지만, z-Buffer만큼 하드웨어 지원이 많지는 않다.
6
The z-buffer Algorithm
몇몇 장비는 Z-Buffer 대신에 HSR(Hidden
Surface Removal) 기법을 지원하며 이는
D3DPRASTERCAPS_ZBUFFERLESSHSR이라
는 이림의 플래그를 사용.
HSR 기능에서는 Z-Buffer를 사용하지 않음
7
Querying for Depth Buffer Support
// pCaps 은 D3DCAPS8 구조체 변수
// 장치가 16비트 깊이 버퍼를 지원하는가를 검사
if( FAILED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
pCaps->DeviceType, Format,
D3DUSAGE_DEPTHSTENCIL,
D3DRTYPE_SURFACE,
D3DFMT_D16 ) ) )
return E_FAIL;
// 장치가 Depth-Stencil 버퍼를 지원하는 가를 검사.
if( FAILED( m_pD3D->CheckDepthStencilMatch( pCaps->AdapterOrdinal,
pCaps->DeviceType,
AdapterFormat,
RTFormat,
D3DFMT_D24S8 ) ) )
return E_FAIL;
8
Creating a Depth Buffer
새로운 depth buffer surface 생성
- IDirect3DDevice8::CreateDepthStencilSurface.
새 depth-buffer surface를 장치에 설정.
- Direct3DDevice8::SetRenderTarget
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed
= TRUE;
d3dpp.SwapEffect
= D3DSWAPEFFECT_COPY_VSYNC;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &d3dDevice ) ) )
return E_FAIL;
9
Functions for Depth Buffer Support
Enabling Depth Buffering
- IDirect3DDevice8::SetRenderState method
Retrieving a Depth Buffer
- LPDIRECT3DSURFACE8 pZBuffer;
- m_d3dDevice->GetDepthStencilSurface( &pZBuffer );
Clearing Depth Buffers
- IDirect3DDevice8::Clear
Changing Depth Buffer Write Access
Changing Depth Buffer Comparison Functions
- IDirect3DDevice8::SetRenderState method
- State parameter set to D3DRS_ZFUNC.
10