QQuickWindow +自定义OpenGL =黑屏

问题描述

我试图将基于OpenGL的自定义代码与QML集成在一起,以创建显示OpenGL渲染结果的QML组件。 但是我总是黑屏。但是,所有这些都可以与QOpenGLWidget或自定义QWindow一起正常工作(如下所示)。 最后,我将越野车程序简化为这种愚蠢的代码。 如果启用USE_QWINDOW,则会看到图片, 如果启用USE_QUICK_WINDOW,则会看到黑屏。 在这两种情况下,opengl代码都是相同的。 知道这里有什么问题吗?

#include <QGuiApplication>
#include <QOpenGLFunctions>
#include <QWindow>
#include <memory>
#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>
#include <QOpenGLTexture>
#include <QQuickWindow>
#include <QTimer>

//#define USE_QWINDOW
#define USE_QUICK_WINDOW

class Render final : public QObject
{
public:
    Render(QOpenGLFunctions *funcs,QObject *parent) : QObject(parent),funcs_(funcs)
    {
        program_ = new QOpenGLShaderProgram(this);
        static const char vertexSrc[] = "\
      attribute vec4 a_position; \
      uniform vec2 u_samplerSize; \
      varying vec2 v_texCoord; \
      \
      void main() \
      { \
        v_texCoord = vec2(a_position.z * u_samplerSize.x,a_position.w * u_samplerSize.y); \
        gl_Position = vec4(a_position.x,a_position.y,0.0,1.0);\
      }\n";

        static const char fragmentSrc[] = "\
      uniform sampler2D u_sampler; \
      varying vec2 v_texCoord; \
      \
      void main() \
      { \
        gl_FragColor = vec4(texture2D(u_sampler,v_texCoord).rgb,1.0); \
      }\n";

        if (!program_->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex,vertexSrc)) {
            qWarning("Vertex shader failure");
        }

        if (!program_->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment,fragmentSrc)) {
            qWarning("Fragment shader failure");
        }

        if (!program_->link()) {
            qWarning("shader link failure");
        }

        vao_ = new QOpenGLVertexArrayObject(this);
        vao_->create();
        vao_->bind();

        vbo_ = std::make_unique<QOpenGLBuffer>(QOpenGLBuffer::VertexBuffer);
        vbo_->setUsagePattern(QOpenGLBuffer::StaticDraw);
        vbo_->create();
        vbo_->bind();

        QVector4D vertices[4] = { QVector4D(-1.0,1.0,1.0),QVector4D(1.0,QVector4D(-1.0,-1.0,0.0),0.0) };
        vbo_->allocate(static_cast<void *>(vertices),sizeof(vertices));

        QImage img("/tmp/map.png");
        qDebug() << "img: " << img.width() << " x " << img.height();
        texture_ = std::make_unique<QOpenGLTexture>(img);

        vao_->release();
        program_->release();
    }
    void render(int w,int h)
    {
        qDebug("render begin");
        funcs_->glViewport(0,w,h);
        vao_->bind();
        if (!program_->bind()) {
            qWarning("program bind failed");
        }
        funcs_->glActiveTexture(GL_TEXTURE0);
        texture_->bind();

        int const samplerLocation = program_->uniformLocation("u_sampler");
        program_->setUniformValue(samplerLocation,0);

        QVector2D const samplerSize { 0.935547,0.510986 };
        int const samplerSizeLocation = program_->uniformLocation("u_samplerSize");
        program_->setUniformValue(samplerSizeLocation,samplerSize);

        program_->enableAttributeArray("a_position");
        program_->setAttributeBuffer("a_position",GL_FLOAT,4,0);

        funcs_->glClearColor(0.0,1.0);
        funcs_->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        funcs_->glDrawArrays(GL_TRIANGLE_STRIP,4);
    }

private:
    QOpenGLFunctions *funcs_ = nullptr;
    QOpenGLShaderProgram *program_ = nullptr;
    QOpenGLVertexArrayObject *vao_ = nullptr;
    std::unique_ptr<QOpenGLBuffer> vbo_;
    std::unique_ptr<QOpenGLTexture> texture_;
};

class OpenGLWindow final : public QWindow,private QOpenGLFunctions
{
public:
    OpenGLWindow(QWindow *parent = nullptr) : QWindow(parent)
    {
        setSurfaceType(QWindow::OpenGLSurface);
    }
    bool event(QEvent *event) override
    {
        switch (event->type()) {
        case QEvent::UpdateRequest:
            renderNow();
            return true;
        default:
            return QWindow::event(event);
        }
    }
    void exposeEvent(QExposeEvent *) override
    {
        if (isExposed())
            renderNow();
    }

    void renderNow()
    {
        if (!isExposed())
            return;

        if (!m_context) {
            m_context = new QOpenGLContext(this);
            m_context->setFormat(requestedFormat());
            m_context->create();
        }

        m_context->makeCurrent(this);

        if (m_render == nullptr) {
            initializeOpenGLFunctions();
            m_render = new Render(this,this);
        }

        m_render->render(width(),height());

        m_context->swapBuffers(this);
    }

private:
    QOpenGLContext *m_context = nullptr;
    Render *m_render = nullptr;
};

class MyQuickWindow final : public QQuickWindow,private QOpenGLFunctions
{
public:
    MyQuickWindow(QWindow *parent = nullptr) : QQuickWindow(parent)
    {
        setClearBeforeRendering(false);
        connect(this,&QQuickWindow::beforeSynchronizing,this,&MyQuickWindow::sync,Qt::DirectConnection);
    }

    void sync()
    {
        // copy to other thread
        size_ = size();
        if (!init_) {
            init_ = true;
            connect(this,&QQuickWindow::beforeRenderPassRecording,&MyQuickWindow::paint,Qt::DirectConnection);
        }
    }

    void paint()
    {
        if (m_render == nullptr) {
            initializeOpenGLFunctions();
            m_render = new Render(this,this);
        }
        m_render->render(size_.width(),size_.height());
    }

private:
    bool init_ = false;
    Render *m_render = nullptr;
    QSize size_;
};

int main(int argc,char *argv[])
{
    QGuiApplication app(argc,argv);

#ifdef USE_QWINDOW
    OpenGLWindow win;
#endif
#ifdef USE_QUICK_WINDOW
    MyQuickWindow win;
#endif
    win.resize(400,400);
    win.show();

    auto timer = new QTimer(&win);
    QObject::connect(timer,&QTimer::timeout,&win,[&win]() {
        qDebug("request update");
#ifdef USE_QWINDOW
        win.requestUpdate();
#endif
#ifdef USE_QUICK_WINDOW
        win.update();
#endif
    });
    timer->setSingleShot(false);
    timer->start(1000);

    return app.exec();
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...