在没有QGLWidget的情况下使用QGLFramebufferObject和着色器

时间:2019-05-26 20:29:46

标签: qt opengl qt4

我想使用QGLFunctions着色器和帧缓冲区创建OGL数据处理器。我不需要任何小部件。但是要创建有效的Shader和framebuffer实例,我需要一个有效的QGLContext并支持相应的glExtensions。

当然,在上下文为零的情况下,没有任何效果。 QPaintDevice也为零。使用Qpixmap作为设备,它可以创建有效的上下文,但是缺少用于着色器和帧缓冲区的glExtensions。

#include <QGLFramebufferObject>
#include <QGLShaderProgram>
#include <QtOpenGL/QGLFunctions>
// ...
void GLProcessor::init()
{
    auto format = QGLFormat::defaultFormat();

    if (!context()){
        m_context = new QGLContext(format, new QPixmap(1, 1));
        bool ok = m_context->create();
        qDebug() << "CREATING CONTEXT "<< ok;
        Q_ASSERT(context()->isValid());
    }

    context()->makeCurrent();
    initializeGLFunctions(context());
    m_binFBO = new QGLFramebufferObject(lowsize    ,lowsize    ,QGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RED);
    m_outFBO = new QGLFramebufferObject(lowsize    ,1          ,QGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RED);
    setupShaders();
    // ...
}

当然,有一个选项可以像从一开始那样从QGLWidget获取上下文并将其隐藏。但不知何故优雅。我不需要PS CUDA,OpenCL,AMP等。对于我的任务,需要OpenGL。

如何在不创建QGLWidget的情况下在qt4中使用着色器和帧缓冲区?

0 个答案:

没有答案