Rotation of a scene in OpenGL (QT)

So, we already know how to initialize an OpenGL window in QT. (/podklyuchenie-opengl-v-qt/) Now let's learn how to rotate the scene. In short, to rotate the scene in OpenGL, you need to call the function void glRotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z). The angle parameter sets the rotation angle from the current position; x, y, z describe the rotation vector, in other words, they determine the direction of rotation. You can also change the position of the vector's origin.

To do this, you need to use the translation matrix. This is done by the function: void glTranslated( GLdouble x, GLdouble y, GLdouble z ).

rotateIn the example, we will simply rotate the rectangle from the previous example. The change in rotation angle is performed with a timer (you can read about timers here).

To rotate, we define a new method void rotate() and a slot for handling timer overflow tiktack().

void WOpengl::rotate()

//Each time the function is called, we increase the rotation angle by one degree angle+=1;

//Check for overflow (360 degrees) (angle>360)? angle = 0: 0; glMatrixMode(GL_MODELVIEW); glLoadIdentity();

//Clear the screen glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

//Perform the rotation glRotated(angle,0,0,1);

//Call the drawing function paintGL(); }

void WOpengl::tiktack() { rotate(); }

Now we can see the rotating rectangle. screen

You can find the complete example here: qtopengl_rotate