I am trying to use an OpenGL Shader on a QGrapicsView. My graphic card is a NVidia GeForce GT 425M. I made my experiments on a Ubuntu 12.04 with the NVidia driver 295.40.

I Create a QMainWindow containing two QGraphicsView, each graphics view are displaying the same scene. On One of the Graphics View, I setup an OpenGL View port. The OpenGL View Port is a QGLWidget I had overloaded with the loading of the vertex and fragment shader programs.

Look at my QMainWindow constructor :

Qt Code:
  1. MainWindow::MainWindow(QWidget *parent) :
  2. QMainWindow(parent),
  3. ui(new Ui::MainWindow)
  4. {
  5. ui->setupUi(this);
  6. m_scene = new QGraphicsScene(this);
  7. ui->gvShaded->setScene(m_scene);
  8. ui->gvUnshaded->setScene(m_scene);
  9. m_glWidget = new QGLScene();
  10. ui->gvShaded->setViewport(m_glWidget);
  11. m_glWidget->updateGL();
  12. if(m_glWidget->context()->isValid())
  13. {
  14. qDebug()<<"GL Context is valid";
  15. }else
  16. {
  17. qWarning()<<"GL Context is not valid";
  18. }
  19.  
  20. qDebug() << "Opengl Version : " << m_glWidget->context()->format().openGLVersionFlags();
  21.  
  22. QBrush brush(QLinearGradient(0,0,20,0));
  23. brush.setColor(Qt::blue);
  24. QGraphicsRectItem* rect = m_scene->addRect(0,0,20,20,QPen(Qt::black), brush);
  25.  
  26. ui->gvShaded->ensureVisible(rect);
  27. ui->gvUnshaded->ensureVisible(rect);
  28. }
To copy to clipboard, switch view to plain text mode 

As you can see I instantiate the object QGLScene which is :

Qt Code:
  1. class QGLScene : public QGLWidget
  2. {
  3. public:
  4. QGLScene(QWidget* _parent=0);
  5. protected :
  6. virtual void initializeGL();
  7. };
To copy to clipboard, switch view to plain text mode 

My GLWidget Construction is :
Qt Code:
  1. QGLScene::QGLScene(QWidget *_parent)
  2. :QGLWidget(QGLFormat(QGL::DepthBuffer), _parent)
  3. {
  4. }
To copy to clipboard, switch view to plain text mode 
And definition of initializeGL is :

Qt Code:
  1. void QGLScene::initializeGL()
  2. {
  3. qDebug() << __PRETTY_FUNCTION__;
  4. m_fragmentShader = new QGLShader(QGLShader::Fragment, context(), this);
  5. m_vertexShader = new QGLShader(QGLShader::Vertex, context(), this);
  6. m_pgm = new QGLShaderProgram(context(), this);
  7. if(m_vertexShader->compileSourceFile("./vertexShader.glsl"))
  8. {
  9. if(m_fragmentShader->compileSourceFile("./fragmentShader.glsl"))
  10. {
  11. m_pgm->addShader(m_fragmentShader);
  12. m_pgm->addShader(m_vertexShader);
  13. if(m_pgm->link()==false)
  14. {
  15. qDebug() << "Error When linking: " << m_pgm->log();
  16. }
  17. if(m_pgm->bind()==false)
  18. {
  19. qDebug() << "Can't bind.";
  20. }
  21. qDebug() << "Shader bien chargé : "<< m_pgm->log();
  22. }else
  23. {
  24. qDebug() << "Problem when loading fragment shader :";
  25. qDebug() << m_fragmentShader->log();
  26. qDebug() << QString(m_fragmentShader->sourceCode());
  27. }
  28. }else
  29. {
  30. qDebug() << "Problem when loading vertex shader :";
  31. qDebug() << m_vertexShader->log();
  32. qDebug() << QString(m_vertexShader->sourceCode());
  33. }
  34. }
To copy to clipboard, switch view to plain text mode 

My shaders are very useless shaders : Vertex :

Qt Code:
  1. #version 150
  2.  
  3. uniform mat4 viewMatrix, projMatrix;
  4.  
  5. in vec4 position;
  6. in vec3 color;
  7.  
  8. out vec3 Color;
  9.  
  10. void main()
  11. {
  12. Color = color;
  13. gl_Position = projMatrix * viewMatrix * position ;
  14. }
To copy to clipboard, switch view to plain text mode 
Fragment :
Qt Code:
  1. #version 150
  2.  
  3. in vec3 Color;
  4. out vec4 outputF;
  5.  
  6. void main()
  7. {
  8. outputF = vec4(1.0, 0, 0, 1.);
  9. }
To copy to clipboard, switch view to plain text mode 

So I attempt to have my Shaded Graphics view as a big red square because I think I enforced the pixels to be red (in my fragment shader).

I have no shader compilation error, but my two scene are the same. Of course, the program pass into the initializeGL function (I can read the PRETTY_FUNCTION).

Does some one have an explanation ?