Qt+OpenGL

OpenGLEntity.h

cpp 复制代码
#ifndef OPENGLENTITY_H
#define OPENGLENTITY_H

//#include <QWidget>
#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>

class OpenGLEntity : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
{
    Q_OBJECT
public:
    explicit OpenGLEntity(QWidget *parent = 0);
    ~OpenGLEntity();

signals:

public slots:

protected:
    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;

private:
    QOpenGLShaderProgram program;
    QOpenGLBuffer vertexBuffer;
    QOpenGLVertexArrayObject vao;
};

#endif // OPENGLENTITY_H

OpenGLEntity.cpp

cpp 复制代码
#include "OpenGLEntity.h"

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

}

OpenGLEntity::~OpenGLEntity()
{
    makeCurrent();
    vertexBuffer.destroy();
    vao.destroy();
    doneCurrent();
}


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

    // 编译顶点着色器
    const char *vertexShaderSource = R"(
        #version 330 core
        layout (location = 0) in vec3 aPos;
        void main()
        {
            gl_Position = vec4(aPos, 1.0);
        }
    )";
    program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);

    // 编译片段着色器
    const char *fragmentShaderSource = R"(
        #version 330 core
        out vec4 FragColor;
        void main()
        {
            FragColor = vec4(1.0, 0.5, 0.2, 1.0); // 橙色
        }
    )";
    program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);

    // 链接程序
    program.link();

    // 检查链接是否成功
    if (!program.isLinked()) {
        qDebug() << "Failed to link program:" << program.log();
        return;
    }

    // 设置顶点数据(一个三角形)
    float vertices[] = {
        -0.5f, -0.5f, 0.0f,
         0.5f, -0.5f, 0.0f,
         0.0f,  0.5f, 0.0f
    };

    // 创建并绑定 VBO
    vertexBuffer.create();
    vertexBuffer.bind();
    vertexBuffer.allocate(vertices, sizeof(vertices));

    // 创建并绑定 VAO
    vao.create();
    vao.bind();

    // 设置顶点属性指针
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    // 解绑 VAO 和 VBO
    vao.release();
    vertexBuffer.release();
}

void OpenGLEntity::resizeGL(int w, int h)
{
    glViewport(0, 0, w, h);
}

void OpenGLEntity::paintGL()
{
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f); // 设置背景颜色为深灰色
    glClear(GL_COLOR_BUFFER_BIT);

    // 使用我们的着色器程序
    program.bind();

    // 绘制三角形
    vao.bind();
    glDrawArrays(GL_TRIANGLES, 0, 3);
    vao.release();
}

MainWindow.cpp

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include"OpenGLEntity.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    OpenGLEntity *openglentity;
};

#endif // MAINWINDOW_H

MainWindow.cpp

cpp 复制代码
#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    resize(800,600);
    openglentity = new OpenGLEntity(this);
    setCentralWidget(openglentity);
}

MainWindow::~MainWindow()
{
    delete ui;
}
相关推荐
Languorous.几秒前
C++智能指针详解:原理、使用及避坑指南
开发语言·c++
广州灵眸科技有限公司12 分钟前
瑞芯微(EASY EAI)RV1126B yolov11-track多目标跟踪部署教程
linux·开发语言·网络·人工智能·yolo·机器学习·目标跟踪
牵牛老人15 分钟前
CAN通讯实战:Qt基于周立功 USBCAN 的 CAN 总线通信开发全攻略
网络·qt·系统架构
智慧物业老杨39 分钟前
智慧物业数智化转型实战:从工单响应到业主满意度的闭环构建
java·开发语言
Kiling_070441 分钟前
Java集合框架:List集合详解与应用
java·开发语言·windows
fan_music1 小时前
C语言如何实现C++的类
开发语言·c++
毋语天1 小时前
Python 常用内置模块详解:日志、随机数、时间、OS 与 JSON
开发语言·python
_君莫笑1 小时前
Qt+Qml前后端分离上位机软件技术方案
c++·qt·用户界面·qml
右耳朵猫AI1 小时前
Python技术周刊 2026年第14周
开发语言·python·okhttp
叼烟扛炮1 小时前
C++ 知识点22 函数模板
开发语言·c++·算法·函数模版