I have a QOpenGLWidget
class:
#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();
...
Inside initializeGL()
I have calls to initialize shaders and buffers, and so on:
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();
//--------------------------
My question is:
As this class is getting more complex with more shaders and buffers I need to organize everything into some better form. What is the best way to implement some sort of shader/buffer manager class so the functionality will be the same as now but in a different file. The problem is ofc with using opengl functions and sharing context.
Inside ::initializeGL()
I want to have something like:
shader_manager.init_simple_shader();
shader_manager.init_simple_buffer();
Inside shader manager something like this:
void ShaderManager::init_simple_shader()
{
simple_vao.create();
simple_vao.bind();
simple_vbo.create();
simple_vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
simple_vbo.bind();
... and so on with opengl functions that work exactly like in the parent class.