DirectX 90 3D 外接体
外接體
?
第一步
在空間d3d中添加類
?
///外接體///struct BoundingBox
{
BoundingBox();
bool isPointInside(D3DXVECTOR3& p);
D3DXVECTOR3 _min;
D3DXVECTOR3 _max;
};
struct BoundingSphere
{
BoundingSphere();
D3DXVECTOR3 _center;
float _radius;
};
//-------常量-------------------
const float INFINITY = FLT_MAX; //最大浮點(diǎn)數(shù)
const float EPSILON = 0.001f; //一個(gè)很小的值
?
第二步:
實(shí)現(xiàn)類里面的函數(shù):
?
d3d::BoundingBox::BoundingBox(){
// infinite small
_min.x = d3d::INFINITY;
_min.y = d3d::INFINITY;
_min.z = d3d::INFINITY;
_max.x = -d3d::INFINITY;
_max.y = -d3d::INFINITY;
_max.z = -d3d::INFINITY;
}
bool d3d::BoundingBox::isPointInside(D3DXVECTOR3& p)
{
if( p.x >= _min.x && p.y >= _min.y && p.z >= _min.z &&
p.x <= _max.x && p.y <= _max.y && p.z <= _max.z )
{
return true;
}
else
{
return false;
}
}
d3d::BoundingSphere::BoundingSphere()
{
_radius = 0.0f;
}
?
第三步
函數(shù)的運(yùn)用:
?
ComputeBoundingSphere(Mesh, &boundingSphere);ComputeBoundingBox(Mesh, &boundingBox);
D3DXCreateSphere(Device, boundingSphere._radius, 20, 10, &SphereMesh, 0);
D3DXCreateBox(Device, boundingBox._max.x - boundingBox._min.x,
boundingBox._max.y - boundingBox._min.y,
boundingBox._max.z - boundingBox._min.z, &BoxMesh, 0);
?
第四步:
對函數(shù)ComputeBoundingSphere()和ComputeBoundingBox()的實(shí)現(xiàn)。
?
bool ComputeBoundingSphere(ID3DXMesh* mesh, d3d::BoundingSphere* sphere){
HRESULT hr = 0;
BYTE* v = 0;
mesh->LockVertexBuffer(0, (void**)&v);
//計(jì)算外接球
hr = D3DXComputeBoundingSphere(
(D3DXVECTOR3*)v,
mesh->GetNumVertices(),
D3DXGetFVFVertexSize(mesh->GetFVF()),
&sphere->_center,
&sphere->_radius);
mesh->UnlockVertexBuffer();
if( FAILED(hr) )
return false;
return true;
}
bool ComputeBoundingBox(ID3DXMesh* mesh, d3d::BoundingBox* box)
{
HRESULT hr = 0;
BYTE* v = 0;
mesh->LockVertexBuffer(0, (void**)&v);
//計(jì)算外接體
hr = D3DXComputeBoundingBox(
(D3DXVECTOR3*)v,
mesh->GetNumVertices(),
D3DXGetFVFVertexSize(mesh->GetFVF()),
&box->_min,
&box->_max);
mesh->UnlockVertexBuffer();
if( FAILED(hr) )
return false;
return true;
}
?
還有最后一步:
即在Device->BeginScene()和Device->EndScene()之間填入
?
SphereMesh->DrawSubset(0)?
或
?
BoxMesh->DrawSubset(0)?
?
外接球或外接體常用于加速可見性檢測和碰撞檢測。
?
計(jì)算一個(gè)網(wǎng)格的外接體和外接球的函數(shù)。
HRESULT?WINAPI?D3DXCompteBoundingSphere(
?????????const?D3DXVECTOR3?*?pFirstPosition,
?????????DWORD?NumVertices,
?????????DWORD?dwStride,
?????????D3DXVECTOR3?*?pCenter,
?????????FLOAT?*pRadius
);
?
說明一下:
·pFirstPosition?指向頂點(diǎn)數(shù)組(該數(shù)組的每個(gè)元素都描述了對應(yīng)頂點(diǎn))中第一?
??個(gè)頂點(diǎn)的位置向量的指針。
·NumVertices?該頂點(diǎn)數(shù)組中頂點(diǎn)的個(gè)數(shù)。
·dwStride?每個(gè)頂點(diǎn)有大小,單位為字節(jié)。
·pCenter?返回外接球的球心位置。
·pRadius?返回外接球的半徑。
HRESULT?D3DXCreateBox(
????LPDIRECT3DDEVICE9?pDevice,
????FLOAT?Width,
????FLOAT?Height,
????FLOAT?Depth,
????LPD3DXMESH?*ppMesh,
????LPD3DXBUFFER?*ppAdjacency
);
最后兩個(gè)參數(shù)?為外接體的最大點(diǎn)和最小點(diǎn)。
?
江西理工大學(xué)?FangSH 2010-5-5
轉(zhuǎn)載于:https://www.cnblogs.com/fangshenghui/archive/2010/05/05/1728340.html
總結(jié)
以上是生活随笔為你收集整理的DirectX 90 3D 外接体的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript字幕滚动效果
- 下一篇: 开发WebService两种开源工具CX