What does the glrotatef () function do to the matrix in OpenGL?
We know that OpenGL holds two sets of matrices: Model View matrix and projection matrix ). Direct3d maintains three matrices. In fact, they are essentially the same, because model (World) matrix X view matrix = modelview matrix, that is, OpenGL Model View matrix. Through the transformation of the OpenGL matrix, we can get a variety of projection effects. This time I will study glrotatef (D), a common function in OpenGL ).
According to the parameter, the glrotatef (angle, x, y, z) function is used to rotate the angle of the current coordinate system using the (x, y, z) vector as the rotation axis. This method can rotate the world coordinate system in a simple and clear way. But inside, what formula does OpenGL use to rotate it?
Original article, opposed to unstated reference. Original blog address: http://blog.csdn.net/gamesdev/article/details/9929211
In order to find out the truth, we began to study the knowledge about the Matrix. First, we consider the rotation matrix obtained after the unit matrix I rotates around the coordinate axis. Three situations are listed:
Knowing the rotation matrix after three axes, the following is the matrix obtained from any vector. SetMIs the unit matrix operator VectorAAfter rotation, andA= (XA, ya, za), if the rotation angle is α, thenM=
I actually don't know how to prove it, but we can write a small program to verify it:
# Include <assert. h> # include <stdio. h> # include <math. h> # include "glwidget. H "Void printmatrix (float matrix [16]) {assert (Matrix! = 0 ); printf ("% 8.2f % 8.2f % 8.2f % 8.2f \ n" "% 8.2f % 8.2f % 8.2f % 8.2f \ n" "% 8.2f % 8.2f % 8.2f % 8.2f \ n" "% 8.2f % 8.2f % 8.2f % 8.2f \ n ", matrix [0], matrix [1], matrix [2], matrix [3], matrix [4], matrix [5], matrix [6], matrix [7], matrix [8], matrix [9], matrix [10], matrix [11], matrix [12], matrix [13], matrix [14], matrix [15]);} void myrotatef (float matrix [16], float angleindegree, float X, float y, float Z) {assert (Matrix! = 0); // vector unitized float length = SQRT (x * x + y * Y + z * z); Assert (! Qfuzzycompare (length, 0.0f); // do not want the length to be 0 x/= length; y/= length; Z/= length; float alpha = angleindegree/180*3.1415926; // converted radian float S = sin (alpha); float c = cos (alpha); float T = 1.0f-C; # define matrix (row, col) matrix [row * 4 + Col] matrix (0, 0) = T * x + C; matrix (0, 1) = T * x * Y + S * z; matrix (0, 2) = T * x * z-S * Y; matrix (0, 3) = 0.0f; matrix (1, 0) = T * x * Y-S * z; matrix (1, 1) = T * y * Y + C; matrix (1, 2) = T * y * z + S * X; matrix (1, 3) = 0.0f; matrix (2, 0) = T * x * z + S * Y; matrix (2, 1) = T * y * z-S * X; matrix (2, 2) = T * z + C; matrix (2, 3) = 0.0f; matrix (3, 0) = 0.0f; matrix (3, 1) = 0.0f; matrix (3, 2) = 0.0f; matrix (3, 3) = 1.0f; # UNDEF matrix} glwidget: glwidget (qwidget * pparent): qglwidget (pparent) {setwindowtitle ("test OpenGL matrix");} void glwidget: initializegl (void) {float angle = 30366f; float x = 12.0f; float y = 8.0f; float z = 3.0f; float matrix1 [16], matrix2 [16]; glmatrixmode (gl_modelview ); glloadidentity (); glgetfloatv (gl_modelview_matrix, matrix1); glgetfloatv (gl_modelview_matrix, matrix2); printf ("the initial identity matrix is: \ n"); printmatrix (matrix2 ); printf ("now perform OpenGL glrotate function. \ n "); glrotatef (angle, x, y, z); glgetfloatv (gl_modelview_matrix, matrix1); printmatrix (matrix1); printf (" now perform myrotate function. \ n "); myrotatef (matrix2, angle, x, y, z); printmatrix (matrix2);} void glwidget: paintgl (void ){}
The program running result is as follows:
This indicates that the above formula is correct. In this way, we know the principle of glrotatef. In fact, the d3dxmatrixrotationaxis () function also operates like this, But d3d is the left-hand coordinate system, and its matrix construction method will be different.