Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. OpenGL render to texture using QOpenGLFramebufferObject

OpenGL render to texture using QOpenGLFramebufferObject

Scheduled Pinned Locked Moved General and Desktop
openglqglframebufferoqopenglbufferrender to textu
6 Posts 3 Posters 8.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    brebarth
    wrote on last edited by
    #1

    Hi,
    I am currently restructuring some code and want to bring it from Qt 4.7.4 to Qt 5.4 with the aim to compile it for android.
    The code makes use of the QGLFramebufferObject (Qt 4.7.4) and because this no longer exists in 5.4, I am attempting to use QOpenGLFramebufferObject (which exists in Qt 5.4) for the updated code.

    The code in question works fine in 4.7.4, but I can't get it to work in 5.4. The only difference in the calls it makes is that instead of QGLFramebufferObject, the code now uses QOpenGLFramebufferObject.

    The code is supposed to render some colored rectangles to an offscreen buffer, and then copy a rectangular section from the offscreen buffer and render the section on the screen.

    in 4.7.4 and in 5.4 both the code looks identical like this:
    (the difference is that for the old code GLWidget inherits from QGLWidget, and in the new code it inherits from QOpenGLWidget).
    "fbo" is the QGLFramebufferObject / QOpenGLFramebufferObject, respectively.
    Also initialization of OpenGL, looks pretty much the same for both code instances. I am really puzzled why the new code doesn't work, just shows a black screen.

    void GLWidget::paintGL()
    {
        // Clear screen and depth buffer
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
        DrawOffScreen();
        DrawScreen();
    	glFlush();
    }
    
    void GLWidget::DrawOffScreen(){
    
        glPushAttrib(GL_ALL_ATTRIB_BITS);
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
    
        glViewport(0, 0, 512, 512);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        // 2D Ortho View
        glOrtho(0.0, 512, 0.0, 512, -1.0f, 1.0f); 
        // back to modelview mode
        glMatrixMode(GL_MODELVIEW);     
        glLoadIdentity();
    
        // render to the framebuffer object
        fbo->bind();
        // Clear screen and depth buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    
            //draw some rectangles..
            DrawRectangle(0,0,512,512, 255,0,255);
            DrawRectangle(200,0,300,510, 0,255,255);
            DrawRectangle(100,30,100,310, 200,155);
        fbo->release();
    
        // pop the projection matrix and GL state back 
        //for rendering to the actual widget
        glPopAttrib();
        glMatrixMode(GL_PROJECTION);
        glPopMatrix();
    
        // back to modelview mode
        glMatrixMode(GL_MODELVIEW);     
    }
    
    void GLWidget::DrawScreen(){
    
        glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
        glBindTexture(GL_TEXTURE_2D, fbo->texture());
    
        glDisable(GL_BLEND);
        glEnable(GL_TEXTURE_2D);
        glColor4f(1.0f, 1.0f, 1.0f, 1);
        glBegin(GL_QUADS);
    
            glTexCoord2f(0,0);   glVertex2f(0, 0);
            glTexCoord2f(1, 0);   glVertex2f(512, 0);
            glTexCoord2f(1, 1);   glVertex2f(512, 512);
            glTexCoord2f(0, 1);   glVertex2f(0, 512);
    
        glEnd();
        glEnable(GL_BLEND);
    }
    
    

    Any ideas? Thanks a lot for reading this.

    B 1 Reply Last reply
    0
    • B brebarth

      Hi,
      I am currently restructuring some code and want to bring it from Qt 4.7.4 to Qt 5.4 with the aim to compile it for android.
      The code makes use of the QGLFramebufferObject (Qt 4.7.4) and because this no longer exists in 5.4, I am attempting to use QOpenGLFramebufferObject (which exists in Qt 5.4) for the updated code.

      The code in question works fine in 4.7.4, but I can't get it to work in 5.4. The only difference in the calls it makes is that instead of QGLFramebufferObject, the code now uses QOpenGLFramebufferObject.

      The code is supposed to render some colored rectangles to an offscreen buffer, and then copy a rectangular section from the offscreen buffer and render the section on the screen.

      in 4.7.4 and in 5.4 both the code looks identical like this:
      (the difference is that for the old code GLWidget inherits from QGLWidget, and in the new code it inherits from QOpenGLWidget).
      "fbo" is the QGLFramebufferObject / QOpenGLFramebufferObject, respectively.
      Also initialization of OpenGL, looks pretty much the same for both code instances. I am really puzzled why the new code doesn't work, just shows a black screen.

      void GLWidget::paintGL()
      {
          // Clear screen and depth buffer
      	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
          DrawOffScreen();
          DrawScreen();
      	glFlush();
      }
      
      void GLWidget::DrawOffScreen(){
      
          glPushAttrib(GL_ALL_ATTRIB_BITS);
          glMatrixMode(GL_PROJECTION);
          glPushMatrix();
      
          glViewport(0, 0, 512, 512);
          glMatrixMode(GL_PROJECTION);
          glLoadIdentity();
          // 2D Ortho View
          glOrtho(0.0, 512, 0.0, 512, -1.0f, 1.0f); 
          // back to modelview mode
          glMatrixMode(GL_MODELVIEW);     
          glLoadIdentity();
      
          // render to the framebuffer object
          fbo->bind();
          // Clear screen and depth buffer
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
      
              //draw some rectangles..
              DrawRectangle(0,0,512,512, 255,0,255);
              DrawRectangle(200,0,300,510, 0,255,255);
              DrawRectangle(100,30,100,310, 200,155);
          fbo->release();
      
          // pop the projection matrix and GL state back 
          //for rendering to the actual widget
          glPopAttrib();
          glMatrixMode(GL_PROJECTION);
          glPopMatrix();
      
          // back to modelview mode
          glMatrixMode(GL_MODELVIEW);     
      }
      
      void GLWidget::DrawScreen(){
      
          glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
          glBindTexture(GL_TEXTURE_2D, fbo->texture());
      
          glDisable(GL_BLEND);
          glEnable(GL_TEXTURE_2D);
          glColor4f(1.0f, 1.0f, 1.0f, 1);
          glBegin(GL_QUADS);
      
              glTexCoord2f(0,0);   glVertex2f(0, 0);
              glTexCoord2f(1, 0);   glVertex2f(512, 0);
              glTexCoord2f(1, 1);   glVertex2f(512, 512);
              glTexCoord2f(0, 1);   glVertex2f(0, 512);
      
          glEnd();
          glEnable(GL_BLEND);
      }
      
      

      Any ideas? Thanks a lot for reading this.

      B Offline
      B Offline
      brebarth
      wrote on last edited by
      #2

      Could it be that Qt 5.4 has a bug supporting the OpenGL library, where Qt 4.7 still worked? Or what else could this be?
      I'm happy to send the sources for this in a minimal project, if that helps?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        AFAIK, QGLFramebufferObject is still available in Qt 5, what error did you get with it ?

        Your software is using the OpenGL fixed pipeline, but IIRC with the new set of OpenGL classes in Qt 5, you need to request a profile that provides support for it otherwise you'll have to refactor your code to use the new dynamic pipeline.

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • A Offline
          A Offline
          agocs
          wrote on last edited by agocs
          #4

          Do a makeCurrent() in GLWidget::paintGL right after drawOffScreen(). When you call fbo->release() you will end up with binding the default framebuffer (id 0) instead of the QOpenGLWidget's backing framebuffer. This has been fixed in Qt 5.5 (QTBUG-43269) so the example you posted should work there without any changes. For previous versions call makeCurrent() manually.

          B 1 Reply Last reply
          0
          • B Offline
            B Offline
            brebarth
            wrote on last edited by
            #5

            @SGaist

            what error did you get with it ?

            when I try to use #include <QGLFramebufferObject> I get a compile error:

            In file included from mainwidget.cpp:44: ./mainwidget.h:56:10: fatal error: 'QGLFramebufferObject' file not found #include <QGLFramebufferObject>

            and I'm compiling on Qt 5.4.1

            1 Reply Last reply
            0
            • A agocs

              Do a makeCurrent() in GLWidget::paintGL right after drawOffScreen(). When you call fbo->release() you will end up with binding the default framebuffer (id 0) instead of the QOpenGLWidget's backing framebuffer. This has been fixed in Qt 5.5 (QTBUG-43269) so the example you posted should work there without any changes. For previous versions call makeCurrent() manually.

              B Offline
              B Offline
              brebarth
              wrote on last edited by
              #6

              @agocs hmmm, I tried doing that, inserting makeCurrent(), but that didn't change anything. Still get a black screen.

              btw, when I deactivate drawOffScreen(), only calling drawScreen() it gives me scrambled data (undefined) on the screen, where I would expect to see a white quad, because of glColor4f(1.0f, 1.0f, 1.0f, 1.0f); in drawScreen()

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved