title: OpenGL : how to get the matrix that mirror the Scene?
body: <p><strong>Context:</strong></p>
  <p>I try to make a mirror in OpenGL.
  So what I need to define a mirror is a plane, defined by a point <code>p</code> and two normal vectors <code>n1</code> and <code>n2</code>. <code>n3=n1^n2</code> defines a vector oriented to the viewer of the mirror (the sign is important to <code>discard</code> fragments that are behind the mirror and also would be reflected in front of the mirror). <code>n1</code>, <code>n2</code> and <code>n3</code> define a basis in the coordinates of the mirror (with origin <code>p</code> an arbitrary point located on the mirror plane).</p>
  <p>Now, to draw the reflection of the scene I need the matrix <code>reflectionMatrix</code> that transform any point into its mirrored. I don't even know if it's possible, but what I tried looks ok but there is problems when the mirror is very close to the point.</p>
  <p>I tried to use transition matrix used in linear algebra, but I don't even know if I can use them with transformation matrix. I am also a bit confused about the transformation order.</p>
  <p>The code is that :</p>
  <pre class="lang-cpp prettyprint-override"><code>/// @brief Compute change-of-basis matrix.
  /// From basis {e1, e2, e3} to canonical basis of R^3.
  mat4 transition(const glm::vec3&amp; e1, const glm::vec3&amp; e2, const glm::vec3&amp; e3)
  {
      // GLM is column major: mat4(col1, col2, col3, col4)
      return {
         vec4(e1, 0),
         vec4(e2, 0),
         vec4(e3, 0),
         vec4(vec3(0), 1)
     };
  }
  
  glm::mat4 getReflectionMatrix() {
              const glm::vec3 mirrorPos{scene.mirrorPos};
              const glm::vec3 n1{1, 0, 0};
              const glm::vec3 n2{0, 1, 0};
              const glm::vec3 n3{glm::cross(n1, n2)};
              
              // n1, n2 are orthogonal vectors of the mirror
              // n3 is to complete the basis
              // the vertices should be in trigonometric order to work
              glm::mat4 reflectionMatrix{1};
              // Let p be the point we mirror
              // Compute p1 = p relative to the mirror [Translation in global coordinate system ~= premultiply]
              reflectionMatrix = glm::translate(reflectionMatrix, -mirrorPos);
              // Compute p2 = same as p1 but in coordinate system {n1, n2, n3}
              reflectionMatrix *= glm::inverse(transition(n1, n2, n3));
              // Compute p3 = (p2.x, p2.y, -p2.z) to go into the mirror (z is orthogonal distance from mirror)
              reflectionMatrix = glm::scale(reflectionMatrix, {1, 1, -1});
                        
              // Compute p4 = same as p3 but in canonical coordinate system (again relative to the mirror)
              reflectionMatrix *= transition(n1, n2, n3);
              // Compute p5 = p4 from relative to the mirror to global space [Translation in global coordinate system]
              reflectionMatrix = glm::translate(reflectionMatrix, mirrorPos);
              return reflectionMatrix;
  }    
  </code></pre>
  <p><strong>What is wrong with the reflection matrix?</strong> <sub><sup> Or is there a simpler way</sub></sup></p>