frustum frustration..

Started by
10 comments, last by cozzie 11 years, 1 month ago

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, &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, &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

Advertisement

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)

Hi columbo.

The topic in the link makes me real fuzzy smile.png 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.a, mFrustumPlane.b, mFrustumPlane.c)) + mFrustumPlane.d;
//		dist = D3DXPlaneDotCoord(&mFrustumPlane, &pBoundingBox->boxArray[0]);

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

		dist = DotProduct(VECTOR3(pBoundingBox->max.x, pBoundingBox->max.y, pBoundingBox->max.z), 
						  VECTOR3(mFrustumPlane.a, mFrustumPlane.b, mFrustumPlane.c)) + mFrustumPlane.d;
//		dist = D3DXPlaneDotCoord(&mFrustumPlane, &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

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

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

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

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, &pBoundingBox->min);
		distmax = D3DXPlaneDotCoord(&mFrustumPlane, &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

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

http://www.chadvernon.com/blog/resources/directx9/frustum-culling/

******************************************************************************************
Youtube Channel

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.a >= 0.0f) x = true;
			else x = false;
		if(mFrustumPlane.b >= 0.0f) y = true;
			else y = false;
		if(mFrustumPlane.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, &_nvtx) > 0) return false;		// completely outside

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

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

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

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

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.

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

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.a >= 0) _pvtx.x = pBoundingBox->max.x;
		if(mFrustumPlane.b >= 0) _pvtx.y = pBoundingBox->max.y;
		if(mFrustumPlane.c >= 0) _pvtx.z = pBoundingBox->max.z;

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

		// check positive vertex; further along the normal's direction
		dist = D3DXPlaneDotCoord(&mFrustumPlane, &_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, &_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

This topic is closed to new replies.

Advertisement