Results 1 to 1 of 1

Thread: Problem with FBO in QGraphicsItem

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

    Default Problem with FBO in QGraphicsItem

    Hi. I have a problem with programming a Framebufferobject in a QGraphicsItem Class.

    It is simple, but it doesn’t work in the Context of the GraphicsItem. When it runs separated its works fine. The Problem when it should runs together has something to do with QGLContext. The following source code init and renders a Framebufferobject in the QGraphicsItem::paint method and after that it binds the FBO Texture to a GL_QUADS in the scene.

    The Problem, look also at the picture in the attachment

    1. If there is an additional QGLContext in QGraphicsItem::paint then the Framebufferobject rendering is correct, but the FBO texture didn’t bind to the Quad.

    2. If there is no additional QGLContext, then the Framebufferobject rendering is disturbed, but the FBO texture is correctly bind to the Quad.


    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. QWidget *widget2;
    24. QGLContext *context;
    25.  
    26. QRectF myBoundingRect;
    27. float xx;
    28. float yy;
    29.  
    30. };
    31.  
    32. #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. // ------------ FBO begin ------------
    15.  
    16. // !!! additional context for rendering a correct framebufferobject
    17. widget2 = new QWidget();
    18. context = new QGLContext(QGLFormat(QGL::SampleBuffers | QGL::DoubleBuffer | QGL::AlphaChannel), widget2);
    19. context->create();
    20. context->makeCurrent();
    21. fbo = new QGLFramebufferObject(512, 512);
    22.  
    23. fbo->bind();
    24. {
    25. // push the projection matrix and the entire GL state before
    26. // doing any rendering into our framebuffer object
    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. glOrtho(-1.0, 1.0, -1.0, 1.0, 99, -99);
    38. gluLookAt(0.0, 0.0, 1.0,
    39. 0.0, 0.0, 0.0,
    40. 0.0, 1.0, 0.0);
    41.  
    42. glMatrixMode(GL_MODELVIEW);
    43. glPushMatrix();
    44. {
    45. glColor3f(1.0f, 0.0f, 0.0f);
    46. glBegin(GL_QUADS);
    47. glVertex2f(-a1, -b1);
    48. glVertex2f(-a1, b1);
    49. glVertex2f( a1, b1);
    50. glVertex2f( a1, -b1);
    51. glEnd();
    52. }
    53. glMatrixMode(GL_MODELVIEW);
    54. glPopMatrix();
    55. }
    56. glMatrixMode(GL_PROJECTION);
    57. glPopMatrix();
    58. }
    59. glPopAttrib();
    60. }
    61. fbo->release();
    62.  
    63. fbo->toImage().save("d:\\fbo.bmp");
    64.  
    65. // ------------ FBO end ------------
    66.  
    67.  
    68. // ------------ SZENE begin ------------
    69.  
    70. painter->beginNativePainting();
    71.  
    72. glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    73. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    74.  
    75. glMatrixMode(GL_PROJECTION);
    76. glPushMatrix();
    77. {
    78. glLoadIdentity();
    79. gluPerspective(70, WIDTH / HEIGHT, 0.01, 1000);
    80.  
    81. glMatrixMode(GL_MODELVIEW);
    82. glPushMatrix();
    83. {
    84. glLoadIdentity();
    85. gluLookAt(0,0,1, // eye
    86. 0,0,0, // center
    87. 0,1,0); // up
    88.  
    89. glTranslatef(0, 0, 0);
    90.  
    91. glEnable(GL_TEXTURE_2D);
    92. {
    93.  
    94. // !!! bind texture to quad only works without additional content
    95. glBindTexture(GL_TEXTURE_2D, fbo->texture());
    96. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    97. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    98. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    99.  
    100. glBegin(GL_QUADS);
    101. glTexCoord2f(0.0f, 0.0f); glVertex2f(-xx, -yy);
    102. glTexCoord2f(1.0f, 0.0f); glVertex2f(-xx, yy);
    103. glTexCoord2f(1.0f, 1.0f); glVertex2f( xx, yy);
    104. glTexCoord2f(0.0f, 1.0f); glVertex2f( xx, -yy);
    105. glEnd();
    106.  
    107. }
    108. glDisable(GL_TEXTURE_2D);
    109. }
    110. glMatrixMode(GL_MODELVIEW);
    111. glPopMatrix();
    112. }
    113. glMatrixMode(GL_PROJECTION);
    114. glPopMatrix();
    115.  
    116. painter->endNativePainting();
    117.  
    118. // ------------ SZENE end ------------
    119.  
    120. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

Similar Threads

  1. QGraphicsItem problem
    By zgulser in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2010, 10:19
  2. QGraphicsItem scale problem
    By qlands in forum Qt Programming
    Replies: 5
    Last Post: 9th June 2010, 17:43
  3. QGraphicsItem Resize Problem
    By c_srikanth1984 in forum Qt Programming
    Replies: 3
    Last Post: 16th May 2009, 12:05
  4. Having problem with QGraphicsItem
    By Kingofhearts_sri in forum Qt Programming
    Replies: 4
    Last Post: 23rd January 2009, 20:10
  5. Problem with rotation of QGraphicsItem
    By ashishsaryar in forum Qt Programming
    Replies: 2
    Last Post: 10th July 2008, 16:03

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.