Water.cpp
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:13k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "Water.h"
  3. CWater::CWater(HINSTANCE hinst,LPDIRECT3DDEVICE9 d3dd,DWORD row,DWORD col)
  4. {
  5. m_pD3DDevice=d3dd;
  6.     dwRow=row;
  7. dwCol=col/2;
  8. m_fcell=30;
  9. //m_fHeight=800;
  10. m_hInst=hinst;
  11. SetTexture();
  12. CreateVertex();
  13. }
  14. CWater::~CWater()
  15. {
  16. m_pIbRiver->Release();
  17. m_pTxRiver->Release();
  18. m_pVbRiver->Release();
  19. //delete []m_pHeightData;
  20. }
  21. D3DXVECTOR3 CWater::GetTriangeNormal(D3DXVECTOR3* vV1, D3DXVECTOR3* vV2, D3DXVECTOR3* vV3)
  22. {
  23. D3DXVECTOR3 vNormal;
  24. D3DXVECTOR3 v1;
  25. D3DXVECTOR3 v2;
  26. D3DXVec3Subtract(&v1, vV2, vV1);
  27. D3DXVec3Subtract(&v2, vV3, vV1);
  28. D3DXVec3Cross(&vNormal, &v1, &v2);
  29. D3DXVec3Normalize(&vNormal, &vNormal);
  30. return vNormal;
  31. }
  32. void CWater::CreateVertex()
  33. {   
  34. VERTEX * VertexBuffer;
  35.     DWORD * IndexBuffer;
  36. D3DXVECTOR3 vNormal;
  37. DWORD Vertex1;
  38. DWORD Vertex2;
  39. DWORD Vertex3;
  40. DWORD NumOfIndex;
  41.     WORD * NumOfShareFace=new WORD[dwRow*dwCol];
  42.     D3DVECTOR *SumOfVertexNormal=new D3DVECTOR[dwRow*dwCol];
  43. /*BITMAP bmp;
  44. HBITMAP hBmp;
  45. HBITMAP oldBmp;
  46. HDC hDC;
  47. hDC=CreateCompatibleDC(NULL);
  48. hBmp=(HBITMAP)LoadImage(m_hInst,"./res/map.bmp",IMAGE_BITMAP,m_dwRow,m_dwCol,LR_LOADFROMFILE);
  49. oldBmp=(HBITMAP)SelectObject(hDC,hBmp);
  50. GetObject(hBmp,sizeof(bmp),&bmp);
  51. m_dwRow=bmp.bmWidth;
  52. m_dwCol=bmp.bmHeight;
  53. m_pHeightData=new BYTE [m_dwRow*m_dwCol];
  54. DWORD color;
  55. for(DWORD i=0;i<m_dwRow;i++)
  56. {
  57. for(DWORD j=0;j<m_dwCol;j++)
  58. {
  59. color=GetPixel(hDC,j,i);
  60. m_pHeightData[i*m_dwCol+j]=*(BYTE*)&color;
  61. }
  62. }
  63.     WORD * NumOfShareFace=new WORD[m_dwRow*m_dwCol];
  64. D3DVECTOR *SumOfVertexNormal=new D3DVECTOR[m_dwRow*m_dwCol];
  65. //delete oldBmp;
  66. //delete hDC;
  67. //delete hBmp;
  68.    NumOfIndex=(m_dwRow-1)*(m_dwCol-1)*6;
  69.     m_pD3DDevice->CreateIndexBuffer(NumOfIndex*sizeof(DWORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX32,D3DPOOL_MANAGED,&m_pIb,NULL);
  70. m_pIb->Lock(0,0,(void **)&IndexBuffer,0);//(m_dwRow-1)*(m_dwCol-1)*6*sizeof(DWORD)
  71. DWORD k=0,a=0,b=0,c=0;
  72. for(DWORD i=0;i<m_dwRow-1;i++)
  73. {
  74. for(DWORD j=0;j<m_dwCol-1;j++)
  75. {   a=i*(m_dwCol)+j;
  76.     b=(i+1)*(m_dwCol)+j;
  77. c=i*(m_dwCol)+j+1;
  78. IndexBuffer[k+0]=a;
  79. IndexBuffer[k+1]=b;
  80. IndexBuffer[k+2]=c;
  81. IndexBuffer[k+3]=c;
  82. IndexBuffer[k+4]=b;
  83. IndexBuffer[k+5]=b+1;
  84. k+=6;
  85. }
  86. }
  87.   
  88. m_pIb->Unlock();
  89. float tu=0,tv=-0.5;
  90. m_pD3DDevice->CreateVertexBuffer(m_dwRow*m_dwCol*sizeof(VERTEX),D3DUSAGE_SOFTWAREPROCESSING,D3DFVF_VERTEX,D3DPOOL_DEFAULT,&m_pVb,NULL);
  91. m_pVb->Lock(0,m_dwRow*m_dwCol*sizeof(VERTEX),(void **)&VertexBuffer,D3DLOCK_DISCARD);
  92. for(DWORD i=0;i<m_dwRow*m_dwCol;i++)
  93. {
  94. NumOfShareFace[i]=0;
  95. SumOfVertexNormal[i]=D3DXVECTOR3(0,0,0);
  96. VertexBuffer[i].y=m_pHeightData[i]/255.0f*m_fHeight;
  97. VertexBuffer[i].x=m_fcell*(i%m_dwCol);
  98. VertexBuffer[i].z=m_fcell*(i/m_dwCol);
  99. tu=((tu+0.05f>1.0f)?(-1.0f):(tu+0.05f)); //tu=-1~1,-1~1,......
  100. if(i%m_dwCol==0) 
  101. {
  102. tv=((tv+0.05f>1.0f)?(-1.0f):(tv+0.05f));
  103. tu=0;
  104. }
  105. VertexBuffer[i].tu=tu>0?tu:-tu;
  106. VertexBuffer[i].tv=tv>0?tv:-tv;
  107. }
  108. for(DWORD i=0;i<NumOfIndex;i+=3)
  109. {
  110.      Vertex1=IndexBuffer[i];
  111.  Vertex2=IndexBuffer[i+1];
  112.  Vertex3=IndexBuffer[i+2];
  113.  vNormal=GetTriangeNormal(&D3DXVECTOR3(VertexBuffer[Vertex1].x,VertexBuffer[Vertex1].y,VertexBuffer[Vertex1].z),
  114.                       &D3DXVECTOR3(VertexBuffer[Vertex2].x,VertexBuffer[Vertex2].y,VertexBuffer[Vertex2].z),
  115.                               &D3DXVECTOR3(VertexBuffer[Vertex3].x,VertexBuffer[Vertex3].y,VertexBuffer[Vertex3].z));
  116.  NumOfShareFace[Vertex1]++;
  117.  NumOfShareFace[Vertex2]++;
  118.  NumOfShareFace[Vertex3]++;
  119.  SumOfVertexNormal[Vertex1].x+=vNormal.x;
  120.  SumOfVertexNormal[Vertex1].y+=vNormal.y;
  121.  SumOfVertexNormal[Vertex1].z+=vNormal.z;
  122.      SumOfVertexNormal[Vertex2].x+=vNormal.x;
  123.  SumOfVertexNormal[Vertex2].y+=vNormal.y;
  124.  SumOfVertexNormal[Vertex2].z+=vNormal.z;
  125.      SumOfVertexNormal[Vertex3].x+=vNormal.x;
  126.  SumOfVertexNormal[Vertex3].y+=vNormal.y;
  127.  SumOfVertexNormal[Vertex3].z+=vNormal.z;
  128. }
  129. for(DWORD i=0;i<(m_dwCol*m_dwRow);i++)
  130. {
  131. vNormal.x=SumOfVertexNormal[i].x/NumOfShareFace[i];
  132. vNormal.y=SumOfVertexNormal[i].y/NumOfShareFace[i];
  133. vNormal.z=SumOfVertexNormal[i].z/NumOfShareFace[i];
  134. D3DXVec3Normalize(&vNormal,&vNormal);
  135. VertexBuffer[i].nx=vNormal.x;
  136.         VertexBuffer[i].ny=vNormal.y;
  137. VertexBuffer[i].nz=vNormal.z;
  138. }
  139.    
  140. m_pVb->Unlock();*/
  141. /*int x=250;
  142. int z=200;
  143. y=165.0f;
  144. m_pD3DDevice->CreateVertexBuffer(4*sizeof(VERTEX),D3DUSAGE_DYNAMIC,D3DFVF_VERTEX,D3DPOOL_DEFAULT,&m_pVbRiver,NULL);
  145. m_pVbRiver->Lock(0,4*sizeof(VERTEX),(void **)&VertexBuffer,D3DLOCK_DISCARD);
  146. VertexBuffer[0].x=x;
  147. VertexBuffer[0].y=y;
  148. VertexBuffer[0].z=z;
  149. VertexBuffer[0].tu=0.0f;
  150. VertexBuffer[0].tv=0.0f;
  151.     
  152.     VertexBuffer[2].x=x+2700.0f;
  153. VertexBuffer[2].y=y;
  154. VertexBuffer[2].z=z;
  155. VertexBuffer[2].tu=1.0f;
  156. VertexBuffer[2].tv=0.0f;
  157.     
  158.     VertexBuffer[1].x=x;
  159. VertexBuffer[1].y=y;
  160. VertexBuffer[1].z=z+3700.0f;
  161. VertexBuffer[1].tu=0.0f;
  162. VertexBuffer[1].tv=1.0f;
  163. VertexBuffer[3].x=x+2700.0f;
  164. VertexBuffer[3].y=y;
  165. VertexBuffer[3].z=z+3700.0f;
  166. VertexBuffer[3].tu=1.0f;
  167. VertexBuffer[3].tv=1.0f;
  168.     for(int i=0;i<4;i++)
  169. {
  170. VertexBuffer[i].nx=0.0f;
  171. VertexBuffer[i].ny=0.0f;
  172.         VertexBuffer[i].nz=0.0f;
  173. }
  174.    
  175.  vNormal=GetTriangeNormal(&D3DXVECTOR3(VertexBuffer[0].x,VertexBuffer[0].y,VertexBuffer[0].z),
  176.                       &D3DXVECTOR3(VertexBuffer[1].x,VertexBuffer[1].y,VertexBuffer[1].z),
  177.                               &D3DXVECTOR3(VertexBuffer[2].x,VertexBuffer[2].y,VertexBuffer[2].z));
  178.  for(int i=0;i<3;i++)
  179.  {
  180.  VertexBuffer[i].nx+=vNormal.x;
  181.  VertexBuffer[i].ny+=vNormal.y;
  182.  VertexBuffer[i].nz+=vNormal.z;
  183.  }
  184.       vNormal=GetTriangeNormal(&D3DXVECTOR3(VertexBuffer[1].x,VertexBuffer[1].y,VertexBuffer[1].z),
  185.                       &D3DXVECTOR3(VertexBuffer[2].x,VertexBuffer[2].y,VertexBuffer[2].z),
  186.                               &D3DXVECTOR3(VertexBuffer[3].x,VertexBuffer[3].y,VertexBuffer[3].z));
  187.   
  188.       for(int i=1;i<4;i++)
  189.  {
  190.  VertexBuffer[i].nx+=vNormal.x;
  191.  VertexBuffer[i].ny+=vNormal.y;
  192.  VertexBuffer[i].nz+=vNormal.z;
  193.  }
  194.  VertexBuffer[1].nx/=2;
  195.  VertexBuffer[1].ny/=2;
  196.  VertexBuffer[1].nz/=2;
  197.      VertexBuffer[2].nx/=2;
  198.  VertexBuffer[2].ny/=2;
  199.  VertexBuffer[2].nz/=2;
  200.  for(int i=0;i<4;i++)
  201.  {
  202.       D3DXVec3Normalize(&D3DXVECTOR3(VertexBuffer[i].nx,VertexBuffer[i].ny,VertexBuffer[i].nz),&D3DXVECTOR3(VertexBuffer[i].nx,VertexBuffer[i].ny,VertexBuffer[i].nz));
  203.  }
  204.  m_pVbRiver->Unlock();*/
  205.      //dwRow=m_dwRow;//每行的顶点数
  206.  //dwCol=m_dwCol/2;//每列的顶点数
  207.     
  208.     //NumOfShareFace=new WORD[dwRow*dwCol];
  209. //SumOfVertexNormal=new D3DVECTOR[dwRow*dwCol];
  210.      y=165.0f;
  211.     NumOfIndex=(dwRow-1)*(dwCol-1)*6;
  212.     m_pD3DDevice->CreateIndexBuffer(NumOfIndex*sizeof(DWORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX32,D3DPOOL_MANAGED,&m_pIbRiver,NULL);
  213. m_pIbRiver->Lock(0,0,(void **)&IndexBuffer,0);//(m_dwRow-1)*(m_dwCol-1)*6*sizeof(DWORD)
  214. DWORD k=0,a=0,b=0,c=0;
  215. for(DWORD i=0;i<dwRow-1;i++)
  216. {
  217. for(DWORD j=0;j<dwCol-1;j++)
  218. {   a=i*(dwCol)+j;
  219.     b=(i+1)*(dwCol)+j;
  220. c=i*(dwCol)+j+1;
  221. IndexBuffer[k+0]=a;
  222. IndexBuffer[k+1]=b;
  223. IndexBuffer[k+2]=c;
  224. IndexBuffer[k+3]=c;
  225. IndexBuffer[k+4]=b;
  226. IndexBuffer[k+5]=b+1;
  227. k+=6;
  228. }
  229. }
  230.   
  231. m_pIbRiver->Unlock();
  232. float tu=0,tv=-0.5;
  233. m_pD3DDevice->CreateVertexBuffer(dwRow*dwCol*sizeof(VERTEX),D3DUSAGE_SOFTWAREPROCESSING,D3DFVF_VERTEX,D3DPOOL_DEFAULT,&m_pVbRiver,NULL);
  234. m_pVbRiver->Lock(0,dwRow*dwCol*sizeof(VERTEX),(void **)&VertexBuffer,D3DLOCK_DISCARD);
  235. bool flag=true;
  236. for(DWORD i=0;i<dwRow*dwCol;i++)
  237. {
  238. NumOfShareFace[i]=0;
  239. SumOfVertexNormal[i]=D3DXVECTOR3(0,0,0);
  240. VertexBuffer[i].y=y;
  241. VertexBuffer[i].x=m_fcell*(i%dwCol);
  242. VertexBuffer[i].z=m_fcell*(i/dwCol);
  243. tu=((tu+0.025f>1.0f)?(-1.0f):(tu+0.025f)); //tu=-1~1,-1~1,......
  244. if(i%dwCol==0) 
  245. {
  246. tv=((tv+0.025f>1.0f)?(-1.0f):(tv+0.025f));
  247. tu=0;
  248. }
  249. VertexBuffer[i].tu=tu>0?tu:-tu;
  250. VertexBuffer[i].tv=tv>0?tv:-tv;
  251. VertexBuffer[i].tvv=tv;
  252. }
  253. for(DWORD i=0;i<NumOfIndex;i+=3)
  254. {
  255.      Vertex1=IndexBuffer[i];
  256.  Vertex2=IndexBuffer[i+1];
  257.  Vertex3=IndexBuffer[i+2];
  258.  vNormal=GetTriangeNormal(&D3DXVECTOR3(VertexBuffer[Vertex1].x,VertexBuffer[Vertex1].y,VertexBuffer[Vertex1].z),
  259.                       &D3DXVECTOR3(VertexBuffer[Vertex2].x,VertexBuffer[Vertex2].y,VertexBuffer[Vertex2].z),
  260.                               &D3DXVECTOR3(VertexBuffer[Vertex3].x,VertexBuffer[Vertex3].y,VertexBuffer[Vertex3].z));
  261.  NumOfShareFace[Vertex1]++;
  262.  NumOfShareFace[Vertex2]++;
  263.  NumOfShareFace[Vertex3]++;
  264.  SumOfVertexNormal[Vertex1].x+=vNormal.x;
  265.  SumOfVertexNormal[Vertex1].y+=vNormal.y;
  266.  SumOfVertexNormal[Vertex1].z+=vNormal.z;
  267.      SumOfVertexNormal[Vertex2].x+=vNormal.x;
  268.  SumOfVertexNormal[Vertex2].y+=vNormal.y;
  269.  SumOfVertexNormal[Vertex2].z+=vNormal.z;
  270.      SumOfVertexNormal[Vertex3].x+=vNormal.x;
  271.  SumOfVertexNormal[Vertex3].y+=vNormal.y;
  272.  SumOfVertexNormal[Vertex3].z+=vNormal.z;
  273. }
  274. for(DWORD i=0;i<(dwCol*dwRow);i++)
  275. {
  276. vNormal.x=SumOfVertexNormal[i].x/NumOfShareFace[i];
  277. vNormal.y=SumOfVertexNormal[i].y/NumOfShareFace[i];
  278. vNormal.z=SumOfVertexNormal[i].z/NumOfShareFace[i];
  279. D3DXVec3Normalize(&vNormal,&vNormal);
  280. VertexBuffer[i].nx=vNormal.x;
  281.         VertexBuffer[i].ny=vNormal.y;
  282. VertexBuffer[i].nz=vNormal.z;
  283. }
  284.    
  285. m_pVbRiver->Unlock();
  286. }
  287. void CWater::SetTexture()
  288. {
  289. //D3DXCreateTextureFromFile(m_pD3DDevice,"./res/floor.jpg",&m_pTx);
  290. D3DXCreateTextureFromFile(m_pD3DDevice,"./res/water.tga",&m_pTxRiver);
  291. }
  292. void CWater::RenerWater(float Vy)
  293. {   static int kkk=0;
  294. VERTEX * VertexBuffer;
  295. SetMaterial();
  296.    /* m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);
  297.    
  298.    // m_pD3DDevice->SetRenderState( D3DRS_ZWRITEENABLE,FALSE);
  299. m_pD3DDevice->SetTexture(0,m_pTx);
  300. m_pD3DDevice->SetStreamSource(0,m_pVb,0,sizeof(VERTEX));
  301. m_pD3DDevice->SetFVF(D3DFVF_VERTEX);
  302. m_pD3DDevice->SetIndices(m_pIb);
  303. m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,m_dwRow*m_dwCol,0,(m_dwRow-1)*(m_dwCol-1)*2);*/
  304.     
  305.    /* ZeroMemory(&material,sizeof(material));
  306.     material.Specular.r=0.6f;
  307. material.Specular.g=0.6f;
  308. material.Specular.b=0.6f;
  309. material.Specular.a=0.5f;
  310.     m_pD3DDevice->SetMaterial(&material);*/
  311.    
  312. m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
  313. m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
  314. m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
  315.     if(Vy<=y)
  316.     m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CW);
  317. else
  318.     m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
  319. m_pD3DDevice->SetTexture(0,m_pTxRiver);
  320. m_pD3DDevice->SetStreamSource(0,m_pVbRiver,0,sizeof(VERTEX));
  321. m_pD3DDevice->SetFVF(D3DFVF_VERTEX);
  322.     m_pVbRiver->Lock(0,dwRow*dwCol*sizeof(VERTEX),(void **)&VertexBuffer,D3DLOCK_DISCARD);
  323.     if(kkk==4)
  324. {
  325. for(int i=0;i<dwRow*dwCol;i++)
  326. {
  327.        
  328. VertexBuffer[i].tvv=((VertexBuffer[i].tvv-0.025f<-1.0f)?(1.0f):(VertexBuffer[i].tvv-0.025f));
  329. VertexBuffer[i].tv=VertexBuffer[i].tvv>0?VertexBuffer[i].tvv:-VertexBuffer[i].tvv;
  330. }
  331.     kkk=0;
  332. }
  333. kkk++;
  334. m_pVbRiver->Unlock();
  335. m_pD3DDevice->SetIndices(m_pIbRiver);
  336. m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,dwRow*dwCol,0,(dwRow-1)*(dwCol-1)*2);
  337.    // m_pD3DDevice->SetRenderState( D3DRS_CLIPPLANEENABLE ,TRUE);
  338.     m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
  339. //m_pD3DDevice->SetRenderState( D3DRS_ZWRITEENABLE,TRUE);
  340. }
  341. void CWater::Translate()
  342. {
  343. D3DXMATRIX matWorld;
  344. D3DXMatrixTranslation(&matWorld,0.0f,0.0f,0.0f);
  345. m_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
  346. }
  347. /*float CWater::GetHeight(float x,float z)
  348. {
  349.         //x+=m_fcell*m_dwCol;
  350. //z+=m_fcell*m_dwRow;
  351. float rx,rz;
  352. rx=x/m_fcell;
  353. rz=z/m_fcell;
  354. long col1,row1,col2,row2;
  355. float h1,h2,h3,h4,ha,hb;
  356. col1=DWORD(rx);
  357. row1=DWORD(rz);
  358. if(col1>m_dwCol || col1<0 ||row1>m_dwRow || row1<0) return 0;
  359. else 
  360. { //双线性插值法计算
  361. col2=(col1==m_dwCol)?col1:col1+1;
  362. row2=(row1==m_dwRow)?row1:row1+1;
  363. h1=(m_pHeightData[row1*m_dwCol+col1]*m_fHeight/255.0f);
  364. h2=(m_pHeightData[row1*m_dwCol+col2]*m_fHeight/255.0f);
  365. h3=(m_pHeightData[row2*m_dwCol+col1]*m_fHeight/255.0f);
  366. h4=(m_pHeightData[row2*m_dwCol+col2]*m_fHeight/255.0f);
  367. ha=(h2*(rx-col1)+h1*(col2-rx));
  368. hb=(h4*(rx-col1)+h3*(col2-rx));
  369. return (hb*(rz-row1)+ha*(row2-rz));
  370. }
  371. }*/
  372. void CWater::SetMaterial()
  373. {
  374. ZeroMemory(&material,sizeof(material));
  375. material.Diffuse.r=material.Ambient.r=1.0f;
  376. material.Diffuse.g=material.Ambient.g=1.0f;
  377. material.Diffuse.b=material.Ambient.b=1.0f;
  378. material.Diffuse.a=material.Ambient.a=1.0f;
  379. material.Specular.r=0.0f;
  380. material.Specular.g=0.0f;
  381. material.Specular.b=0.0f;
  382. material.Specular.a=0.0f;
  383. material.Emissive.r=0.0f;
  384. material.Emissive.g=0.0f;
  385. material.Emissive.b=0.0f;
  386. material.Emissive.a=0.0f;
  387. m_pD3DDevice->SetMaterial(&material);
  388. }