alpha源混合系数通常设置为D3DBLEND_SRCALPHA,即当前绘制像素的alpha值。目标混合系数设置为D3DBLEND_INVSRCALPHA,即1减去当前绘制像素的alpha值。那么当前绘制像素的alpha值又是如何得到的呢?如果没有使用材质和纹理,当前绘制像素的alpha值来自每个顶点颜色设置的alpha值;如果使用光照和材质,则当前像素的alpha值来自物体表面材质;如果为物体表面使用了纹理,则alpha值还与纹理有关。
顶点alpha
如果在程序中直接指定每个顶点的颜色,则可以直接给出每个顶点颜色的 alpha值,可以在定义顶点时直接声明该顶点的alpha值,也可以在程序运行时动态地修改顶点的alpha值。有了顶点的alpha值,渲染对象中每个像素的alpha值由该对象的alpha值和着色模式决定。当着色模式为FLAT着色模式时,构成对象的各个多边形中所有像素的alpha都等于该多边形的第一个顶点的alpha值。当着色模式为GOURAUD着色模式时,每个多边形面上的像素的alpha值由它的各个顶点的alpha值进行线性插值得到的。
示例程序:
圆筒在不断的绕x, y, z轴旋转。
按下数字键"1",启用alpha顶点混合
按下数字键"0",禁用alpha顶点混合
源程序:
#include <d3dx9.h>
#pragma warning(disable : 4127) // disable warning: conditional expression is constant
#define CLASS_NAME "GameApp"
#define release_com(p) do { if(p) { (p)->Release(); (p) = NULL; } } while(0)
typedef unsigned char uchar;
IDirect3D9* g_d3d;
IDirect3DDevice9* g_device;
IDirect3DVertexBuffer9* g_vertex_buffer;
struct sCustomVertex
{
float x, y, z;
DWORD color;
};
#define D3DFVF_CUSTOM_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
void setup_world_matrix()
{
D3DXMATRIX mat_world;
D3DXMatrixIdentity(&mat_world);
float angle = (timeGetTime() % 1000) * (2 * D3DX_PI) / 1000.0f;
D3DXQUATERNION quat;
D3DXMATRIX mat_rotation;
D3DXQuaternionRotationYawPitchRoll(&quat, angle, angle, angle);
D3DXMatrixRotationQuaternion(&mat_rotation, &quat);
D3DXMatrixMultiply(&mat_world, &mat_rotation, &mat_world);
g_device->SetTransform(D3DTS_WORLD, &mat_world);
}
void setup_view_proj_matrices()
{
// setup view matrix
D3DXVECTOR3 eye(0.0f, 3.0f, -5.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, 100.0f);
g_device->SetTransform(D3DTS_PROJECTION, &mat_proj);
}
void init_vb()
{
g_device->CreateVertexBuffer(50 * 2 * sizeof(sCustomVertex), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_DEFAULT,
&g_vertex_buffer, NULL);
sCustomVertex* vertices;
g_vertex_buffer->Lock(0, 0, (void**)&vertices, 0);
for(int i = 0; i < 50; i++)
{
float theta = (2 * D3DX_PI * i) / (50 - 1);
vertices[2 * i + 0].x = sin(theta);
vertices[2 * i + 0].y = -1.0f;
vertices[2 * i + 0].z = cos(theta);
vertices[2 * i + 0].color = 0x88FF0000;
vertices[2 * i + 1].x = sin(theta);
vertices[2 * i + 1].y = 1.0f;
vertices[2 * i + 1].z = cos(theta);
vertices[2 * i + 1].color = 0x8844FF00;
}
g_vertex_buffer->Unlock();
}
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;
if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_device)))
{
return false;
}
init_vb();
setup_view_proj_matrices();
g_device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_device->SetRenderState(D3DRS_LIGHTING, FALSE);
g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
return true;
}
void cleanup()
{
release_com(g_vertex_buffer);
release_com(g_device);
release_com(g_d3d);
}
inline void extract_argb(D3DCOLOR color, uchar* alpha, uchar* red, uchar* green, uchar* blue)
{
// Extract alpha, red, green, blue from D3D color value.
if(alpha != NULL) *alpha = uchar((color >> 24) & 0xff);
if(red != NULL) *red = uchar((color >> 16) & 0xff);
if(green != NULL) *green = uchar((color >> 8) & 0xff);
if(blue != NULL) *blue = uchar(color & 0xff);
}
void render()
{
g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(5, 5, 5), 1.0f, 0);
g_device->BeginScene();
setup_world_matrix();
g_device->SetStreamSource(0, g_vertex_buffer, 0, sizeof(sCustomVertex));
g_device->SetFVF(D3DFVF_CUSTOM_VERTEX);
g_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2 * 50 - 2);
// change all vertices's color and alpha
sCustomVertex* vertex;
g_vertex_buffer->Lock(0, 50 * 2 * sizeof(sCustomVertex), (void**)&vertex, 0);
for(int i = 0; i < 50 * 2; i++)
{
uchar alpha, red, green, blue;
extract_argb(vertex->color, &alpha, &red, &green, &blue);
if(++alpha > 255) alpha = 0;
if(++red > 255) red = 0;
if(++green > 255) green = 0;
if(++blue > 255) blue = 0;
vertex->color = D3DCOLOR_RGBA(red, green, blue, alpha);
vertex++;
}
g_vertex_buffer->Unlock();
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 VK_ESCAPE:
DestroyWindow(hwnd);
break;
case 48: // press key "0", disable alpha blend.
g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
break;
case 49: // press key "1", enable alpha blend.
g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
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, 600, 500,
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;
}