简体   繁体   中英

“shader program is not linked” when running QT hello opengl

I have downloaded a QT OpenGL tutorial from http://releases.qt-project.org/learning/developerguides/qtopengltutorial/OpenGLTutorial.pdf and am trying to run the simplest example they have in there, which simply draws a triangle on top of a black background.

I managed to compile the example, but when I run it, I only get a black window and the console reports the following:

Starting /home/minas/Desktop/hello-opengl-build-Desktop_Qt_5_0_1_GCC_64bit-Debug/hello-opengl... QGLShaderProgram::uniformLocation( mvpMatrix ): shader program is not linked QGLShaderProgram::uniformLocation( color ): shader program is not linked QGLShaderProgram::attributeLocation( vertex ): shader program is not linked QGLShaderProgram::attributeLocation( vertex ): shader program is not linked QGLShaderProgram::attributeLocation( vertex ): shader program is not linked QGLShaderProgram::uniformLocation( mvpMatrix ): shader program is not linked QGLShaderProgram::uniformLocation( color ): shader program is not linked QGLShaderProgram::attributeLocation( vertex ): shader program is not linked QGLShaderProgram::attributeLocation( vertex ): shader program is not linked QGLShaderProgram::attributeLocation( vertex ): shader program is not linked QGLShaderProgram::uniformLocation( mvpMatrix ): shader program is not linked QGLShaderProgram::uniformLocation( color ): shader program is not linked QGLShaderProgram::attributeLocation( vertex ): shader program is not linked QGLShaderProgram::attributeLocation( vertex ): shader program is not linked QGLShaderProgram::attributeLocation( vertex ): shader program is not linked QGLShaderProgram::uniformLocation( mvpMatrix ): shader program is not linked QGLShaderProgram::uniformLocation( color ): shader program is not linked QGLShaderProgram::attributeLocation( vertex ): shader program is not linked QGLShaderProgram::attributeLocation( vertex ): shader program is not linked QGLShaderProgram::attributeLocation( vertex ): shader program is not linked

The .pro file looks like this (the "unix:!mac" part is my addition, so it finds the OpenGL .so files).

QT += core gui opengl

TARGET = hello-opengl

TEMPLATE = app

SOURCES += main.cpp\
           glwidget.cpp

HEADERS += glwidget.h

OTHER_FILES += fragmentShader.fsh\
               vertexShader.vsh

RESOURCES += resources.qrc

unix:!mac{
    QMAKE_LFLAGS += -Wl,--rpath=/usr/lib64/nvidia-current
    QMAKE_LFLAGS += -L/usr/lib64/nvidia-current
}

The output of "glxinfo | grep -i opengl" is

OpenGL vendor string: NVIDIA Corporation
OpenGL renderer string: GeForce 9800 GT/PCIe/SSE2
OpenGL version string: 2.1.2 NVIDIA 304.64
OpenGL shading language version string: (null)
OpenGL extensions:

and the part of the code that does the actual rendering is

void GlWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QMatrix4x4 mMatrix;
    QMatrix4x4 vMatrix;

    // shaderProgram initialized in initializeGL()    
    shaderProgram.bind();

    // "mvpMatrix", "color" and "vertex" do exist in the shaders

    shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);

    shaderProgram.setUniformValue("color", QColor(Qt::white));

    shaderProgram.setAttributeArray("vertex", vertices.constData());
    shaderProgram.enableAttributeArray("vertex");

    glDrawArrays(GL_TRIANGLES, 0, vertices.size());

    shaderProgram.disableAttributeArray("vertex");

    shaderProgram.release();
}

Also, as a side-question, why does the output of "glxinfo | grep -i opengl" contain the line

OpenGL shading language version string: (null)

(kinda seems relevant to me... is it?)


EDIT 1

The shaderProgram is declared in my subclass of QGLWidget , which I call GLWidget and initialized in initializeGL as such:

void GlWidget::initializeGL()
{
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    qglClearColor(QColor(Qt::black));

    shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
    shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");
    shaderProgram.link();

    vertices << QVector3D(1, 0, -2) << QVector3D(0, 1, -2) << QVector3D(-1, 0, -2);
}

EDIT 2

Here is my initializeGL() method:

void GlWidget::initializeGL()
{
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    qglClearColor(QColor(Qt::black));

    shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
    shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");
    shaderProgram.link();

    vertices << QVector3D(1, 0, -2) << QVector3D(0, 1, -2) << QVector3D(-1, 0, -2);
}

and putting

qDebug() << shaderProgram.log();
exit(0);

immediately after the call to link() above does not produce any output.

Was seeing this just now. Was strange because link() was returning true, yet it claimed the program was not linked?

Turned out I needed to move two bindAttributeLocation() statements to before my link() call. Apparently if you use bindAttributeLocation() on a program after it's linked, it will cause the program to no longer be linked.

Wrong:

    program.link();

    program.bindAttributeLocation("position", 0);
    program.bindAttributeLocation("texcoord", 1);

Right:

    program.bindAttributeLocation("position", 0);
    program.bindAttributeLocation("texcoord", 1);

    program.link();

// shaderProgram was initialized in the class constructor

If you compiled and linked shader source files in your class constructor then that is most likely the culprit. You should override the initializeGL member of QGLWidget and do the intialization for OpenGL objects there. You cannot do this any earlier as your OpenGL context has not been established, and thus you cannot create or manage any OpenGL objects.

I found that problem too, I just used FromSourceCode instead from FromSourceFile like this:

shaderProgram.addShaderFromSourceCode(QGLShader::Fragment,  "uniform vec4 color;\n"
                                      "out vec4 fragColor;\n"
                                      "void main(void)\n"
                                      "{\n"
                                      "   fragColor = color;\n"
                                      "}");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM