简体   繁体   English

运行 QT hello opengl 时出现“着色器程序未链接”

[英]“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.我已经从http://releases.qt-project.org/learning/developerguides/qtopengltutorial/OpenGLTutorial.pdf下载了一个 QT OpenGL 教程,并试图运行他们在那里的最简单的例子,它只是在上面画一个三角形黑色背景。

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 link启动 /home/minas/Desktop/hello-opengl-build-Desktop_Qt_5_0_1_GCC_64bit-Debug/hello-opengl... QGLShaderProgram::uniformLocation( mvpMatrix ): 着色器程序未链接 QGLShaderProgram::uniformLocation( color ): 着色器程序未链接QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::uniformLocation( mvpMatrix ): 着色器程序是未链接 QGLShaderProgram::uniformLocation( color ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::uniformLocation( mvpMatrix ): 着色器程序未链接 QGLShaderProgram::uniformLocation( color ): 着色器程序未链接ed 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 ed QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::uniformLocation( mvpMatrix ): 着色器程序未链接 QGLShaderProgram::uniformLocation( color ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ):着色器程序未链接

The .pro file looks like this (the "unix:!mac" part is my addition, so it finds the OpenGL .so files). .pro 文件看起来像这样(“unix:!mac”部分是我添加的,所以它会找到 OpenGL .so 文件)。

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 “glxinfo | grep -i opengl”的输出是

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另外,作为一个附带问题,为什么“glxinfo | grep -i opengl”的输出包含该行

OpenGL shading language version string: (null) OpenGL 着色语言版本字符串:(空)

(kinda seems relevant to me... is it?) (似乎与我有关......是吗?)


EDIT 1编辑 1

The shaderProgram is declared in my subclass of QGLWidget , which I call GLWidget and initialized in initializeGL as such: shaderProgram在我的QGLWidget子类中声明,我称之为GLWidget并在initializeGLinitializeGL如下:

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编辑 2

Here is my initializeGL() method:这是我的 initializeGL() 方法:

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.在调用上面的 link() 之后立即不会产生任何输出。

Was seeing this just now.刚刚看到这个。 Was strange because link() was returning true, yet it claimed the program was not linked?很奇怪,因为link()返回 true,但它声称程序没有链接?

Turned out I needed to move two bindAttributeLocation() statements to before my link() call.结果我需要在我的link()调用之前移动两个bindAttributeLocation()语句。 Apparently if you use bindAttributeLocation() on a program after it's linked, it will cause the program to no longer be linked.显然,如果在程序链接后使用bindAttributeLocation() ,它将导致程序不再链接。

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 // 在类构造函数中初始化了 shaderProgram

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.您应该覆盖 QGLWidget 的 initializeGL 成员并在那里对 OpenGL 对象进行初始化。 You cannot do this any earlier as your OpenGL context has not been established, and thus you cannot create or manage any OpenGL objects.由于尚未建立 OpenGL 上下文,因此您无法更早地执行此操作,因此您无法创建或管理任何 OpenGL 对象。

I found that problem too, I just used FromSourceCode instead from FromSourceFile like this:我也发现了这个问题,我只是使用FromSourceCode而不是FromSourceFile像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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