I would appreciate if anyone can help me with the following:
Is it possible to obtain the seperate transformation matrices like translation, scaling, rotation, etc. from the modelview matrix that is obtained from
If yes, can anyone tell me how?
My exact problem is:
I am calling my OpenGL code in a MFC window, whose base class implements zoom in and out for mouse scroll wheel. As a result anything I draw by calling OpenGL code in the window, is scaled according to the scaling(zoom in/out) transformation applied in that window’s base class. I want to display some text in the window, which I do not want to be scaled when I zoom in/out. Is it possible to do it? If yes, how?
(The orthogonal/parallel projection is being used, so I am assuming that the given window’s base class is implementing zoom in/out by using the scaling transformation.)
It is not possible to obtain the individual transformations that were used to create the current MODELVIEW matrix. However, depending on what exactly you need, it is still possible to extract a lot of information from the matrix, by looking at its individual columns.
Basically, the matrix is a coordinate system. The first three columns are the coordinate system’s X, Y and Z axis. The fourth column is the coordinate system’s origin.
/ X Y Z O \
| X Y Z O |
| X Y Z O |
\ X Y Z O /
The summed translation is simply the fourth column vector. Scaling is encoded in the length of the three axis vectors, rotation in their orientation.
To remove any previous scalings, you can just normalize the three axis vectors.
And remember that the matrix is stored column-major ordered. So the X vector uses indices 0…3, Y uses 4…7, Z uses 8…11, and the origin uses 12…15.
after you’ve drawn the scene, save the modelview and projection matrix using glPush.
then build a new 2D-view which is not scaled/rotated using glOrtho and gluLookAt, and draw your text.
afterwards, fetch your matrices with glPop.
// DRAW SOMETHING
// save projection matrix
// create matrix for 2d view in a 800x600 window
glOrtho(0., 800., 0., 600., 1., 1000.);
// save modelview matrix with rotations etc.
// create new matrix for displaying text in x-y-plane
gluLookAt(400., 300., 100., 400., 300., 0., 0., 1., 0.);
// DRAW TEXT
// restore matrices
Managed it in a very roundabout fashion. But it is working fine.
I took 2 known points in screen coordinates and converted them to world coordinates. This gave me, how many world units correspond to a single pixel. This pixelToWolrdUnit ratio I calculate at every instant and compare it with the first pixelToWorldUnit ratio obtained on start. The ratio of these two ratios ( ) gives me the scaling factor, the inverse of which I apply to keep anything to be displayed from scaling.
Anyways, thanks for replying, Any other suggestions are welcome.