gpt4 book ai didi

c# - 如何跨多个网格计算边界框/球体 (C#)

转载 作者:太空宇宙 更新时间:2023-11-03 11:47:26 25 4
gpt4 key购买 nike

我从不同网格变量中的 .x 文件加载多个网格。现在我想计算我加载的所有网格(以及正在显示的网格)的包围球请指导我如何实现这一目标。可以将 VertexBuffers 附加到一个变量中并使用它计算 boundingSphere 吗? (如果是的话,它们是如何将 vertexBuffers 添加到一起的)否则你会建议什么替代方案!?谢谢

最佳答案

做到这一点非常容易:

首先,您需要对所有顶点进行平均。这为您提供了中心位置。

这是在 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),因此,您拥有了定义边界球体所需的所有信息。

关于c# - 如何跨多个网格计算边界框/球体 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3044912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com