Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-90505

glReadPixel returns wrong buffer pixels

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Reported
    • Priority: P3: Somewhat important
    • Resolution: Unresolved
    • Affects Version/s: 5.15.2
    • Fix Version/s: None
    • Component/s: GUI: OpenGL
    • Labels:
      None
    • Platform/s:
      Linux/Wayland, Linux/X11, Linux/Yocto

      Description

      Hello,

      I tried to extend a qt existing example on Qt Creator from Examples tab.The example is using QOpenglFrameBufferObject and rendering two separate FBOs, one to use for display and one to use for rendering. What I did is to add some gl functions to create a render buffer to save qml screen.So following is what I added to existing code:

      ////////////////////////////////////////////////////////////////////////////////////////////////
      GLuint renderBuffer;
      GLuint frameBuffer;
      int rowPack;

      glGetIntegerv(GL_PACK_ALIGNMENT, &rowPack);
      qDebug() << "Row pack"<< rowPack;

      int mWinWidth = 1280;
      int mWinHeight = 720;

      glGenRenderbuffers( 1, &renderBuffer );
      glBindRenderbuffer( GL_RENDERBUFFER, renderBuffer );
      glRenderbufferStorage( GL_RENDERBUFFER, GL_RGB, mWinWidth, mWinHeight );
      glBindRenderbuffer(GL_RENDERBUFFER, context->defaultFramebufferObject()); //mwindow->openglContext()->defaultFramebufferObject());

      if (glGetError()==GL_NO_ERROR)

      { qDebug() << "Render buff storage is OK" << glGetError(); }

      else

      { qDebug() << "Render buff storage error is " << glGetError(); }

      glGenFramebuffers( 1, &frameBuffer );
      glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer);
      glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderBuffer);

      if( glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)

      { qDebug() << "Framebuffer error is " << glGetError(); }

      else

      { qDebug() << "Framebuffer is OK" << glGetError(); }

      glBindFramebuffer( GL_FRAMEBUFFER, context->defaultFramebufferObject() );

      int size = mWinWidth * mWinHeight *3;
      glPixelStorei(GL_PACK_ALIGNMENT, 1);

      int* buffer = new int[size];
      glReadPixels( 0, 0, mWinWidth, mWinHeight, GL_RGB, GL_UNSIGNED_BYTE, buffer );

      QImage hell((uchar*)buffer,mWinWidth,mWinHeight,QImage::Format_RGB16);
      if (!hell.save("hell.png"))

      { qDebug() << "hell on image save"; }

      else

      { qDebug() << "hell is " << hell; }

      /*FILE *out = fopen("tga_file", "w");
      short TGAhead[] =

      {0, 2, 0, 0, 0, 0, 1280, 720, 16}

      ;
      fwrite(&TGAhead, sizeof(TGAhead), 1, out);
      fwrite(buffer, size, 1, out);
      fflush(out);
      fsync(fileno(out));
      fclose(out);*/
      delete [] buffer;

      //QImage fboImage (m_renderFbo->toImage(false));
      //QImage image = fboImage.convertToFormat(QImage::Format_RGB16);

      glDeleteRenderbuffers(1,&renderBuffer);
      renderBuffer = 0 ;
      glDeleteFramebuffers(1,&frameBuffer);
      frameBuffer = 0;
      glPixelStorei(GL_PACK_ALIGNMENT, rowPack);

      ////////////////////////////////////////////////////////////////////////////////////////////////

      so from above code section glReadPixel returns wrong buffer.

      If I commented out the two lines after deletion of buffer:
      QImage fboImage (m_renderFbo->toImage(false));
      QImage image = fboImage.convertToFormat(QImage::Format_RGB16);

      all the time glReadPixel returns correct buffer and creates correct png image.

      I am attaching the project as a zip file into attachments.
      Could you please check the issue ?

      Regards

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            lagocs Laszlo Agocs
            Reporter:
            ekrems Ekrem Şahin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes