OpenGL (four) graphics flip

1.OpenglWidget

OpenglWidget.h

#ifndef OPENGLWIDGET_H
#define OPENGLWIDGET_H
#include <QtOpenGL>

class OpenglWidget : public QGLWidget
{
public:
    OpenglWidget(QWidget *parent=0);
protected:
    void initializeGL();
    void initWidget();
    void paintGL();
    void resizeGL(int width, int height);

private:
    GLfloat angle;
};

#endif // OPENGLWIDGET_H

OpenglWidget.cpp

#include "openglwidget.h"

OpenglWidget::OpenglWidget(QWidget *parent):QGLWidget(parent)
{
    initWidget();
    initializeGL();
}
void OpenglWidget::initializeGL()
{
    //Set the shading mode, smooth
    glShadeModel(GL_SMOOTH);
    // clear all previous colors
    glClearColor(0.0,0.0,0.0,0.0);
    //Depth buffer, set the initial value to 1.0, the part less than 1.0 is visible
    glClearDepth(1.0);
    //Start the related functions of OpenGL, which is determined by the parameters, here refers to
    //(After it is enabled, OpenGL will check when drawing to see if there are other pixels in front of the current pixel. If other pixels block it, it will not draw, that is to say, OpenGL will only draw the most front layer)
    glEnable(GL_DEPTH_TEST);
    / / Set the depth buffer comparison value
    //This parameter refers to passing if the input depth value is less than or equal to the reference value
    glDepthFunc (GL_LEQUAL);

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

}

void OpenglWidget::initWidget()
{
    //From the screen (400, 400) as the starting point, display a 640*400 interface
    setGeometry(400,200,640,480);
    setWindowTitle("My OPenGL");
}

void OpenglWidget::paintGL()
{
    //Clear color buffer and depth buffer
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    //Move the current point to the center of the screen, which is equivalent to the reset operation
    glLoadIdentity();
    //Translation function, the parameters refer to the translation from the X-axis, Y-axis, Z-axis respectively
    glTranslatef(-1.5,0.0,-6.0);


    glRotated(angle,0.0,1.0,0.0);
    //Create a primitive, which is the interface for drawing what kind of graphics, the parameters are the meaning of drawing polygons
    glBegin(GL_QUADS);
    //Set the vertex value (f stands for floating point), three-dimensional space coordinates
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(-1.0,1.0,0.0);
    glColor3f(0.0,1.0,0.0);
    glVertex3f(1.0,1.0,0.0);
    glColor3f(0.0,0.0,1.0);
    glVertex3f(1.0,-1.0,0.0 );
    glColor3f(1.0,1.0,1.0);
    glVertex3f(-1.0,-1.0,0.0);
    glEnd();

    glTranslatef(3.0,0.0,0.0 );
    /*
    void APIENTRY glRotatef (GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
    
    This function has four parameters, how do we understand these parameters? Here are some of my summaries.
    
    The first is the angle of rotation. If we want to make the above picture move quickly, we must keep dragging the window so that we can keep drawing. And the angle of rotation must be constantly added, or changed, before it can move.
    
    It is like drawing a picture, and the angle of this picture is the angle we set. If the angle does not change, the picture will not change, so it will be a fixed picture display, only when the angle is changing Sometimes, because we drag the window, the picture from different angles is constantly redrawn, which produces a dynamic effect.
    
    Later, the three parameters, x, y, z, represent the coordinates of this three-dimensional space. The rotation of this picture can be understood as, we take the origin of the coordinates as the starting point, and then take (x, y, z) as the end point, these two points are connected to form a line, and the picture goes around this line with its center as the origin to rotate. Therefore, when two of the three parameters (x, y, z) are empty, it is like rotating around a coordinate axis.
     * /
    glRotated(angle,1.0,0.0,0.0);
    glColor3f(0.3,0.2,0.5);
    glBegin( GL_TRIANGLES );
    qDebug() << "this is a paintGL test!";
    glVertex3f(  0.0,  1.0,  0.0 );
    glVertex3f( -1.0, -1.0,  0.0 );
    glVertex3f(  1.0, -1.0,  0.0 );
    glEnd();
    angle+=70;
}

void OpenglWidget::resizeGL(int width, int height)
{
    if(0==height)
        height=1;
    //Tell where to draw to the form
    glViewport(0,0,width,height);
    // Set the matrix mode, the parameter is set to the projection matrix
    glMatrixMode(GL_PROJECTION);
    // reset operation
    glLoadIdentity();

    GLdouble aspectRatio=(GLfloat)width/(GLfloat)height;
    GLdouble rFov=45.0*3.14159265/180.0;
    GLdouble zNear=0.1;
    GLdouble zFar=100.0;
    //Call glFrustum to multiply the generated matrix with the current matrix to generate a perspective effect
    glFrustum(-zNear*tan(rFov/2.0)*aspectRatio,
              zNear*tan(rFov/2.0)*aspectRatio,
              -zNear*tan(rFov/2.0),
              zNear*tan(rFov/2.0),
              zNear,zFar);
    // switch back to the model view matrix
    glMatrixMode(GL_MODELVIEW);
    // reset
    glLoadIdentity();
}

2.main

#include"openglwidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    Widget w;
//    w.show();
    OpenglWidget window;
    window.show();
    return a.exec();
}

3. Program running results


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326076155&siteId=291194637