bounding box culling with just model view projection matrix

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

Recommended Posts

Hello,

i'm trying to implement culling in my project. for various reasons at the point that i can make a draw decision and want to test if i should cull the geometry i have the following available to me

1. a bounding box of the geometry in question

2. the model view projection matrix that will be sent to the shader.

i really have no other information in which to base a decision on.

my current algorithm is similiar to (pseudocode)



for(int i = 0 ; i < 7 ; i++)
{
testCorner[i] = boxCorner[i] * mvpMatrix;
testCorner[i].x /= testCorner[i].w;
testCorner[i].y /= testCorner[i].w;
testCorner[i].z /= testCorner[i].w;
}

bool clipMe = true;
for(int i = 0 ; (i < 7) && (clipMe == true) ; i++)
{
if(testCorner[i].z > 0.0)
{
if(testCorner[i].x > -1 && testCorner[i].x < 1)
{
clipMe = false;
}
if(testCorner[i].y > -1 && testCorner[i].y < 1)
{
clipMe = false;
}
}
}


However i find that sometimes a piece of geometry that should not get clipped will get clipped. Has anyone else used a method similiar to this before? Anyone can point me toward what i'm doing wrong?

very much appreciated.

Edited by hdxpete

Share on other sites
- You're only testing seven corners of the box (i < 8)
- You don't need to divide by w, you can test against w itself (x / w < 1 -> x < 1 * w)

Right now it's possible to clip a box that has vertices that lie outside the frustum, but has other features that still intersect.
You can only clip a box when all of its vertices lie outside one the six clipping planes.

For example (nicer when storing the six bools in a bit field):

bool clipMe = true, clipPlanes[] = { true, true, true, true, true, true };

for(int i = 0 ; clipMe && i < 8 ; i++)
{
vec4 testCorner = boxCorner[i] * mvpMatrix;

clipPlanes[0] = clipPlanes[0] && (testCorner.x < -testCorner.w);
clipPlanes[1] = clipPlanes[1] && (testCorner.x > testCorner.w);
clipPlanes[2] = clipPlanes[2] && (testCorner.y < -testCorner.w);
clipPlanes[3] = clipPlanes[3] && (testCorner.y > testCorner.w);
clipPlanes[4] = clipPlanes[4] && (testCorner.z < 0);
clipPlanes[5] = clipPlanes[5] && (testCorner.z > testCorner.w);

clipMe = clipPlanes[0] || clipPlanes[1] || clipPlanes[2] || clipPlanes[3] || clipPlanes[4] || clipPlanes[5];
}

Edited by eppo

Share on other sites

eppo,

thanks for the help. I understand what your saying. one question though... when you do the ORing of all the clipPlanes. don't you really want that to be and ANDing? otherwise it would get clipped unless the entire BB was inside the camera view. correct?

Share on other sites

No, it means the box can be clipped as long as all its points fall outside of at least one of the planes. As soon as it finds that the box intersects a plane, that plane can longer be a valid clipping plane. When all planes are invalidated like this, the box cannot be clipped and the loop breaks early with clipMe = false;

If you were to AND them you would require a box to lie outside all of the planes (which is impossible).

Edited by eppo

Share on other sites

eppo,

thanks for the assist. really appreciate the it.

• 11
• 9
• 17
• 13
• 11