Followers 0

frustum frustration..

11 posts in this topic

Hi,

I think I'm overseeing something when moving from 8 point check to extents/limits checking a bounding box.

All 8 points and the min and max I checked (also by checking against D3DX function to generate bounding box extents).

The sympton:

- when i check against all 8 point, result is OK / as expected

- when i check against the extents (2 points) result is not OK (culled to early, strange behaviour, some meshes OK/ not OK)

Here's the difference in the code.

Really hope someone can help me out/ in the right direction.

// checking all 8 points, OK

bool CD3dcam::BoxInFrustum(BOUNDINGBOX *pBoundingBox)
{
int iTotalIn = 0;

for(int i=0;i<6;++i)
{
int iInCount = 8;
int iPtIn = 1;

for(int bc=0;bc<8;++bc)
{
if(D3DXPlaneDotCoord(&mFrustumPlane[i], &pBoundingBox->boxArray[bc]) < 0)
{
iPtIn = 0;
iInCount--;
}
}
if(iInCount == 0) return false;
iTotalIn += iPtIn;
}
if(iTotalIn == 0) return false;
else return true;
}

// checking only extents, array index 0 and 1

bool CD3dcam::BoxInFrustum(BOUNDINGBOX *pBoundingBox)
{
int iTotalIn = 0;

for(int i=0;i<6;++i)
{
int iInCount = 2;
int iPtIn = 1;

for(int bc=0;bc<2;++bc)
{
if(D3DXPlaneDotCoord(&mFrustumPlane[i], &pBoundingBox->boxArray[bc]) < 0)
{
iPtIn = 0;
iInCount--;
}
}
if(iInCount == 0) return false;
iTotalIn += iPtIn;
}
if(iTotalIn == 0) return false;
else return true;
}

// generating the bounding box

void CalcBoundingBox(TVERTEX *pVtxArray, DWORD pStart, DWORD pNr, BOUNDINGBOX *pBox)
{
pBox->min.x = pVtxArray[pStart].position.x;
pBox->min.y = pVtxArray[pStart].position.y;
pBox->min.z = pVtxArray[pStart].position.z;

pBox->max.x = pVtxArray[pStart].position.x;
pBox->max.y = pVtxArray[pStart].position.y;
pBox->max.z = pVtxArray[pStart].position.z;

for(DWORD vc=pStart;vc<pStart+pNr;++vc)
{
if(pVtxArray[vc].position.x < pBox->min.x) pBox->min.x = pVtxArray[vc].position.x;
if(pVtxArray[vc].position.x > pBox->max.x) pBox->max.x = pVtxArray[vc].position.x;

if(pVtxArray[vc].position.y < pBox->min.y) pBox->min.y = pVtxArray[vc].position.y;
if(pVtxArray[vc].position.y > pBox->max.y) pBox->max.y = pVtxArray[vc].position.y;

if(pVtxArray[vc].position.z < pBox->min.z) pBox->min.z = pVtxArray[vc].position.z;
if(pVtxArray[vc].position.z > pBox->max.z) pBox->max.z = pVtxArray[vc].position.z;
}

// full 8 points of the AABB (0 and 1 == min and max for i.e. culling)
pBox->boxArray[0].x = pBox->min.x;
pBox->boxArray[0].y = pBox->min.y;
pBox->boxArray[0].z = pBox->min.z;

pBox->boxArray[1].x = pBox->max.x;
pBox->boxArray[1].y = pBox->max.y;
pBox->boxArray[1].z = pBox->max.z;

pBox->boxArray[2].x = pBox->max.x;
pBox->boxArray[2].y = pBox->max.y;
pBox->boxArray[2].z = pBox->min.z;

pBox->boxArray[3].x = pBox->min.x;
pBox->boxArray[3].y = pBox->max.y;
pBox->boxArray[3].z = pBox->min.z;

pBox->boxArray[4].x = pBox->min.x;
pBox->boxArray[4].y = pBox->min.y;
pBox->boxArray[4].z = pBox->max.z;

pBox->boxArray[5].x = pBox->max.x;
pBox->boxArray[5].y = pBox->min.y;
pBox->boxArray[5].z = pBox->max.z;

pBox->boxArray[6].x = pBox->max.x;
pBox->boxArray[6].y = pBox->min.y;
pBox->boxArray[6].z = pBox->min.z;

pBox->boxArray[7].x = pBox->min.x;
pBox->boxArray[7].y = pBox->max.y;
pBox->boxArray[7].z = pBox->max.z;

// dimensions
pBox->width		= pBox->max.x - pBox->min.x;
pBox->height	= pBox->max.y - pBox->min.y;
pBox->depth		= pBox->max.z - pBox->min.z;
}



0

Share on other sites

My understanding is that you can get away with only testing 2 corners of a bounding box against each plane. However, the 2 corners need to be chosen according to the sign of each individual plane's normal. You can work out which corners your planes need to test once per frame, to reduce your per-BB cost.

I found these two resources helped me implement it in my engine: http://www.gamedev.net/topic/224424-frustum-culling-box-outside-but-still-inside/ and http://www.cse.chalmers.se/~uffe/vfc_bbox.pdf (section 3.1)

0

Share on other sites

Hi columbo.

The topic in the link makes me real fuzzy the PDF though explain it all. Thanks for that.

I've tried it out but unfortunately no result. Well, the result is that everything is culled.

Here's what I've done (not clean code, just rough implementation/test. intersecting for now same as inside):

bool CD3dcam::BoxInFrustum(BOUNDINGBOX *pBoundingBox)
{
bool intersect = false;
float dist;

for(int i=0;i<6;++i)
{
dist = DotProduct(VECTOR3(pBoundingBox->min.x, pBoundingBox->min.y, pBoundingBox->min.z),
VECTOR3(mFrustumPlane[i].a, mFrustumPlane[i].b, mFrustumPlane[i].c)) + mFrustumPlane[i].d;
//		dist = D3DXPlaneDotCoord(&mFrustumPlane[i], &pBoundingBox->boxArray[0]);

if(dist > 0) return false;		// completely outside

dist = DotProduct(VECTOR3(pBoundingBox->max.x, pBoundingBox->max.y, pBoundingBox->max.z),
VECTOR3(mFrustumPlane[i].a, mFrustumPlane[i].b, mFrustumPlane[i].c)) + mFrustumPlane[i].d;
//		dist = D3DXPlaneDotCoord(&mFrustumPlane[i], &pBoundingBox->boxArray[1]);

if(dist > 0) intersect = true;		// intersecting
}
if(intersect) return true;
return true;
}



any ideas what I'm doing wrong?

(I've debugged the min and max vectors, there correct. same used when I get correct result when checking all 8 points)

Update;

I might have found something, I don't check the relation of 'min' and 'max' vectors against the plane. Will get into that now

Edited by cozzie
0

Share on other sites

thanks ewclay. I'll get into it. For now using the near and far points (instead of center and extent).

First trying to make it work 'functionally'/ not OK in performance. Still struggling a bit though.

Thought I had it, but still everyting's culled:

bool CD3dcam::BoxInFrustum(BOUNDINGBOX *pBoundingBox)
{
bool intersect = false;
float distmin, distmax;

D3DXVECTOR3 _near, _far;

for(int i=0;i<6;++i)
{
distmin = D3DXPlaneDotCoord(&mFrustumPlane[i], &pBoundingBox->min);
distmax = D3DXPlaneDotCoord(&mFrustumPlane[i], &pBoundingBox->max);

if(distmin < distmax)	// min = near
{
if(distmin > 0) return false;
if(distmax > 0) intersect = true;
}
if(distmin > distmax)	// max = near
{
if(distmax > 0) return false;
if(distmin > 0) intersect = true;
}
}
if(intersect) return true;
return true;
}



any ideas?

0

Share on other sites

thanks, I also read it and 'used' it.

Sphere and point checking are all good, I'm no trying to check against a bounding box (AABB), checking all 8 points works.

I'm trying to figure out how to do it with just the p and n vertex (extents). I found out how to determine the 'p' and 'n' vertex. Now up to making it work....

bool CD3dcam::BoxInFrustum(BOUNDINGBOX *pBoundingBox)
{
bool intersect = false;
D3DXVECTOR3 _pvtx, _nvtx;
bool x, y, z;

for(int i=0;i<6;++i)
{
if(mFrustumPlane[i].a >= 0.0f) x = true;
else x = false;
if(mFrustumPlane[i].b >= 0.0f) y = true;
else y = false;
if(mFrustumPlane[i].c >= 0.0f) z = true;
else z = false;

if(x && y && z)
{
_pvtx = pBoundingBox->max;
_nvtx = pBoundingBox->min;
}
if(x && y && !z)
{
_pvtx.x = pBoundingBox->max.x;
_pvtx.y = pBoundingBox->max.y;
_pvtx.z = pBoundingBox->min.z;

_nvtx.x = pBoundingBox->min.x;
_nvtx.y = pBoundingBox->min.y;
_nvtx.z = pBoundingBox->max.z;
}
if(x && !y && z)
{
_pvtx.x = pBoundingBox->max.x;
_pvtx.y = pBoundingBox->min.y;
_pvtx.z = pBoundingBox->max.z;

_nvtx.x = pBoundingBox->min.x;
_nvtx.y = pBoundingBox->max.y;
_nvtx.z = pBoundingBox->min.z;
}
if(x && !y && !z)
{
_pvtx.x = pBoundingBox->max.x;
_pvtx.y = pBoundingBox->min.y;
_pvtx.z = pBoundingBox->min.z;

_nvtx.x = pBoundingBox->min.x;
_nvtx.y = pBoundingBox->max.y;
_nvtx.z = pBoundingBox->max.z;
}
if(!x && y && z)
{
_pvtx.x = pBoundingBox->min.x;
_pvtx.y = pBoundingBox->max.y;
_pvtx.z = pBoundingBox->max.z;

_nvtx.x = pBoundingBox->max.x;
_nvtx.y = pBoundingBox->min.y;
_nvtx.z = pBoundingBox->min.z;
}
if(!x && y && !z)
{
_pvtx.x = pBoundingBox->min.x;
_pvtx.y = pBoundingBox->max.y;
_pvtx.z = pBoundingBox->min.z;

_nvtx.x = pBoundingBox->max.x;
_nvtx.y = pBoundingBox->min.y;
_nvtx.z = pBoundingBox->max.z;
}
if(!x && !y && z)
{
_pvtx.x = pBoundingBox->min.x;
_pvtx.y = pBoundingBox->min.y;
_pvtx.z = pBoundingBox->max.z;

_nvtx.x = pBoundingBox->max.x;
_nvtx.y = pBoundingBox->max.y;
_nvtx.z = pBoundingBox->min.z;
}
if(!x && !y && z)
{
_pvtx = pBoundingBox->min;
_nvtx = pBoundingBox->max;
}

if(D3DXPlaneDotCoord(&mFrustumPlane[i], &_nvtx) > 0) return false;		// completely outside

if(D3DXPlaneDotCoord(&mFrustumPlane[i], &_pvtx) > 0) intersect = true;	// intersecting
}
if(intersect) return true;
return true;
}



Till now everything is still culled, have to find out why..

Edited by cozzie
0

Share on other sites

thanks, I also read it and 'used' it.
Sphere and point checking are all good, I'm no trying to check against a bounding box (AABB), checking all 8 points works. Just cant figure out how to get it working with only 2 points (the extents).

It's all in the first link. The extents are not the min and max of the bounding box, they change for each plane. If you use centre and size instead it becomes simpler. In the end it comes down to two dot products, versus one for a sphere.
0

Share on other sites

Thanks I think I got, how to find out the extents (need a look-up table later for efficiency).

Still not got it working though with the code above, with the correct 'n' and 'p' vertex

0

Share on other sites

This is becoming a blog ;)

Got it, here's the code.

It's not completely accurate, maybe just accept it or dig into finding the min/ max vertex etc, and see if I'm doing something 'wrong'.

Thanks for the help.

int CD3dcam::BoxInFrustum(BOUNDINGBOX *pBoundingBox)
{
bool intersect = false;
D3DXVECTOR3 _pvtx, _nvtx;

float dist;

for(int i=0;i<6;++i)
{
// find the nearest and farthest point along normal direction
_pvtx = pBoundingBox->min;
if(mFrustumPlane[i].a >= 0) _pvtx.x = pBoundingBox->max.x;
if(mFrustumPlane[i].b >= 0) _pvtx.y = pBoundingBox->max.y;
if(mFrustumPlane[i].c >= 0) _pvtx.z = pBoundingBox->max.z;

_nvtx = pBoundingBox->max;
if(mFrustumPlane[i].a >= 0) _nvtx.x = pBoundingBox->min.x;
if(mFrustumPlane[i].b >= 0) _nvtx.y = pBoundingBox->min.y;
if(mFrustumPlane[i].c >= 0) _nvtx.z = pBoundingBox->min.z;

// check positive vertex; further along the normal's direction
dist = D3DXPlaneDotCoord(&mFrustumPlane[i], &_pvtx);
if(dist < 0) return OUTSIDE;		// wrong side of plane, completely outside

// check negative vertex; less far along the normal's direction
dist = D3DXPlaneDotCoord(&mFrustumPlane[i], &_nvtx);
if(dist < 0) return INTERSECT;  	// intersecting, negative vertex within frustum
}
return INSIDE;
}

// creating the boundingbox

void CalcBoundingBox(TVERTEX *pVtxArray, DWORD pStart, DWORD pNr, BOUNDINGBOX *pBox)
{
pBox->min.x = pVtxArray[pStart].position.x;
pBox->min.y = pVtxArray[pStart].position.y;
pBox->min.z = pVtxArray[pStart].position.z;

pBox->max.x = pVtxArray[pStart].position.x;
pBox->max.y = pVtxArray[pStart].position.y;
pBox->max.z = pVtxArray[pStart].position.z;

for(DWORD vc=pStart;vc<pStart+pNr;++vc)
{
if(pVtxArray[vc].position.x < pBox->min.x) pBox->min.x = pVtxArray[vc].position.x;
if(pVtxArray[vc].position.x > pBox->max.x) pBox->max.x = pVtxArray[vc].position.x;

if(pVtxArray[vc].position.y < pBox->min.y) pBox->min.y = pVtxArray[vc].position.y;
if(pVtxArray[vc].position.y > pBox->max.y) pBox->max.y = pVtxArray[vc].position.y;

if(pVtxArray[vc].position.z < pBox->min.z) pBox->min.z = pVtxArray[vc].position.z;
if(pVtxArray[vc].position.z > pBox->max.z) pBox->max.z = pVtxArray[vc].position.z;
}

// full 8 points of the AABB		- STILL NEEDED???
pBox->boxArray[0].x = pBox->min.x;
pBox->boxArray[0].y = pBox->min.y;
pBox->boxArray[0].z = pBox->min.z;

pBox->boxArray[1].x = pBox->max.x;
pBox->boxArray[1].y = pBox->max.y;
pBox->boxArray[1].z = pBox->max.z;

pBox->boxArray[2].x = pBox->max.x;
pBox->boxArray[2].y = pBox->max.y;
pBox->boxArray[2].z = pBox->min.z;

pBox->boxArray[3].x = pBox->min.x;
pBox->boxArray[3].y = pBox->max.y;
pBox->boxArray[3].z = pBox->min.z;

pBox->boxArray[4].x = pBox->min.x;
pBox->boxArray[4].y = pBox->min.y;
pBox->boxArray[4].z = pBox->max.z;

pBox->boxArray[5].x = pBox->max.x;
pBox->boxArray[5].y = pBox->min.y;
pBox->boxArray[5].z = pBox->max.z;

pBox->boxArray[6].x = pBox->max.x;
pBox->boxArray[6].y = pBox->min.y;
pBox->boxArray[6].z = pBox->min.z;

pBox->boxArray[7].x = pBox->min.x;
pBox->boxArray[7].y = pBox->max.y;
pBox->boxArray[7].z = pBox->max.z;
}



If you see anything odd, please shout.

0

Share on other sites
The problem might be that you should not return INTERSECT until the end because the box could still be outside.

You can avoid all of the tests to get the p and n vertices, either by precalculating an index per plane (which will be the same for all bounding boxes), or using centre and size like the example code I linked to.
0

Share on other sites

thanks, i tried it out, no visible result yet though:

		// check negative vertex; less far along the normal's direction
dist = D3DXPlaneDotCoord(&mFrustumPlane[i], &_nvtx);
if(dist < 0) intersect = true; // return INTERSECT;  	// intersecting, negative vertex within frustum
}
if(intersect) return INTERSECT;



Good idea on precalculating the index per plane, saves me a lot when having a lot of bounding box checks.

The 'inaccuracy' sympton is that ik looks accurate for small objects. The larger the object, the more inaccurary. When the camera is further away from the object, the accuracy gets better (for larger objects specifically). The inaccuracy I mean is that an object is not culled while it's not in the frustum anymore (for 1 up to 3 worldspace units or so).

It's the same when I loop through all 8 points against all planes, so probably doesn't have to do with using pos and neg vertex (Extents) instead.

First I thought it was the shape of the besh, but using a real aligned box as a mesh gives the same results.

Edited by cozzie
0

Create an account

Register a new account