【发布时间】:2013-12-23 02:49:00
【问题描述】:
我在我的 OpenGL 应用程序中遇到了一个奇怪的行为。我在程序的初始化过程中生成了一些 GLSL 程序。着色器程序从文本文件中读取,程序被编译和链接。但是,我随机遇到其中一个着色器程序(直通顶点着色器)的编译错误。我不明白为什么程序加载得非常好,着色器程序成功编译了几次,但在其他时候却失败了!
这是着色器代码:
#version 330
// vertex position in the model space
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec2 inTexCoord;
// will be interporlated for each fragment
smooth out vec2 vTexCoord;
// uniforms
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
void main(void) {
gl_Position = projectionMatrix * modelViewMatrix * vec4(inPosition, 1.0);
vTexCoord = inTexCoord;
}
这里是编译着色器的代码(passThroughVertShader是一个QString):
this->passThroughVertShader = glCreateShader(GL_VERTEX_SHADER);
const char *passThroughVertShaderCodeC = passThroughVertShaderCode.toStdString().c_str();
sourceCode = passThroughVertShaderCodeC;
glShaderSource(this->passThroughVertShader, 1, &sourceCode, NULL);
glCompileShader(this->passThroughVertShader);
glGetShaderiv(this->passThroughVertShader, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE)
{
qDebug("ERROR compiling pass-through vertex shader..");
exit(-1);
}
glAttachShader(this->shaderProgram, this->passThroughVertShader);
以及加载它的函数:
QString MyClass::readShaderFile(const QString &filename) {
QString content;
QFile file(filename);
if (file.open(QIODevice::ReadOnly)) {
QTextStream tStream(&file);
content = tStream.readAll();
}
return content;
}
更新:
按照安东的建议,我仔细检查了失败后没有检查日志。日志是这样写的:
Error: 0(17) : error C0000: syntax error, unexpected '!', expecting "::" at token "!"
【问题讨论】:
-
您知道什么是真正有用的吗?您的随机错误的实际错误日志:P
-
@AndonM.Coleman 这就是重点。日志没有显示任何内容!
-
实际上,让我仔细检查一下。
标签: c++ qt opengl glsl vertex-shader