Jump to content
  • Advertisement
Sign in to follow this  
Basiror

AABB vs Frustum test

This topic is 3866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am implementing the aabb vs frustum test as suggested in this article, the 2 vertex version Efficient AABB frustum intersection test n is the (negative/closest)? vertex of the aabb to the plane. I just wonder whether it isn't sufficient to only test the vertex 'n' against the plane, if it is in front( outside the frustum) the aabb is also outside. Or is there a special case I don t handle this way? thx for your help.
 34 bool frustum::intersect(const math::aabb& b, uint& mask) const
 35 {
 36         uint nx;
 37         uint ny;
 38         uint nz;
 39         math::vector4 normal;
 40         math::vector4 p;
 41         math::vector4 n;
 42         math::vector4 bn[2] = {b.min(),b.max()};
 43         math::vector4 bp[2] = {b.max(),b.min()};
 44
 45
 46         if(mask && mask<6)
 47         {
 48                 normal = m_Plane[mask].normal();
 49                 nx = (*((uint*)&normal[0]))>>31;
 50                 ny = (*((uint*)&normal[1]))>>31;
 51                 nz = (*((uint*)&normal[2]))>>31;
 52
 53                 p = math::vector4(bp[nx][0],bp[ny][1],bp[nz][2]);
 54                 n = math::vector4(bn[nx][0],bn[ny][1],bn[nz][2]);
 55
 56                 if(math::point_to_plane(n,m_Plane[mask]) > 0.0f)
 57                         return false;
 58         }
 59         for(uint i=0;i<6;++i)
 60         {
 61                 if(i==mask)
 62                         continue;
 63
 64                 normal = m_Plane.normal();
 65                 nx = (*((uint*)&normal[0]))>>31;
 66                 ny = (*((uint*)&normal[1]))>>31;
 67                 nz = (*((uint*)&normal[2]))>>31;
 68
 69                 p = math::vector4(bp[nx][0],bp[ny][1],bp[nz][2]);
 70                 n = math::vector4(bn[nx][0],bn[ny][1],bn[nz][2]);
 71
 72                 if(math::point_to_plane(n,m_Plane) > 0.0f)
 73                 {
 74                         mask = i;
 75                         return false;
 76                 }
 77         }
 78
 79         return true;
 80 }



[Edited by - Basiror on March 17, 2008 2:40:26 AM]

Share this post


Link to post
Share on other sites
Advertisement
I didn't read through all of your source code as it seemed a bit involved compared to the code presented in the article you gave a link to. Maybe you tried to implement the plane masking and choherency stuff?. I also used that article as a base when implementing my own AABB vs. Frustum check routine and it is shorter than yours and nearly identical to the one presented in the article.


struct AABB {
vec3x min;
vec3x max;
};

struct Frustum {
vec4f planes[6];

Frustum(const mat4f &m)
{
const vec4f r1(m.elem[0][0], m.elem[0][1], m.elem[0][2], m.elem[0][3]);
const vec4f r2(m.elem[1][0], m.elem[1][1], m.elem[1][2], m.elem[1][3]);
const vec4f r3(m.elem[2][0], m.elem[2][1], m.elem[2][2], m.elem[2][3]);
const vec4f r4(m.elem[3][0], m.elem[3][1], m.elem[3][2], m.elem[3][3]);

planes[0] = r4 + r1;
planes[1] = r4 - r1;
planes[2] = r4 + r2;
planes[3] = r4 - r2;
planes[4] = r4 + r3;
planes[5] = r4 - r3;
}
};

int aabb_vs_frustum(const AABB &aabb, const Frustum &f)
{
int result = 1;

for (int i = 0; i < 6; ++i) {
const vec4f &plane = f.planes;

const vec3f pv(
plane.x > 0 ? aabb.max.x : aabb.min.x,
plane.y > 0 ? aabb.max.y : aabb.min.y,
plane.z > 0 ? aabb.max.z : aabb.min.z
);

const vec3f nv(
plane.x < 0 ? aabb.max.x : aabb.min.x,
plane.y < 0 ? aabb.max.y : aabb.min.y,
plane.z < 0 ? aabb.max.z : aabb.min.z
);

const float n = dot(vec4f(pv, 1.0f), plane);
if (n < 0) return -1;

const float m = dot(vec4f(nv, 1.0f), plane);
if (m < 0) result = 0;
}

return result;
}



This is all the code i use. I feel that the plane masking and especially the cohereny stuff only complicate things a lot. AABB vs Frustums checks are not that expensive at all compared to other stuff.

The above code computes the frustum planes from a matrix. The matrix is assumed to be like the matrices used in OpenGL. DirectX uses a bit different matrices, so the computation would have to be different. I also inverted the plane normals of the frustum to point inside the frustum instead of outside which required to switch the computation of n and m compared to the article.

Share this post


Link to post
Share on other sites
It is sufficient; the other tests simply allow you to distinguish between overlapping and completely contained. If you don't care about that, you can just use the one vertex.

Share this post


Link to post
Share on other sites
Quote:
Original post by Trenki
I didn't read through all of your source code as it seemed a bit involved compared to the code presented in the article you gave a link to. Maybe you tried to implement the plane masking and choherency stuff?. I also used that article as a base when implementing my own AABB vs. Frustum check routine and it is shorter than yours and nearly identical to the one presented in the article.

*** Source Snippet Removed ***

This is all the code i use. I feel that the plane masking and especially the cohereny stuff only complicate things a lot. AABB vs Frustums checks are not that expensive at all compared to other stuff.

The above code computes the frustum planes from a matrix. The matrix is assumed to be like the matrices used in OpenGL. DirectX uses a bit different matrices, so the computation would have to be different. I also inverted the plane normals of the frustum to point inside the frustum instead of outside which required to switch the computation of n and m compared to the article.


I just remember the plane that was used to clip the AABB last frame, this has the huge advantage that you may reduce the number of dot products, which will release some cycles for other operations, ray triangle intersections...

*hence* less work means less energy consumption *cough*

@emeyex: Thanks for pointing this out, is there any used for knowing whether the AABBs are completely inside?

Share this post


Link to post
Share on other sites
Quote:
@emeyex: Thanks for pointing this out, is there any used for knowing whether the AABBs are completely inside?

Well, if you have a volume hierarchy (i.e., AABBs inside other AABBs, where "inside" really means inside), then if a parent AABB tests as fully contained in the frustum, then you don't even have to test any of the children AABBs, as you know they will be fully contained in the frustum as well. This obviously works for any volume hierarchy (not just AABBs).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!