I’ve used glClipPlane in rendering reflections in the past. The usual deal, flipping the scene, setting a clipping plane, rendering to FBO, etc – it worked fine.
Now, I’m refactoring how I do this to make it more generic, and have encountered a baffling problem. The clip plane “slides” with the camera! It’s as if I’m putting it into the projection matrix ( or something to that effect, I’m totally confused ).
It can be best described by this video I recorded:
ClippingPlaneWoes.mov
Here’s my relevant code:
void World::displayForCamera( const CameraRef &camera, bool shadows )
{
LightRef noLight;
/*
Set camera
*/
glError();
Frustum frustum = camera->frustum();
if ( _renderingReflection )
{
_activeReflectionCamera->setFarPlane( _farPlane );
_activeReflectionCamera->set();
plane reflectionPlane = _activeReflectionCamera->reflectionPlane();
mat4 householderTransform = _activeReflectionCamera->householderTransform();
vec3 refPoint = vec3( 0,0,0 ) + ( vec3( reflectionPlane.a, reflectionPlane.b, reflectionPlane.c ) * reflectionPlane.d );
glError();
GLdouble clipPlane[4] = { reflectionPlane.a, reflectionPlane.b, reflectionPlane.c, reflectionPlane.d };
glEnable( GL_CLIP_PLANE0 );
glClipPlane( GL_CLIP_PLANE0, clipPlane );
glPushMatrix();
glTranslatef( refPoint.x, refPoint.y, refPoint.z );
glMultMatrixf( householderTransform );
glTranslatef( -refPoint.x, -refPoint.y, -refPoint.z );
glError();
frustum = _activeReflectionCamera->reflectedFrustum();
}
else
{
camera->setFarPlane( _farPlane );
camera->set();
}
// render the world
if ( _renderingReflection )
{
glDisable( GL_CLIP_PLANE0 );
glPopMatrix();
}
}
And here’s ( relevant parts of ) the method Camera::set()
void Camera::set( void )
{
glMatrixMode( GL_PROJECTION );
glLoadMatrixf( _projection );
glMatrixMode( GL_MODELVIEW );
glLoadMatrixf( _modelview );
_frustum.set( projectionWithFarPlane(), _modelview, _position );
}
Finally, I use a macro ‘glError’ sprinkled around to dump gl errors at runtime ( it’s only enabled for debug builds ) and usually I can catch mistakes like matrix underflow from too much popping and other problems. But I’m not seeing any errors…
Observing the clip plane sliding around, you’ll notice it is always directly in line with the camera – e.g., you’re looking straight down the clipping plane. This implies to me it’s not being multiplied by the current modelview to me. That’s the best I can come up with.