PDA

View Full Version : Reflections: Reflected object appears off "mirror"



Structural
01-04-2003, 02:19 AM
I've been playing about with reflections and the stencil buffer for the first time the past few days and I've run into a problem again:
The reflected object (a sphere) also appears in the "void" around the "mirror" object.

So basically I have a plane and a sphere, and when I move the reflection off the plane, it becomes visible from beneath the plane.

Code:




void Game3D::reflect(Ball3D ball, int wall)
{
double eqr[] = {0,0,0,0};
float scaler[] = {1,1,1};
if (wall == 1)
{
eqr[1] = 1.0f;
eqr[3] = w_scale;
}
if (wall == 2)
{
eqr[1] = -1.0f;
eqr[3] = -w_scale;//floor
}
if (wall == 3)
{
eqr[0] = 1.0f;
eqr[3] = w_scale;
}
if (wall == 4)
{
//double eqr[] = {0.0f,-1.0f, 0.0f, -1.0f};
eqr[0] = -1.0f;
eqr[3] = -w_scale;
}

if (eqr[0] != 0)
scaler[0]=-1;

if (eqr[1] != 0)
scaler[1]=-1;

if (eqr[2] != 0)
scaler[2]=-1;


glClear(GL_STENCIL_BUFFER_BIT);

glColorMask(0,0,0,0);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

glDisable(GL_DEPTH_TEST);
drawWalls(wall);


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();
glTranslatef(eqr[3]*2*abs(eqr[0]), eqr[3]*2*abs(eqr[1]), eqr[3]*2*abs(eqr[2]));


glScalef(scaler[0], scaler[1], scaler[2]);
glLightfv(GL_LIGHT0, GL_POSITION, LightPos);

ball.draw(); // Draw The Sphere (Reflection)
glPopMatrix();
glDisable(GL_CLIP_PLANE0);
glDisable(GL_STENCIL_TEST);
glLightfv(GL_LIGHT0, GL_POSITION, LightPos);
glEnable(GL_BLEND);
glDisable(GL_LIGHTING);
glColor4f(1.0f, 1.0f, 1.0f, 0.8f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
drawWalls(wall);

glEnable(GL_LIGHTING);
glDisable(GL_BLEND);
}


If anyone can help me with this I'd be very happy http://www.opengl.org/discussion_boards/ubb/smile.gif

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

JustHanging
01-04-2003, 06:07 AM
Hi,

I'm not very sure about this, but my best guess is that you must have depth test enabled when using stencil. After all, the stencil operations depend on the result of the depth test.

Also, make sure you properly clear the stencil buffer. If you have many reflecting walls, use a different reference value for each. In that case you must change mask to 0, which means no mask. And last, make sure you request stencil buffer when creating the window, otherwise you won't get it.

-Ilkka

Structural
01-04-2003, 07:50 AM
Thank you,
I looked into the things you mentioned and I noticed that I was missing the most important thing: Request the stencil buffer on window creation.
This part isn't covered AT ALL in my book so no wonder I couldn't figure it out.

Thanks!