透過那些透明度非常高的物體看其他物體,例如透過幾乎完全透明的玻璃看其他物體,會感到玻璃好像不存在,在三維圖形程序中渲染時就可以不渲染這些透明度非常高的物體,從而可以提高渲染速度,這可以通過alpha測試來實現。
alpha測試根據當前像素是否滿足alpha測試條件(即是否達到一定的透明度)來控制是否繪制該像素,圖形程序應用alpha測試可以有效地屏蔽某些像素顏色。與alpha混合相比,alpha測試不將當前像素的顏色與顏色緩沖區中像素的顏色混合,像素要么完全不透明,要么完全透明。由于無需進行顏色緩沖區的讀操作和顏色混合,因此alpha測試在速度上要優于alpha混合。
alpha測試通過激活渲染狀態D3DRS_ALPHATESTENABLE來設置,示例代碼如下:
g_device->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
渲染狀態D3DRS_ALPHAREF用來設置alpha測試的參考值,alpha測試函數比較當前繪制的像素的alpha值和參考值,如果返回TRUE,則通過測試并繪制像素,反之不予繪制。參考值的取值范圍是0x00000000到0x000000ff。
渲染狀態D3DRS_ALPHAFUNC用來設置alpha測試函數,alpha測試函數屬于D3DCMPFUNC枚舉類型,默認狀態為D3DCMP_ALWAYS。下列代碼設置alpha測試函數為D3DCMP_GREATER,表示測試點像素的alpha值大于設置的alpha參考值時則返回TRUE:
g_device->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
g_device->SetRenderState(D3DRS_ALPHAREF, 0x00000081);
g_device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
已知藍色玻璃的alpha值為浮點值0.5f,等價于兩位16進制數0x80,小于程序中設置的alpha測試參考值0x81,并且alpha測試函數被設為D3DCMP_GREATER,所以藍色玻璃的顏色不會被繪制出來。

按下數字鍵"1",啟用alpha測試。

按下數字鍵"0",禁用alpha測試。
源程序:
#include <d3dx9.h>
#pragma warning(disable : 4127)
#define CLASS_NAME "GameApp"
#define release_com(p) do { if(p) { (p)->Release(); (p) = NULL; } } while(0)
IDirect3D9* g_d3d;
IDirect3DDevice9* g_device;
ID3DXMesh* g_mesh;
D3DMATERIAL9* g_mesh_materials;
IDirect3DTexture9** g_mesh_textures;
DWORD g_num_materials;
void setup_world_matrix()
{
D3DXMATRIX mat_world;
D3DXMatrixRotationY(&mat_world, timeGetTime() / 1000.0f);
g_device->SetTransform(D3DTS_WORLD, &mat_world);
}
void setup_view_proj_matrices()
{
// setup view matrix
D3DXVECTOR3 eye(0.0f, 15.0f, -20.0f);
D3DXVECTOR3 at(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX mat_view;
D3DXMatrixLookAtLH(&mat_view, &eye, &at, &up);
g_device->SetTransform(D3DTS_VIEW, &mat_view);
// setup projection matrix
D3DXMATRIX mat_proj;
D3DXMatrixPerspectiveFovLH(&mat_proj, D3DX_PI/4, 1.0f, 1.0f, 500.0f);
g_device->SetTransform(D3DTS_PROJECTION, &mat_proj);
}
bool init_geometry()
{
ID3DXBuffer* material_buffer;
/*
D3DXLoadMeshFromXA(
LPCSTR pFilename,
DWORD Options,
LPDIRECT3DDEVICE9 pD3DDevice,
LPD3DXBUFFER *ppAdjacency,
LPD3DXBUFFER *ppMaterials,
LPD3DXBUFFER *ppEffectInstances,
DWORD *pNumMaterials,
LPD3DXMESH *ppMesh);
*/
if(FAILED(D3DXLoadMeshFromX("heli.x", D3DXMESH_SYSTEMMEM, g_device, NULL, &material_buffer, NULL,
&g_num_materials, &g_mesh)))
{
MessageBox(NULL, "Could not find heli.x", "ERROR", MB_OK);
return false;
}
D3DXMATERIAL* xmaterials = (D3DXMATERIAL*) material_buffer->GetBufferPointer();
g_mesh_materials = new D3DMATERIAL9[g_num_materials];
g_mesh_textures = new IDirect3DTexture9*[g_num_materials];
for(DWORD i = 0; i < g_num_materials; i++)
{
g_mesh_materials[i] = xmaterials[i].MatD3D;
// set ambient reflected coefficient, because .x file do not set it.
g_mesh_materials[i].Ambient = g_mesh_materials[i].Diffuse;
g_mesh_textures[i] = NULL;
if(xmaterials[i].pTextureFilename != NULL && strlen(xmaterials[i].pTextureFilename) > 0)
D3DXCreateTextureFromFile(g_device, xmaterials[i].pTextureFilename, &g_mesh_textures[i]);
}
material_buffer->Release();
return true;
}
bool init_d3d(HWND hwnd)
{
g_d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(g_d3d == NULL)
return false;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_device)))
{
return false;
}
if(! init_geometry())
return false;
setup_view_proj_matrices();
g_device->SetRenderState(D3DRS_ZENABLE, TRUE);
g_device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
g_device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
g_device->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
g_device->SetRenderState(D3DRS_ALPHAREF, 0x00000081);
g_device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
g_device->SetRenderState(D3DRS_AMBIENT, 0xFFFFBB55);
return true;
}
void cleanup()
{
delete[] g_mesh_materials;
if(g_mesh_textures)
{
for(DWORD i = 0; i < g_num_materials; i++)
release_com(g_mesh_textures[i]);
delete[] g_mesh_textures;
}
release_com(g_mesh);
release_com(g_device);
release_com(g_d3d);
}
void render()
{
g_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(5, 5, 5), 1.0f, 0);
g_device->BeginScene();
setup_world_matrix();
// render opaque object first
for(DWORD i = 0; i < g_num_materials; i++)
{
if(g_mesh_materials[i].Diffuse.a == 1.0f)
{
g_device->SetMaterial(&g_mesh_materials[i]);
g_device->SetTexture(0, g_mesh_textures[i]);
g_mesh->DrawSubset(i);
}
}
// render transparent object second
for(DWORD i = 0; i < g_num_materials; i++)
{
if(g_mesh_materials[i].Diffuse.a != 1.0f)
{
g_device->SetMaterial(&g_mesh_materials[i]);
g_device->SetTexture(0, g_mesh_textures[i]);
g_mesh->DrawSubset(i);
}
}
g_device->EndScene();
g_device->Present(NULL, NULL, NULL, NULL);
}
LRESULT WINAPI WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_KEYDOWN:
switch(wParam)
{
case 48: // press key "0", disable alpha test.
g_device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
break;
case 49: // press key "1", enable alpha test.
g_device->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
break;
case VK_ESCAPE:
DestroyWindow(hwnd);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR, INT)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_CLASSDC;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = inst;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = CLASS_NAME;
wc.hIconSm = NULL;
if(! RegisterClassEx(&wc))
return -1;
HWND hwnd = CreateWindow(CLASS_NAME, "Direct3D App", WS_OVERLAPPEDWINDOW, 200, 100, 640, 480,
NULL, NULL, wc.hInstance, NULL);
if(hwnd == NULL)
return -1;
if(init_d3d(hwnd))
{
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
render();
Sleep(10);
}
}
cleanup();
UnregisterClass(CLASS_NAME, wc.hInstance);
return 0;
}
下載示例工程