【问题标题】:GLSL Shader Program Randomly Fails to CompileGLSL Shader 程序随机编译失败
【发布时间】: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


【解决方案1】:

好的,在这里回答我自己。感谢 AndonM.Coleman 指出我应该检查错误日志。输出不清楚,但它引导我找到another SO question,它揭示了我在代码中的一个错误。显然,我在编译程序中的所有其他着色器时做的事情是正确的,并且在我为这个程序编写代码时不知何故忘记了一行。我应该做的是:

string passThroughVertShaderCodeS = passThroughVertShaderCode.toStdString();
const char *passThroughVertShaderCodeC = passThroughVertShaderCodeS.c_str();

简而言之,toStdString() 方法正在返回缓冲区的临时副本。由于我在该行之后被销毁的临时副本上调用c_str(),因此该指针变得无效。我不确定为什么它偶尔会成功。

【讨论】:

  • 我已经在问题的编辑中提到了它。请检查问题中的更新
猜你喜欢
  • 2019-04-19
  • 1970-01-01
  • 2010-09-12
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多