• 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

Frustum Culling

6 posts in this topic

I want to do some basic view frustum culling, using bounding spheres for my objects.

The spheres are positioned in world space and I am constructing planes for my frustum.

I then calculate the distance between my spheres centers and each plane and if it is bigger than the radius and on the wrong side, the object is culled. This can be optimized by first culling with a sphere around the frustum and maybe some caching for the plane that culled an object in the previous frame and generally works great. Something like this can be found easily online and is most probably good enough for most cases.


A problematic case looks like this:


where "View" is the visible area, "PL" the left plane, "PB" the bottom plane and "P" the center of the sphere to cull.

It is obviously inside the top and right plane (where ever those are) and while the point is clearly on the wrong side of PL and PB, the distance to each minus the radius is inside the both planes, but still the sphere would not be visible.

This usually shouldn´t be a problem, but I am using it to cull lights for tiled shading, so I´ve got many small frustums and many in comparison big spheres, so this is a big issue.


The solution I came up with is simple, all I do is (additional to the culling above) storing the distances to the planes and using pythagoras to get the squared distance to the frustums corner which means for example for the case above: distToPL*distToPL+distToPB*distToPB

For visibility, the result can be compared to the squared radius.


Now I am wondering if this makes sense (I am not completely sure if maybe the perspective frustum breaks it? From my understanding it shuldn´t, but that doesn´t have to mean much...) and if there is any better alternative?


Share this post

Link to post
Share on other sites

I'm confused with the problem you're facing, all I can offer is some code I use for Frustum Culling. Hope it helps in any way.


/* Before rendering the object check if it is visible */
Object::IsVisible() {
	Frustum frustum = scene->GetCamera()->GetFrustum();
	frustum.Inside(this.position, this.radius);

   Create: Plane planes[6]
   Iterate through all 6 planes, if the point is outside ANY plane;
   it's not visible
bool Frustum::Inside(Vec3 point, float radius) {
	for (int i=0; i<6; ++i) {
		if (!planes[i].Inside(point, radius)) return false;
	return true; // Point is inside all planes

/* Up to you to find out how PointToPlaneDistance function works */
bool Plane::Inside(Vec3 point, float radius) {
	float distance = Math::PointToPlaneDistance(this, point);
	return (distance >= -radius); //We are out of visible area

Share this post

Link to post
Share on other sites

Assuming you know the point of intersection (the corner) between PB and PL (let's call it PI), you can calculate a vector V = (P - PI). If (length(V) < sphereRadius) your point is inside, otherwise, check the dot product of normalized(V) with PB and PL plane normals, if and only if both values are negative, point is outside, if the comparison fails, it's inside.


Take this with a grain of salt since I've never tested or even considered this case when doing frustum culling, but this would be my first guess to solve this issue.


Hope it helped.


Share this post

Link to post
Share on other sites

IgnatusZuk, thanks, but your code has the exact same problem I am trying to solve: There are some cases, where the function return visibility, while the object is actually outside the view frustum. In most cases I expect this to be just very few objects, so anything more complicated for culling is not worth it.


TMarques, I may should have mentioned that I am culling spheres in a 3D frustum, so those corners are actually lines and not points, but my approach to get that V as the distance to the planes should be fine, except the thing you mentioned, where I have to check which side of the planes the center is on, which explains why now in my implementation too many lights are culled.

Thank you very much for that hint :)


If there are any more ideas, please post them, as I´d love to find a better way if there is one :)


Share this post

Link to post
Share on other sites
its not clear to me from your description whether this problem must be solved in 3d or can be solved in 2d.
if you just want to test if the lit area from a light is in view, you may be able to cull in 2d, perhaps with something as simple as bounding box distance.
if you're culling a large light sphere in 3d (hanging from a post, etc, FPS shooter view), you might try "cull if outside all 6 planes" vs "don't cull if within any of the 6 planes".
here's the code i use to cull large chunks to the frustum. the chunks are 300x300. the far plane is 1000. the frustum is NOT tilted up/down. chuncks are only clipped to the near, far, left, and right planes:
// calculate the planes 
void Zcalc_frustum_planes2()          // rotation only
D3DXMATRIX  projectionMatrix,       // ROCKLAND: copy of current projection matrix
            viewMatrix,             // ROCKLAND: copy of current view matrix
            matrix,m;                 // ROCKLAND: frustum matrix  ( view * modified proj )
projectionMatrix = Zprojection_matrix;
D3DXMatrixMultiply(&matrix, &viewMatrix, &projectionMatrix);
// Calculate left plane of frustum.
m_planes2[2].a = matrix._14 + matrix._11; 
m_planes2[2].b = matrix._24 + matrix._21;
m_planes2[2].c = matrix._34 + matrix._31;
m_planes2[2].d = matrix._44 + matrix._41;
D3DXPlaneNormalize(&m_planes2[2], &m_planes2[2]);
// Calculate right plane of frustum.
m_planes2[3].a = matrix._14 - matrix._11; 
m_planes2[3].b = matrix._24 - matrix._21;
m_planes2[3].c = matrix._34 - matrix._31;
m_planes2[3].d = matrix._44 - matrix._41;
D3DXPlaneNormalize(&m_planes2[3], &m_planes2[3]);
// Calculate top plane of frustum.
m_planes2[4].a = matrix._14 - matrix._12; 
m_planes2[4].b = matrix._24 - matrix._22;
m_planes2[4].c = matrix._34 - matrix._32;
m_planes2[4].d = matrix._44 - matrix._42;
D3DXPlaneNormalize(&m_planes2[4], &m_planes2[4]);
// Calculate bottom plane of frustum.
m_planes2[5].a = matrix._14 + matrix._12;
m_planes2[5].b = matrix._24 + matrix._22;
m_planes2[5].c = matrix._34 + matrix._32;
m_planes2[5].d = matrix._44 + matrix._42;
D3DXPlaneNormalize(&m_planes2[5], &m_planes2[5]);
// Calculate near plane of frustum.
m_planes2[0].a = matrix._13;        
m_planes2[0].b = matrix._23;        
m_planes2[0].c = matrix._33;        
m_planes2[0].d = matrix._43;        
D3DXPlaneNormalize(&m_planes2[0], &m_planes2[0]);
// Calculate far plane of frustum.
m_planes2[1].a = matrix._14 - matrix._13; 
m_planes2[1].b = matrix._24 - matrix._23;
m_planes2[1].c = matrix._34 - matrix._33;
m_planes2[1].d = matrix._44 - matrix._43;
D3DXPlaneNormalize(&m_planes2[1], &m_planes2[1]);
// test corner vs plane.
int Zpoint_inside_frustum_plane(int x,int y,int z,int plane_index)
if ( D3DXPlaneDotCoord(&m_planes2[plane_index],&v) <  0.0f )  
// test area vs plane.
int Zarea_inside_frustum_plane(int x1,int z1,int x2,int z2,int plane_index)
if ( Zpoint_inside_frustum_plane(x1,0,z1,plane_index) ||
     Zpoint_inside_frustum_plane(x1,0,z2,plane_index) ||
     Zpoint_inside_frustum_plane(x2,0,z1,plane_index) ||
     Zpoint_inside_frustum_plane(x2,0,z2,plane_index) )   return(1);
// test area vs near,far,left,and right planes...
int Zarea_in_frustum(int x1,int z1,int x2,int z2)
if (! Zarea_inside_frustum_plane(x1,z1,x2,z2,2)) return(0); // left
if (! Zarea_inside_frustum_plane(x1,z1,x2,z2,3)) return(0); // right
if (! Zarea_inside_frustum_plane(x1,z1,x2,z2,0)) return(0); // near
if (! Zarea_inside_frustum_plane(x1,z1,x2,z2,1)) return(0); // far

Share this post

Link to post
Share on other sites

I am using it to cull lights for tiled shading


this is the killer here. any unculled light will be a performance hit. if you were merely culling a mesh, it wouldn't matter, as the poly engine would eventually clip it with relatively little overhead, so it would be ok to let it "slip through" your frustum cull. i assume you have some sort of list of lights you go through and cull to see which you should activate when setting lights for the scene. have you considered a change of data structure? i've had a lot of luck recently using that approach to speed up drawing stuff. for example, instead of a list of lights, a 2d array map of lights and you simply activate all within some bounding box distance of the camera, no culling required. i've had a lot of luck copying data from my existing data structures into new ones that are in a format which can be quickly processed for drawing.


Share this post

Link to post
Share on other sites

For now it is just a list, later they will probably be sorted into an octree or something similar, which should of course be an improvement but for now it is good enough to optimize the culling itself a little (for less than 1000 lights, it performs very well, using multithreading and some basic code optimizations).


My code seems to work well and looks like this now:

#define Distance(plane, op, r) { \
	float dot = (position.x * plane.normal.x + position.y * plane.normal.y + position.z * plane.normal.z);\
	distance = dot - plane.d; \
	if(distance op r) \
		continue; \

const Vector3& position = light->_worldPosition;
const float range = light->_range;
float distance, dr, dl, dt, db;
Distance(plright, >, range);
dr = distance;
Distance(plleft, <, -range);
dl = distance;
Distance(plbottom, <, -range);
db = distance;
Distance(pltop, >, range);
dt = distance;

float sqrange = range*range;
if(dr > 0.0f && db < 0.0f && dr*dr+db*db > sqrange)
if(dr > 0.0f && dt > 0.0f && dr*dr+dt*dt > sqrange)
if(dl < 0.0f && db < 0.0f && dl*dl+db*db > sqrange)
if(dl < 0.0f && dt > 0.0f && dl*dl+dt*dt > sqrange)


It´s not pretty, but I have to fix some other things before cleaning up and while my planes have a function to get it´s distance to a point, at 32*24*1024*4 calls, the function call overhead was the biggest performance hit ;)

Edited by _Slin_

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