Qt5与现代OpenGL学习(二)画一个彩色三角形

mywidget.h

cpp 复制代码
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>


class MyWidget : public QOpenGLWidget,protected QOpenGLFunctions
{

    Q_OBJECT

public:
    MyWidget(QWidget* parent=0);
    ~MyWidget();

protected:
    void initializeGL() override;
    void paintGL() override;
    void resizeGL(int width, int height) override;

private:
    QOpenGLShaderProgram* program;
    QOpenGLVertexArrayObject m_vao;
    QOpenGLBuffer m_vbo;

    int m_attr;
    int m_color;

};

mywidget.cpp

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

#include <QDebug>


static GLfloat vertices[] = {//我们所准备的需要提供给openGL的顶点数据
    // 位置              // 颜色
     0.5f, -0.5f, 0.0f,  1.0f, 0.0f, 0.0f,   // 右下
    -0.5f, -0.5f, 0.0f,  0.0f, 1.0f, 0.0f,   // 左下
     0.0f,  0.5f, 0.0f,  0.0f, 0.0f, 1.0f    // 顶部
};



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

}

MyWidget::~MyWidget()
//建议在析构函数中手动销毁openGL相关的对象,
//文档中特意提到QT的回收机制难以保证回收所有openGL使用的资源
//不销毁的话在关闭程序时可能会出现异常
{
    makeCurrent();
    m_vao.destroy();
    m_vbo.destroy();
    doneCurrent();
}


void MyWidget::initializeGL()
{

    initializeOpenGLFunctions();


    // 创建并绑定着色器程序
    program = new QOpenGLShaderProgram;
    program->bind();
    //向program中添加顶点着色器
    if(!program->addShaderFromSourceFile(QOpenGLShader::Vertex,":/triangle.vert"))
    {
        qDebug()<< (program->log());
        return;
    }
    //向program中添加片段着色器
    if(!program->addShaderFromSourceFile(QOpenGLShader::Fragment,":/triangle.frag"))
    {
        qDebug()<< (program->log());
        return;
    }
    if(!program->link())
    {
        qDebug()<< (program->log());
        return;
    }

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

    //创建并绑定VBO
    m_vbo.create();
    m_vbo.bind();
    m_vbo.allocate(vertices, sizeof(vertices));//向VBO传递我们准备好的数据(本文件起始部分的静态数组)

    //向顶点着色器传递其中定义为"aPos"的变量所需的数据
    m_attr=program->attributeLocation("aPos");
    program->setAttributeBuffer(m_attr,GL_FLOAT, 0, 3,6*sizeof(GLfloat));
    program->enableAttributeArray(m_attr);

    //向顶点着色器传递其中定义为"aColor"的变量所需的数据
    m_color=program->attributeLocation("aColor");
    program->setAttributeBuffer(m_color,GL_FLOAT,3*sizeof(GLfloat),3,6*sizeof(GLfloat));
    program->enableAttributeArray(m_color);

    program->release();//解绑程序

}

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


    program->bind();//绑定绘制所要使用的openGL程序
    m_vao.bind();//绑定包含openGL程序所需信息的VAO

    glDrawArrays(GL_TRIANGLES, 0, 3);//绘制
    m_vao.release();//解绑VAO
    program->release();//解绑程序

    //update();//调用update()函数会执行paintGL,现在绘制一个静态的三角形可以不使用
    //也可以用定时器连接update()函数来控制帧率,直接在paintGL函数中调用update()大概是60帧
}

void MyWidget::resizeGL(int width, int height)
{

}

添加资源

点击工程---右键添加---选择QT---选择Qt Resource File--随便搞个名字:

添加资源文件:

triangle.vert

cpp 复制代码
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;

out vec3 ourColor;

void main()
{
	ourColor = aColor;
	gl_Position =vec4(aPos, 1.0);    
}

triangle.frag

cpp 复制代码
#version 330 core
in vec3 ourColor;
void main()
{
     gl_FragColor = vec4(ourColor,1.0);
}

在mainwindow.ui中,添加QWidget,并提升为:MyWidget

相关推荐
一隅论数智13 小时前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
小羊没烦恼!14 小时前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
XiHongShi201614 小时前
STM32F407 RTC定时器例程,建议保存
stm32·单片机·学习
爱吃苹果的日记本14 小时前
离散数学第六课
学习·离散数学
伞伞悦读15 小时前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
C语言小火车15 小时前
C/C++ 为什么需要编译器?
开发语言·c++
AI职业加油站16 小时前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
旖旎夜光16 小时前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
奇思妙想聪明勤奋的小羊16 小时前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型