我从不同网格变量中的 .x 文件加载多个网格。现在我想计算我已加载(以及正在显示的)所有网格的边界球体,请指导我如何实现这一点。可以将 VertexBuffers 添加到一个变量中并使用该变量计算 boundingSphere 吗?(如果是的话,他们是如何将 vertexBuffers 添加在一起的)否则你会建议什么替代方案!?谢谢
问问题
2690 次
2 回答
2
这样做非常容易:
首先,您需要平均所有顶点。这为您提供了中心位置。
这是在 C++ 中完成的(对不起,我的 C# 很生锈,但它应该给你一个想法):
D3DXVECTOR3 avgPos;
const rcpNum = 1.0f / (float)numVerts; // Do this here as divides are far more epxensive than multiplies.
int count = 0;
while( count < numVerts )
{
// Instead of adding everything up and then dividing by the number (which could lead
// to overflows) I'll divide by the number as I go along. The result is the same.
avgPos.x += vert[count].pos.x * rcpNum;
avgPos.y += vert[count].pos.y * rcpNum;
avgPos.z += vert[count].pos.z * rcpNum;
count++;
}
现在你需要遍历每个顶点并计算出离中心点最远的顶点。
像这样的东西会起作用(在 C++ 中):
float maxSqDist = 0.0f;
int count = 0;
while( count < numVerts )
{
D3DXVECTOR3 diff = avgPos - vert[count].pos;
// Note we may as well use the square length as the sqrt is very expensive and the
// maximum square length will ALSO be the maximum length and yet we only need to
// do one sqrt this way :)
const float sqDist = D3DXVec3LengthSq( diff );
if ( sqDist > maxSqDist )
{
maxSqDist = sqDist;
}
count++;
}
const float radius = sqrtf( maxSqDist );
你现在有了你的中心位置(avgPos)和你的半径(radius),因此,你需要定义一个边界球体的所有信息。
于 2010-06-23T09:34:06.227 回答
0
我有一个想法,我要做的是确定每个网格对象的中心,然后使用上述信息确定网格对象集合的中心......
于 2010-06-23T09:10:14.543 回答