【问题标题】:How to compute bounding box/sphere across multiple meshes (C#)如何跨多个网格计算边界框/球体(C#)
【发布时间】:2011-03-03 23:04:19
【问题描述】:
我从不同网格变量中的 .x 文件加载多个网格。
现在我想计算我已加载的所有网格(以及正在显示的)的边界球
请指导我如何实现这一目标。
可以将 VertexBuffers 添加到一个变量中并使用该变量计算 boundingSphere 吗? (如果是,它们是如何添加到一起的 vertexBuffers 的)
否则你会建议什么替代方案!?
谢谢x
【问题讨论】:
标签:
c#
directx
mesh
bounding-box
vertex-buffer
【解决方案1】:
这样做非常容易:
首先,您需要对所有顶点进行平均。这为您提供了中心位置。
这是在 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),因此,您有了定义边界球体所需的所有信息。
【解决方案2】:
我有一个想法,我要做的是确定每个网格对象的中心,然后使用上述信息确定网格对象集合的中心......