# Check if point is in frustum.

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]=Am[0][0] + Bm[1][0] + Cm[2][0] + Dm[3][0];
lpOut[1]=Am[1][0] + Bm[1][1] + Cm[2][1] + Dm[3][1];
lpOut[2]=Am[2][0] + Bm[1][2] + Cm[2][2] + Dm[3][2];
lpOut[3]=Am[3][0] + Bm[1][3] + Cm[2][3] + Dm[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 );

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();
}

}

lpOut[0]=Am[0][0] + Bm[1][0] + Cm[2][0] + Dm[3][0];
lpOut[1]=Am[1][0] + Bm[1][1] + Cm[2][1] + Dm[3][1];
lpOut[2]=Am[2][0] + Bm[1][2] + Cm[2][2] + Dm[3][2];
lpOut[3]=Am[3][0] + Bm[1][3] + Cm[2][3] + Dm[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)

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”.