Direct3D雾化效果浅析

       在Direct3D 中,雾化是通过将景物颜色与雾的颜色,以随物体到观察点距离增加而衰减的混合因子混合而实现的。

       两种雾化方法:顶点雾化和像素雾化。

       三种雾化公式:线性雾化,指数雾化,指数平方雾化。

       两种雾化处理:基于深度的雾化处理和基于范围的雾化处理。基于深度是指两个点之间的深度(Z)差值,基于范围则是两点间的直线距离。Direct3D默认的是基于深度的雾化。可设置基于范围的雾化,但要先检测设备是否支持:

       g_pd3dDevice->GetDeviceCaps(&staps);

       if ( stCaps.RasterCaps & D3DPASTERCAPS_FOGRANGE )

            return TRUE;

       Direct3D默认禁用雾化效果,可以激活雾化:g_pd3dDevice->SetRendState( D3DRS_FOGENABLE, true );

       设置雾的颜色 g_pd3dDevice->SetRendState( D3DRS_FOGCOLOR, 0xffffffff); //白色

       根据按键消息进行多种雾化效果的切换

        if (gInputSystem->KeyDown(DIK_V))
        {
             //禁用雾化
             g_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,FALSE );
        }

        

        if (gInputSystem->KeyDown(DIK_M))
        {

             g_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,TRUE );
             //设置雾化混合因子计算公式 :  指数雾化
             g_pd3dDevice->GetDevice()->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_EXP);
             //设置雾的浓度

             static float fogDensity = 0.005f; 
             g_pd3dDevice->SetRenderState(D3DRS_FOGDENSITY, *(DWORD*)&fogDensity);
        }

        


        if (gInputSystem->KeyDown(DIK_N))
        {

             g_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,TRUE );

             //激活基于范围的雾化
             g_pd3dDevice->SetRenderState( D3DRS_RANGEFOGENABLE,TRUE );

             //设置雾化混合因子计算公式  :  线性雾化
             g_pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
             //设置线性雾化开始位置和结束位置
             static float fogStart = 20;
             static float fogEnd = 200;
             g_pd3dDevice->SetRenderState(D3DRS_FOGSTART,*(DWORD*)&fogStart);     

             g_pd3dDevice->SetRenderState(D3DRS_FOGEND,*(DWORD*)&fogEnd);

        }

        
        if (gInputSystem->KeyDown(DIK_B))
        {
             g_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,TRUE );
             //设置雾化混合因子计算公式 :  指数平方雾化
             g_pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_EXP2);
             //设置雾的浓度

             static float fogDensity = 0.01f;
             g_pd3dDevice->SetRenderState(D3DRS_FOGDENSITY, *(DWORD*)&fogDensity);
        }

        

        其中设置雾化混合因子计算公式时,SetRenderState的第一个参数可以是:

        D3DRS_FOGTABLEMODE(像素雾化)  或  D3DRS_FOGVERTEXMODE(顶点雾化)

posted @ 2013-07-01 12:29  雨霖林  阅读(436)  评论(0编辑  收藏  举报