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. glReadPixels works fine in Linux but not in Windows 10
Forum Updated to NodeBB v4.3 + New Features

glReadPixels works fine in Linux but not in Windows 10

Scheduled Pinned Locked Moved Solved General and Desktop
opengl windows
3 Posts 2 Posters 318 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    DavidRS
    wrote on last edited by
    #1

    I'm trying to save an image drawn in an QOpenGLWindow. The code is simple

    void XYScene::CreateTIFF()
    {
       emit AlertMsg("Creating Screenshot image as TIFF ....",' ');
    
       int nNoRGBABytes = XImageSize*YImageSize*4;
    
       GLubyte* ScreenData=nullptr;
       try{
           ScreenData = new GLubyte[size_t(nNoRGBABytes)];
       }
       catch (std::bad_alloc& ba)
       {
           emit AlertMsg(QString("Unable to allocate memory for TIFF creation<br>Reason: %1").arg(ba.what()),'r');
           return;
       }
       glPixelStorei(GL_PACK_ALIGNMENT, 1);
       glReadPixels(0, 0, XImageSize, YImageSize, GL_RGBA, GL_UNSIGNED_BYTE, ScreenData);
      .........
    
    

    This code works perfectly in Linux (Fedora using XWindows and g++) but not in Windows (Win10 using MSVC 2019). When I look at ScreenData in Windows it's filled with zero's which would seem to indicate that something is being transferred but not from the right place. Is there some code that I need to insert for it to work on Windows?

    This is on a dual-boot machine so that all that differs is the Operating system and the NVIDIA drivers (and whatever difference there is in how OpenGL is implemented). In Windows Qt::AA_UseDesktopOpenGL is set via QCoreApplication.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DavidRS
      wrote on last edited by
      #3

      I did, but same problem - it turns out that in Windows, with my setup, the image is on the back buffer, not the front, so the solution was:

      void XYScene::CreateTIFF()
      {
         emit AlertMsg("Creating Screenshot image as TIFF ....",' ');
      
         int nNoRGBABytes = XImageSize*YImageSize*4;
      
         GLubyte* ScreenData=nullptr;
         try{
             ScreenData = new GLubyte[size_t(nNoRGBABytes)];
         }
         catch (std::bad_alloc& ba)
         {
             emit AlertMsg(QString("Unable to allocate memory for TIFF creation<br>Reason: %1").arg(ba.what()),'r');
             return;
         }
      #ifdef WIN32
         QOpenGLContext* ctx = context();
         QSurface* ctsurf = ctx->surface();
         ctx->swapBuffers(ctsurf);       // swap buffers to bring image to the front buffer to be read.
      #endif
         glReadPixels(0, 0, XImageSize, YImageSize, GL_RGBA, GL_UNSIGNED_BYTE, ScreenData);
      #ifdef WIN32
         ctx->swapBuffers(ctsurf);  // need this to see the image otherwise screen is black 
         makeCurrent();
      #endif
      
      

      Why this happening in my setup and whether it would occur on other Window systems, I don't know.

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

        Hi,

        Why not use QOpenGLWindow::grabFramebuffer ?

        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
        1
        • D Offline
          D Offline
          DavidRS
          wrote on last edited by
          #3

          I did, but same problem - it turns out that in Windows, with my setup, the image is on the back buffer, not the front, so the solution was:

          void XYScene::CreateTIFF()
          {
             emit AlertMsg("Creating Screenshot image as TIFF ....",' ');
          
             int nNoRGBABytes = XImageSize*YImageSize*4;
          
             GLubyte* ScreenData=nullptr;
             try{
                 ScreenData = new GLubyte[size_t(nNoRGBABytes)];
             }
             catch (std::bad_alloc& ba)
             {
                 emit AlertMsg(QString("Unable to allocate memory for TIFF creation<br>Reason: %1").arg(ba.what()),'r');
                 return;
             }
          #ifdef WIN32
             QOpenGLContext* ctx = context();
             QSurface* ctsurf = ctx->surface();
             ctx->swapBuffers(ctsurf);       // swap buffers to bring image to the front buffer to be read.
          #endif
             glReadPixels(0, 0, XImageSize, YImageSize, GL_RGBA, GL_UNSIGNED_BYTE, ScreenData);
          #ifdef WIN32
             ctx->swapBuffers(ctsurf);  // need this to see the image otherwise screen is black 
             makeCurrent();
          #endif
          
          

          Why this happening in my setup and whether it would occur on other Window systems, I don't know.

          1 Reply Last reply
          1

          • Login

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