PDA

View Full Version : frustum culling code buggy ?



HamsterofDeath
02-06-2003, 12:31 PM
is the following code correct ?
i'm asking because it seems to return true everytime....

public boolean rectangleInFrustum( float x, float y, float z, float xSize,float ySize,float zSize )
{
lsu[0]=x-xSize;
lsu[1]=x+xSize;
lsu[2]=y-ySize;
lsu[3]=y+ySize;
lsu[4]=z-zSize;
lsu[5]=z+zSize;
for(int i = 0; i < 6; i++ )
{
if(m_Frustum[i][A] * (lsu[0]) + m_Frustum[i][B] * (lsu[2]) + m_Frustum[i][C] * (lsu[4]) + m_Frustum[i][D] > 0)
continue;
if(m_Frustum[i][A] * (lsu[1]) + m_Frustum[i][B] * (lsu[2]) + m_Frustum[i][C] * (lsu[4]) + m_Frustum[i][D] > 0)
continue;
if(m_Frustum[i][A] * (lsu[0]) + m_Frustum[i][B] * (lsu[3]) + m_Frustum[i][C] * (lsu[4]) + m_Frustum[i][D] > 0)
continue;
if(m_Frustum[i][A] * (lsu[1]) + m_Frustum[i][B] * (lsu[3]) + m_Frustum[i][C] * (lsu[4]) + m_Frustum[i][D] > 0)
continue;
if(m_Frustum[i][A] * (lsu[0]) + m_Frustum[i][B] * (lsu[2]) + m_Frustum[i][C] * (lsu[5]) + m_Frustum[i][D] > 0)
continue;
if(m_Frustum[i][A] * (lsu[1]) + m_Frustum[i][B] * (lsu[2]) + m_Frustum[i][C] * (lsu[5]) + m_Frustum[i][D] > 0)
continue;
if(m_Frustum[i][A] * (lsu[0]) + m_Frustum[i][B] * (lsu[3]) + m_Frustum[i][C] * (lsu[5]) + m_Frustum[i][D] > 0)
continue;
if(m_Frustum[i][A] * (lsu[1]) + m_Frustum[i][B] * (lsu[3]) + m_Frustum[i][C] * (lsu[5]) + m_Frustum[i][D] > 0)
continue;

return false;
}

return true;
}

shinpaughp
02-06-2003, 01:59 PM
The only time your function will return false is if on the final iteration of the for loop all 6 equations are less than or equal to 0. Hard to tell exactly what is going on in your code, but I think you probably want to nest your if statements rather than use continue which jumps to next value of i at start of for loop. I assume if all criteria is met it should go to next iteration otherwise return false.

So, probably :




if...
if...
if...
if...
if...
if...
continue;
return false;


If I am wrong in my assumption, please state so.

added code tags so tabs would remain

[This message has been edited by shinpaughp (edited 02-06-2003).]

[This message has been edited by shinpaughp (edited 02-06-2003).]

HamsterofDeath
02-06-2003, 11:59 PM
i found the bug.. it was outside the function
but don't worry, i got another question :
is there a way to optimize the code ?

i took the one from gametutorials and modified it from cubeInFrustum to RectangleinFrustum, but i remember someone saying there is a faster way to do this...

JustHanging
02-07-2003, 01:59 AM
It's fast enough, don't worry. It's how you use it, I hope you're not trying to cull individual polygons.

With your octree you should have big cells, which get divided into 8 smaller ones, each of which again gets divided into 8 smaller ones and so. You start from the big ones. If the cel gets culled, stop there and don't draw the cel or any of it's children. If it's not culled, proceed to it's children, testing each of them. Continue recursing until you hit a cel you want to draw, and if it's not culled, draw it.

-Ilkka