📄 mesh.cpp
字号:
#include "Mesh.h"
Mesh::Mesh(LPDIRECT3DDEVICE9 pDev,WORD size,WORD pCol,WORD pRow,LPSTR Texmap)
{
m_pd3dDevice=pDev;
this->m_Size =size;//一行有多少个格
//加载地形纹理图
D3DXCreateTextureFromFile( m_pd3dDevice, Texmap, &m_pDTex);
m_Col = pCol;
m_Row = pRow;
this->m_dwNumOfVertices =(m_Col+1)*(m_Row+1);//4行,4列,就是每行,每列都有5个顶点,总共25个顶点
this->m_dwNumOfPolygons =m_Col*m_Row*2;
this->m_dwNumOfIndices =this->m_dwNumOfPolygons *3;
}
Mesh::~Mesh()
{
m_pDTex->Release ();
m_pMesh->Release ();
}
HRESULT Mesh::Initilise()
{
D3DXCreateMeshFVF(this->m_dwNumOfPolygons,this->m_dwNumOfVertices,D3DXMESH_MANAGED,TERRAIN_D3DFVF_CUSTOMVERTEX ,m_pd3dDevice,&m_pMesh);
//根据给定的灵活顶点格式来创建模型,返回的模型指针指向这个模型的首地址
this->InitVertex ();
this->InitIndex ();
return S_OK;
}
HRESULT Mesh::InitVertex ()
{
TERRAIN_CUSTOMVERTEX* pcvVertices;
m_pMesh->LockVertexBuffer (0,(void**)&pcvVertices);
//============================创建一个平面============
float x, z;
DWORD i=0;
//把地图放在中心
float zStart = (float)(0.0 - (m_Row/2.0));
float zEnd = (float)(m_Row/2.0);
float xStart = (float)(0.0 - (m_Col/2.0));
float xEnd = (float)(m_Col/2.0);
//清除内存,设置顶点
int m=0,n=0; //m,n控制行和列
for(z = zStart; z <= zEnd; z++)
{
for(x = xStart; x <= xEnd; x++)
{
pcvVertices[i].x = x *m_Size ; //行-> . . . . . 列
pcvVertices[i].y=0; // . . . . . |
pcvVertices[i].z= -z * m_Size; // . . . . . |
pcvVertices[i].u=(float)n/ m_Row; // . . . . . |
pcvVertices[i].v=(float)m/m_Col; // . . . . . |
i++;// i++用来循环所有的顶点 // . . . . .
n++;// n++用来控制当前行的所有元素,一个一个赋值
}
m++; //当第一行的点赋值完之后,m++去给下一行的点赋值,
n=0; //此时的列的控制变量 n 要归0,从下一行的第一个元素开始赋值
}
//==================================
//解锁
m_pMesh->UnlockVertexBuffer();
return S_OK;
}
HRESULT Mesh::InitIndex ()
{
//设置索引缓冲的值
WORD* pIndices;
m_pMesh->LockIndexBuffer(0,(void**)&pIndices);
WORD a = (WORD)m_Col + 1;// “a” 为:一行有多少个顶点,5
WORD b = 0;
WORD c = a + 1;
WORD x, z;
DWORD i=0;
for(z = 0; z < m_Row; z++)
{
for(x = 0; x < m_Col; x++)
{
//pIndices[i] = a; //5 0----1
//pIndices[i + 1] = b; //0 |\ |
//pIndices[i + 2] = c; //6 | \ |
//pIndices[i + 3] = b + 1; //1 | \ |
//pIndices[i + 4] = c; //6 | \|
//pIndices[i + 5] = b; //0 5----6
pIndices[i] = b; //0 0----1
pIndices[i + 1] = b+1; //1 | /|
pIndices[i + 2] = a; //5 | / |
pIndices[i + 3] = c; //6 | / |
pIndices[i + 4] = a; //5 |/ |
pIndices[i + 5] = b+1; //1 5----6
a++;
b++;
c++;
i += 6;
}
a = c;
b++;
c=a+1;
}
//解锁
m_pMesh->UnlockIndexBuffer();
//清除对象
return S_OK;
}
HRESULT Mesh::Render()
{
m_pd3dDevice->SetTexture (0,m_pDTex );
//m_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID);
//渲染地形
m_pMesh->DrawSubset(0);
return S_OK;
}
HRESULT Mesh::GetTerrainVertex(D3DXVECTOR3 *cross,DWORD index,float u,float v)
{
WORD* pIndices;
WORD index1,index2,index3;
D3DXVECTOR3 v1,v2,v3;
m_pMesh->LockIndexBuffer(0,(void**)&pIndices);
index1=pIndices[index*3+0];
index2=pIndices[index*3+1];
index3=pIndices[index*3+2];
m_pMesh->UnlockIndexBuffer();
TERRAIN_CUSTOMVERTEX* pcvVertices;
m_pMesh->LockVertexBuffer (0,(void**)&pcvVertices);
v1.x=pcvVertices[index1].x;
v1.y=pcvVertices[index1].y;
v1.z=pcvVertices[index1].z;
v2.x=pcvVertices[index2].x;
v2.y=pcvVertices[index2].y;
v2.z=pcvVertices[index2].z;
v3.x=pcvVertices[index3].x;
v3.y=pcvVertices[index3].y;
v3.z=pcvVertices[index3].z;
m_pMesh->UnlockVertexBuffer();
*cross = v1 + u * (v2 - v1) + v * (v3 - v1);
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -