Terrain.cpp
资源名称:3DRPG.rar [点击查看]
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:6k
源码类别:
游戏
开发平台:
Visual C++
- #include "stdafx.h"
- #include "Terrain.h"
- CTerrain::CTerrain(HINSTANCE hinst,LPDIRECT3DDEVICE9 d3dd)
- {
- m_pD3DDevice=d3dd;
- m_dwRow=0;
- m_dwCol=0;
- m_fcell=30;
- m_fHeight=800;
- m_hInst=hinst;
- SetTexture();
- CreateVertex();
- }
- CTerrain::~CTerrain()
- {
- m_pVb->Release();
- m_pIb->Release();
- m_pTx->Release();
- delete []m_pHeightData;
- }
- D3DXVECTOR3 CTerrain::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 CTerrain::CreateVertex()
- {
- VERTEX * VertexBuffer;
- DWORD * IndexBuffer;
- D3DXVECTOR3 vNormal;
- DWORD Vertex1;
- DWORD Vertex2;
- DWORD Vertex3;
- DWORD NumOfIndex;
- 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];
- //oldBmp;
- //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();
- }
- void CTerrain::SetTexture()
- {
- D3DXCreateTextureFromFile(m_pD3DDevice,"./res/floor.jpg",&m_pTx);
- }
- void CTerrain::RenerTerrain()
- { 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);
- }
- void CTerrain::Translate()
- {
- D3DXMATRIX matWorld;
- D3DXMatrixTranslation(&matWorld,0.0f,0.0f,0.0f);
- m_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
- }
- float CTerrain::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 CTerrain::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);
- }