// Draw mirror polygon into stencil buffer
glEnable(GL_STENCIL_TEST);
glClear(GL_STENCIL_BUFFER_BIT);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glBegin(GL_POLYGON);
for (unsigned int j = 0; j < mirrors[i]->vertexCount(); ++j)
glVertex3f(mirrors[i]->vertex(j).x(), mirrors[i]->vertex(j).y(), mirrors[i]->vertex(j).z());
glEnd();
// Clear depth buffer
glClear(GL_DEPTH_BUFFER_BIT);
// Reflect position and direction in mirror plane
Plane3d mirrorPlane(mirrors[i]->plane());
const Vec3 originalPosition = Camera::instance().position();
const Vec3 originalDirection = Camera::instance().direction();
const Vec3 mirroredPosition = originalPosition - 2.0f * mirrorPlane.n() *
mirrorPlane.distance(originalPosition);
Vec3 lookAt = originalPosition + originalDirection;
const Vec3 mirroredDirection = lookAt - 2.0f * mirrorPlane.n() *
mirrorPlane.distance(lookAt) - mirroredPosition;
// Save new viewpoint/position
Camera::instance().position() = mirroredPosition;
Camera::instance().direction() = mirroredDirection;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Update worldview matrix to mirrored viewpoint
Camera::instance().setMatrix();
// Create mirror clip plane
const double clipPlane [] = { mirrors[i]->plane().n().x(),
mirrors[i]->plane().n().y(),
mirrors[i]->plane().n().z(),
mirrors[i]->plane().d() };
glEnable(GL_CLIP_PLANE0);
glClipPlane(GL_CLIP_PLANE0, clipPlane);
// Setup stencil to only draw pixels where stencil is set to 1
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
// Draw scene (from mirrors viewpoint)
drawScene(mirroredPosition, mirroredDirection, mirrors[i]->sector(), frustrum_);
// Disable mirror clip plane and stencil testing
glDisable(GL_CLIP_PLANE0);
glDisable(GL_STENCIL_TEST);
Camera::instance().position() = originalPosition;
Camera::instance().direction() = originalDirection;
glPopMatrix();
Problems with stencil mirror reflection
I am trying to implement stencil mirrors and the image I am seeing in the mirror is not the correct reflection of the scene. When I look at the mirror i get kind of the right scene reflection but when i look at the mirror on a sharp angle i see the stuff behind the mirror being clipped by the clip plane. I am not sure if i am creating the correct mirrored viewpoint ?
Here is the mirroring code:
Any help would be appreciated,
Thanks
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement