qt之opengl使用

使用qt中的openglWidget绘制一个三角形。自定义的类继承关系sunOpengl : public QOpenGLWidget,QOpenGLFunctions_3_3_Core

代码如下

cpp 复制代码
/*----MainWindow.cpp----------------------------------------------*/
#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //setCentralWidget(ui->openGLWidget);
    openGLWidget=new sunOpengl(parent);
    setCentralWidget(openGLWidget);
}

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


/*----MainWindow.h----------------------------------------------*/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include"sunopengl.h"
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    sunOpengl*openGLWidget;
};
#endif // MAINWINDOW_H
cpp 复制代码
/*-----------------sunopengl.cpp-----------------------*/
#include "sunopengl.h"

sunOpengl::sunOpengl(QWidget *parent) {}
unsigned int shaderProgram;
unsigned int VBO,VAO;
float vertices[]={
    -0.5f,-0.5f,0.0f,
    0.5f,-0.5f,0.0f,
    0.0f,0.5f,0.0f,
};

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(1.0f,0.5f,0.2f,1.0f);\n"
                                   "}\n\0";
void sunOpengl::initializeGL()
{
    this->initializeOpenGLFunctions();
    glGenVertexArrays(1,&VAO);
    glGenBuffers(1,&VBO);

    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER,VBO);
    //将数据传入显存
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
    //显卡解析参数
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(GLvoid*)0);
    //开始VOA属性
    glEnableVertexAttribArray(0);

    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER,0);
    //编译顶点着色器
    unsigned int vertexShader=glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader,1,&vertexShaderSource,NULL);
    glCompileShader(vertexShader);
    //编译片段着色器
    unsigned int fragmentShader=glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
    glCompileShader(fragmentShader);

    //link shade
    shaderProgram=glCreateProgram();
    glAttachShader(shaderProgram,vertexShader);
    glAttachShader(shaderProgram,fragmentShader);
    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);
}

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

}

void sunOpengl::paintGL()
{
     glUseProgram(shaderProgram);
    glClearColor(0.2f,0.3f,0.3f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT);


    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES,0,3);
    glBindVertexArray(0);

}
//定点着色器-》几何着色-》图元装配-》光栅化-》片段着色器-》测试与混合

/*-----------------sunopengl.h-----------------------*/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include"sunopengl.h"
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    sunOpengl*openGLWidget;
};
#endif // MAINWINDOW_H

运行效果

相关推荐
我不是疯子是傻子3 小时前
Qt 实时曲线卡顿优化:从QPainter到OpenGL的3级加速实战
开发语言·qt
我不是疯子是傻子4 小时前
串口通信数据帧粘包拆包再进化:基于 Qt 的滑动窗口与 CRC 校验实战
开发语言·qt
程序员老陆5 小时前
Qt中实现窗口真全屏(覆盖Windows任务栏)
开发语言·windows·qt
艾莉丝努力练剑9 小时前
【QT:解决问题】Qt5Core.dll:无法定位程序输入点
java·开发语言·qt·学习·面试
秋田君1 天前
QT_JSON文件操作
开发语言·qt·json
大白同学4211 天前
初识Qt——信号和槽
开发语言·qt
秋田君1 天前
QT_INI文件操作
开发语言·qt
程序员老陆1 天前
OpenGL 在视频渲染里的角色:它到底加速了什么?
音视频·gpu·opengl
≮傷£≯√1 天前
FFmpeg + qt 音频播放
qt·ffmpeg·音视频
youqingyike1 天前
【无标题】
前端·qt