Advertisement

reflections

Started by August 22, 2002 01:19 PM
2 comments, last by Crispy 22 years, 6 months ago
Hi, I'm trying to implement reflections in my baby-engine, using NeHe's stencil buffer tutorial as a reference point. Can someone please give me a down-to-earth explanation what the stencil buffer is (as I figure it, it is part of the depth buffer that can be used to mask whatever's happening in the depth buffer). Here's some code for drawing a polygon above a bigger polygon which reflects it:
    
//based on NeHe totorial 26

 
double eqr[] = {0.0f, -1.0f, 0.0f, 0.0f};
glLoadIdentity();
glColorMask(0,0,0,0);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glDisable(GL_DEPTH_TEST);
//draw the reflecting surface

DrawFloor();
glEnable(GL_DEPTH_TEST);
glColorMask(1,1,1,1);
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glEnable(GL_CLIP_PLANE0);
glClipPlane(GL_CLIP_PLANE0, eqr);
glPushMatrix();
glScalef(1.0f, -1.0f, 1.0f);
  //draw the smaller polygon, the "reflectee"

  DrawObject();
glPopMatrix();
glDisable(GL_CLIP_PLANE0);
glDisable(GL_STENCIL_TEST);
glEnable(GL_BLEND);
glColor4f(1.0f, 1.0f, 1.0f, 0.8f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
DrawFloor();
glDisable(GL_BLEND);
DrawObject();
glFlush();
  
The problem is that the mirrored object is drawn as well (the smaller polygon appears symmetrically on both sides of the mirror). Can anyone see anything wrong with the code? And finally, implementing the mirror effect in a real environment. When I draw my bsp world around the reflective surface it gets all messed up. Can someone name the steps I'd have to take to properly create a mirror face that would reflect what's oppsite to it, not create a mirrored (upside down) copy of the entire world attached to the bottom of the original world, also messing up face opacity in the "original" map (I believe "shearing" is the word here). Many thanks in advance, Crispy edit: formatting problems [edited by - crispy on August 22, 2002 2:22:43 PM]
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
My understanding of the stencil buffer:
The stencil buffer is a test preformed on every pixel in the frame buffer to determine if it should be drawn onto a screen. Its like a cookie cutter. For example if you r making a mirror and want to hang it on a wall like so:


  ///////////////////__________/////|          |////|  _     __|////| / \   | *|////|/___\ I___|////|  I   I___|////|__I_______|//////////////////////////////////  


(That is a lamp and a couch reflected in the mirror if u were wondering) To create the reflected effect you draw the scene reflected around the plane of the mirror. See how the couch is half cut off? You dont want to draw the couch entirely and using the stencil buffer accomplishes this. You can cut out any shape as long as you draw it and then specify it to the stencil buffer.

This is all great if you can manage all the reflected surfaces and have the processing power to draw everything at least twice. However if youre not this fortunate you can use cube mapping. If nothing in your environment changes (you can probably pull off a few changes in your env) you can use a technique similar to spheremapping. However in cube mapping there r 6 textures that u take from your environment. All you need is support on your video card.


"Free advice is seldom cheap."
Advertisement
ok.

with an 8 bit stencil buffer, you get, well, 8 bits for every pixel. Ie, it''s just like a colour channel. What you can do with it is when drawing normally, you can increment the value at each pixel drawn, decrement it, set it to a fixed value, and a couple of other operations. Things like textures, etc, don''t come into it at all.
Then, you can set up drawing so that drawing of a pixel to ALL other buffesr (eg, colour, depth, etc) will only occur if the stencil value at that pixel passes an experession, such as being not equal to 10, or something like that.

This is the basis for stencil shadows, where the shadow volumes will increment/decrement stencil values, and the shadow will only occur where the number of inc''s and dec''s is not the same. (well, roughly anyway) - ie, you start with the entire stencil buffer set to 128''s, and do all your volumes, then only draw shadows where the value is not 128, or draw light where it is. Etc.
Okay, thanks. I suppose I have a fair understanding of the stencil buffer now. Still, now there remains the question of the code - can someone point out why it''s drawing two symmetrical objects of the "reflectee" - as I understand it the stencil buffer shouldn''t allow the other object to be drawn anywhere else than on the reflecting surface on the reflecting side...

Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared

This topic is closed to new replies.

Advertisement