Qt qml with raw OpenGL containing FBO



  • #edit
    Oh, I found solution - window->openglContext()->defaultFramebufferObject().

    Hi,
    I have engine based on raw OpenGL, which contains Frame Buffer Objects.
    Wiithout qt everything works fine.
    I wrote simple test code with glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

    SceneRenderer.h:

            class SceneRenderer : 
                public QObject
            {
                Q_OBJECT;
    
            public:
                SceneRenderer();
                ~SceneRenderer();
    
                void SetWindow(QQuickWindow* window);
                QQuickWindow* GetWindow() const { return m_window; }
    
            public slots:
                void Render();
    
            private:
                QQuickWindow* m_window;
    
            };
    

    SceneRenderer.cpp:

            SceneRenderer::SceneRenderer() :  
                m_window(nullptr)
            { 
            }
    
            SceneRenderer::~SceneRenderer()
            {
            }
    
            void SceneRenderer::SetWindow(QQuickWindow* window)
            { 
                m_window = window; 
            }
    
            void SceneRenderer::Render()
            {
                glViewport(0, 0,
                    m_window->width() * m_window->devicePixelRatio(),
                    m_window->height() * m_window->devicePixelRatio()
                );
    			
                glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
                glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
                glClear(GL_COLOR_BUFFER_BIT);
                m_window->resetOpenGLState();
                m_window->update();
    
            }
    

    SceneView.h:

            class SceneView : public QQuickItem
            {
                Q_OBJECT;
    
            public:
                explicit SceneView(QQuickItem* parent = nullptr);
                ~SceneView();
    
            public slots:
                void Synchronize();
                void Cleanup();
    
            private slots:
                void HandleWindowChanged(QQuickWindow* window);
    
            private:
                std::unique_ptr<SceneRenderer> m_renderer;
    
            };
    

    SceneView.cpp:

            SceneView::SceneView(QQuickItem* parent) :
                QQuickItem(parent),
                m_renderer(nullptr)
            {     
                connect(this, &QQuickItem::windowChanged, 
                    this, &SceneView::HandleWindowChanged);
            }
    
            SceneView::~SceneView()
            {
            }
    
            void SceneView::HandleWindowChanged(QQuickWindow* window)
            {
                if (window) 
                {
                    connect(window, &QQuickWindow::beforeSynchronizing, 
                        this, &SceneView::Synchronize, Qt::DirectConnection);
                    connect(window, &QQuickWindow::sceneGraphInvalidated, 
                        this, &SceneView::Cleanup, Qt::DirectConnection);
                    window->setClearBeforeRendering(false);
                }
            }
    
            void SceneView::Synchronize()
            {
                if (m_renderer == nullptr)
                {
                    m_renderer = std::make_unique<SceneRenderer>();
                    connect(window(), &QQuickWindow::beforeRendering,
                        m_renderer.get(), &SceneRenderer::Render,
                        Qt::DirectConnection);
                }
                m_renderer->SetWindow(window());
            }
    
            void SceneView::Cleanup()
            {
                m_renderer.release();
                m_renderer = nullptr;
            }
    

    Main window format:

                QSurfaceFormat format;
                format.setMajorVersion(4);
                format.setMinorVersion(5);
    
                // CoreProfile is not supported by engine
                format.setProfile(QSurfaceFormat::CompatibilityProfile);
                format.setDepthBufferSize(24);
                format.setStencilBufferSize(8);
                format.setSamples(4);
                m_workspaceWidget->setFormat(format);
    

    Result: white window, not blue...
    Engine is built as a shared library, so It can not be integrated with qt functions.
    How can I get initial frame buffer object or how to fix it?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.