Hello! I’ve implemented a simple stencil mirror. It works fine with one mirror. But when I make two mirrors facing each other, the (recursive) mirrors appear in the wrong location.
Here is the code:
// check mirror recursion count
if (MirrorRecursion >= MAX_MIRROR_RECURSION)
return DM_MIRROR_NO_ERROR;MirrorRecursion++;
// create stencil mask
glClear(GL_STENCIL_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glColorMask(0, 0, 0, 0);
glBegin(GL_QUADS);
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(a.x, a.y, a.z);
glVertex3f(b.x, b.y, b.z);
glVertex3f(c.x, c.y, c.z);
glVertex3f(d.x, d.y, d.z);
glEnd();// draw mirrored scene
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, p);dmFlipFaces();
glPushMatrix();
glMultMatrixf(m);
glClear(GL_DEPTH_BUFFER_BIT);
Scene->Render();
glPopMatrix();
dmFlipFaces();glDisable(GL_CLIP_PLANE0);
glDisable(GL_STENCIL_TEST);MirrorRecursion–;
p is the mirror’s plane equation, m is the following matrix (yoyo gave me):
m[0] = 1 -2 * p[0] * p[0];
m[1] = - 2 * p[1] * p[0];
m[2] = - 2 * p[2] * p[0];
m[3] = 0.0;
m[4] = - 2 * p[0] * p[1];
m[5] = 1 - 2 * p[1] * p[1];
m[6] = - 2 * p[2] * p[1];
m[7] = 0.0;
m[8] = - 2 * p[0] * p[2];
m[9] = - 2 * p[1] * p[2];
m[10] = 1 - 2 * p[2] * p[2];
m[11] = 0.0;
m[12] = - 2 * p[0] * p[3];
m[13] = - 2 * p[1] * p[3];
m[14] = - 2 * p[2] * p[3];
m[15] = 1.0;
and dmFlipFaces() changes the polygon facing (GL_CW if it was GL_CCW, and GL_CCW if it was GL_CW). Have you any idea what am I making wrong? Thanks.