我已经从http://releases.qt-project.org/learning/developerguides/qtopengltutorial/OpenGLTutorial.pdf下载了 QT OpenGL 教程,并尝试运行其中最简单的示例,它只是在顶部绘制一个三角形黑色背景。
我设法编译了这个例子,但是当我运行它时,我只得到一个黑色窗口,控制台报告以下内容:
开始 /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 ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::uniformLocation( mvpMatrix ): 着色器程序未链接 QGLShaderProgram::uniformLocation( color ): 着色器程序未链接 QGLShaderProgram::attributeLocation ( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接着色器程序未链接着色器程序未链接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 ):未链接着色器程序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 ):未链接着色器程序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 ):着色器程序未链接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::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::uniformLocation( mvpMatrix ): 着色器程序未链接 QGLShaderProgram::uniformLocation( color ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 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::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接 QGLShaderProgram::attributeLocation( vertex ): 着色器程序未链接
.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
}
“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:
并且进行实际渲染的代码部分是
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();
}
另外,作为一个附带问题,为什么“glxinfo | grep -i opengl”的输出包含该行
OpenGL 着色语言版本字符串:(null)
(对我来说似乎有点相关......是吗?)
编辑 1
是在我的shaderProgram
子类中声明的,QGLWidget
我这样调用GLWidget
并初始化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);
}
编辑 2
这是我的 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);
}
并把
qDebug() << shaderProgram.log();
exit(0);
在上面调用 link() 之后立即不会产生任何输出。