• 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

strange frustum culling behaviour

4 posts in this topic


I've noticed that my frustum culling is behaving strange when doing a boundingsphere check on entities of meshes. It looks like entities are being culled just before there out of the frustum/ screen.


Before I dig into my code calculation the radius of my entities, I'd like to know if someone sees any strange things my frustum calculation and check boundingsphere functions. Any help is really appreciated:


void CD3dcam::CalculateFrustum()
	D3DXMatrixMultiply(&mFrustumMatrix, &mMatView, &mMatProjection);

	// left plane
	mFrustumPlane[0].a = mFrustumMatrix._14 + mFrustumMatrix._11;
	mFrustumPlane[0].b = mFrustumMatrix._24 + mFrustumMatrix._21;
	mFrustumPlane[0].c = mFrustumMatrix._34 + mFrustumMatrix._31;
	mFrustumPlane[0].d = mFrustumMatrix._44 + mFrustumMatrix._41;
	D3DXPlaneNormalize(&mFrustumPlane[0], &mFrustumPlane[0]);

	// right plane
	mFrustumPlane[1].a = mFrustumMatrix._14 - mFrustumMatrix._11;
	mFrustumPlane[1].b = mFrustumMatrix._24 - mFrustumMatrix._21;
	mFrustumPlane[1].c = mFrustumMatrix._34 - mFrustumMatrix._31;
	mFrustumPlane[1].d = mFrustumMatrix._44 - mFrustumMatrix._41;
	D3DXPlaneNormalize(&mFrustumPlane[1], &mFrustumPlane[1]);

	// top plane
	mFrustumPlane[2].a = mFrustumMatrix._14 - mFrustumMatrix._12;
	mFrustumPlane[2].b = mFrustumMatrix._24 - mFrustumMatrix._22;
	mFrustumPlane[2].c = mFrustumMatrix._34 - mFrustumMatrix._32;
	mFrustumPlane[2].d = mFrustumMatrix._44 - mFrustumMatrix._42;
	D3DXPlaneNormalize(&mFrustumPlane[2], &mFrustumPlane[2]);

	// bottom plane
	mFrustumPlane[3].a = mFrustumMatrix._14 + mFrustumMatrix._12;
	mFrustumPlane[3].b = mFrustumMatrix._24 + mFrustumMatrix._22;
	mFrustumPlane[3].c = mFrustumMatrix._34 + mFrustumMatrix._32;
	mFrustumPlane[3].d = mFrustumMatrix._44 + mFrustumMatrix._42;
	D3DXPlaneNormalize(&mFrustumPlane[3], &mFrustumPlane[3]);

	// near plane
	mFrustumPlane[4].a = mFrustumMatrix._13;
	mFrustumPlane[4].b = mFrustumMatrix._23;
	mFrustumPlane[4].c = mFrustumMatrix._33;
	mFrustumPlane[4].d = mFrustumMatrix._43;
	D3DXPlaneNormalize(&mFrustumPlane[4], &mFrustumPlane[4]);

	// far plane
	mFrustumPlane[5].a = mFrustumMatrix._14 - mFrustumMatrix._13;
	mFrustumPlane[5].b = mFrustumMatrix._24 - mFrustumMatrix._23;
	mFrustumPlane[5].c = mFrustumMatrix._34 - mFrustumMatrix._33;
	mFrustumPlane[5].d = mFrustumMatrix._44 - mFrustumMatrix._43;
	D3DXPlaneNormalize(&mFrustumPlane[5], &mFrustumPlane[5]);

bool CD3dcam::SphereInFrustum(D3DXVECTOR3 *pPosition, float pRadius)
	float fDistance;
	for(int i=0;i<6;++i)
		fDistance = D3DXPlaneDotCoord(&mFrustumPlane[i], pPosition) + pRadius;

		if(fDistance < -pRadius) return false; // outside the frustum
		if((float)fabs(fDistance) < pRadius) return true; // intersects
    return true; // inside the frustum completely or intersecting; see XLS

// calculating the entities/ meshes radius

bool CD3dmesh::CalcRadiusAndWorldPos()
	mAttrWorldPos		= new D3DXVECTOR3[mAttrSize];
	mAttrBoundingRadius = new float[mAttrSize];
	mAttrBoundingBox	= new BOUNDINGBOX[mAttrSize];

	TVERTEX *verticesPointer;
	TVERTEX *vertices = new TVERTEX[mMesh->GetNumVertices()];

	mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, 0); //D3DLOCK_READONLY);
	memcpy(vertices, verticesPointer, mMesh->GetNumVertices()*D3DXGetDeclVertexSize(dec, 0)); 

	// scale the vertices, before determining bounding radius, mesh (world)pos etc.

	for(DWORD vc=0;vc<mMesh->GetNumVertices();++vc)
		D3DXVec3Scale(&vertices[vc].position, &vertices[vc].position, mScale);

	// calculatie bounding radius, using always absolute, 0.0.0 mMeshCenter
	D3DXComputeBoundingSphere(&vertices[0].position, mMesh->GetNumVertices(), D3DXGetDeclVertexSize(dec, 0), &mMeshCenter, &mBoundingRadius);	

	// do the same for all subobjects in the attr table, mAttrWorldPos is DETERMINED based on subobject vertices

	for(DWORD obj=0;obj<mAttrSize;++obj)
		D3DXComputeBoundingSphere(&vertices[mAttrTable[obj].VertexStart].position, (DWORD)mAttrTable[obj].VertexCount, D3DXGetDeclVertexSize(dec, 0), &mAttrWorldPos[obj], &mAttrBoundingRadius[obj]); 
		mAttrBoundingRadius[obj] += 0.1f;

		CalcBoundingBox(vertices, mAttrTable[obj].VertexStart, mAttrTable[obj].VertexCount, &mAttrBoundingBox[obj]);

	// add WorldPos of object to all sub objects

	for(DWORD obj=0;obj<mAttrSize;++obj)
		D3DXVec3Add(&mAttrWorldPos[obj], &mAttrWorldPos[obj], &mWorldPos); 
		for(int vc=0;vc<8;++vc) 
			D3DXVec3Add(&mAttrBoundingBox[obj].boxArray[vc], &mAttrBoundingBox[obj].boxArray[vc], &mWorldPos);
	return true;


It doesn't seem to occur on full meshes though (first boundingsphere check in rendering a frame).



Share this post

Link to post
Share on other sites

addition; I also tried to switch true and false in the boundingsphere check function, to see if nothing's culled, but strangely a lot of entities are still not culled (depending on the frustum at that moment)..


Share this post

Link to post
Share on other sites

Found the cause already, when I don't rotate my meshes (and thus entities), everything works fine.

I scale all vertices before calculating the boundingradius, but I don't do any rotation... probably have to do that to first


Share this post

Link to post
Share on other sites
Found the cause already, when I don't rotate my meshes (and thus entities), everything works fine.

I scale all vertices before calculating the boundingradius, but I don't do any rotation... probably have to do that to first


You mean you werent rotating your bounding sphere to be in the same place as your rendering mesh?


Did you try rendering out the bounding sphere on top of the mesh to make sure they look correct?


It doesnt sound like you're sure what the problem is.  You should know what the problem is before moving on, not just put in a fix and hope that it's the right fix for the real problem.


Share this post

Link to post
Share on other sites
What i do after loading the beshes, during initialization, is calculation the bounding radius of the meshes and entities. So i can use them for boundinghere checks in culling.

What i did was:
- lock vertexbuffer in local space (not scaled or rotated)
- scale the vertices
- then calculate the boundingradius (using d3dxcreateboundingsphere)
- then do the same for all entities

I was nearly there but forgot to rotate where i scale the vertices.
Found that out by setting rotation xyz for all meshes to 0 and then the problem wasn't there.

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