1
votes

I'm trying to create scene with flat planar mirror using stencil buffer. I'm stuck at the point when I'm trying to reflect my eyepoint for the second render pass as depictured here https://www.opengl.org/archives/resources/code/samples/advanced/advanced97/notes/node90.html. What transformation do I need to apply on viewmatrix to achieve desired effect? To simplify things i tried to set plane at XY plane and scale view matrix by (1, 1, -1) but it doesn't work as I expected. Could point me how to reflect this view matrix so that I can render mirrored scene or is there any other way around?

1

1 Answers

0
votes

This code is simple and probably not completely correct, but works for me.

It uses GLM. If you don't want to use GLM, you can find information about this methods online.

bool scene::CalcMirrorMatrix (
    const glm::vec3 & eyePos, 
    const glm::vec3 & mirrorPos, 
    const glm::vec3 & mirrorNormal, 
    float mirrorSize, 
    glm::mat4 & outMirrorProjMatrix, 
    glm::mat4 & outMirrorViewMatrix )
{
    //           Eye   N   R
    //             \   /\ /\
    //              \  |  /
    //               \ | /
    //               \/|/
    //         ----------------- Mirror
    //                / 
    //               / -R
    //              /
    //             \/
    //         MirrorEye

    glm::vec3 eyeV (mirrorPos-eyePos);      // eye to mirror center

    float dist = glm::length (eyeV);        // dist from eye to mirror
    eyeV = glm::normalize (eyeV);

    glm::vec3 N = glm::normalize (mirrorNormal); 

    if (glm::dot(N,eyeV) <= 0.0) 
        return false;                       // mirror looking backguards

    glm::vec3 R = glm::reflect (eyeV, N);   // reflected light vector, normalized.

    // Position of the 'eye' inside the mirror.
    glm::vec3 mirrorEyePos = mirrorPos - (R * dist);

    // Mirror camera FOV (perfect square mirror)
    //
    //                     _-| 
    //                  _-   | mirrorSize/2
    //               _-      |
    //   mirrorEye -----------
    //                  dist
    //
    float fovAngleRad = 2.0f * atanf ((mirrorSize/2.0f) / dist);

    // Final matrix
    outMirrorProjMatrix = glm::perspective (fovAngleRad, 1.0f, dist, cCamFarP);
    outMirrorViewMatrix = glm::lookAt (mirrorEyePos, mirrorPos, glm::vec3(0.0f,1.0f,0.0f));

    return true;
}