材质alpha
顶点alpha是没有使用光照和材质的情况,如果对场景内的物体添加光照和材质而没有添加纹理时,顶点alpha值取决于材质属性中漫反射颜色的alpha系数和灯光颜色中的alpha系数,顶点alpha值是根据光照计算得到的。顶点光照计算是分别针对红、绿、蓝和alpha进行的,其中alpha光照计算的结果就是顶点的alpha值。有了顶点的alpha值就可根据着色模式计算出每个像素的alpha值,第一个示例程序就是材质alpha的例子。
纹理alpha
当对物体表面使用了纹理之后,像素的alpha值就是纹理alpha混合之后的值,所以这又取决于纹理的alpha混合方法,纹理alpha混合方法决定了纹理alpha混合之后的alpha值是取自材质,还是取自纹理,或者取自二者的某种运算。像素alpha值的具体计算过程是这样的,首先得到顶点alpha值,顶点alpha值可能是直接指定的,也可能是光照计算得到,然后根据着色模式对顶点alpha值进行插值,得到的结果再根据纹理alpha混合方法和纹理采样得到的alpha值进行指定的运算,得到最终每个像素的alpha值。
示例程序中将一幅纹理应用到一个矩形表面,其中纹理alpha混合的设置如下:
g_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
g_device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
g_device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
- D3DTOP_MODULATE
- Multiply the components of the arguments.
SRGBA = Arg1 x
Arg2
在示例程序中将矩形4个顶点的颜色值设置为0xFFFFFFFF,其alpha成分设置为ff,即alpha值为1.0f,所以纹理alpha混合的最终结果就是取纹理的alpha值。
sCustomVertex vertices[] =
{
{ -3, -3, 0.0f, 0xffffffff, 0.0f, 1.0f},
{ -3, 3, 0.0f, 0xffffffff, 0.0f, 0.0f},
{ 3, -3, 0.0f, 0xffffffff, 1.0f, 1.0f},
{ 3, 3, 0.0f, 0xffffffff, 1.0f, 0.0f}
}
示例程序在一个矩形表面贴了一颗树的纹理,在树的纹理中,没有树叶和树枝的地方alpha值为0,即完全透明;有树叶和树枝的地方alpha值为1,即完全不透明。所以通过alpha混合后,渲染的结果就像是一棵真的树。
按下数字键"1",启用纹理alpha混合。
按下数字键"0",禁用纹理alpha混合。
将顶点alpha由ff改为88后启用纹理混合的效果,可以看出纹理的颜色变暗了。
sCustomVertex vertices[] =
{
{ -3, -3, 0.0f, 0x88ffffff, 0.0f, 1.0f},
{ -3, 3, 0.0f, 0x88ffffff, 0.0f, 0.0f},
{ 3, -3, 0.0f, 0x88ffffff, 1.0f, 1.0f},
{ 3, 3, 0.0f, 0x88ffffff, 1.0f, 0.0f}
};
源程序:
#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;
IDirect3DTexture9* g_texture;
struct sCustomVertex
{
float x, y, z;
DWORD color;
float u, v;
};
#define D3DFVF_CUSTOM_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
void setup_matrices()
{
// setup world matrix
D3DXMATRIX mat_world;
D3DXMatrixIdentity(&mat_world);
g_device->SetTransform(D3DTS_WORLD, &mat_world);
// setup view matrix
D3DXVECTOR3 eye(0.0f, 0.0f, -10.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);
}
bool init_vb()
{
if(FAILED(D3DXCreateTextureFromFile(g_device, "tree.tga", &g_texture)))
{
MessageBox(NULL, "Can not load texture file tree.tga!", "ERROR", MB_OK);
return false;
}
sCustomVertex vertices[] =
{
{ -3, -3, 0.0f, 0xffffffff, 0.0f, 1.0f},
{ -3, 3, 0.0f, 0xffffffff, 0.0f, 0.0f},
{ 3, -3, 0.0f, 0xffffffff, 1.0f, 1.0f},
{ 3, 3, 0.0f, 0xffffffff, 1.0f, 0.0f}
/*
{ -3, -3, 0.0f, 0x88ffffff, 0.0f, 1.0f},
{ -3, 3, 0.0f, 0x88ffffff, 0.0f, 0.0f},
{ 3, -3, 0.0f, 0x88ffffff, 1.0f, 1.0f},
{ 3, 3, 0.0f, 0x88ffffff, 1.0f, 0.0f}
*/
};
g_device->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_DEFAULT, &g_vertex_buffer, NULL);
void* ptr;
g_vertex_buffer->Lock(0, 0, &ptr, 0);
memcpy(ptr, vertices, sizeof(vertices));
g_vertex_buffer->Unlock();
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;
if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_device)))
{
return false;
}
if(! init_vb())
return false;
setup_matrices();
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);
g_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
g_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
g_device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
g_device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
return true;
}
void cleanup()
{
release_com(g_vertex_buffer);
release_com(g_texture);
release_com(g_device);
release_com(g_d3d);
}
void render()
{
g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(55, 5, 5), 1.0f, 0);
g_device->BeginScene();
g_device->SetTexture(0, g_texture);
g_device->SetStreamSource(0, g_vertex_buffer, 0, sizeof(sCustomVertex));
g_device->SetFVF(D3DFVF_CUSTOM_VERTEX);
g_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
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;
}
下载示例工程