像素雾化示例程序
添加像素雾化效果也需要对Direct3D渲染设备进行三方面的设置:启用雾化效果、设置雾化混合因子计算公式、设置雾化参数。添加像素雾化效果和添加顶点雾化效果基本相同,唯一的区别是雾化混合因子计算公式的设置。像素雾化混合因子计算公式也是采用函数IDirect3DDevice9::SetRenderState()设置,不同的是第一个参数需要设置为D3DRS_FOGTABLEMODE。设置雾化混合因子计算公式的代码如下:
case WM_KEYDOWN:
switch(wParam)
{
case 48: // press key "0", disable fog.
g_device->SetRenderState(D3DRS_FOGENABLE, FALSE);
break;
case 49: // press key "1", enable linear fog.
g_device->SetRenderState(D3DRS_FOGENABLE, TRUE);
g_device->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
g_device->SetRenderState(D3DRS_FOGSTART, *(DWORD*)&fog_start);
g_device->SetRenderState(D3DRS_FOGEND, *(DWORD*)&fog_end);
break;
case 50: // press key "2", enable exp fog.
g_device->SetRenderState(D3DRS_FOGENABLE, TRUE);
g_device->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_EXP);
g_device->SetRenderState(D3DRS_FOGDENSITY, *(DWORD*)&fog_density);
break;
case 51: // press key "3", enable exp2 fog.
g_device->SetRenderState(D3DRS_FOGENABLE, TRUE);
g_device->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_EXP2);
g_device->SetRenderState(D3DRS_FOGDENSITY, *(DWORD*)&fog_density);
break;
case VK_ESCAPE:
DestroyWindow(hwnd);
break;
}
break;
示例截图:
源程序:
#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;
struct sVertex
{
float x, y, z;
float u, v;
};
inline float height_field(float x, float z)
{
float y = 0.0f;
y += 10.0f * cosf(0.051f * x) * sinf(0.055f * x);
y += 10.0f * cosf(0.053f * z) * sinf(0.057f * z);
y += 2.0f * cosf(0.101f * x) * sinf(0.105f * x);
y += 2.0f * cosf(0.103f * z) * sinf(0.107f * z);
y += 2.0f * cosf(0.251f * x) * sinf(0.255f * x);
y += 2.0f * cosf(0.253f * z) * sinf(0.257f * z);
return y;
}
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, 30.0f, -100.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("seafloor.x", D3DXMESH_SYSTEMMEM, g_device, NULL, &material_buffer, NULL,
&g_num_materials, &g_mesh)))
{
MessageBox(NULL, "Could not find seafloor.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();
// change model's height
IDirect3DVertexBuffer9* vertex_buffer;
g_mesh->GetVertexBuffer(&vertex_buffer);
sVertex* vertices;
vertex_buffer->Lock(0, 0, (void**)&vertices, 0);
DWORD num_vertices = g_mesh->GetNumVertices();
for(DWORD i = 0; i < num_vertices; i++)
vertices[i].y = height_field(vertices[i].x, vertices[i].z);
vertex_buffer->Unlock();
vertex_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->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
g_device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
g_device->SetRenderState(D3DRS_FOGCOLOR, 0xFFAAAA00);
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();
for(DWORD i = 0; i < g_num_materials; i++)
{
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)
{
static float fog_start = 50;
static float fog_end = 300;
static float fog_density = 0.01f;
switch(msg)
{
case WM_KEYDOWN:
switch(wParam)
{
case 48: // press key "0", disable fog.
g_device->SetRenderState(D3DRS_FOGENABLE, FALSE);
break;
case 49: // press key "1", enable linear fog.
g_device->SetRenderState(D3DRS_FOGENABLE, TRUE);
g_device->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
g_device->SetRenderState(D3DRS_FOGSTART, *(DWORD*)&fog_start);
g_device->SetRenderState(D3DRS_FOGEND, *(DWORD*)&fog_end);
break;
case 50: // press key "2", enable exp fog.
g_device->SetRenderState(D3DRS_FOGENABLE, TRUE);
g_device->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_EXP);
g_device->SetRenderState(D3DRS_FOGDENSITY, *(DWORD*)&fog_density);
break;
case 51: // press key "3", enable exp2 fog.
g_device->SetRenderState(D3DRS_FOGENABLE, TRUE);
g_device->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_EXP2);
g_device->SetRenderState(D3DRS_FOGDENSITY, *(DWORD*)&fog_density);
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;
}
下载示例工程