Results 1 to 3 of 3

Thread: QGraphicsScene and GL textures

  1. #1
    Join Date
    Feb 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsScene and GL textures

    Hi all,
    I'm new to GL, and I've got to get this working soon. I've several images to be blended together in the background of the scene, and it seems logical to upload the images to the textures outside of the drawBackground(). But I only get a white rectangle, so the textures are lost somewhere. If I uncomment the code in drawBackground() then it works, but not efficient. Please enlighten me.

    Qt Code:
    1. #define GL_GLEXT_PROTOTYPES 1
    2.  
    3. #include <QtGui>
    4. #include <QGLWidget>
    5. #include <GL/glext.h>
    6.  
    7. class GraphicsView : public QGraphicsView
    8. {
    9. public:
    10. GraphicsView(QWidget* parent = 0) : QGraphicsView(parent) { }
    11.  
    12. protected:
    13. void resizeEvent(QResizeEvent *event) {
    14. if (scene())
    15. scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
    16. QGraphicsView::resizeEvent(event);
    17. }
    18. };
    19.  
    20. class GLScene : public QGraphicsScene
    21. {
    22. public:
    23. GLScene();
    24. ~GLScene();
    25.  
    26. protected:
    27. virtual void drawBackground(QPainter *painter, const QRectF &rect);
    28. virtual void wheelEvent(QGraphicsSceneWheelEvent*);
    29.  
    30. private:
    31. QImage m_img1;
    32. QImage m_img2;
    33. double m_zoom;
    34. GLuint m_tex1;
    35. GLuint m_tex2;
    36. };
    37.  
    38. GLScene::GLScene() : QGraphicsScene(),
    39. m_img1("img1.png"),
    40. m_img2("img2.png"),
    41. m_zoom(0.8)
    42. {
    43. setBackgroundBrush(Qt::NoBrush);
    44.  
    45. addWidget(new QPushButton("hello"))->setPos(10, 10);
    46. addEllipse(QRectF(50, 50, 100, 70), QPen(Qt::red), QBrush(QColor(128, 192, 0, 128)));
    47.  
    48. glGenTextures(1, &m_tex1);
    49. glGenTextures(1, &m_tex2);
    50.  
    51. glBindTexture(GL_TEXTURE_2D, m_tex1);
    52. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    53. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    54. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    55. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_img1.width(), m_img1.height(), 0,
    56. GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, m_img1.bits());
    57.  
    58. glBindTexture(GL_TEXTURE_2D, m_tex2);
    59. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    60. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    61. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    62. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_img2.width(), m_img2.height(), 0,
    63. GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, m_img2.bits());
    64. glFlush();
    65. }
    66.  
    67. GLScene::~GLScene()
    68. {
    69. glDeleteTextures(1, &m_tex1);
    70. glDeleteTextures(1, &m_tex2);
    71. }
    72.  
    73. void GLScene::drawBackground(QPainter*, const QRectF&)
    74. {
    75. glClearColor(0.0, 0.0, 0.0, 1.0f);
    76. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    77.  
    78. GLint blendSrc;
    79. GLint blendDst;
    80. glGetIntegerv(GL_BLEND_SRC, &blendSrc);
    81. glGetIntegerv(GL_BLEND_DST, &blendDst);
    82.  
    83. glViewport(0, 0, (GLsizei) width(), (GLsizei) height());
    84. glMatrixMode(GL_PROJECTION);
    85. glPushMatrix();
    86. glLoadIdentity();
    87. gluOrtho2D(-1.0, 1.0, 1.0, -1.0);
    88. glMatrixMode(GL_MODELVIEW);
    89. glPushMatrix();
    90. glLoadIdentity();
    91.  
    92. glEnable(GL_BLEND);
    93. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    94. glEnable(GL_TEXTURE_2D);
    95.  
    96. glColor4f(1.0, 1.0, 1.0, 1.0);
    97. glBindTexture(GL_TEXTURE_2D, m_tex1);
    98. // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    99. // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    100. // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_img1.width(), m_img1.height(), 0,
    101. // GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, m_img1.bits());
    102. glBegin(GL_QUADS);
    103. glTexCoord2f(0.0, 0.0); glVertex2f(-m_zoom, -m_zoom);
    104. glTexCoord2f(1.0, 0.0); glVertex2f(m_zoom, -m_zoom);
    105. glTexCoord2f(1.0, 1.0); glVertex2f(m_zoom, m_zoom);
    106. glTexCoord2f(0.0, 1.0); glVertex2f(-m_zoom, m_zoom);
    107. glEnd();
    108.  
    109. glColor4f(1.0, 1.0, 1.0, m_zoom);
    110. glBindTexture(GL_TEXTURE_2D, m_tex2);
    111. // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    112. // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    113. // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_img2.width(), m_img2.height(), 0,
    114. // GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, m_img2.bits());
    115. glBegin(GL_QUADS);
    116. glTexCoord2f(0.0, 0.0); glVertex2f(-m_zoom*.9, -m_zoom*.9);
    117. glTexCoord2f(1.0, 0.0); glVertex2f(m_zoom*.9, -m_zoom*.9);
    118. glTexCoord2f(1.0, 1.0); glVertex2f(m_zoom*.9, m_zoom*.9);
    119. glTexCoord2f(0.0, 1.0); glVertex2f(-m_zoom*.9, m_zoom*.9);
    120. glEnd();
    121.  
    122. glDisable(GL_TEXTURE_2D);
    123.  
    124. glPopMatrix();
    125. glMatrixMode(GL_PROJECTION);
    126. glPopMatrix();
    127. glBlendFunc(blendSrc, blendDst);
    128. glFlush();
    129. }
    130.  
    131. void GLScene::wheelEvent(QGraphicsSceneWheelEvent* event)
    132. {
    133. m_zoom += (double)event->delta()/1200.0;
    134. update();
    135. }
    136.  
    137. int main(int argc, char **argv)
    138. {
    139. QApplication app(argc, argv);
    140.  
    141. GraphicsView view;
    142. view.setViewport(
    143. new QGLWidget(QGLFormat(QGL::DoubleBuffer |
    144. QGL::Rgba | QGL::AlphaChannel |
    145. QGL::DirectRendering | QGL::SampleBuffers)));
    146. view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    147. view.setScene(new GLScene);
    148. view.setRenderHints(QPainter::Antialiasing);
    149. view.show();
    150.  
    151. view.resize(400, 400);
    152.  
    153. return app.exec();
    154. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsScene and GL textures

    First error is that enabling blend feature must be moved after drawing the first rectangle, but I still don't get the textures.

    Qt Code:
    1. glDisable(GL_BLEND);
    2. glEnable(GL_TEXTURE_2D);
    3. glColor4f(1.0, 1.0, 1.0, 1.0);
    4. glBindTexture(GL_TEXTURE_2D, m_tex1);
    5. // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    6. // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    7. // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_img1.width(), m_img1.height(), 0,
    8. // GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, m_img1.bits());
    9. glBegin(GL_QUADS);
    10. ...
    11. glEnd();
    12.  
    13. glEnable(GL_BLEND);
    14. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    15. glColor4f(1.0, 1.0, 1.0, m_zoom);
    16. glBindTexture(GL_TEXTURE_2D, m_tex2);
    17. // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    18. // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    19. // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_img2.width(), m_img2.height(), 0,
    20. // GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, m_img2.bits());
    21. glBegin(GL_QUADS);
    22. ...
    23. glEnd();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsScene and GL textures

    Never mind.
    When uploading the textures there must be a current GL context, e.g. the viewport can be passed here to call makeCurrent() on it, so glGenTextures() can return valid texture ids, which wasn't the case.

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 17:37
  2. QGraphicsScene performance
    By Aceman2000 in forum Qt Programming
    Replies: 4
    Last Post: 2nd June 2008, 19:10
  3. QPrinter on QGraphicsScene Border Problem
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 16:49
  4. Replies: 4
    Last Post: 11th July 2007, 05:21
  5. When to use QGraphicsScene or QWidget
    By fossill in forum Qt Programming
    Replies: 2
    Last Post: 10th February 2007, 00:58

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.