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

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

Tezuka Osamu's Black Jack, "Shrinking"

I like several novel authors. My first favorite author is probably Teduka, Osamu. I still love him. The list grows by adding Hoshi, Shinichi, Agatha Christie, Hermann Hesse, and so forth. My first favorite article of Tezuka was Atom as most of the (boy's) Tezuka fans did. But my favorite is Black Jack. I try to summarize one story, it is still quite vivid in my memory. I first read this story when I was 13 - 15 years old. I re-read it at least several times since Black Jack is composed of many short episodes. The title should be "ちぢむ (SHRINKING)" or it might be "縮む(Shrinking)". (It is not so convenient to translate this to English, since English does not have a system to say the exact same word in several ways. So I just simulate it with capital letters.) Black Jack is a genius surgeon, but he does not have the license. In short, his medical activity is illegal. His skill is top level in the world, but, the fee is also out-of-law expensive. In the story ...

My solution of Google drive hang up at "One moment please"

Today I installed Google drive to my Windows 7 environment to share files with my Linux machines. After sign in, the application window said "processing," then it hanged up. There was a button "you must enable javascript". I pushed it, then "One moment please..." after 5 minutes, I exited the program tried it again. It seems some security setting causes this problem. My solution: set  https://accounts.google.com  as a trusted site. Procedure: Open the control panel Go to network and control Go to Internet Options Open Security Tab Click Trusted sites Click the "site" button copy & paste  https://accounts.google.com  to "Add this website to the zone" and click Add button Now it worked for me. But if I removed this site, it still works. That puzzled me a bit...