OpenGL -- Qt OpenGL rotates polygons around the coordinate axis

OpenGL (12) -- Qt OpenGL rotates polygons around the coordinate axis

1, Rotate polygon

The first two chapters introduced how to draw polygons and color them. This article describes how to rotate polygons.

For the rotation of polygons, add two variables in the class to control the rotation of these two objects. They are floating-point variables that enable us to rotate objects very accurately.
Floating point numbers contain decimal places, which means we don't need to use 1, 2, 3 Angle of. You will find that floating point numbers are the basis of OpenGL programming. The variable rTri is used to rotate triangles, and rQuad rotates quadrilaterals.

2, Rotation function

Polygon rotation mainly uses functions

glRotatef( rTri, 0.0, 1.0, 0.0);

Introduction to glRotatef function:

Glrotatef (angle, xvector, yvector, zvector) is responsible for rotating objects around an axis. This function has many uses. Angle is usually a variable representing the angle at which the object turns.
Xvector, Yvector and Zvector jointly determine the direction of the rotation axis. For example, the vector described by (1, 0, 0) passes through 1 unit of the X coordinate axis and the direction is to the right.
The vector described by (-1, 0, 0) passes through 1 unit of the X coordinate axis, but the direction is to the left.

    //! D. Michael Traub: provides the above explanation of xvector, yvector and Zvector.
    //! In order to better understand the rotation of X, Y and Z, I give some examples
    //! X-axis - you are using a table saw. The axis in the center of the saw blade is placed from left to right (like the X axis in OpenGL). The sharp serrations rotate wildly around the X axis,
It seems to turn up or down. Depends on the direction when the saw blade starts to rotate.
    //! This is the same as what we rotate around the X axis in OpenGL. (Note: if you want to put your face to the monitor now,
It must have been sawed.)
    //! Y-axis - suppose you are in the center of a huge tornado, and the center of the tornado points to the sky from the ground (like the y-axis in OpenGL).
Garbage and debris rotate wildly from left to right or right to left around the Y-axis.
    //! This is the same as what we rotate around the Y axis in OpenGL.
    //! Z-axis - you look at a fan from the front. The center of the fan is directly facing you (like the Z axis in OpenGL). The blades of the fan are clockwise around the Z axis
Or turn counterclockwise. This is the same as what we rotate around the Z axis in OpenGL.

3, Code

Header file:

#include <QObject>
#include <QWidget>
#include <qgl.h>
#include <QTimer>
/*
*Rotate the polygon around the coordinate axis
*/

class NeHe_4_Widget : public QGLWidget
{
    Q_OBJECT
public:
    NeHe_4_Widget(QWidget *parent = 0);
    ~NeHe_4_Widget();

protected:
    void initializeGL(); 
    void paintGL();     
    void resizeGL( int width, int height ); 
public slots:
    void slotTimer();

private:
    GLfloat rTri;
    GLfloat rQuad;

    QTimer* m_timer;
};

cpp file

#include "nehe_4_widget.h"
#include <GL/glu.h>

/**
*In the last lesson, I taught you the coloring of triangles and quadrangles. In this lesson, I will teach you how to rotate these colored objects around the coordinate axis.
In fact, just add a few lines to the code of the last lesson.

We will add two variables to the NeHeWidget class to control the rotation of these two objects. They are floating-point variables that enable us to rotate objects very accurately.
Floating point numbers contain decimal places, which means we don't need to use 1, 2, 3 Angle of. You will find that floating point numbers are the basis of OpenGL programming.
The new variable called rTri is used to rotate triangles, and rQuad rotates quadrilaterals
*/

NeHe_4_Widget::NeHe_4_Widget(QWidget *parent):QGLWidget(parent)
{
    setGeometry(0,0, 640, 480);
    rTri = 1.0;
    rQuad = 1.0;

    m_timer = new QTimer(this);
    m_timer->setInterval(50);
    connect(m_timer,SIGNAL(timeout()),this,SLOT(slotTimer()));
    m_timer->start();
}

NeHe_4_Widget::~NeHe_4_Widget()
{

}

void NeHe_4_Widget::initializeGL()
{
    glShadeModel( GL_SMOOTH );


    glClearColor( 0.0, 0.0, 0.0, 0.0 );
  

    glClearDepth( 1.0 );


    glEnable( GL_DEPTH_TEST );
  

    glDepthFunc( GL_LEQUAL );


    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
 
}

void NeHe_4_Widget::resizeGL( int width, int height )
{
    if ( height == 0 )
    {
      height = 1;
    }


    glViewport( 0, 0, (GLint)width, (GLint)height );


    glMatrixMode( GL_PROJECTION );


    glLoadIdentity();


    gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );


    glMatrixMode( GL_MODELVIEW );


    glLoadIdentity();

}

void NeHe_4_Widget::slotTimer()
{
    rTri += 0.2;
    rQuad += 0.5;
    update();
}

void NeHe_4_Widget::paintGL()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    //Clear screen and depth cache.
    glLoadIdentity();
    //Reset the current model observation matrix.

    glTranslatef( -1.5,  0.0, -6.0 );
 

    glRotatef( rTri, 0.0, 1.0, 0.0);
    //Glrotatef (angle, xvector, yvector, zvector) is responsible for rotating objects around an axis. This function has many uses. Angle is usually a variable representing the angle at which the object turns\
    Xvector,Yvector and Zvector The three parameters together determine the direction of the rotation axis. such as( 1, 0, 0 )The described vector passes X 1 unit of the coordinate axis and the direction is to the right.\
    ( -1, 0, 0 )The described vector passes X 1 unit of the coordinate axis, but the direction is to the left.

    //! D. Michael Traub: provides the above explanation of xvector, yvector and Zvector.
    //!  In order to better understand the rotation of X, Y and Z, I give some examples
    //!  X-axis - you are using a table saw. The axis in the center of the saw blade is placed from left to right (like the X axis in OpenGL). The sharp serrations rotate wildly around the X axis, looking either up or down. Depends on the direction when the saw blade starts to rotate.
    //!      This is the same as what we rotate around the X axis in OpenGL. (Note: if you want to put your face close to the monitor at this time, you must be sawed.)
    //!  Y-axis - suppose you are in the center of a huge tornado, and the center of the tornado points to the sky from the ground (like the y-axis in OpenGL). Garbage and debris rotate wildly from left to right or right to left around the y-axis.
    //!      This is the same as what we rotate around the Y axis in OpenGL.
    //!  Z-axis - you look at a fan from the front. The center of the fan is directly facing you (like the Z axis in OpenGL). The blades of the fan rotate wildly clockwise or counterclockwise around the Z axis. This is the same as what we rotate around the Z axis in OpenGL.

    //In the above line of code, if rtri is equal to 7, we rotate the triangle around the Y axis from left to right by 7. You can also change the value of the parameter to make the triangle rotate around the X and Y axes at the same time.

    glBegin( GL_TRIANGLES );
    //Start drawing triangles.
    glColor3f( 1.0, 0.0, 0.0 );

    glVertex3f(  0.0,  1.0,  0.0 );



    glColor3f( 0.0, 1.0, 0.0 );
    //Green
    glVertex3f( -1.0, -1.0,  0.0 );

    glColor3f( 0.0, 0.0, 1.0 );

    glVertex3f(  1.0, -1.0,  0.0 );

    glEnd();



    glLoadIdentity();


    //glTranslatef(  3.0,  0.0,  0.0 );
    glTranslatef(  1.5,  0.0, -6.0 );
    glRotatef( rQuad,  1.0,  0.0,  0.0 );


    glColor3f( 0.5, 0.5, 1.0 );


    glBegin( GL_QUADS );

    glVertex3f( -1.0,  1.0,  0.0 );

    glVertex3f( 1.0,  1.0,  0.0 );

    glVertex3f( 1.0,  -1.0,  0.0 );

    glVertex3f( -1.0,  -1.0,  0.0 );

    glEnd();

    glTranslatef(  -1.5,  0.0,  0.0 );
    glColor3f( 1.0, 1.0, 0.0 );
    glBegin( GL_LINES );
    glVertex3f(0.0, 1.0, 0.0);
    glVertex3f(0.0, -1.0, 0.0);
    glEnd();

    rTri += 0.2;
    rQuad -= 0.15;

    //!  We have set the values of rTri and rQuad to 0.0 in the constructor. Here, we modify these two variables every time we draw an image. The change of the two variables will change the rotation angle of the object
    //!  Try to change the + and - in the following code to understand how the rotation direction of the object changes. And try to change 0.2 to 1.0. The larger the number, the faster the object rotates. The smaller the number, the slower the object rotates.

}

4, Code running effect

Let's run it and see the effect.

You can see that by refreshing the values of rTri and rQuad through the timer and redrawing with update(), you can see the rotation of triangles and quadrangles. The effect of rotation can be seen. You can run the code in Qt to have a look.

Previous:

Next:

Original author of this article: Feng Yichuan( ifeng12358@163.com ), please do not reprint without the authorization and consent of the author.

Tags: OpenGL C++ Qt

Posted by Petsmacker on Sun, 17 Jul 2022 05:19:12 +0930