OpenGL (3) Coloring of plane graphics

1.OpenGL.pro

QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = OPenGL12
TEMPLATE = app

SOURCES += main.cpp\
    openglwidget.cpp

HEADERS  += \
    openglwidget.h

LIBS += -lopengl32

2.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);
};

#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);
    //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 );
    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();

}

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();
}

3. Effects


Guess you like

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