Reflection basics

I’ve been going through Nehe’s tutorial on reflections with the stencil buffer and I have tried to implement it myself, but I’m not able to pull the trick off… Therefore a few questions:

Nehe uses a single quad to put the reflection on, I have a crapload of quads (all in the same plane tho) I want to put my reflection on. Does this effect the method?

Does lighting effect the reflection? Nehe uses a single diffused light, I followed this example, but is it nescesary for the reflection?

How important is the plane equation given to “glClipPlane” and which effect does it have on the reflection?

That’s it for now.
My hardware DOES support the stencil buffer (as nehe’s code works) and I almost exactly copy-pasted his code (only changed the functions that draw the objects)


Many quads won’t affect this IF they are all coplanar.

The glClipPlane is essential ONLY if you have geometry on both sides of the reflecting plane. If you can guarantee that the geometry you are drawing for the reflection will be on one side of the reflector yo don’t need the glip plane.

Make sure you clear the depth buffer before the reflection render.

For best results the light should be reflected with the geometry (reposition it after the reflection matrix) but fundamentally it won’t break things if it isn’t right. More complex lighting shouldn’t break this either.

[This message has been edited by dorbie (edited 01-03-2003).]


I got a reflection going with a single plane, and I know what went wrong though I haven’t solved it yet.

I played about with Nehe’s source code a tad more and changed the position of the plane to the position that the plane has in my application. The result is that the “reflection” appears TOO SOON on the plane because the plane equation is incorrect.
The plane equation is given to the glClipPlane and equals to {0,-1,0,0}. I have changed this to match the new plane ({0,-1,0,-1}) but there is NO change. Whatever I do with the fourth parameter I can’t get it ON the plane. There doesn’t happen anything.

the code I used is this:


// Clip Plane Equations
double eqr[] = {0.0f,-1.0f, 0.0f, -1.0f}; // Plane Equation To Use For The Reflected Objects

glLoadIdentity(); // Reset The Modelview Matrix
glTranslatef(0.0f, -0.6f, zoom); // Zoom And Raise Camera Above The Floor (Up 0.6 Units)
glColorMask(0,0,0,0); // Set Color Mask
glEnable(GL_STENCIL_TEST); // Enable Stencil Buffer For “marking” The Floor
glStencilFunc(GL_ALWAYS, 1, 1); // Always Passes, 1 Bit Plane, 1 As Mask
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); // We Set The Stencil Buffer To 1 Where We Draw Any Polygon
// Keep If Test Fails, Keep If Test Passes But Buffer Test Fails
// Replace If Test Passes
glDisable(GL_DEPTH_TEST); // Disable Depth Testing
DrawFloor(); // Draw The Floor (Draws To The Stencil Buffer)
// We Only Want To Mark It In The Stencil Buffer
glEnable(GL_DEPTH_TEST); // Enable Depth Testing
glColorMask(1,1,1,1); // Set Color Mask to TRUE, TRUE, TRUE, TRUE
glStencilFunc(GL_EQUAL, 1, 1); // We Draw Only Where The Stencil Is 1
// (I.E. Where The Floor Was Drawn)
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); // Don’t Change The Stencil Buffer
glEnable(GL_CLIP_PLANE0); // Enable Clip Plane For Removing Artifacts
// (When The Object Crosses The Floor)
glClipPlane(GL_CLIP_PLANE0, eqr); // Equation For Reflected Objects
glPushMatrix(); // Push The Matrix Onto The Stack
glScalef(1.0f, -1.0f, 1.0f); // Mirror Y Axis
glLightfv(GL_LIGHT0, GL_POSITION, LightPos); // Set Up Light0
glTranslatef(0.0f, height, 0.0f); // Position The Object
glRotatef(xrot, 1.0f, 0.0f, 0.0f); // Rotate Local Coordinate System On X Axis
glRotatef(yrot, 0.0f, 1.0f, 0.0f); // Rotate Local Coordinate System On Y Axis
DrawObject(); // Draw The Sphere (Reflection)
glPopMatrix(); // Pop The Matrix Off The Stack
glDisable(GL_CLIP_PLANE0); // Disable Clip Plane For Drawing The Floor
glDisable(GL_STENCIL_TEST); // We Don’t Need The Stencil Buffer Any More (Disable)
glLightfv(GL_LIGHT0, GL_POSITION, LightPos); // Set Up Light0 Position
glEnable(GL_BLEND); // Enable Blending (Otherwise The Reflected Object Wont Show)
glDisable(GL_LIGHTING); // Since We Use Blending, We Disable Lighting
glColor4f(1.0f, 1.0f, 1.0f, 0.8f); // Set Color To White With 80% Alpha
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Blending Based On Source Alpha And 1 Minus Dest Alpha
DrawFloor(); // Draw The Floor To The Screen
glEnable(GL_LIGHTING); // Enable Lighting
glDisable(GL_BLEND); // Disable Blending
glTranslatef(0.0f, height, 0.0f); // Position The Ball At Proper Height
glRotatef(xrot, 1.0f, 0.0f, 0.0f); // Rotate On The X Axis
glRotatef(yrot, 0.0f, 1.0f, 0.0f); // Rotate On The Y Axis
DrawObject(); // Draw The Ball
xrot += xrotspeed; // Update X Rotation Angle By xrotspeed
yrot += yrotspeed; // Update Y Rotation Angle By yrotspeed
glFlush(); // Flush The GL Pipeline
return TRUE; // Everything Went OK

Basically a cut 'n paste job and I only changed the plane equation in the code above (double eqr[] = {0.0f,-1.0f, 0.0f, -1.0f} ) and I lowered the floor to y==-1 (no code included, it’s just a simple blended quad). No results…

What am I missing?

[This message has been edited by Structural (edited 01-03-2003).]

Nevermind… I solved the previous problem… It had to do with the translation of the object.

Next problem: the reflection is NOT ONLY projected on the correct plane… It is also projected on not reflecting surfaces (which I have not drawn on the stencil buffer as far as I know)…
Any tips/hints of what it could be?