具有共享上下文的QOpenGLWidget的子类

时间:2019-01-22 08:53:35

标签: qt opengl qopenglwidget

我有一个QOpenGLWidget班:

#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLFramebufferObject>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLFunctions_4_3_Compatibility>
...

QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)


class GLWidget : public QOpenGLWidget, protected 
QOpenGLFunctions_4_3_Compatibility
{
    Q_OBJECT

public:
    GLWidget(QWidget *parent = 0);
    ~GLWidget();
...

initializeGL()内部,我有调用来初始化着色器和缓冲区,依此类推:

void GLWidget::initializeGL()
{

    connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &GLWidget::cleanup);

    initializeOpenGLFunctions();

    QSurfaceFormat format;
    format.setMajorVersion( 4 );
    format.setMinorVersion( 3 );
    format.setProfile( QSurfaceFormat::CompatibilityProfile );
    context()->setFormat(format);
    //glClearColor(0.8f, 0.9f, 1.0f, m_transparent ? 0 : 1);
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glEnable(GL_DEPTH_TEST);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBlendEquation(GL_FUNC_ADD);


    //LOAD OBJECTS
    makeCurrent();
    loadObjects();


    //SHADER INIT (SIMPLE)

    simple_shader = new QOpenGLShaderProgram;
    simple_shader->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/simple.vert");
    simple_shader->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/simple.frag");
    simple_shader->bindAttributeLocation("vertex", 0);
    simple_shader->link();
    simple_shader->bind();
    simple_proj_matrix_loc = simple_shader->uniformLocation("projMatrix");
    simple_mv_matrix_loc = simple_shader->uniformLocation("mvMatrix");
    simple_tex_loc = simple_shader->uniformLocation("texture");
    simple_shader->release();



    //BUFFER INIT (SIMPLE)

    simple_vao.create();
    simple_vao.bind();
    simple_vbo.create();
    simple_vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
    simple_vbo.bind();
    simple_vbo.allocate(VertexPosUvData.constData(),  VertexPosUvData.count() * sizeof(VertexPosUv));
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexPosUv), (void*)0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(VertexPosUv), (void*)12);
    simple_vbo.release();
    simple_vao.release();

    //--------------------------

我的问题是:

随着此类的增加,着色器和缓冲区越来越多,我需要将所有内容组织成更好的形式。实现某种着色器/缓冲区管理器类的最佳方法是什么,因此功能将与现在相同,但位于不同的文件中。问题是使用opengl函数和共享上下文。

::initializeGL()里面,我想要类似的东西:

shader_manager.init_simple_shader();
shader_manager.init_simple_buffer();

在内部着色器管理器中,如下所示:

void ShaderManager::init_simple_shader()
{
    simple_vao.create();
    simple_vao.bind();
    simple_vbo.create();
    simple_vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
    simple_vbo.bind();

...等等,与opengl函数一样,其功能与父类完全相同。

0 个答案:

没有答案