• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
cozzie

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 this post


Link to post
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 this post


Link to post
Share on other sites

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[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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0