Results 1 to 2 of 2

Thread: Why QGLFramebufferObject in QGraphicsItem don’t work?

  1. #1
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Why QGLFramebufferObject in QGraphicsItem don’t work?

    Hi. I still have the problem with programming a QGLFramebufferObject in a QGraphicsItem class.

    This time i used another approach and got an incorrect rendering of the framebufferobject and a message in my output result. (look at the attachment)

    QGLContext::chooseContext() : SetPixelFormat failed : The pixel format is invalid.

    The following source code init and renders a QGLFramebufferObject in the QGraphicsItem::paint method and after that it binds the FBO Texture to a GL_QUADS in the scene.

    header file:
    Qt Code:
    1. #ifndef __YidQt_FBOGraphicsItem_h__
    2. #define __YidQt_FBOGraphicsItem_h__
    3.  
    4. #include <QGraphicsItem>
    5. #include <QtOpenGL>
    6.  
    7.  
    8. class FBOGraphicsItem : public QGraphicsItem
    9. {
    10.  
    11. //Q_OBJECT
    12.  
    13. public:
    14.  
    15. FBOGraphicsItem();
    16.  
    17. // pure virtual functions from QGraphicsItem
    18. virtual QRectF boundingRect() const;
    19. virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    20.  
    21. private:
    22.  
    23. QGLContext *context;
    24.  
    25. QRectF myBoundingRect;
    26. float xx;
    27. float yy;
    28.  
    29. };
    30.  
    31. #endif // __YidQt_FBOGraphicsItem_h__
    To copy to clipboard, switch view to plain text mode 


    source file:
    Qt Code:
    1. void FBOGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3.  
    4. if (painter->paintEngine()->type() != QPaintEngine::OpenGL &&
    5. painter->paintEngine()->type() != QPaintEngine::OpenGL2)
    6. {
    7. qWarning("TestGLGraphicsItem: drawBackground needs a QGLWidget to be set as viewport on the graphics view");
    8. return;
    9. }
    10.  
    11. float a1 = 0.5f * 1;
    12. float b1 = 0.5f * 1;
    13.  
    14.  
    15. // !!! error with this context !!!
    16. context = new QGLContext(QGLFormat(QGL::SampleBuffers | QGL::DoubleBuffer | QGL::AlphaChannel), widget);
    17. context->create(); // this line create an error: QGLContext::chooseContext() : SetPixelFormat failed
    18. context->makeCurrent();
    19.  
    20.  
    21. // ------------ FBO begin ------------
    22.  
    23. fbo = new QGLFramebufferObject(512, 512);
    24.  
    25. fbo->bind();
    26. {
    27. glPushAttrib(GL_ALL_ATTRIB_BITS);
    28. {
    29. glViewport(0, 0, fbo->size().width(), fbo->size().height());
    30.  
    31. glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
    32. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    33.  
    34. glMatrixMode(GL_PROJECTION);
    35. glPushMatrix();
    36. {
    37. glLoadIdentity();
    38. glOrtho(-1.0, 1.0, -1.0, 1.0, 99, -99);
    39. gluLookAt(0.0, 0.0, 1.0,
    40. 0.0, 0.0, 0.0,
    41. 0.0, 1.0, 0.0);
    42.  
    43. glMatrixMode(GL_MODELVIEW);
    44. glPushMatrix();
    45. {
    46. glLoadIdentity();
    47.  
    48. glColor3f(1.0f, 0.0f, 0.0f);
    49. glBegin(GL_QUADS);
    50. glVertex2f(-a1, -b1);
    51. glVertex2f(-a1, b1);
    52. glVertex2f( a1, b1);
    53. glVertex2f( a1, -b1);
    54. glEnd();
    55.  
    56. // control points
    57. glPointSize(5.0f);
    58. glColor3f(0.0f, 0.0f, 0.0f);
    59. glBegin(GL_POINTS);
    60. glVertex3f(0.0f, 0.0f, 0.0f);
    61.  
    62. glVertex3f(a1, 0.0f, 0.0f);
    63. glVertex3f(-a1, 0.0f, 0.0f);
    64.  
    65. glVertex3f(0.0f, a1, 0.0f);
    66. glVertex3f(0.0f, -a1, 0.0f);
    67.  
    68. glVertex3f(0.0f, 0.0f, a1);
    69. glVertex3f(0.0f, 0.0f, -a1);
    70. glEnd();
    71. }
    72. glMatrixMode(GL_MODELVIEW);
    73. glPopMatrix();
    74. }
    75. glMatrixMode(GL_PROJECTION);
    76. glPopMatrix();
    77. }
    78. glPopAttrib();
    79. }
    80.  
    81. fbo->release();
    82. fbo->toImage().save("d:\\fbo.bmp");
    83.  
    84. // ------------ FBO end ------------
    85.  
    86.  
    87. // ------------ SZENE begin ------------
    88.  
    89. painter->beginNativePainting();
    90.  
    91. glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    92. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    93.  
    94. glMatrixMode(GL_PROJECTION);
    95. glPushMatrix();
    96. {
    97. glLoadIdentity();
    98. gluPerspective(70, WIDTH / HEIGHT, 0.01, 1000);
    99.  
    100. glMatrixMode(GL_MODELVIEW);
    101. glPushMatrix();
    102. {
    103. glLoadIdentity();
    104. gluLookAt(0,0,1,
    105. 0,0,0,
    106. 0,1,0);
    107.  
    108. glTranslatef(0, 0, 0);
    109.  
    110. glEnable(GL_TEXTURE_2D);
    111. {
    112. glBindTexture(GL_TEXTURE_2D, fbo->texture());
    113. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    114. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    115. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    116.  
    117. glBegin(GL_QUADS);
    118. glTexCoord2f(0.0f, 0.0f); glVertex2f(-xx, -yy);
    119. glTexCoord2f(0.0f, 1.0f); glVertex2f(-xx, yy);
    120. glTexCoord2f(1.0f, 1.0f); glVertex2f( xx, yy);
    121. glTexCoord2f(1.0f, 0.0f); glVertex2f( xx, -yy);
    122. glEnd();
    123. }
    124. glDisable(GL_TEXTURE_2D);
    125. }
    126. glMatrixMode(GL_MODELVIEW);
    127. glPopMatrix();
    128. }
    129. glMatrixMode(GL_PROJECTION);
    130. glPopMatrix();
    131.  
    132. painter->endNativePainting();
    133.  
    134. // ------------ SZENE end ------------
    135.  
    136. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Why QGLFramebufferObject in QGraphicsItem don’t work?

    good news, i’ve found a way to rendering a correct fbo to the quad in the scene.

    it was dependend of the order of the painter->beginNativePainting(); line. i switch the order, that the painter->beginNativePainting(); is before the QGLContext and QGLFramebufferobject is init.

    the message is still active but the result is correct.

    source
    Qt Code:
    1. painter->beginNativePainting();
    2.  
    3. // ------------ FBO begin ------------
    4.  
    5. context = new QGLContext(QGLFormat(QGL::SampleBuffers | QGL::DoubleBuffer | QGL::AlphaChannel), widget);
    6. context->create(); // !!!
    7. context->makeCurrent();
    8.  
    9. fbo = new QGLFramebufferObject(512, 512); // GLWidget::GLWidget (constructor)
    10.  
    11. fbo->bind();
    12. {
    13. ...
    14. }
    15. fbo->release();
    To copy to clipboard, switch view to plain text mode 


Similar Threads

  1. QGLWidget and QGLFramebufferObject
    By ConfusedSushi in forum Qt Programming
    Replies: 1
    Last Post: 22nd September 2010, 20:47
  2. I just don´t get the MySQL Driver Compiled
    By alex04721 in forum Installation and Deployment
    Replies: 24
    Last Post: 8th March 2010, 17:20
  3. QGraphicsItem::setToolTip does not work
    By Nadia in forum Qt Programming
    Replies: 2
    Last Post: 6th September 2009, 19:54
  4. problem on QGLWidget and QGLFramebufferObject
    By xylosper in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2008, 11:57
  5. Targets in QGLFramebufferObject
    By trskel in forum Qt Programming
    Replies: 0
    Last Post: 3rd March 2008, 13:18

Tags for this Thread

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.