PDA

View Full Version : Check if point is in frustum.



rogerhe
03-03-2004, 06:53 PM
following code isn't work correctly,
I don't know why, could you help me?

/////////////////////////////////////////////////////////////////////
void MultiMatrix(GLfloat m[4][4], GLfloat lpOut[4])
{
GLfloat A,B,C,D;
//
A=lpOut[0]; B=lpOut[1];
C=lpOut[2]; D=lpOut[3];

lpOut[0]=A*m[0][0] + B*m[1][0] + C*m[2][0] + D*m[3][0];
lpOut[1]=A*m[1][0] + B*m[1][1] + C*m[2][1] + D*m[3][1];
lpOut[2]=A*m[2][0] + B*m[1][2] + C*m[2][2] + D*m[3][2];
lpOut[3]=A*m[3][0] + B*m[1][3] + C*m[2][3] + D*m[3][3];
}
///////////////////////////////////////////////////////////////////
BOOL bInFrustum(Point3F p)
{
float proj[4][4];
float modl[4][4];
glGetFloatv( GL_PROJECTION_MATRIX, (GLfloat *)proj );
glGetFloatv( GL_MODELVIEW_MATRIX, (GLfloat *)modl );

float P_Array[4]={ p.x, p.y, p.z, 1.0f};
MultiMatrix(modl, P_Array);
MultiMatrix(proj, P_Array);

//Divide W
P_Array[0]/=P_Array[3];
P_Array[1]/=P_Array[3];
P_Array[2]/=P_Array[3];

if( P_Array[0]>-1 && P_Array[0]<1 &&
P_Array[1]>-1 && P_Array[1]<1 &&
P_Array[2]>-1 && P_Array[2]<1 )
return TRUE;
else
return FALSE;
}

/////////////////////////////////////////////////
void render()
{
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

static float angle=0.1f;
glTranslatef( 0,0,-15);
glRotatef(angle,0,0,1);
glRotatef(angle/2,0,1,0);
angle+=1.7f;

Point3F p[2];
p[0].x=-8,p[0].y=0,p[0].z=0;
p[1].x=8,p[1].y=0,p[1].z=0;
if( bInFrustum( p[0] ) && bInFrustum( p[1] ) )
{
glBegin(GL_LINES);
for ( int i=0;i<2;i++)
glVertex3f(p[i].x,p[i].y,p[i].z);
glEnd();
}

}

3B
03-03-2004, 08:18 PM
lpOut[0]=A*m[0][0] + B*m[1][0] + C*m[2][0] + D*m[3][0];
lpOut[1]=A*m[1][0] + B*m[1][1] + C*m[2][1] + D*m[3][1];
lpOut[2]=A*m[2][0] + B*m[1][2] + C*m[2][2] + D*m[3][2];
lpOut[3]=A*m[3][0] + B*m[1][3] + C*m[2][3] + D*m[3][3];


that bit looks suspicious...specifically the array indices when multiplying by A (or the other 3, don't remember which way you want them with a 2d array...either way, seems like they should match)

KuriousOrange
03-22-2004, 07:16 AM
Crickey!
allocating 32 floats on the stack for each point?!!
reading back modelview and projection for each point?!
xforming the point by both matrices each time?!
3 float divides?!!

I think I'm going to have a heart attack.
There are alternatives you know.
For instance, extract the 6 frustum planes from the modelview*projection matrix every *frame*, and simply do a few dot products between the point and the planes to find your answer. You can 'early out' of the dot product tests for a good percentage of queries. There is a tutorial somewhere, just google for "extract planes frustum".