PDA

View Full Version : Applying a QGLShader to a whole QGraphicsView



abarral
26th August 2012, 19:51
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 :


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_scene = new QGraphicsScene(this);
ui->gvShaded->setScene(m_scene);
ui->gvUnshaded->setScene(m_scene);
m_glWidget = new QGLScene();
ui->gvShaded->setViewport(m_glWidget);
m_glWidget->updateGL();
if(m_glWidget->context()->isValid())
{
qDebug()<<"GL Context is valid";
}else
{
qWarning()<<"GL Context is not valid";
}

qDebug() << "Opengl Version : " << m_glWidget->context()->format().openGLVersionFlags();

QBrush brush(QLinearGradient(0,0,20,0));
brush.setColor(Qt::blue);
QGraphicsRectItem* rect = m_scene->addRect(0,0,20,20,QPen(Qt::black), brush);

ui->gvShaded->ensureVisible(rect);
ui->gvUnshaded->ensureVisible(rect);
}

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


class QGLScene : public QGLWidget
{
public:
QGLScene(QWidget* _parent=0);
protected :
virtual void initializeGL();
};

My GLWidget Construction is :


QGLScene::QGLScene(QWidget *_parent)
:QGLWidget(QGLFormat(QGL::DepthBuffer), _parent)
{
}
And definition of initializeGL is :


void QGLScene::initializeGL()
{
qDebug() << __PRETTY_FUNCTION__;
m_fragmentShader = new QGLShader(QGLShader::Fragment, context(), this);
m_vertexShader = new QGLShader(QGLShader::Vertex, context(), this);
m_pgm = new QGLShaderProgram(context(), this);
if(m_vertexShader->compileSourceFile("./vertexShader.glsl"))
{
if(m_fragmentShader->compileSourceFile("./fragmentShader.glsl"))
{
m_pgm->addShader(m_fragmentShader);
m_pgm->addShader(m_vertexShader);
if(m_pgm->link()==false)
{
qDebug() << "Error When linking: " << m_pgm->log();
}
if(m_pgm->bind()==false)
{
qDebug() << "Can't bind.";
}
qDebug() << "Shader bien chargé : "<< m_pgm->log();
}else
{
qDebug() << "Problem when loading fragment shader :";
qDebug() << m_fragmentShader->log();
qDebug() << QString(m_fragmentShader->sourceCode());
}
}else
{
qDebug() << "Problem when loading vertex shader :";
qDebug() << m_vertexShader->log();
qDebug() << QString(m_vertexShader->sourceCode());
}
}


My shaders are very useless shaders : Vertex :


#version 150

uniform mat4 viewMatrix, projMatrix;

in vec4 position;
in vec3 color;

out vec3 Color;

void main()
{
Color = color;
gl_Position = projMatrix * viewMatrix * position ;
}
Fragment :


#version 150

in vec3 Color;
out vec4 outputF;

void main()
{
outputF = vec4(1.0, 0, 0, 1.);
}

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 ?