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