天行健 君子当自强而不息

Direct3D中的雾化(3)

基于范围雾化的示例程序

要使用基于范围的雾化,首先需要检查当前硬件是否支持基于范围的雾化:

	// check if hardware supports range fog
	D3DCAPS9 caps;
g_device->GetDeviceCaps(&caps);
	if(! (caps.RasterCaps & D3DPRASTERCAPS_FOGRANGE))
return false;

在设置雾的颜色时,可将雾的颜色和背景颜色设置为一致,这样远处的物体看起来就像逐渐融入到背景中,视觉效果比较逼真。

雾化参数中,雾的开始距离和结束距离只对线性雾化有效,雾的浓度只对指数雾化和指数平方雾化有效。

无论顶点雾化还是像素雾化,在程序中必须设置合适的投影变换矩阵确保雾化效果正确,这在为那些不使用顶点坐标变换和光照流水线的程序中添加雾化效果造成了一定的困难,不过这种情况不多。

为了比较基于范围的雾化和不基于范围的雾化效果的不同,可以按下"R"键在基于范围的雾化和不基于范围的雾化之间进行切换。

 

源程序:

#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_matrices()
{
    
// setup world matrix
    D3DXMATRIX mat_world;    
    D3DXMatrixIdentity(
&mat_world);
    g_device
->SetTransform(D3DTS_WORLD, &mat_world);

    
// setup view matrix

    D3DXVECTOR3 eye(
0.0f30.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/41.0f1.0f500.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(00, (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;
    }

    
// check if hardware supports range fog

    D3DCAPS9 caps;
    g_device
->GetDeviceCaps(&caps);

    
if(! (caps.RasterCaps & D3DPRASTERCAPS_FOGRANGE))
        
return false;
    
    
if(! init_geometry())
        
return false;

    setup_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_RANGEFOGENABLE,    TRUE);
    g_device
->SetRenderState(D3DRS_FOGCOLOR,        0xFF101010);    
    g_device
->SetRenderState(D3DRS_AMBIENT,            0xFF00BB55);
    
    
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(555), 1.0f0);

    g_device
->BeginScene();

    
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     = 200;
    
static float fog_density = 0.01f;

    
static bool use_range_fog = true;

    
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_FOGVERTEXMODE,    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_FOGVERTEXMODE,    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_FOGVERTEXMODE,    D3DFOG_EXP2);
            g_device
->SetRenderState(D3DRS_FOGDENSITY,        *(DWORD*)&fog_density);            
            
break;

        
case 65 + 'R' - 'A':
            use_range_fog 
= !use_range_fog;
            g_device
->SetRenderState(D3DRS_RANGEFOGENABLE, use_range_fog);
            
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, 200100640480,
                             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, 00, PM_REMOVE))
            {
                TranslateMessage(
&msg);
                DispatchMessage(
&msg);
            }
                
            render();
            Sleep(
10);
        }
    }

    cleanup();
    UnregisterClass(CLASS_NAME, wc.hInstance);    

    
return 0;
}

 

下载示例工程


posted on 2008-05-11 18:20 lovedday 阅读(792) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论