Incorrect results for frustum cull
Posted
by
DeadMG
on Game Development
See other posts from Game Development
or by DeadMG
Published on 2012-04-07T21:44:04Z
Indexed on
2012/04/07
23:45 UTC
Read the original article
Hit count: 286
c++
|collision-detection
Previously, I had a problem with my frustum culling producing too optimistic results- that is, including many objects that were not in the view volume. Now I have refactored that code and produced a cull that should be accurate to the actual frustum, instead of an axis-aligned box approximation. The problem is that now it never returns anything to be in the view volume.
As the mathematical support library I'm using does not provide plane support functions, I had to code much of this functionality myself, and I'm not really the mathematical type, so it's likely that I've made some silly error somewhere. As follows is the relevant code:
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;
};
This class represents one plane as a point and a normal vector.
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];
};
The points are passed in order where (the inverse of) each bit of the index of each point indicates whether it's the left, top, and back of the frustum, respectively. As such, I just picked any three points where they all shared one bit in common to define the planes.
My intersection test is as follows (based on this):
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(rhs.planes[i].r0, nvertex) < 0.0f) {
return false;
}
}
return true;
}
Also of note is that because I'm using a left-handed co-ordinate system, I wrote my Cross
function to return the negative of the formula given on Wikipedia.
Any suggestions as to where I've made a mistake?
© Game Development or respective owner