Important: Please read the Qt Code of Conduct -

OpengGL Shader Program fails

  • Yes you are right .I found this mistake.I removed the pointer and tried to work with the shader program variable directly.Now the program runs with no errors.But the viewport is empty.It could be really helpful if Qt team could put an example of OpenGL 3x setup.

  • Thanks for this thread.So it seems that in Qt 4.7 there is a bug? Anybody has an idea when it is going to be fixed?

  • I've not checked but it may already be fixed in 4.7.4. If not then you may need to build a 4.8.0 pre-release and try that. What version are you using?

  • I am using 4.7.4. ...And I am getting an empty screen in GL

  • As long as you specify to use the CompatibilityProfile it should work anyway. Maybe the bug is elsewhere is your code? Can you provide a small compilable example please?

  • Here it is .Pretty same as of the guy in the above mentioned thread:
    Window class ,calls the OpenGLWrapper widget:
    QGLFormat fmt;
    glWidget=new OpenGLWrapper(fmt);




    #include "openglwrapper.h"
    #include "GL/glext.h"
    #include "qtlogo.h"

    #ifndef GL_MULTISAMPLE
    #define GL_MULTISAMPLE 0x809D
    bool OpenGLWrapper::USE_SHADERS = 1;
    static GLfloat const triangleVertices[] = {
    60.0f, 10.0f, 0.0f,
    110.0f, 110.0f, 0.0f,
    10.0f, 110.0f, 0.0f
    OpenGLWrapper::OpenGLWrapper(const QGLFormat & format,QWidget *parent):QGLWidget(format,parent),mShaderProgram(this)

    qtGreen = QColor::fromCmykF(0.10, 0.78, 1.0, 0.0);
    qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);


    void OpenGLWrapper::initShaderProgram(){

    "#version 330\n"
    "in highp vec4 vertex;\n"
    "in mediump mat4 matrix;\n"
    "void main(void)\n"
    " gl_Position = matrix * vertex;\n"
    "#version 330\n"
    "uniform mediump vec4 color;\n"
    "out mediump vec4 out_color;\n"
    "void main(void)\n"
    " out_color = color;\n"
    bool linked=;
    bool binded= mShaderProgram.bind();


    QSize OpenGLWrapper::minimumSizeHint()const{
    return QSize(50,50);
    QSize OpenGLWrapper::sizeHint()const{
    return QSize(400,400);
    static void qNormalizeAngle(int &angle){
    while (angle < 0)
    angle += 360 * 16;
    while (angle > 360 * 16)
    angle -= 360 * 16;

    void OpenGLWrapper::setXRotation(int angle){
    emit xRotationChanged(angle);
    void OpenGLWrapper::setYRotation(int angle)
    if (angle != yRot) {
    yRot = angle;
    emit yRotationChanged(angle);

    void OpenGLWrapper::setZRotation(int angle)
    if (angle != zRot) {
    zRot = angle;
    emit zRotationChanged(angle);

    void OpenGLWrapper::initializeGL()

    logo = new QtLogo(this, 64);
    static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);


    void OpenGLWrapper::paintGL()
    QMatrix4x4 pmvMatrix;
    int vertexLocation = mShaderProgram.attributeLocation("vertex");
    int matrixLocation = mShaderProgram.attributeLocation("matrix");
    int colorLocation = mShaderProgram.uniformLocation("color");

    mShaderProgram.setAttributeArray(vertexLocation, triangleVertices,3);
    mShaderProgram.setUniformValue(matrixLocation, pmvMatrix);
    mShaderProgram.setUniformValue(colorLocation,QColor(0, 255, 0, 255));

    glDrawArrays(GL_TRIANGLES, 0, 3);


    void OpenGLWrapper::printGLString()
    const char* version = reinterpret_cast<const char *>(glGetString(GL_VERSION));
    qDebug() << "GL_VERSION:" << version;


    void OpenGLWrapper::resizeGL(int width, int height)
    int side = qMin(width, height);
    glViewport((width - side) / 2, (height - side) / 2, side, side);


    #ifdef QT_OPENGL_ES_1
    glOrthof(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
    glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);

    void OpenGLWrapper::mousePressEvent(QMouseEvent *event)
    lastPos = event->pos();

    void OpenGLWrapper::mouseMoveEvent(QMouseEvent *event)
    int dx = event->x() - lastPos.x();
    int dy = event->y() - lastPos.y();

    if (event->buttons() & Qt::LeftButton) {
        setXRotation(xRot + 8 * dy);
        setYRotation(yRot + 8 * dx);
    } else if (event->buttons() & Qt::RightButton) {
        setXRotation(xRot + 8 * dy);
        setZRotation(zRot + 8 * dx);
    lastPos = event->pos();





  • [quote author="sasmaster" date="1315900787"]I am trying to write a modern OpenGL (programmable pipeline) program.Qt OpenGL examples show only the fixed pipeline implementation.The documentation on how to initialize Shader Program is very poor.[/quote]

    No, it's not poor. You should learn how to setup rendering pipeline in OpenGL. This is not Qt fault.

    BTW, did you bind attribute location?

    And usually there is error report from graphic driver why something failed.

  • The code I am using here to pass attributes into shaders is taken from the "not poor" "docs": of Qt.

    Regarding the attributes location binding ,I think this does this work in Qt :
    int vertexLocation = mShaderProgram.attributeLocation("vertex");
    int matrixLocation = mShaderProgram.attributeLocation("matrix");
    int colorLocation = mShaderProgram.uniformLocation("color");

    Yes this is slightly different from how it is done in "raw" OpenGL .May be you can point me where I am wrong ? Also the Dude from here does it the same way.

    I am getting no shader program errors either .Everything compiles and runs OK .But the display is empty.

  • What is the reason of getting empty screen in GL ?

  • That is the question of this post :)

  • Yes it is but what is the code about ?

  • Triangle.

  • [quote author="sasmaster" date="1316083047"]
    I am getting no shader program errors either .Everything compiles and runs OK .But the display is empty.[/quote]

    Sorry for lateness, I am looking at your code, first question, why did you enable GL_LIGHTING/GL_LIGHT? That's fixed pipeline stuff.

    Second look, GL_MODELVIEW and GL_PROJECTION matrices, you don't have that also in programmable pipeline. You have to do it yourself in vertex shader. Yes, there is option to set the matrices and access them in shader, but all of the multiplying you have to do it yourself.

  • Those are remnants from my previous trials.I don't think those influence the modern pipeline as I am using a compatibility mode .So those should be discarded by the OpenGL. Regarding the matrices ,you can see that I load a orthogonal projection matrix into the vertex shader on each render loop. Still nothing works.If anybody could put a full example on how to write a OpenGL 3x in Qt it could be really helpful to many people I believe :)

  • Hey, I have example, Oh, I am sorry it's in my native language (if it is for somebody interesting I would translate/write in english) but code is simple for understanding


  • He He ,very resembles Russian which I know :) .Is it wrapped with Qt? Thanks a lot .I will check it out!

  • Also you can use it for learning basic usage of VBO. :)

  • I can see you didn't define the format and it actually works ?Another question ,are those inclosed GLEW headers and sources used in your program?

  • [quote author="sasmaster" date="1316340394"]I can see you didn't define the format and it actually works ?Another question ,are those inclosed GLEW headers and sources used in your program? [/quote]

    By format you mean 'window framebuffer pixel format'?

    Yes, it's using GLEW, since there are glGenBuffers, glBindBuffers, glBufferData, glVertexAttribPointer etc.

  • But why haven't you used the Qt version of those methods?
    By Format I mean the QGLFormat.

  • [quote author="sasmaster" date="1316343899"]But why haven't you used the Qt version of those methods?[/quote]

    You mean QGLBuffer. Well at that time there was no QGLBuffer.

    [quote]By Format I mean the QGLFormat.[/quote]

    It uses default format. I don't see why would I need a fancy format for that code. :)

  • What's the motivation to use QGLShaderProgram anyway instead of standard OpenGL for portability?

  • An idea (maybe wrong...) : Implementation of openGL is platform (hardware) dependent. So I guess it could permit an universal call through various platforms or OpenGL revision (and avoid use of OGL extension which I used to add on mac OS platform for exemple).
    Nervertheless I prefer use pure OpenGL functions call as long as OGL 4.0 (and so OGL ES 2.0) will not be supported anywhere...

  • minimoog77 .Getting can't open the shader files errors when running your demo.Do you know what can be the problem?

    QGLShader: Unable to open file "/vert1.vert"
    QGLShader: Unable to open file "simple.frag"


  • Well ,currently can get it working with inline string typed shaders.shader files somehow don't get loaded.Also I see that the best thing is to use native (non Gt wrappers) OpenGL libs

Log in to reply