• FEATURED

View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

frustum frustration..

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.

11 replies to this topic

#1cozzie  Members

Posted 07 March 2013 - 05:10 PM

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;
}



Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

#2C0lumbo  Members

Posted 08 March 2013 - 02:20 AM

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)

#3cozzie  Members

Posted 08 March 2013 - 03:42 AM

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, 08 March 2013 - 03:44 AM.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

#4EWClay  Members

Posted 08 March 2013 - 03:57 AM

Good explanation here, though you have to read through all the slow ways first:

http://fgiesen.wordpress.com/2010/10/17/view-frustum-culling/

Some code doing the same thing:

http://blog.makingartstudios.com/?p=155

#5cozzie  Members

Posted 08 March 2013 - 04:10 AM

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?

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

#6LancerSolurus  Members

Posted 08 March 2013 - 04:55 AM

This is the one I understood the best plus it has source code for DX and c++

******************************************************************************************

#7cozzie  Members

Posted 08 March 2013 - 05:24 AM

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, 08 March 2013 - 07:04 AM.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

#8EWClay  Members

Posted 08 March 2013 - 07:05 AM

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.

#9cozzie  Members

Posted 08 March 2013 - 07:10 AM

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

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

#10cozzie  Members

Posted 08 March 2013 - 10:35 AM

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.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

#11EWClay  Members

Posted 08 March 2013 - 01:38 PM

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.

#12cozzie  Members

Posted 08 March 2013 - 02:05 PM

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, 08 March 2013 - 02:06 PM.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

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.