PDA

View Full Version : How come my stencil buffer demo doesn't work?



Joe the Programmer
03-29-2006, 06:15 PM
The idea is to take a beach ball that is hovering over a floor and put a reflection of the ball on the floor using the stencil buffer. How it's implemented is like this:

Pass #1
- the color mask is set to 0 so that nothing will be seen
- tell opengl that the stencil test will always pass; reference and destination values are 1
- when the test passes, 1s are put into the stencil buffer where the floor would be drawn
- the floor is drawn (but not visible)

Pass #2
- the color mask is set to all 1s this time
- set the stencil test to GL_EQUAL; reference and destination values are still 1
- clip the plane (0, -1, 0) (the floor is in the middle of the screen)
- reflect the ball upside down glScale3f(1, -1, 1)
- the ball is drawn (this will be the reflection)

Pass #3
- stencil testing is not used this time
- the floor is drawn (with blending) on top of the ball that was drawn before

Pass #4
- the real ball that hovers is drawn

The problem is that the reflection is clipped when it comes "up", but when the reflection goes down (as the real ball goes higher) the reflection pops out of the floor, which is what the stencil buffer is supposed to stop. Hopefully this image works:
http://static.flickr.com/41/120019012_0865bb0ddf.jpg
You can also view the code (http://dhost.info/joetheprogrammer/stupidcode.cpp) (only the part that I'm having problems with)

Ehsan Kamrani
03-29-2006, 06:57 PM
I taked a quick look at your code.It seems that your code is correct.Have you requested a stencil buffer with the following function:
glutInitDisplayMode( GLUT_STENCIL );
-Ehsan-

Joe the Programmer
03-29-2006, 08:03 PM
Hey, it works! Amazing what you can do once you know how to initalize glut :) .

jide
03-29-2006, 09:51 PM
Strange that it half works without stencil having been enabled.

Tin Whisker
03-30-2006, 07:17 AM
The reflection works fine -- it's just that it's not restricted to the floor. Still, the magic beach-ball looks pretty good anyway ;)

Joe the Programmer
03-30-2006, 09:54 AM
It wasn't really my idea, it was from the NeHe tutorials. The next one I'm trying to do is make shadows. :)