Hi,
So this is a stupid question but I cant seem to get it right.
I am trying to draw a light’s frustum by drawing a unit cube (-1, -1, -1 ) -> (1,1,1) yet I cant seem to get the matrix right.
I have tryed doing Inverse( modelView * projection ) from both the light space, plus many more. Yet these all result in either a simple box being in front of the light or a moving box. Neither correct. So any ideas or help. Thanks
The unit cube is the light’s frustum in the light’s clip space. To actually draw it from the viewer’s POV you need to transform it from the light’s clip space to a common world space, then from this world space to the viewer’s clip space in the usual fashion.
The matrix would thus be
view-to-clip[viewer] * world-to-view[viewer] * inverse( view-to-clip[light] * world-to-view[light] )
where view-to-clip refers to the respective projection matrices and world-to-view is the part of the modelview matrix that doesn’t change per object.
so if I understand you right it would be
projection[camera] * modelview[camera] * inverse( projection[light] * modelview[light] ) right??
edit: That did not seem to work right…looks as if it almost does but the unit cube moves with the camera…
In that case it would seem that your “modelview[camera]” matrix is missing the camera translation.
well here is what I have… maybe you can tell me whats wrong…
getting the light values
void RenderShadows(){
float tmp[16];
//for( int i = 0; i < NUMLIGHTS; i++ ){
// if( !light[i].isEnabled )
// break; //no need to do anything
Light* l = light;
//set set and pop the matrixes so nothing is saved
//NOTE: NOT OGL3.0, needs to be changed, how I dont know
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
//set the viewport so we only draw what the map can haddel
//dont belive this is OGL3.0 either
//also very similar to sissors test, speed test??
glPushAttrib( GL_VIEWPORT_BIT );
glViewport( 0, 0, l->width, l->height );
//prevent z-fighting
glPolygonOffset( 1.0, 4096.0 );
glEnable( GL_POLYGON_OFFSET_FILL );
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, shadow1FBO );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glTranslatef( l->center[0], l->center[1], l->center[2] );
glGetFloatv( GL_MODELVIEW_MATRIX, tmp );
mvInv.set_value( tmp );
DrawScene();
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
glDisable( GL_POLYGON_OFFSET_FILL );
glPopAttrib();
glPopMatrix();
glMatrixMode( GL_PROJECTION );
glGetFloatv( GL_PROJECTION_MATRIX, tmp );
projInv.set_value( tmp );
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
}
and the camera values
void Render(){
glslLoadProgram( vsmFirst );
RenderShadows();
glslUnloadProgram();
glLoadIdentity();
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
camera.setView();
glMatrixMode( GL_PROJECTION );
glGetFloatv( GL_PROJECTION_MATRIX, proj );
glMatrixMode( GL_MODELVIEW );
glGetFloatv( GL_MODELVIEW_MATRIX, mv );
DrawLight();
DrawScene();
if( showShadows )
DrawShadows();
SDL_GL_SwapBuffers();
}
and drawing the cube
void DrawFrustum(Light* l){
nv::matrix4<float> tmp = inverse( projInv * mvInv );
glPushMatrix();
glMultMatrixf( proj );
glMultMatrixf( mv );
glMultMatrixf( tmp._array );
glColor3f( 1.0, 0.0, 0.0 );
DrawUnitCube();
glPopMatrix();
}