📄 meshes.cpp
字号:
#include "Meshes.h"
CMeshes::CMeshes(void)
:m_pDevice(NULL),
m_pMesh(NULL),
m_pMeshMaterial(NULL),
m_pMeshTexture(NULL),
SubsetNumber(0)
{
}
CMeshes::~CMeshes(void)
{
}
HRESULT CMeshes::InitStaticMesh(LPDIRECT3DDEVICE9 m_pDevice, char *MeshName)
{
this->m_pDevice = m_pDevice;
LPD3DXBUFFER pD3DXMtrlBuffer;
// Load the mesh from the specified file
if( FAILED( D3DXLoadMeshFromX( MeshName, D3DXMESH_SYSTEMMEM,
m_pDevice, NULL,
&pD3DXMtrlBuffer, NULL, &SubsetNumber,
&m_pMesh ) ) )
{
// If model is not in current folder, try parent folder
if( FAILED( D3DXLoadMeshFromX( MeshName, D3DXMESH_SYSTEMMEM,
m_pDevice, NULL,
&pD3DXMtrlBuffer, NULL, &SubsetNumber,
&m_pMesh ) ) )
{
MessageBox(NULL, "Could not find tiger.x", "Meshes.exe", MB_OK);
return E_FAIL;
}
}
// We need to extract the material properties and texture names from the
// pD3DXMtrlBuffer
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
m_pMeshMaterial = new D3DMATERIAL9[SubsetNumber];
if( m_pMeshMaterial == NULL )
return E_OUTOFMEMORY;
m_pMeshTexture = new LPDIRECT3DTEXTURE9[SubsetNumber];
if( m_pMeshTexture == NULL )
return E_OUTOFMEMORY;
for( DWORD i=0; i<SubsetNumber; i++ )
{
// Copy the material
m_pMeshMaterial[i] = d3dxMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
m_pMeshMaterial[i].Ambient = m_pMeshMaterial[i].Diffuse;
m_pMeshTexture[i] = NULL;
if( d3dxMaterials[i].pTextureFilename != NULL &&
lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )
{
// Create the texture
if( FAILED( D3DXCreateTextureFromFile( m_pDevice,
d3dxMaterials[i].pTextureFilename,
&m_pMeshTexture[i] ) ) )
{
// If texture is not in current folder, try parent folder
const TCHAR* strPrefix = TEXT("..\\");
TCHAR strTexture[MAX_PATH];
StringCchCopy( strTexture, MAX_PATH, strPrefix );
StringCchCat( strTexture, MAX_PATH, d3dxMaterials[i].pTextureFilename );
// If texture is not in current folder, try parent folder
if( FAILED( D3DXCreateTextureFromFile( m_pDevice,
strTexture,
&m_pMeshTexture[i] ) ) )
{
MessageBox(NULL, "Could not find texture map", "Meshes.exe", MB_OK);
}
}
}
}
//释放材质缓冲
pD3DXMtrlBuffer->Release();
//===============输出一个优化后的MESH=======================
LPD3DXMESH pOptimizedMesh;
m_pMesh->Optimize(
D3DXMESHOPT_ATTRSORT,
NULL,NULL,
NULL,NULL,
&pOptimizedMesh);
//引用记数1-1delete
m_pMesh->Release();
m_pMesh = pOptimizedMesh;
return S_OK;
}
void CMeshes::SetMeshForWorld()
{
D3DXMATRIX worldMatrix,worldMatrix1,worldMatrix2;
D3DXMatrixIdentity(&worldMatrix);
D3DXMatrixTranslation(&worldMatrix1,Mesh_x,Mesh_y,Mesh_z);
D3DXMatrixRotationY(&worldMatrix2,timeGetTime()/1000);
worldMatrix = worldMatrix2 * worldMatrix1;
m_pDevice->SetTransform(D3DTS_WORLD,&worldMatrix);
}
void CMeshes::RenderStaticMesh()
{
SetMeshForWorld();
for(int i=0;i<SubsetNumber;i++)
{
m_pDevice->SetMaterial(&m_pMeshMaterial[i]);
m_pDevice->SetTexture(0,m_pMeshTexture[i]);
m_pMesh->DrawSubset(i);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -