演示效果 

OpenGL纹理加载_#include

1.声明自定义类并继续QOpenGLWidget与QOpenGLExtraFunctions

2.重写基类函数

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

3. 创建缓存对象与shader程序

m_vao.create();
    m_vbo.create();

    m_prog = new QOpenGLShaderProgram();

4. 添加着色器GLSL代码与绑定

m_prog->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex,u8R"(
            #version 330 core
            in vec3 vPos;
            in vec2 vTexture;
            out vec2 oTexture;
            void main()
            {
                gl_Position = vec4(vPos, 1.0);
                oTexture = vTexture;
            }
    )");

    m_prog->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment,u8R"(
            #version 330 core
            in vec2 oTexture;
            uniform sampler2D uTexture;
            out vec4 fragColor;
            void main()
            {
                fragColor = texture(uTexture, oTexture);
            }
    )");

    m_prog->link();

 5.加载纹理并初始化顶点数据

//加载纹理
    m_texture = new QOpenGLTexture(QImage("C:/Users/dev2/apple.jpg").mirrored());

    //顶点加纹理,前三顶点,后二纹理
    float _vertex[] = {
        -1,  1, 0,	0, 1, // 左上
        -1, -1, 0,	0, 0, // 左下
        1, -1, 0,	1, 0, // 右下
        1,  1, 0,	1, 1, // 右上
    };

 6. 使用缓存

m_vao.bind();
    m_vbo.bind();
    m_vbo.allocate(_vertex,20*sizeof(float));

7.使用着色器

m_prog->setAttributeArray("vPos",GL_FLOAT,0*sizeof(float),3,5*sizeof(float));
    m_prog->enableAttributeArray("vPos");
    m_prog->setAttributeArray("vTexture",GL_FLOAT,0*sizeof(float),3,5*sizeof(float));
    m_prog->enableAttributeArray("vTexture");

8.绘制

m_texture->bind();
    m_vao.bind();
    m_prog->bind();
    glDrawArrays(GL_TRIANGLE_FAN,0,4);

9.显示OPENGL窗口

 显示OPENGL窗口 

GLWidget4 widget4;
    widget4.show();

    return a.exec();

 

 示例头文件

#ifndef GLWIDGET4_H
#define GLWIDGET4_H

#include <QOpenGLWidget>
#include <QOpenGLExtraFunctions>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>
#include <QOpenGLShaderProgram>
#include <QOpenGLShader>
#include <QOpenGLTexture>

class GLWidget4 : public QOpenGLWidget , public QOpenGLExtraFunctions
{
public:
    GLWidget4();

    // QOpenGLWidget interface
protected:
    virtual void initializeGL() override;
    virtual void resizeGL(int w, int h) override;
    virtual void paintGL() override;

private:
    QOpenGLVertexArrayObject m_vao;
    QOpenGLBuffer m_vbo;
    QOpenGLShaderProgram *m_prog;
    QOpenGLTexture *m_texture = nullptr; //OPENGL纹理
};

#endif // GLWIDGET4_H

示例源文件

#include "glwidget4.h"

GLWidget4::GLWidget4()
{

}

void GLWidget4::initializeGL()
{
    initializeOpenGLFunctions();
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.0f,0.0f,1.0f,1.0f);

    m_vao.create();
    m_vbo.create();

    m_prog = new QOpenGLShaderProgram();
    m_prog->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex,u8R"(
            #version 330 core
            in vec3 vPos;
            in vec2 vTexture;
            out vec2 oTexture;
            void main()
            {
                gl_Position = vec4(vPos, 1.0);
                oTexture = vTexture;
            }
    )");

    m_prog->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment,u8R"(
            #version 330 core
            in vec2 oTexture;
            uniform sampler2D uTexture;
            out vec4 fragColor;
            void main()
            {
                fragColor = texture(uTexture, oTexture);
            }
    )");

    m_prog->link();

    //加载纹理
    m_texture = new QOpenGLTexture(QImage("C:/Users/dev2/apple.jpg").mirrored());

    //顶点加纹理,前三顶点,后二纹理
    float _vertex[] = {
        -1,  1, 0,	0, 1, // 左上
        -1, -1, 0,	0, 0, // 左下
        1, -1, 0,	1, 0, // 右下
        1,  1, 0,	1, 1, // 右上
    };

    m_vao.bind();
    m_vbo.bind();
    m_vbo.allocate(_vertex,20*sizeof(float));

    m_prog->setAttributeArray("vPos",GL_FLOAT,0*sizeof(float),3,5*sizeof(float));
    m_prog->enableAttributeArray("vPos");
    m_prog->setAttributeArray("vTexture",GL_FLOAT,0*sizeof(float),3,5*sizeof(float));
    m_prog->enableAttributeArray("vTexture");


    m_prog->release();
    m_vao.release();


}

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

}

void GLWidget4::paintGL()
{

    //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    m_texture->bind();
    m_vao.bind();
    m_prog->bind();
    glDrawArrays(GL_TRIANGLE_FAN,0,4);
    m_prog->release();
    m_vao.release();
    m_texture->release();


}