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 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) . 

Geometric Multiplicity: eignvectors (2)

If eigenvectors of a matrix A are independent, it is a happy property. Because the matrix A can be diagonalized with a matrix S that column vectors are eigenvectors of A . For example, Why this is a happy property of A? Because I can find A's power easily. A^{10} is not a big deal. Because Λ is a diagonal matrix and power of a diagonal matrix is quite simple. A^{10} = SΛ^{10} S^{-1} Then, why if I want to compute power of A ? That is the same reason to find eigenvectors. Eigenvectors are a basis of a matrix. A matrix can be represented by a single scalar. I repeat this again. This is the happy point, a matrix becomes a scalar. What can be simpler than a scalar value. But, this is only possible when the matrix S's columns are independent. Because S^{-1} must be exist. Now I come back to my first question. Is the λ's multiplicity related with the number of eigenvectors? This time I found this has the name. Geometric multiplicity (GM): the number of in...

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...