Jump to content

  • Log In with Google      Sign In   
  • Create Account

Fast (!) and correct Frustum - AABB Intersection


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 schupf   Members   -  Reputation: 216

Like
0Likes
Like

Posted 20 October 2008 - 07:51 AM

Hello,

I needed an intersection test of the view frustum vs an axis aligned bounding box (AABB). Of course I could check all 8 box vertices against all 6 planes of the frustum, but I found a faster algorithm on this website, which uses a p-vertex and n-vertex: Link (section Geometric Approach - Testing Boxes II)

To implement this algorithm I just used this code:
// Returns: INTERSECT : 0 
//          INSIDE : 1 
//          OUTSIDE : 2 
int FrustumAABBIntersect(Plane *planes, Vector &mins, Vector &maxs) { 
   int    ret = INSIDE; 
   Vector vmin, vmax; 

   for(int i = 0; i < 6; ++i) { 
      // X axis 
      if(planes[i].normal.x > 0) { 
         vmin.x = mins.x; 
         vmax.x = maxs.x; 
      } else { 
         vmin.x = maxs.x; 
         vmax.x = mins.x; 
      } 
      // Y axis 
      if(planes[i].normal.y > 0) { 
         vmin.y = mins.y; 
         vmax.y = maxs.y; 
      } else { 
         vmin.y = maxs.y; 
         vmax.y = mins.y; 
      } 
      // Z axis 
      if(planes[i].normal.z > 0) { 
         vmin.z = mins.z; 
         vmax.z = maxs.z; 
      } else { 
         vmin.z = maxs.z; 
         vmax.z = mins.z; 
      } 
      if(Vector::DotProduct(planes[i].normal, vmin) + planes[i].d > 0) 
         return OUTSIDE; 
      if(Vector::DotProduct(planes[i].normal, vmax) + planes[i].d >= 0) 
         ret = INTERSECT; 
   } 
   return ret;
} 



I assume that this algorithm is correct.
But this is the problem: The frustum is described by 6 planes, where each plane has an infinite extentsion in all 3 dimensions. The real frustum actually is not composed of planes, rather it is composed of 6 rectangles (each with a finite extension). So the problem that I have is that the algorithm doesnt check if the AABB intersects the finite frustum; it checks for intersections with the 6 infinite planes. (Because I don't know if I explained the problem quite well, I will give an example: Imagine you have a far plane of 1000 units and a box which goes from 900 to 1100 units on the z-axis. The x coordinates of the box are huge, so the box is placed at a huge distance on the right side of the frustum and does NOT intersect the real frustum. But the box DOES intersect the far plane, so the algorithm will deliver an intersection).

My question: Is there a (relatively) simple way to modify this algorithm (or use an other algorithm) to get only the REAL intersections with the frustum (and not with the infinite planes)?

Sponsor:

#2 clb   Members   -  Reputation: 1788

Like
0Likes
Like

Posted 20 October 2008 - 08:23 AM

Quote:
Original post by schupf
But this is the problem: The frustum is described by 6 planes, where each plane has an infinite extentsion in all 3 dimensions. The real frustum actually is not composed of planes, rather it is composed of 6 rectangles (each with a finite extension). So the problem that I have is that the algorithm doesnt check if the AABB intersects the finite frustum; it checks for intersections with the 6 infinite planes.


It is a normal property of 3D planes that they extend to infinity. A plane can be seen to divide the 3D space into a positive and a negative half-space, where the positive half space is the side of the space towards which the normal of the plane points to.

Now, define a frustum to be the intersection of the negative half-spaces of 6 planes left,right,up,down, near and far. (this is how we usually define arbitrary-shaped convex polyhedrons in 3D)

The code you pasted works by checking one plane at a time, whether the AABB is in the positive half-space of the plane (if so, no intersection, AABB lies outside), or whether the AABB intersects the plane (if so, return intersection). If the AABB doesn't lie in the positive half-space of ANY of the 6 planes, then it is completely in the negative half-spaces of the planes and hence inside the frustum.

The optimization that is being done in that code is that instead of checking all the 8 corner points of the AABB, we can skip some computations by the observation that it is easy to determine which of the corner vertices lie in the "most negative and positive directions" of the plane just by looking at the plane normal.

The code looks fine to me. Perhaps the problem lies in how you extract the planes of the frustum? The code uses the assumption that the normals point outwards from the frustum volume.

Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#3 schupf   Members   -  Reputation: 216

Like
0Likes
Like

Posted 20 October 2008 - 11:37 AM

@clb: You did not understand my problem;)
Just forget my question, I have just found the solution (shame on me that I did not see it from the beginning). I just have to skip the early out and always loop through all 6 planes. If one plane test resulted in OUTSIDe then it is outside, else its an intersection.


Anyway, I have an another question regarding the linked paper: click
In the section "Geometric Approach - Extracting the Planes" they show how to extract the 6 frustum planes in a geometrical way, by calculating the normal vector and a point on the plane. For the right plane their formula is this:
a = (nc + right * Wnear / 2) - p
(this is visualized by this picture:

They want to calculate the vector of the right point (a point on the right plane). The term in the parentheses is clear to me: nc + right * Wnear / 2, nc is the center point of the near plane, right is a unit vector to the right, Wnear/2 the half width of the near plane. But why do they substract p?? (p = eyepoint of camera). I mean they calculate a (vector from p to point on the right plane) and nc + right * Wnear / 2 already gives what they want. Anyone knows why they subtract p?

#4 scgames   Members   -  Reputation: 1977

Like
0Likes
Like

Posted 20 October 2008 - 12:09 PM

Quote:
Original post by schupf
Hello,

I needed an intersection test of the view frustum vs an axis aligned bounding box (AABB). Of course I could check all 8 box vertices against all 6 planes of the frustum, but I found a faster algorithm on this website, which uses a p-vertex and n-vertex: Link (section Geometric Approach - Testing Boxes II)

To implement this algorithm I just used this code:
*** Source Snippet Removed ***
I assume that this algorithm is correct.
But this is the problem: The frustum is described by 6 planes, where each plane has an infinite extentsion in all 3 dimensions. The real frustum actually is not composed of planes, rather it is composed of 6 rectangles (each with a finite extension). So the problem that I have is that the algorithm doesnt check if the AABB intersects the finite frustum; it checks for intersections with the 6 infinite planes. (Because I don't know if I explained the problem quite well, I will give an example: Imagine you have a far plane of 1000 units and a box which goes from 900 to 1100 units on the z-axis. The x coordinates of the box are huge, so the box is placed at a huge distance on the right side of the frustum and does NOT intersect the real frustum. But the box DOES intersect the far plane, so the algorithm will deliver an intersection).

My question: Is there a (relatively) simple way to modify this algorithm (or use an other algorithm) to get only the REAL intersections with the frustum (and not with the infinite planes)?
The AABB frustum culling algorithm you describe (which is fairly standard) is conservative; it favors efficiency at the expense of the occasional false positive. It's possible to perform an exact test, but it's unlikely to be worth the extra development effort or run-time cost (under normal circumstances, at least).




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS