我正在实现一些与截锥相关的代码,即使有许多可见的对象,剔除测试也不会返回任何内容。我的数学支持库没有提供平面支持或类似的东西,所以这些代码中的大部分都是从头开始编写的,几乎没有可用的测试。如果您对故障点有任何建议,请提供建议。幸运的是,它不是很多,所以小‘’Wall‘o’代码如下:
class Plane {
public:
Plane() {
r0 = Math::Vector(0,0,0);
normal = Math::Vector(0,1,0);
}
Plane(Math::Vector p1, Math::Vector p2, Math::Vector p3) {
r0 = p1;
normal = Math::Cross(p2 - p1, p3 - p1);
}
Math::Vector r0;
Math::Vector normal;
};
class Frustum {
public:
Frustum(
const std::array<Math::Vector, 8>& points
)
{
planes[0] = Plane(points[0], points[1], points[2]);
planes[1] = Plane(points[4], points[5], points[6]);
planes[2] = Plane(points[0], points[1], points[4]);
planes[3] = Plane(points[2], points[3], points[6]);
planes[4] = Plane(points[0], points[2], points[4]);
planes[5] = Plane(points[1], points[3], points[5]);
}
Plane planes[6];
};
// http://www.cescg.org/CESCG-2002/DSykoraJJelinek/index.html
bool Intersects(Math::AABB lhs, const Frustum& rhs) const {
for(int i = 0; i < 6; i++) {
Math::Vector pvertex = lhs.TopRightFurthest;
Math::Vector nvertex = lhs.BottomLeftClosest;
if (rhs.planes[i].normal.x <= -0.0f) {
std::swap(pvertex.x, nvertex.x);
}
if (rhs.planes[i].normal.y <= -0.0f) {
std::swap(pvertex.y, nvertex.y);
}
if (rhs.planes[i].normal.z <= -0.0f) {
std::swap(pvertex.z, nvertex.z);
}
if (Math::Dot(nvertex - rhs.planes[i].r0, rhs.planes[i].normal) > 0.0f) {
return false;
}
}
return true;
}同样值得注意的是,我使用的是左手坐标系,所以我反转了叉积的结果(在cross函数中)。
编辑:准确地说,我错过了顶点索引。它们被编入索引,以便每个位指示一个轴上的角-即,0按顺序指示右、上和后。
另外,我为这个问题的一般低质量道歉,但我不知道还能补充什么。我没有收到任何编译器警告或错误,也没有足够的理解来理解我可能在调试器中读取的任何内容--这超出了我的正常范围。并且代码使用相对明显的Vector和AABB实现进行编译。
发布于 2012-04-09 02:43:34
我怀疑这归结于您对顶点的标记以及您指定点的顺序。在指定顶点的方向上应保持一致,根据坐标系的不同,是顺时针还是逆时针。这应该是关于观察外部面(或内部面,取决于您如何看待它)。
在我看来,法线指向相同的方向,这是错误的。

因此,如果指定这些曲面的法线方向为逆时针,则会得到
0 1 2
5 4 7
1 5 6
4 0 3
3 2 6
1 0 4您的示例0 1 2和4 5 6产生了两条法线指向相同的方向,而它们应该指向相反的方向
https://stackoverflow.com/questions/10064600
复制相似问题