Skip to main content

How to get the camera parameters from OpenGLperspective/modelview matrix? (2)


Get camera parameters from the OpenGL modelview matrix

OpenGL modelview matrix includes model related operations, therefore, the scene was scaled, translated, or rotated, these effects are also in there. There is no way to separate these operation from the pure camera parameters from the matrix only. Here we assume the model operation is identity. If your renderer separated the model operations and camera parameters, you need to undo the model operation with your implementation dependent way. Our renderer has separation of them, so we needed to undo the effect.

The elements of OpenGL modelview matrix is the following. If you want to know the details of the each element, please refer my other blog (http://shitohichiumaya.blogspot.de/2011/01/what-matrix-glulookat-generates-1.html)
\begin{eqnarray*}
 \left[
  \begin{array}{cccc}
   x_x & y_x & z_x & 0 \\
   x_y & y_y & z_y & 0 \\
   x_z & y_z & z_z & 0 \\
   -(\vec{x} \cdot \vec{e}) &
    -(\vec{y} \cdot \vec{e}) &
    -(\vec{z} \cdot \vec{e}) & 1 \\
  \end{array}
 \right]
\end{eqnarray*}
Let rewrite this matrix with replacing the 4th row with \(a,b,c\).
\begin{eqnarray*}
 \left[
  \begin{array}{cccc}
   x_x & y_x & z_x & 0 \\
   x_y & y_y & z_y & 0 \\
   x_z & y_z & z_z & 0 \\
   a   & b   & c   & 1 \\
  \end{array}
 \right]
\end{eqnarray*}
This matrix has camera basis, the only problem is the eye position. This is in the 4th row, 1-3 columns as shown in the following. (Here, the matrix and vector are written in the column vectors \(\vec{x}, \vec{y}, \vec{z}, \vec{e}\))
\begin{eqnarray*}
 \left[
  \begin{array}{ccc}
   & & \\
   \vec{x} & \vec{y} & \vec{z}\\
   & & \\
  \end{array}
 \right]
 \left[
  \begin{array}{c}
   \\
   \vec{e} \\
   \\
  \end{array}
 \right]
 & = &
 \left[
  \begin{array}{c}
   a \\
   b \\
   c \\
  \end{array}
 \right]
\end{eqnarray*}
Therefore,
\begin{eqnarray*}
 \left[
  \begin{array}{c}
   \\
   \vec{e} \\
   \\
  \end{array}
 \right]
 & = &
 \left[
  \begin{array}{ccc}
   & & \\
   \vec{x} & \vec{y} & \vec{z}\\
   & & \\
  \end{array}
 \right]^{-1}
 \left[
  \begin{array}{c}
   a \\
   b \\
   c \\
  \end{array}
 \right]
\end{eqnarray*}
is the eye position. The implementation example in C++ of those is the
following.

/// get camera parameters from
/// the OpenGL modelview matrix
///
/// \param[out] eyepos  eye position
/// \param[out] viewdir viewing direction
/// \param[out] updir   up direction vector
void gl_get_camera_parameters_from_modelview_matrix(
    Vector3d & eyepos,
    Vector3d & viewdir,
    Vector3d & updir)
{
    GLdouble mat[16];
    glGetDoublev(GL_MODELVIEW_MATRIX, mat);

    Vector3d xdir(0.0, 0.0, 0.0);
    Vector3d ydir(0.0, 0.0, 0.0);
    Vector3d zdir(0.0, 0.0, 0.0);

    xdir[0] = mat[0];  ydir[0] = mat[1];  zdir[0] = mat[2];
    xdir[1] = mat[4];  ydir[1] = mat[5];  zdir[1] = mat[6];
    xdir[2] = mat[8];  ydir[2] = mat[9];  zdir[2] = mat[10];

    // This is a, b, c components.
    Vector3d bvec(-mat[12], -mat[13], -mat[14]);

    Matrix33d basis_mat(xdir[0], xdir[1], xdir[2],
                        ydir[0], ydir[1], ydir[2],
                        zdir[0], zdir[1], zdir[2]);
    // This matrix should not be singular.
    // invert() gives matrix inverse.
    bool const is_inv = basis_mat.invert();
    assert(is_inv);

    eyepos  = basis_mat * bvec;
    viewdir = -zdir;
    updir   =  ydir;
}


For simplicity again, I omitted the error checks.

Conclusion

In this blog, I showed
  • How to get perspective camera parameters from the OpenGL perspective matrix.
  • How to get other camera parameters (i.e., camera position, view direction, and eye position) from the OpenGL modelview matrix.
There is not so much case that this is needed, but I think some people may find this is interesting.

Comments

Popular posts from this blog

Why A^{T}A is invertible? (2) Linear Algebra

Why A^{T}A has the inverse Let me explain why A^{T}A has the inverse, if the columns of A are independent. First, if a matrix is n by n, and all the columns are independent, then this is a square full rank matrix. Therefore, there is the inverse. So, the problem is when A is a m by n, rectangle matrix.  Strang's explanation is based on null space. Null space and column space are the fundamental of the linear algebra. This explanation is simple and clear. However, when I was a University student, I did not recall the explanation of the null space in my linear algebra class. Maybe I was careless. I regret that... Explanation based on null space This explanation is based on Strang's book. Column space and null space are the main characters. Let's start with this explanation. Assume  x  where x is in the null space of A .  The matrices ( A^{T} A ) and A share the null space as the following: This means, if x is in the null space of A , x is also in the null spa

Gauss's quote for positive, negative, and imaginary number

Recently I watched the following great videos about imaginary numbers by Welch Labs. https://youtu.be/T647CGsuOVU?list=PLiaHhY2iBX9g6KIvZ_703G3KJXapKkNaF I like this article about naming of math by Kalid Azad. https://betterexplained.com/articles/learning-tip-idea-name/ Both articles mentioned about Gauss, who suggested to use other names of positive, negative, and imaginary numbers. Gauss wrote these names are wrong and that is one of the reason people didn't get why negative times negative is positive, or, pure positive imaginary times pure positive imaginary is negative real number. I made a few videos about explaining why -1 * -1 = +1, too. Explanation: why -1 * -1 = +1 by pattern https://youtu.be/uD7JRdAzKP8 Explanation: why -1 * -1 = +1 by climbing a mountain https://youtu.be/uD7JRdAzKP8 But actually Gauss's insight is much powerful. The original is in the Gauß, Werke, Bd. 2, S. 178 . Hätte man +1, -1, √-1) nicht positiv, negative, imaginäre (oder gar um

Why parallelogram area is |ad-bc|?

Here is my question. The area of parallelogram is the difference of these two rectangles (red rectangle - blue rectangle). This is not intuitive for me. If you also think it is not so intuitive, you might interested in my slides. I try to explain this for hight school students. Slides:  A bit intuitive (for me) explanation of area of parallelogram  (to my site, external link) .