Water.cpp
资源名称:3DRPG.rar [点击查看]
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:13k
源码类别:
游戏
开发平台:
Visual C++
- #include "stdafx.h"
- #include "Water.h"
- CWater::CWater(HINSTANCE hinst,LPDIRECT3DDEVICE9 d3dd,DWORD row,DWORD col)
- {
- m_pD3DDevice=d3dd;
- dwRow=row;
- dwCol=col/2;
- m_fcell=30;
- //m_fHeight=800;
- m_hInst=hinst;
- SetTexture();
- CreateVertex();
- }
- CWater::~CWater()
- {
- m_pIbRiver->Release();
- m_pTxRiver->Release();
- m_pVbRiver->Release();
- //delete []m_pHeightData;
- }
- D3DXVECTOR3 CWater::GetTriangeNormal(D3DXVECTOR3* vV1, D3DXVECTOR3* vV2, D3DXVECTOR3* vV3)
- {
- D3DXVECTOR3 vNormal;
- D3DXVECTOR3 v1;
- D3DXVECTOR3 v2;
- D3DXVec3Subtract(&v1, vV2, vV1);
- D3DXVec3Subtract(&v2, vV3, vV1);
- D3DXVec3Cross(&vNormal, &v1, &v2);
- D3DXVec3Normalize(&vNormal, &vNormal);
- return vNormal;
- }
- void CWater::CreateVertex()
- {
- VERTEX * VertexBuffer;
- DWORD * IndexBuffer;
- D3DXVECTOR3 vNormal;
- DWORD Vertex1;
- DWORD Vertex2;
- DWORD Vertex3;
- DWORD NumOfIndex;
- WORD * NumOfShareFace=new WORD[dwRow*dwCol];
- D3DVECTOR *SumOfVertexNormal=new D3DVECTOR[dwRow*dwCol];
- /*BITMAP bmp;
- HBITMAP hBmp;
- HBITMAP oldBmp;
- HDC hDC;
- hDC=CreateCompatibleDC(NULL);
- hBmp=(HBITMAP)LoadImage(m_hInst,"./res/map.bmp",IMAGE_BITMAP,m_dwRow,m_dwCol,LR_LOADFROMFILE);
- oldBmp=(HBITMAP)SelectObject(hDC,hBmp);
- GetObject(hBmp,sizeof(bmp),&bmp);
- m_dwRow=bmp.bmWidth;
- m_dwCol=bmp.bmHeight;
- m_pHeightData=new BYTE [m_dwRow*m_dwCol];
- DWORD color;
- for(DWORD i=0;i<m_dwRow;i++)
- {
- for(DWORD j=0;j<m_dwCol;j++)
- {
- color=GetPixel(hDC,j,i);
- m_pHeightData[i*m_dwCol+j]=*(BYTE*)&color;
- }
- }
- WORD * NumOfShareFace=new WORD[m_dwRow*m_dwCol];
- D3DVECTOR *SumOfVertexNormal=new D3DVECTOR[m_dwRow*m_dwCol];
- //delete oldBmp;
- //delete hDC;
- //delete hBmp;
- NumOfIndex=(m_dwRow-1)*(m_dwCol-1)*6;
- m_pD3DDevice->CreateIndexBuffer(NumOfIndex*sizeof(DWORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX32,D3DPOOL_MANAGED,&m_pIb,NULL);
- m_pIb->Lock(0,0,(void **)&IndexBuffer,0);//(m_dwRow-1)*(m_dwCol-1)*6*sizeof(DWORD)
- DWORD k=0,a=0,b=0,c=0;
- for(DWORD i=0;i<m_dwRow-1;i++)
- {
- for(DWORD j=0;j<m_dwCol-1;j++)
- { a=i*(m_dwCol)+j;
- b=(i+1)*(m_dwCol)+j;
- c=i*(m_dwCol)+j+1;
- IndexBuffer[k+0]=a;
- IndexBuffer[k+1]=b;
- IndexBuffer[k+2]=c;
- IndexBuffer[k+3]=c;
- IndexBuffer[k+4]=b;
- IndexBuffer[k+5]=b+1;
- k+=6;
- }
- }
- m_pIb->Unlock();
- float tu=0,tv=-0.5;
- m_pD3DDevice->CreateVertexBuffer(m_dwRow*m_dwCol*sizeof(VERTEX),D3DUSAGE_SOFTWAREPROCESSING,D3DFVF_VERTEX,D3DPOOL_DEFAULT,&m_pVb,NULL);
- m_pVb->Lock(0,m_dwRow*m_dwCol*sizeof(VERTEX),(void **)&VertexBuffer,D3DLOCK_DISCARD);
- for(DWORD i=0;i<m_dwRow*m_dwCol;i++)
- {
- NumOfShareFace[i]=0;
- SumOfVertexNormal[i]=D3DXVECTOR3(0,0,0);
- VertexBuffer[i].y=m_pHeightData[i]/255.0f*m_fHeight;
- VertexBuffer[i].x=m_fcell*(i%m_dwCol);
- VertexBuffer[i].z=m_fcell*(i/m_dwCol);
- tu=((tu+0.05f>1.0f)?(-1.0f):(tu+0.05f)); //tu=-1~1,-1~1,......
- if(i%m_dwCol==0)
- {
- tv=((tv+0.05f>1.0f)?(-1.0f):(tv+0.05f));
- tu=0;
- }
- VertexBuffer[i].tu=tu>0?tu:-tu;
- VertexBuffer[i].tv=tv>0?tv:-tv;
- }
- for(DWORD i=0;i<NumOfIndex;i+=3)
- {
- Vertex1=IndexBuffer[i];
- Vertex2=IndexBuffer[i+1];
- Vertex3=IndexBuffer[i+2];
- vNormal=GetTriangeNormal(&D3DXVECTOR3(VertexBuffer[Vertex1].x,VertexBuffer[Vertex1].y,VertexBuffer[Vertex1].z),
- &D3DXVECTOR3(VertexBuffer[Vertex2].x,VertexBuffer[Vertex2].y,VertexBuffer[Vertex2].z),
- &D3DXVECTOR3(VertexBuffer[Vertex3].x,VertexBuffer[Vertex3].y,VertexBuffer[Vertex3].z));
- NumOfShareFace[Vertex1]++;
- NumOfShareFace[Vertex2]++;
- NumOfShareFace[Vertex3]++;
- SumOfVertexNormal[Vertex1].x+=vNormal.x;
- SumOfVertexNormal[Vertex1].y+=vNormal.y;
- SumOfVertexNormal[Vertex1].z+=vNormal.z;
- SumOfVertexNormal[Vertex2].x+=vNormal.x;
- SumOfVertexNormal[Vertex2].y+=vNormal.y;
- SumOfVertexNormal[Vertex2].z+=vNormal.z;
- SumOfVertexNormal[Vertex3].x+=vNormal.x;
- SumOfVertexNormal[Vertex3].y+=vNormal.y;
- SumOfVertexNormal[Vertex3].z+=vNormal.z;
- }
- for(DWORD i=0;i<(m_dwCol*m_dwRow);i++)
- {
- vNormal.x=SumOfVertexNormal[i].x/NumOfShareFace[i];
- vNormal.y=SumOfVertexNormal[i].y/NumOfShareFace[i];
- vNormal.z=SumOfVertexNormal[i].z/NumOfShareFace[i];
- D3DXVec3Normalize(&vNormal,&vNormal);
- VertexBuffer[i].nx=vNormal.x;
- VertexBuffer[i].ny=vNormal.y;
- VertexBuffer[i].nz=vNormal.z;
- }
- m_pVb->Unlock();*/
- /*int x=250;
- int z=200;
- y=165.0f;
- m_pD3DDevice->CreateVertexBuffer(4*sizeof(VERTEX),D3DUSAGE_DYNAMIC,D3DFVF_VERTEX,D3DPOOL_DEFAULT,&m_pVbRiver,NULL);
- m_pVbRiver->Lock(0,4*sizeof(VERTEX),(void **)&VertexBuffer,D3DLOCK_DISCARD);
- VertexBuffer[0].x=x;
- VertexBuffer[0].y=y;
- VertexBuffer[0].z=z;
- VertexBuffer[0].tu=0.0f;
- VertexBuffer[0].tv=0.0f;
- VertexBuffer[2].x=x+2700.0f;
- VertexBuffer[2].y=y;
- VertexBuffer[2].z=z;
- VertexBuffer[2].tu=1.0f;
- VertexBuffer[2].tv=0.0f;
- VertexBuffer[1].x=x;
- VertexBuffer[1].y=y;
- VertexBuffer[1].z=z+3700.0f;
- VertexBuffer[1].tu=0.0f;
- VertexBuffer[1].tv=1.0f;
- VertexBuffer[3].x=x+2700.0f;
- VertexBuffer[3].y=y;
- VertexBuffer[3].z=z+3700.0f;
- VertexBuffer[3].tu=1.0f;
- VertexBuffer[3].tv=1.0f;
- for(int i=0;i<4;i++)
- {
- VertexBuffer[i].nx=0.0f;
- VertexBuffer[i].ny=0.0f;
- VertexBuffer[i].nz=0.0f;
- }
- vNormal=GetTriangeNormal(&D3DXVECTOR3(VertexBuffer[0].x,VertexBuffer[0].y,VertexBuffer[0].z),
- &D3DXVECTOR3(VertexBuffer[1].x,VertexBuffer[1].y,VertexBuffer[1].z),
- &D3DXVECTOR3(VertexBuffer[2].x,VertexBuffer[2].y,VertexBuffer[2].z));
- for(int i=0;i<3;i++)
- {
- VertexBuffer[i].nx+=vNormal.x;
- VertexBuffer[i].ny+=vNormal.y;
- VertexBuffer[i].nz+=vNormal.z;
- }
- vNormal=GetTriangeNormal(&D3DXVECTOR3(VertexBuffer[1].x,VertexBuffer[1].y,VertexBuffer[1].z),
- &D3DXVECTOR3(VertexBuffer[2].x,VertexBuffer[2].y,VertexBuffer[2].z),
- &D3DXVECTOR3(VertexBuffer[3].x,VertexBuffer[3].y,VertexBuffer[3].z));
- for(int i=1;i<4;i++)
- {
- VertexBuffer[i].nx+=vNormal.x;
- VertexBuffer[i].ny+=vNormal.y;
- VertexBuffer[i].nz+=vNormal.z;
- }
- VertexBuffer[1].nx/=2;
- VertexBuffer[1].ny/=2;
- VertexBuffer[1].nz/=2;
- VertexBuffer[2].nx/=2;
- VertexBuffer[2].ny/=2;
- VertexBuffer[2].nz/=2;
- for(int i=0;i<4;i++)
- {
- D3DXVec3Normalize(&D3DXVECTOR3(VertexBuffer[i].nx,VertexBuffer[i].ny,VertexBuffer[i].nz),&D3DXVECTOR3(VertexBuffer[i].nx,VertexBuffer[i].ny,VertexBuffer[i].nz));
- }
- m_pVbRiver->Unlock();*/
- //dwRow=m_dwRow;//每行的顶点数
- //dwCol=m_dwCol/2;//每列的顶点数
- //NumOfShareFace=new WORD[dwRow*dwCol];
- //SumOfVertexNormal=new D3DVECTOR[dwRow*dwCol];
- y=165.0f;
- NumOfIndex=(dwRow-1)*(dwCol-1)*6;
- m_pD3DDevice->CreateIndexBuffer(NumOfIndex*sizeof(DWORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX32,D3DPOOL_MANAGED,&m_pIbRiver,NULL);
- m_pIbRiver->Lock(0,0,(void **)&IndexBuffer,0);//(m_dwRow-1)*(m_dwCol-1)*6*sizeof(DWORD)
- DWORD k=0,a=0,b=0,c=0;
- for(DWORD i=0;i<dwRow-1;i++)
- {
- for(DWORD j=0;j<dwCol-1;j++)
- { a=i*(dwCol)+j;
- b=(i+1)*(dwCol)+j;
- c=i*(dwCol)+j+1;
- IndexBuffer[k+0]=a;
- IndexBuffer[k+1]=b;
- IndexBuffer[k+2]=c;
- IndexBuffer[k+3]=c;
- IndexBuffer[k+4]=b;
- IndexBuffer[k+5]=b+1;
- k+=6;
- }
- }
- m_pIbRiver->Unlock();
- float tu=0,tv=-0.5;
- m_pD3DDevice->CreateVertexBuffer(dwRow*dwCol*sizeof(VERTEX),D3DUSAGE_SOFTWAREPROCESSING,D3DFVF_VERTEX,D3DPOOL_DEFAULT,&m_pVbRiver,NULL);
- m_pVbRiver->Lock(0,dwRow*dwCol*sizeof(VERTEX),(void **)&VertexBuffer,D3DLOCK_DISCARD);
- bool flag=true;
- for(DWORD i=0;i<dwRow*dwCol;i++)
- {
- NumOfShareFace[i]=0;
- SumOfVertexNormal[i]=D3DXVECTOR3(0,0,0);
- VertexBuffer[i].y=y;
- VertexBuffer[i].x=m_fcell*(i%dwCol);
- VertexBuffer[i].z=m_fcell*(i/dwCol);
- tu=((tu+0.025f>1.0f)?(-1.0f):(tu+0.025f)); //tu=-1~1,-1~1,......
- if(i%dwCol==0)
- {
- tv=((tv+0.025f>1.0f)?(-1.0f):(tv+0.025f));
- tu=0;
- }
- VertexBuffer[i].tu=tu>0?tu:-tu;
- VertexBuffer[i].tv=tv>0?tv:-tv;
- VertexBuffer[i].tvv=tv;
- }
- for(DWORD i=0;i<NumOfIndex;i+=3)
- {
- Vertex1=IndexBuffer[i];
- Vertex2=IndexBuffer[i+1];
- Vertex3=IndexBuffer[i+2];
- vNormal=GetTriangeNormal(&D3DXVECTOR3(VertexBuffer[Vertex1].x,VertexBuffer[Vertex1].y,VertexBuffer[Vertex1].z),
- &D3DXVECTOR3(VertexBuffer[Vertex2].x,VertexBuffer[Vertex2].y,VertexBuffer[Vertex2].z),
- &D3DXVECTOR3(VertexBuffer[Vertex3].x,VertexBuffer[Vertex3].y,VertexBuffer[Vertex3].z));
- NumOfShareFace[Vertex1]++;
- NumOfShareFace[Vertex2]++;
- NumOfShareFace[Vertex3]++;
- SumOfVertexNormal[Vertex1].x+=vNormal.x;
- SumOfVertexNormal[Vertex1].y+=vNormal.y;
- SumOfVertexNormal[Vertex1].z+=vNormal.z;
- SumOfVertexNormal[Vertex2].x+=vNormal.x;
- SumOfVertexNormal[Vertex2].y+=vNormal.y;
- SumOfVertexNormal[Vertex2].z+=vNormal.z;
- SumOfVertexNormal[Vertex3].x+=vNormal.x;
- SumOfVertexNormal[Vertex3].y+=vNormal.y;
- SumOfVertexNormal[Vertex3].z+=vNormal.z;
- }
- for(DWORD i=0;i<(dwCol*dwRow);i++)
- {
- vNormal.x=SumOfVertexNormal[i].x/NumOfShareFace[i];
- vNormal.y=SumOfVertexNormal[i].y/NumOfShareFace[i];
- vNormal.z=SumOfVertexNormal[i].z/NumOfShareFace[i];
- D3DXVec3Normalize(&vNormal,&vNormal);
- VertexBuffer[i].nx=vNormal.x;
- VertexBuffer[i].ny=vNormal.y;
- VertexBuffer[i].nz=vNormal.z;
- }
- m_pVbRiver->Unlock();
- }
- void CWater::SetTexture()
- {
- //D3DXCreateTextureFromFile(m_pD3DDevice,"./res/floor.jpg",&m_pTx);
- D3DXCreateTextureFromFile(m_pD3DDevice,"./res/water.tga",&m_pTxRiver);
- }
- void CWater::RenerWater(float Vy)
- { static int kkk=0;
- VERTEX * VertexBuffer;
- SetMaterial();
- /* m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);
- // m_pD3DDevice->SetRenderState( D3DRS_ZWRITEENABLE,FALSE);
- m_pD3DDevice->SetTexture(0,m_pTx);
- m_pD3DDevice->SetStreamSource(0,m_pVb,0,sizeof(VERTEX));
- m_pD3DDevice->SetFVF(D3DFVF_VERTEX);
- m_pD3DDevice->SetIndices(m_pIb);
- m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,m_dwRow*m_dwCol,0,(m_dwRow-1)*(m_dwCol-1)*2);*/
- /* ZeroMemory(&material,sizeof(material));
- material.Specular.r=0.6f;
- material.Specular.g=0.6f;
- material.Specular.b=0.6f;
- material.Specular.a=0.5f;
- m_pD3DDevice->SetMaterial(&material);*/
- m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
- m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
- m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
- if(Vy<=y)
- m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CW);
- else
- m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
- m_pD3DDevice->SetTexture(0,m_pTxRiver);
- m_pD3DDevice->SetStreamSource(0,m_pVbRiver,0,sizeof(VERTEX));
- m_pD3DDevice->SetFVF(D3DFVF_VERTEX);
- m_pVbRiver->Lock(0,dwRow*dwCol*sizeof(VERTEX),(void **)&VertexBuffer,D3DLOCK_DISCARD);
- if(kkk==4)
- {
- for(int i=0;i<dwRow*dwCol;i++)
- {
- VertexBuffer[i].tvv=((VertexBuffer[i].tvv-0.025f<-1.0f)?(1.0f):(VertexBuffer[i].tvv-0.025f));
- VertexBuffer[i].tv=VertexBuffer[i].tvv>0?VertexBuffer[i].tvv:-VertexBuffer[i].tvv;
- }
- kkk=0;
- }
- kkk++;
- m_pVbRiver->Unlock();
- m_pD3DDevice->SetIndices(m_pIbRiver);
- m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,dwRow*dwCol,0,(dwRow-1)*(dwCol-1)*2);
- // m_pD3DDevice->SetRenderState( D3DRS_CLIPPLANEENABLE ,TRUE);
- m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
- //m_pD3DDevice->SetRenderState( D3DRS_ZWRITEENABLE,TRUE);
- }
- void CWater::Translate()
- {
- D3DXMATRIX matWorld;
- D3DXMatrixTranslation(&matWorld,0.0f,0.0f,0.0f);
- m_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
- }
- /*float CWater::GetHeight(float x,float z)
- {
- //x+=m_fcell*m_dwCol;
- //z+=m_fcell*m_dwRow;
- float rx,rz;
- rx=x/m_fcell;
- rz=z/m_fcell;
- long col1,row1,col2,row2;
- float h1,h2,h3,h4,ha,hb;
- col1=DWORD(rx);
- row1=DWORD(rz);
- if(col1>m_dwCol || col1<0 ||row1>m_dwRow || row1<0) return 0;
- else
- { //双线性插值法计算
- col2=(col1==m_dwCol)?col1:col1+1;
- row2=(row1==m_dwRow)?row1:row1+1;
- h1=(m_pHeightData[row1*m_dwCol+col1]*m_fHeight/255.0f);
- h2=(m_pHeightData[row1*m_dwCol+col2]*m_fHeight/255.0f);
- h3=(m_pHeightData[row2*m_dwCol+col1]*m_fHeight/255.0f);
- h4=(m_pHeightData[row2*m_dwCol+col2]*m_fHeight/255.0f);
- ha=(h2*(rx-col1)+h1*(col2-rx));
- hb=(h4*(rx-col1)+h3*(col2-rx));
- return (hb*(rz-row1)+ha*(row2-rz));
- }
- }*/
- void CWater::SetMaterial()
- {
- ZeroMemory(&material,sizeof(material));
- material.Diffuse.r=material.Ambient.r=1.0f;
- material.Diffuse.g=material.Ambient.g=1.0f;
- material.Diffuse.b=material.Ambient.b=1.0f;
- material.Diffuse.a=material.Ambient.a=1.0f;
- material.Specular.r=0.0f;
- material.Specular.g=0.0f;
- material.Specular.b=0.0f;
- material.Specular.a=0.0f;
- material.Emissive.r=0.0f;
- material.Emissive.g=0.0f;
- material.Emissive.b=0.0f;
- material.Emissive.a=0.0f;
- m_pD3DDevice->SetMaterial(&material);
- }