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

运行效果

相关推荐
森G3 小时前
46、环境配置---------QChart
c++·qt
冉佳驹7 小时前
Qt【第六篇】 ——— 事件处理、多线程、网络与文件等操作详解
qt·http·udp·tcp·事件·多线程与互斥锁
用户805533698038 小时前
嵌入式Linux驱动开发——模块参数与内核调试:让模块"活"起来的魔法
qt
冉佳驹10 小时前
Qt【第七篇】 ——— QSS 样式表与绘图 API 核心用法及 UI 定制功能总结
qt·qbrush·qpainter·qss·paintevent·qpen
森G10 小时前
45、QGraphicsScene 与 QGraphicsView 框架---------绘图
c++·qt
sycmancia10 小时前
QT——计算器核心算法
开发语言·qt·算法
Pyeako21 小时前
PyQt5 + PaddleOCR实战:打造桌面级实时文字识别工具
开发语言·人工智能·python·qt·paddleocr·pyqt5
FL16238631291 天前
基于yolov26+pyqt5的混凝土墙面缺陷检测系统python源码+pytorch模型+评估指标曲线+精美GUI界面
python·qt·yolo
森G1 天前
39、拓展知识---------事件系统
c++·qt
不会写DN1 天前
Go中如何跨语言实现传输? - GRPC
开发语言·qt·golang