#include ".\water.h"
CWater::CWater(LPDIRECT3DDEVICE9 pD3DDevice)
{
m_pd3dDevice = pD3DDevice;
m_pBufferWater = NULL;
m_num = 0.0f;
}
CWater::~CWater(void)
{
}
HRESULT CWater::InitVB()
{
if(FAILED(m_pd3dDevice->CreateVertexBuffer(4*sizeof(WATERVERTEX)
,0
,D3DFVF_WATERVERTEX
,D3DPOOL_DEFAULT
,&m_pBufferWater
,NULL)))
{
return E_FAIL;
}
WATERVERTEX* pVertices;
if(FAILED(m_pBufferWater->Lock(0, 4*sizeof(WATERVERTEX),(void**)&pVertices,
0)))
{
return E_FAIL;
}
//初始化地面的顶点缓冲区
pVertices->position = D3DXVECTOR3(-12.0f, 0.0f, -12.0f);
pVertices->color = D3DCOLOR_ARGB(0,255,0,0);
pVertices->tu = 0.0f;
pVertices->tv = 20.0f;
pVertices->tu1 = 0.0f;
pVertices->tv1 = 20.0f;
pVertices++;
pVertices->position = D3DXVECTOR3(-12.0f, 0.0f, 10.0f);
pVertices->color = D3DCOLOR_ARGB(0,255,0,0);
pVertices->tu = 0.0f;
pVertices->tv = 0.0f;
pVertices->tu1 = 0.0f;
pVertices->tv1 = 0.0f;
pVertices++;
pVertices->position = D3DXVECTOR3(10.0f, 0.0f, -12.0f);
pVertices->color = D3DCOLOR_ARGB(0,255,0,0);
pVertices->tu = 20.0f;
pVertices->tv = 20.0f;
pVertices->tu1 = 20.0f;
pVertices->tv1 = 20.0f;
pVertices++;
pVertices->position = D3DXVECTOR3(10.0f, 0.0f, 10.0f);
pVertices->color = D3DCOLOR_ARGB(0,255,0,0);
pVertices->tu = 20.0f;
pVertices->tv = 0.0f;
pVertices->tu1 = 20.0f;
pVertices->tv1 = 0.0f;
pVertices++;
m_pBufferWater->Unlock();
return S_OK;
}
void CWater::SetCamera()
{
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &m_Camera.g_vPos,
&m_Camera.g_vViewAt,
&m_Camera.g_vUp);
m_pd3dDevice->SetTransform(D3DTS_VIEW,&matView);
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/2, 1.0f,1.0f,100.0f);
m_pd3dDevice->SetTransform(D3DTS_PROJECTION,&matProj);
}
bool CWater::SetTexture(const char *FileTexture, int flag)
{
if(FAILED(D3DXCreateTextureFromFile(m_pd3dDevice, FileTexture,
&m_pTexture[flag])))
{
return false;
}
return true;
}
void CWater::SetLight()
{
//设置光源属性
D3DLIGHT9 light;
ZeroMemory(&light,sizeof(D3DLIGHT9));
light.Type=D3DLIGHT_POINT;
light.Ambient.r=0.1;
light.Ambient.b=0.1;
light.Ambient.g=0.1;
light.Diffuse.r=1.0;
light.Diffuse.g=1.0;
light.Diffuse.b=1.0;
light.Specular.r=0.0;
light.Specular.g=0.0;
light.Specular.b=0.0;
light.Position.x=0.0;
light.Position.y=10.0;
light.Position.z=0.0;
light.Range = 50.0f;
light.Attenuation0=1;
light.Attenuation1=0;
light.Attenuation2=0;
m_pd3dDevice->SetLight(0,&light);
m_pd3dDevice->LightEnable(0,TRUE);
m_pd3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
m_pd3dDevice->SetRenderState(D3DRS_AMBIENT,D3DCOLOR_XRGB(64, 64, 64));
}
void CWater::SetMaterial()
{
D3DMATERIAL9 mtrl;
mtrl.Diffuse.r = mtrl.Ambient.r = 0.0f;
mtrl.Diffuse.g = mtrl.Ambient.g = 0.5f;
mtrl.Diffuse.b = mtrl.Ambient.b = 1.0f;
mtrl.Diffuse.a = mtrl.Ambient.a = 0.3f;
;
m_pd3dDevice->SetMaterial(&mtrl);
}
void CWater::Render()
{
m_Camera.ProcInput();
SetCamera();
SetLight();
SetMaterial();
float TimeFactor = (float)(timeGetTime() / 1000.0f);
D3DXMATRIX matTexture;
D3DXMatrixTranslation(&matTexture, 0.0f, -TimeFactor, 0.0f);
//因为是二维纹理的变换所以实现。所以要把4*4的矩阵转换成
//3*3的矩阵
Matrix4x4To3x3(&matTexture, &matTexture);
//平移纹理坐标,自动达到卷轴的效果
m_pd3dDevice->SetTransform(D3DTS_TEXTURE0, &matTexture);
//以2维纹理的形式输出
m_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_COUNT2);
m_pd3dDevice->SetTexture(0,m_pTexture[0]);
m_pd3dDevice->SetTextureStageState(0,D3DTSS_TEXCOORDINDEX,0);
m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,false);
//多层纹理混合
D3DCAPS9 caps;
m_pd3dDevice->GetDeviceCaps(&caps);
int num = caps.MaxTextureBlendStages;
if(num > 1)
{
m_pd3dDevice->SetTexture(1,m_pTexture[1]);
m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_SUBTRACT);
m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLORARG1,D3DTA_TEXTURE);
m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLORARG2,D3DTA_CURRENT);
m_pd3dDevice->SetTextureStageState(1,D3DTSS_TEXCOORDINDEX,1);
D3DXMatrixTranslation(&matTexture, 0.0f, TimeFactor, 0.0f);
Matrix4x4To3x3(&matTexture, &matTexture);
m_pd3dDevice->SetTransform(D3DTS_TEXTURE1, &matTexture);
m_pd3dDevice->SetTextureStageState(1,
D3DTSS_TEXTURETRANSFORMFLAGS,
D3DTTFF_COUNT2);
}
////对第二层的纹理进行 alpha的混合。 用纹理图中的alpha通道
m_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
m_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_TEXTURE);
m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
m_pd3dDevice->SetStreamSource(0, m_pBufferWater,0,sizeof(WATERVERTEX));
m_pd3dDevice->SetFVF(D3DFVF_WATERVERTEX);
m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
// Turn off texture transformations
m_pd3dDevice->SetTextureStageState(0,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_DISABLE);
}
void CWater::Matrix4x4To3x3(D3DXMATRIX *matOut, D3DXMATRIX *matIn)
{
matOut->_11 = matIn->_11; // Copy over 1st row
matOut->_12 = matIn->_12;
matOut->_13 = matIn->_13;
matOut->_14 = 0.0f;
matOut->_21 = matIn->_21; // Copy over 2nd row
matOut->_22 = matIn->_22;
matOut->_23 = matIn->_23;
matOut->_24 = 0.0f;
matOut->_31 = matIn->_41; // Copy bottom row
matOut->_32 = matIn->_42; // used for translation
matOut->_33 = matIn->_43;
matOut->_34 = 0.0f;
matOut->_41 = 0.0f; // Clear the bottom row
matOut->_42 = 0.0f;
matOut->_43 = 0.0f;
matOut->_44 = 1.0f;
}