📄 mesh.cpp
字号:
#include "StdAfx.h"
#include "../common/myd3d.h"
#include "mesh.h"
extern IDirect3DDevice9* g_device;
extern HWND g_hwnd;
bool ComputeBoundingSphere(ID3DXMesh* mesh, d3d::BoundingSphere* sphere);
bool ComputeBoundingBox(ID3DXMesh* mesh, d3d::BoundingBox* box);
Mymesh::Mymesh(void)
{
}
Mymesh::~Mymesh(void)
{
}
HRESULT Mymesh::LoadMesh( LPCSTR pFilename )
{
HRESULT hr = 0;
hr = D3DXLoadMeshFromX(
pFilename,
D3DXMESH_MANAGED,
g_device,
&_adjBuffer,
&_mtrlBuffer,
0,
&_numMtrls,
&_Mesh);
if(FAILED(hr))
{
::MessageBox(g_hwnd, TEXT("LoadMeshFromX() - FAILED"), 0, 0);
return false;
}
return hr;
}
void Mymesh::GetTexAndMat(void)
{
if( _mtrlBuffer != 0 && _numMtrls != 0 )
{
D3DXMATERIAL* mtrls = (D3DXMATERIAL*)_mtrlBuffer->GetBufferPointer();
for(int i = 0; i < (int)_numMtrls; i++)
{
// the MatD3D property doesn't have an ambient value set
// when its loaded, so set it now:
mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;
// save the ith material
_Mtrls.push_back( mtrls[i].MatD3D );
// check if the ith material has an associative texture
if( mtrls[i].pTextureFilename != 0 )
{
// yes, load the texture for the ith subset
IDirect3DTexture9* tex = 0;
D3DXCreateTextureFromFile(
g_device,
mtrls[i].pTextureFilename,
&tex);
// save the loaded texture
_Textures.push_back( tex );
}
else
{
// no texture for the ith subset
_Textures.push_back( 0 );
}
}
}
d3d::Release<ID3DXBuffer*>(_mtrlBuffer);
}
HRESULT Mymesh::Optimize(void)
{
HRESULT hr = 0;
hr =_Mesh->OptimizeInplace(
D3DXMESHOPT_ATTRSORT |
D3DXMESHOPT_COMPACT |
D3DXMESHOPT_VERTEXCACHE,
(DWORD*)_adjBuffer->GetBufferPointer(),
0, 0, 0);
d3d::Release<ID3DXBuffer*>(_adjBuffer);
if(FAILED(hr))
{
::MessageBox(g_hwnd, TEXT("OptimizeInplace() - FAILED"), 0, 0);
return false;
}
return true;
}
bool Mymesh::Init( LPCSTR pFilename )
{
_Mesh = 0;
_adjBuffer = 0;
_numMtrls = 0;
D3DXMatrixIdentity(&_world);
LoadMesh( pFilename );
GetTexAndMat();
Optimize();
//初始化边界容积
ComputeBoundingSphere(_Mesh, &_bSphere);
ComputeBoundingBox(_Mesh, &_bBox);
return true;
}
bool Mymesh::Draw(void)
{
g_device->SetTransform(D3DTS_WORLD, &_world);
for(int i = 0; i < (int)_Mtrls.size(); i++)
{
g_device->SetMaterial( &_Mtrls[i] );
g_device->SetTexture(0, _Textures[i]);
_Mesh->DrawSubset(i);
}
return true;
}
void Mymesh::Clean(void)
{
d3d::Release<ID3DXMesh*>( _Mesh );
for(int i = 0; i < (int)_Textures.size(); i++)
d3d::Release<IDirect3DTexture9*>( _Textures[i] );
}
D3DXMATRIX Mymesh::GetPosMtrls()
{
return _world;
}
//初始化边界容积
bool ComputeBoundingSphere(ID3DXMesh* mesh, d3d::BoundingSphere* sphere)
{
HRESULT hr = 0;
BYTE* v = 0;
mesh->LockVertexBuffer(0, (void**)&v);
hr = D3DXComputeBoundingSphere(
(D3DXVECTOR3*)v,
mesh->GetNumVertices(),
D3DXGetFVFVertexSize(mesh->GetFVF()),
&sphere->_center,
&sphere->_radius);
mesh->UnlockVertexBuffer();
if( FAILED(hr) )
return false;
return true;
}
//
//
// ------------------------------
// /| /|
// / | / |
// / | / |
// / | / |
// / | / |
// / | / |
// / | / |
// / | / |
// / | / |
// / | / |
// /----------------------------/ |
// | | | |
// | | * | | +Y
// | | | |
// | |-----------------|----------| |
// | / | / |
// | / | / |
// | / | / | +Z
// | / | / | /
// | / | / 0 | / 1
// | / | / ---|---/--------/
// | / | / / | / /
// | / | / / | / /
// | / | / / |/ /
// |/ |/ / 4 --------/---------- +X
// ------------------------------ / /
// / /
// 3 /---------------- 2
bool ComputeBoundingBox(ID3DXMesh* mesh, d3d::BoundingBox* box)
{
HRESULT hr = 0;
BYTE* v = 0;
mesh->LockVertexBuffer(0, (void**)&v);
hr = D3DXComputeBoundingBox(
(D3DXVECTOR3*)v,
mesh->GetNumVertices(),
D3DXGetFVFVertexSize(mesh->GetFVF()),
&box->_min,
&box->_max);
mesh->UnlockVertexBuffer();
box->_vxz[0].x = box->_min.x;
box->_vxz[0].z = box->_max.z;
box->_vxz[0].y = 0.0f;
box->_Nvxz[0] = box->_vxz[0];
box->_vxz[1].x = box->_max.x;
box->_vxz[1].z = box->_max.z;
box->_vxz[1].y = 0.0f;
box->_Nvxz[1] = box->_vxz[1];
box->_vxz[2].x = box->_max.x;
box->_vxz[2].z = box->_min.z;
box->_vxz[2].y = 0.0f;
box->_Nvxz[2] = box->_vxz[2];
box->_vxz[3].x = box->_min.x;
box->_vxz[3].z = box->_min.z;
box->_vxz[3].y = 0.0f;
box->_Nvxz[3] = box->_vxz[3];
box->_vxz[4].x = (box->_min.x+box->_max.x)/2;
box->_vxz[4].z = (box->_min.z+box->_max.z)/2;
box->_vxz[4].y = 0.0f;
box->_Nvxz[4] = box->_vxz[4];
box->_xzlmax = sqrt(fabs(box->_max.x*box->_max.x - box->_max.z*box->_max.z));
if( FAILED(hr) )
return false;
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -