日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DirectX 90 3D 外接体

發布時間:2025/7/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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; //最大浮點數

const float EPSILON = 0.001f; //一個很小的值

?

第二步:

實現類里面的函數:

?

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;

}

?

第三步

函數的運用:

?

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);

?

第四步:

對函數ComputeBoundingSphere()和ComputeBoundingBox()的實現。

?

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;

}



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();



if( FAILED(hr) )

return false;



return true;

}

?

還有最后一步:

即在Device->BeginScene()和Device->EndScene()之間填入

?

SphereMesh->DrawSubset(0)

?

?

BoxMesh->DrawSubset(0)

?

?

外接球或外接體常用于加速可見性檢測和碰撞檢測。

?

計算一個網格的外接體和外接球的函數。

HRESULT?WINAPI?D3DXCompteBoundingSphere(

?????????const?D3DXVECTOR3?*?pFirstPosition,

?????????DWORD?NumVertices,

?????????DWORD?dwStride,

?????????D3DXVECTOR3?*?pCenter,

?????????FLOAT?*pRadius

);

?

說明一下:

·pFirstPosition?指向頂點數組(該數組的每個元素都描述了對應頂點)中第一?

??個頂點的位置向量的指針。

·NumVertices?該頂點數組中頂點的個數。

·dwStride?每個頂點有大小,單位為字節。

·pCenter?返回外接球的球心位置。

·pRadius?返回外接球的半徑。

HRESULT?D3DXCreateBox(
????LPDIRECT3DDEVICE9?pDevice,

????FLOAT?Width,

????FLOAT?Height,

????FLOAT?Depth,

????LPD3DXMESH?*ppMesh,

????LPD3DXBUFFER?*ppAdjacency

);

最后兩個參數?為外接體的最大點和最小點。

?

江西理工大學?FangSH 2010-5-5

轉載于:https://www.cnblogs.com/fangshenghui/archive/2010/05/05/1728340.html

總結

以上是生活随笔為你收集整理的DirectX 90 3D 外接体的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。