d3d中实现简单的水
1. 最简单的一张带alpha的贴图直接uv动画
2. 另一种是使用序列贴图, 即水的动画贴图,可在每个自定义大小的格子播放相同的一帧图片来实现.
![tt](http://byfiles.storage.live.com/y1pQyiAAjFojXL1UAbjX5-iMIMUoMSvqRpNfC4gJDxATWuGkZ1HPrq7ecLvx5WFXL30Cba6vqm5Rn4)
3. 反射水面:
上述其中一种方法再加上带bump map效果的反射图
下面给出最后一种具体例子:
用d3d固定管道实现bump mapping振动/移动的水:
(注意:该例子只用D3DTOP_BUMPENVMAP没用D3DTOP_BUMPENVMAPLUMINANCE):
// 混合状态:
m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
// 贴图基台0:
// 贴图因子(见后面的alpha参数D3DTA_TFACTOR):
m_pD3DDevice->SetRenderState(D3DRS_TEXTUREFACTOR, 0x90000000);
// 设置Bump map: 就是一些uv偏移值,保存在bump贴图中(还可以保存一个附加的光照值)
m_pD3DDevice->SetTexture(0, m_pTexBump);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_BUMPENVMAP);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTA_DIFFUSE);//D3DTOP_DISABLE);
// bump矩阵(参见上面msdn链接说明):
m_pD3DDevice->SetTextureStageState(0, D3DTSS_BUMPENVMAT00, F2DW(0.05f));
m_pD3DDevice->SetTextureStageState(0, D3DTSS_BUMPENVMAT01, F2DW(0.0f));
m_pD3DDevice->SetTextureStageState(0, D3DTSS_BUMPENVMAT10, F2DW(0.0f));
m_pD3DDevice->SetTextureStageState(0, D3DTSS_BUMPENVMAT11, F2DW(0.05f));
m_pD3DDevice->SetTextureStageState(0, D3DTSS_BUMPENVLSCALE, F2DW(1.0f));
m_pD3DDevice->SetTextureStageState(0, D3DTSS_BUMPENVLOFFSET, F2DW(0.0f));
// uv矩阵(uv动画用):
m_pD3DDevice->SetTransform(D3DTS_TEXTURE0, &m_MatTransEau);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
// 贴图基台1:
// 反射图(可实时计算):
// Etage 1 : affichage de la texture de r閒lexion
m_pD3DDevice->SetTexture(1, m_pEnvMiroir->GetMiroir());
//
m_pD3DDevice->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
m_pD3DDevice->SetTextureStageState (1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pD3DDevice->SetTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
m_pD3DDevice->SetTextureStageState (1, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
// uv矩阵(投影矩阵,将贴图映射到水面用):
m_pD3DDevice->SetTransform(D3DTS_TEXTURE1, &m_pScene->GetCameraCourante()->GetMatriceProjectionTexture());
m_pD3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_PROJECTED |D3DTTFF_COUNT3);
m_pD3DDevice->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
m_pD3DDevice->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
// set fvf, vb 和 ib:
m_pD3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
m_pD3DDevice->SetStreamSource(0, m_pVB, 0, sizeof(CUSTOMVERTEX));
m_pD3DDevice->SetIndices(m_pIB);
// draw:
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, NB_VERTEX_EAU, 0, NB_TRIANGLE_EAU);
提示:
上述的bump矩阵只是简单的偏移,实际的uv动画使用了另一个贴图uv矩阵(每帧更新,使得uv移动)来设置的,
另外一个更好技巧是可以使用bump矩阵直接应用sin函数,这样会使用得水波有一定的振动,效果挺好:
D3DXMATRIX matBumpMat;
const static DWORD startick(GetTickCount());
DWORD dwtime=GetTickCount()-startick;
float time=(float)dwtime/1000;
FLOAT r = 0.005f;
matBumpMat._11 = r*cosf(time*8.0f);
matBumpMat._12 = r*sinf(time*8.0f);
matBumpMat._21 = r*sinf(time*8.0f);
matBumpMat._22 =-r*cosf(time*8.0f);
m_pD3DDevice->SetTextureStageState(0,D3DTSS_BUMPENVMAT00,F2DW(matBumpMat._11));
m_pD3DDevice->SetTextureStageState(0,D3DTSS_BUMPENVMAT01,F2DW(matBumpMat._12));
m_pD3DDevice->SetTextureStageState(0,D3DTSS_BUMPENVMAT10,F2DW(matBumpMat._21));
m_pD3DDevice->SetTextureStageState(0,D3DTSS_BUMPENVMAT11,F2DW(matBumpMat._22));
上述方法需要检测是否支持 D3DTEXOPCAPS_BUMPENVMAP
关于bump mapping参见msdn:
1. Bump Mapping (Direct3D 9)
http://msdn2.microsoft.com/en-us/library/bb172379.aspx
2. Bump Mapping Formulas (Direct3D 9)
http://msdn2.microsoft.com/en-us/library/bb172380.aspx
3. Bump Map Pixel Formats (Direct3D 9)
http://msdn2.microsoft.com/en-us/library/bb172381.aspx
4.Using Bump Mapping (Direct3D 9)
http://msdn2.microsoft.com/en-us/library/bb206304.aspx
原理参见第2点,使用方法参见第4点
附:
关于bumpmap图是可以直接程序创建的,下面给出该方法的代码(这是从 盖小房(BYOH)source code中看到的,直接贴这里可参考):
LPDIRECT3DTEXTURE9 CreateBumpMap(LPDIRECT3DDEVICE9 device, DWORD dwWidth, DWORD dwHeight,D3DFORMAT d3dBumpFormat )
{
LPDIRECT3DTEXTURE9 psBumpMap;
// Create the bump map texture
if( FAILED( device->CreateTexture( dwWidth, dwHeight, 1, 0/* Usage */,
d3dBumpFormat, D3DPOOL_MANAGED,&psBumpMap,NULL ) ) )
return NULL;
// Lock the surface and write in some bumps for the waves
D3DLOCKED_RECT d3dlr;
psBumpMap->LockRect( 0, &d3dlr, 0, 0 );
CHAR* pDst = (CHAR*)d3dlr.pBits;
CHAR iDu=0, iDv=0;
for( DWORD y=0; y<dwHeight; y++ )
{
CHAR* pPixel = pDst;
for( DWORD x=0; x<dwWidth; x++ )
{
FLOAT fx = x/(FLOAT)dwWidth - 0.5f;
FLOAT fy = y/(FLOAT)dwHeight - 0.5f;
FLOAT r = sqrtf( fx*fx + fy*fy );
// iDu = (CHAR)( 32 * cosf( 300.0f * r ) * expf( -r * 5.0f ) );
iDu += (CHAR)( 128 * cosf( 100.0f * ( fx + fy ) ) );
// iDu += (CHAR)( 16 * cosf( 140.0f * ( fx * 0.85f - fy ) ) );
// iDv = (CHAR)( 32 * sinf( 300.0f * r ) * expf( -r * 5.0f ) );
iDv += (CHAR)( 128 * sinf( 100.0f * ( fx + fy ) ) );
// iDv += (CHAR)( 16 * sinf( 140.0f * ( fx * 0.85f - fy ) ) );
*pPixel++ = iDu;
*pPixel++ = iDv;
}
pDst += d3dlr.Pitch;
}
psBumpMap->UnlockRect(0);
return psBumpMap;
}