深度测试与alpha混合
( Depth Test and Alpha Blending )
按下数字键"1",禁用深度测试,被矩形部分遮住的坦克被绘制出来。
按下数字键"2",启用深度测试,被矩形部分遮住的坦克没有被绘制出来。
按下数字键"0",禁用alpha混合。
设置alpha混合方法为D3DBLENDOP_SUBTRACT的效果图,启用alpha混合时。
g_device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
设置以下代码时的效果图,可以看到直升机变亮了,启用alpha混合:
g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_DESTCOLOR);
设置以下代码时的效果图,可以看到直升机变暗了,启用alpha混合:
g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVDESTCOLOR);
可以根据需要自己修改设置,以实现不同的颜色混合效果。
由于alpha混合是当前绘制的像素颜色与颜色缓冲区中存在的颜色的混合运算,因此,在绘制半透明物体前,必须保证位于半透明物体后的物体先于半透明物体绘制,也就是说,先绘制不透明物体,再绘制半透明物体。
有些计算机硬件由于功能的限制,可能不支持某些混合方法,这时Direct3D会自动使用alpha混合方法D3DBLENDOP_ADD。
源程序:
#include
#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 = xmaterials.MatD3D;
// set ambient reflected coefficient, because .x file do not set it.
g_mesh_materials.Ambient = g_mesh_materials.Diffuse;
g_mesh_textures = NULL;
if(xmaterials.pTextureFilename != NULL && strlen(xmaterials.pTextureFilename) > 0)
D3DXCreateTextureFromFile(g_device, xmaterials.pTextureFilename, &g_mesh_textures);
}
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_ALPHABLENDENABLE, TRUE);
g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
//g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
//g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVDESTCOLOR);
//g_device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
g_device->SetRenderState(D3DRS_ZENABLE, TRUE);
g_device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
g_device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
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);
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.Diffuse.a == 1.0f)
{
g_device->SetMaterial(&g_mesh_materials);
g_device->SetTexture(0, g_mesh_textures);
g_mesh->DrawSubset(i);
}
}
// render transparent object second
for(DWORD i = 0; i < g_num_materials; i++)
{
if(g_mesh_materials.Diffuse.a != 1.0f)
{
g_device->SetMaterial(&g_mesh_materials);
g_device->SetTexture(0, g_mesh_textures);
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 blend.
g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
break;
case 49: // press key "1", enable alpha blend.
g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, 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();
}
}
cleanup();
UnregisterClass(CLASS_NAME, wc.hInstance);
return 0;
}
深度测试与alpha混合(3)
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
#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;
}
深度测试与alpha混合(4)
材质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混合。
将顶点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
#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;
}
深度测试与alpha混合(5)
透过那些透明度非常高的物体看其他物体,例如透过几乎完全透明的玻璃看其他物体,会感到玻璃好像不存在,在三维图形程序中渲染时就可以不渲染这些透明度非常高的物体,从而可以提高渲染速度,这可以通过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,所以蓝色玻璃的颜色不会被绘制出来。
按下数字键"0",禁用alpha测试。
源程序:
#include
#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 = xmaterials.MatD3D;
// set ambient reflected coefficient, because .x file do not set it.
g_mesh_materials.Ambient = g_mesh_materials.Diffuse;
g_mesh_textures = NULL;
if(xmaterials.pTextureFilename != NULL && strlen(xmaterials.pTextureFilename) > 0)
D3DXCreateTextureFromFile(g_device, xmaterials.pTextureFilename, &g_mesh_textures);
}
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);
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.Diffuse.a == 1.0f)
{
g_device->SetMaterial(&g_mesh_materials);
g_device->SetTexture(0, g_mesh_textures);
g_mesh->DrawSubset(i);
}
}
// render transparent object second
for(DWORD i = 0; i < g_num_materials; i++)
{
if(g_mesh_materials.Diffuse.a != 1.0f)
{
g_device->SetMaterial(&g_mesh_materials);
g_device->SetTexture(0, g_mesh_textures);
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;
}
from : http://www.cppblog.com/lovedday/archive/2008/05/09/49337.html
http://www.cppblog.com/lovedday/archive/2008/05/10/49416.html
http://www.cppblog.com/lovedday/archive/2008/05/10/49428.html
http://www.cppblog.com/lovedday/archive/2008/05/10/49431.html
http://www.cppblog.com/lovedday/archive/2008/05/10/49434.html