Results 1 to 3 of 3

Thread: Alpha channel weirdness with QGLContext

  1. #1
    Join Date
    Feb 2006
    Location
    Norwalk, CT, USA
    Posts
    6
    Qt products
    Qt3
    Platforms
    Unix/X11

    Question Alpha channel weirdness with QGLContext

    Hi, I'm using QGLContext to render a small viewport with some triangles and rectangles in it. I have problems when trying to read the alpha channel back out using glReadPixels. Can this have something to do with using QGLContext?

    Please see below:

    I initialize like so:

    Qt Code:
    1. QGLContext * context = 0;
    2. QGLFormat format;
    3. context = new QGLContext(format, (QPaintDevice *)glPanel);
    4. context->create();
    5. context->makeCurrent();
    6. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    7. glClearDepth(1.0f);
    To copy to clipboard, switch view to plain text mode 

    I draw like so:

    Qt Code:
    1. context->makeCurrent();
    2. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    3. glEnable(GL_CULL_FACE);
    4. glMatrixMode(GL_MODELVIEW);
    5. glLoadIdentity();
    6. glEnable(GL_ALPHA_TEST);
    7. glAlphaFunc(GL_GREATER, 0.0);
    8.  
    9. for(int i = 0; i < numWidgets; i++)
    10. {
    11. if(Widget[i]->type == TriangleWidget)
    12. {
    13. Widget[i]->DrawTriangle(mode);
    14. }
    15. else
    16. {
    17. Widget[i]->DrawRectangle(mode);
    18. }
    19. }
    20.  
    21. glFlush();
    22.  
    23. if(context->format().doubleBuffer())
    24. context->swapBuffers();
    To copy to clipboard, switch view to plain text mode 

    I use glReadPixels like so:

    Qt Code:
    1. unsigned char * LUT2D = new unsigned char[256 * 256 * 4];
    2. context->makeCurrent();
    3. glReadBuffer(GL_FRONT);
    4. glReadPixels(0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, LUT2D);
    5.  
    6. // debug
    7. FILE * fp = fopen("/tmp/LUT2D.txt", "w+");
    8. if(fp)
    9. {
    10. unsigned int * tmp = (unsigned int *)LUT2D;
    11. for(int h = 0; h < 256; h++)
    12. {
    13. for(int w = 0; w < 256; w++)
    14. {
    15. fprintf(fp, "0x%08X, ", tmp[w * h]);
    16. }
    17. fprintf(fp, "\n");
    18. }
    19. fclose(fp);
    20. }
    21. // debug
    To copy to clipboard, switch view to plain text mode 

    As you can see, I'm writing the buffer to a file to see what's in the alpha channel. It's 0xFF (or 1.0 or 255 depending on your preference) for every value.

    Once again, is this some weirdness with using QGLContext?

  2. #2
    Join Date
    Feb 2006
    Location
    Norwalk, CT, USA
    Posts
    6
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Alpha channel weirdness with QGLContext

    OK, I also tried

    format->setAlpha(TRUE);

    And that gave me an invalid context. Help anyone?

  3. #3
    Join Date
    Feb 2006
    Location
    Norwalk, CT, USA
    Posts
    6
    Qt products
    Qt3
    Platforms
    Unix/X11

    Talking Re: Alpha channel weirdness with QGLContext

    Solved!

    OK, this is for any programmer who comes along with a similar problem. If you need the alpha channel for any purpose, you need to enable it when you create the application by using a special X11 (this was done on Linux) version of the QApplication constructor.

    Code as follows:

    Qt Code:
    1. static int attrListDbl[] =
    2. {
    3. GLX_RGBA, GLX_DOUBLEBUFFER,
    4. GLX_RED_SIZE, 4,
    5. GLX_GREEN_SIZE, 4,
    6. GLX_BLUE_SIZE, 4,
    7. GLX_ALPHA_SIZE, 4,
    8. GLX_DEPTH_SIZE, 16,
    9. None
    10. };
    11.  
    12. int main( int argc, char ** argv )
    13. {
    14. Display * dpy = XOpenDisplay(0);
    15. int scr = DefaultScreen(dpy);
    16. XVisualInfo * vis = glXChooseVisual(dpy, scr, attrListDbl);
    17.  
    18. // QApplication a( argc, argv );
    19. QApplication a(dpy, argc, argv, (long unsigned int)vis);
    20.  
    21. CFormVolumeViewer w;
    22. a.setMainWidget((QWidget *)&w);
    23. w.show();
    24. a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
    25. return a.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    This enables the alpha channel for any subsequent QGLContext that you create. Remember that you still need to call QGLFormat::setAlpha(TRUE) before it will work.

    Unfortunately, it doesn't look like there's a WIN32 equivalent to this. I know from programming on WIN32 with OpenGL that you DO need to set the alpha bits using the Pixel Format Descriptor.
    Last edited by renaissanz; 15th March 2006 at 17:16. Reason: no win32 equivalent...

Similar Threads

  1. Alpha Channel support is broken for GIMP png and bmp files
    By mschifter in forum Qt Programming
    Replies: 3
    Last Post: 25th June 2015, 22:52
  2. SVG to alpha channel QPixmaps?
    By WinchellChung in forum Newbie
    Replies: 5
    Last Post: 24th August 2007, 22:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.