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;
}
相关推荐
我好喜欢你~23 分钟前
C#---StopWatch类
开发语言·c#
lifallen2 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研2 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
cui__OaO3 小时前
Linux软件编程--线程
linux·开发语言·线程·互斥锁·死锁·信号量·嵌入式学习
鱼鱼说测试4 小时前
Jenkins+Python自动化持续集成详细教程
开发语言·servlet·php
艾莉丝努力练剑4 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
CHEN5_024 小时前
【Java基础面试题】Java基础概念
java·开发语言
杜子不疼.6 小时前
《Python学习之字典(一):基础操作与核心用法》
开发语言·python·学习
落霞的思绪6 小时前
Java设计模式详细解读
java·开发语言·设计模式
阿巴~阿巴~6 小时前
深入解析C++ STL链表(List)模拟实现
开发语言·c++·链表·stl·list