Results 1 to 2 of 2

Thread: Snap openGL image with transparent background

  1. #1
    Join Date
    May 2012
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Snap openGL image with transparent background

    Hi all,
    i'm trying to capture an image from a QGLWidget's back buffer (so, no redraw must be seen from user) into a QImage having a transparent background, but the alpha value always return 255. Here's what i did (till now):

    I have reimplemented QGLWidget to myGLWidget:

    Qt Code:
    1. myGLWidget::myGLWidget
    2. {
    3. setAutoBufferSwap(false);
    4. takingSnapshot = false; // member variable. When taking snapshot turns true
    5. }
    6.  
    7. void myGLWidget::initializeGL()
    8. {
    9. glClearColor(1., 1., 1., 0.);
    10. }
    11.  
    12. void myGLWidget::resizeGL(int ww, int hh)
    13. {
    14. glViewport(0, 0, (GLint)ww, (GLint)hh);
    15. }
    16.  
    17. void myGLWidget::paintGL()
    18. {
    19. qDebug() << "paintGL";
    20.  
    21. glPushAttrib(GL_TRANSFORM_BIT);
    22.  
    23. glMatrixMode(GL_PROJECTION);
    24. glPushMatrix();
    25. glLoadIdentity();
    26. glMatrixMode(GL_MODELVIEW);
    27. glPushMatrix();
    28. glLoadIdentity();
    29. glOrtho(-1., 1., -1., 1., -1., 1.);
    30.  
    31.  
    32. /* Clear screen */
    33. if (takingSnapshot) {
    34. glEnable(GL_BLEND);
    35. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    36. glClearColor(0., 0., 1., 0.0f);
    37. glClear(GL_COLOR_BUFFER_BIT);
    38. glDisable(GL_BLEND);
    39. } else {
    40. glClearColor(1., 1., 0., 1.);
    41. glClear(GL_COLOR_BUFFER_BIT);
    42. }
    43.  
    44.  
    45. /* Begin Draw */
    46. glColor4f(1., 0., 0., 1.);
    47. glLineWidth(10);
    48.  
    49. glBegin(GL_LINES);
    50. glVertex2f(-0.3, 0.0);
    51. glVertex2f(0.3, 0.0);
    52. glEnd();
    53.  
    54. glEnable(GL_BLEND);
    55. glColor4f(0., 1., 0., 0.4);
    56. glRotatef(90., 0., 0., 1.);
    57. glBegin(GL_LINES);
    58. glVertex2f(-0.3, 0.0);
    59. glVertex2f(0.3, 0.0);
    60. glEnd();
    61.  
    62. glPopMatrix();
    63. glMatrixMode(GL_PROJECTION);
    64. glPopMatrix();
    65.  
    66. glPopAttrib();
    67. glDisable(GL_BLEND);
    68.  
    69. if (!takingSnapshot) {
    70. swapBuffers();
    71. } else {
    72. qDebug() << "Finished drawing after snapshot\n";
    73. }
    74. }
    To copy to clipboard, switch view to plain text mode 


    And here is my mainWindow ( MainWindow : public QMainWindow):

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. mainFrame = new QFrame(this);
    5. setCentralWidget(mainFrame);
    6.  
    7. mainLay = new QVBoxLayout(mainFrame);
    8. mainFrame->setLayout(mainLay);
    9.  
    10. glw = new myGLWidget(mainFrame);
    11. snap = new QPushButton("Snap", mainFrame);
    12. label = new QLabel(mainFrame);
    13.  
    14. mainLay->addWidget(glw);
    15. mainLay->addWidget(snap);
    16. mainLay->addWidget(label);
    17.  
    18. mainFrame->show();
    19. glw->show();
    20. snap->show();
    21. label->show();
    22.  
    23. QObject::connect(snap,SIGNAL(clicked()),this,SLOT(takeImage()));
    24. }
    25.  
    26. static QImage createQImage(const unsigned char *image2, int ww, int hh)
    27. {
    28. /* Do some stuff here with "image2" */
    29. /* and return the final image */
    30. }
    31.  
    32. void MainWindow::takeImage()
    33. {
    34. int ww = glw->width();
    35. int hh = glw->height();
    36. QImage finalImage;
    37. GLubyte *image = (GLubyte *)malloc(ww * hh * sizeof(GLubyte) * 4);
    38.  
    39. glw->takingSnapshot = true;
    40. glPushAttrib(GL_PIXEL_MODE_BIT | GL_COLOR_BUFFER_BIT); // readBuffer + drawBuffer
    41. glReadBuffer(GL_BACK);
    42. glDrawBuffer(GL_BACK);
    43. qDebug() << "takeImage, will call updateGL";
    44.  
    45. glw->updateGL();
    46. glReadPixels(0,0,ww,hh,GL_RGBA,GL_UNSIGNED_BYTE,image);
    47.  
    48. finalImage = createQImage(image, ww, hh);
    49.  
    50. if (!finalImage.isNull()) {
    51. QPixmap pix;
    52. pix.convertFromImage(finalImage);
    53. label->setPixmap(pix);
    54. }
    55. glPopAttrib();
    56. glw->takingSnapshot = false;
    57. if (image) free(image);
    58. }
    To copy to clipboard, switch view to plain text mode 

    In function "createQImage", when i loop the pixels of "image2" and test the alpha value, as i said before, it is always 255! In order to check if i do something wrong with the created QImage in "createQImage", i forced a block of pixels to have 0 alpha and my final QImage had that block with 100% transparency.

    Any ideas?

  2. #2
    Join Date
    May 2012
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Snap openGL image with transparent background

    Bah! I solved it. Alpha buffer is disabled by default. I needed to enable it. It works fine

Similar Threads

  1. Transparent Push button over background image
    By KumarKandasamy in forum Newbie
    Replies: 2
    Last Post: 15th May 2014, 07:22
  2. Replies: 8
    Last Post: 10th December 2013, 13:31
  3. Replies: 4
    Last Post: 27th November 2013, 15:15
  4. Problem with OpenGL support (transparent background)
    By developer-nsk in forum Qt Programming
    Replies: 0
    Last Post: 2nd November 2009, 19:31
  5. Transparent background on QLabel on transparent QWidget
    By codeslicer in forum Qt Programming
    Replies: 1
    Last Post: 13th February 2008, 02:10

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.