2-1QT UI调用OpenGL控件功能_哔哩哔哩_bilibili

注意析构问题。

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_action_Triangle_triggered();

    void on_action_clear_triggered();

    void on_action_line_triggered();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setCentralWidget(ui->openGLWidget);
    ui->action_line->setChecked(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_action_Triangle_triggered()
{
    ui->openGLWidget->drawShape(OpenGLWidget::Triangle);
}

void MainWindow::on_action_clear_triggered()
{
    ui->openGLWidget->drawShape(OpenGLWidget::None);
}

void MainWindow::on_action_line_triggered()
{
    ui->openGLWidget->set_Line(ui->action_line->isChecked());
}
#ifndef OPENGLWIDGET_H
#define OPENGLWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>

class OpenGLWidget : public QOpenGLWidget,QOpenGLFunctions_3_3_Core
{
    Q_OBJECT
public:
    enum Shape{None,Triangle};
    explicit OpenGLWidget(QWidget *parent = nullptr);
    ~OpenGLWidget();
    void drawShape(Shape shape);
    void set_Line(bool is_line);
protected:
    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void paintGL();
signals:

public slots:
private:
    Shape shape;
};

#endif // OPENGLWIDGET_H
#include "openglwidget.h"
#include <QDebug>

unsigned int VBO,VAO;
unsigned int shaderProgram;

//顶点着色器
const char* vertexShaderSource="#version 330 core\n"
        "layout(location=0) in vec3 aPos;\n"
        "void main()\n"
        "{\n"
        "gl_Position=vec4(aPos.x,aPos.y,aPos.z,1.0);\n"
        "}\0";
//片段着色器
const char* fragmentShaderSource="#version 330 core\n"
        "out vec4 FragColor;\n"
        "void main()\n"
        "{\n"
        "FragColor=vec4(0.3f,0.5f,0.2f,0.2f);\n"
        "}\0";

OpenGLWidget::OpenGLWidget(QWidget *parent) : QOpenGLWidget(parent)
{

}
OpenGLWidget::~OpenGLWidget()
{
    makeCurrent();
    glDeleteBuffers(1,&VBO);
    glDeleteVertexArrays(1,&VAO);
    glDeleteProgram(shaderProgram);
    doneCurrent();
}

void OpenGLWidget::drawShape(OpenGLWidget::Shape shape)
{
    this->shape=shape;
    update();
}

void OpenGLWidget::set_Line(bool is_line)
{
    qDebug()<<is_line;
    makeCurrent();
    if(is_line){
        glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//使用线条填充
    }
    else{
        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
    }
    update();
    doneCurrent();
}

void OpenGLWidget::initializeGL()
{
    initializeOpenGLFunctions();

    float firstTriangle[]={
        -0.9f,-0.5f,0.0f,
        -0.0f,-0.5f,0.0f,
        -0.45f,0.5f,0.0f
    };


    //顶点数组
    glGenVertexArrays(1,&VAO);
    //顶点缓冲对象
    glGenBuffers(1,&VBO);

    //绑定顶点数组
    glBindVertexArray(VAO);
    //绑定顶点缓冲对象
    glBindBuffer(GL_ARRAY_BUFFER,VBO);

    //把相关数据复制到缓冲内存中
    glBufferData(GL_ARRAY_BUFFER,sizeof(firstTriangle),firstTriangle,GL_STATIC_DRAW);
    //第二个参数:数据的大小(字节为单位)
    //第三个参数:数据指针

    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void *)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER,0);//解除绑定
    glBindVertexArray(0);

    unsigned int vertexShader=glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader,1,&vertexShaderSource,NULL);
    glCompileShader(vertexShader);

    int  success;
    char infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);

    if(!success)
    {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
    }

    unsigned int fragmentShader=glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
    glCompileShader(fragmentShader);

    shaderProgram=glCreateProgram();
    glAttachShader(shaderProgram,vertexShader);
    glAttachShader(shaderProgram,fragmentShader);
    glLinkProgram(shaderProgram);

    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if(!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
    }

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);
    glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//使用线条填充
}

void OpenGLWidget::resizeGL(int w, int h)
{

}

void OpenGLWidget::paintGL()
{
    glClearColor(0.2f,0.3f,0.3f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    switch (shape) {
    case Triangle:
        glUseProgram(shaderProgram);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES,0,3);
        break;
    default:
        break;
    }
}

Qt+OpenGL-part5-LMLPHP

04-06 12:26