OpenGL - 获取着色器进行编译的问题

时间:2012-07-23 03:48:18

标签: c++ opengl shader

最后孤立了我的着色器无法用于编译失败的问题。

这是我的着色器加载例程。第一部分在着色器中读取:

void GLSLShader::LoadFromFile(GLenum whichShader, const string filename)
{   
    ifstream fp;

    // Attempt to open the shader
    fp.open(filename.c_str(), ios_base::in);

    // If the file exists, load it
    if(fp) 
    {
        // Copy the shader into the buffer
        string buffer(std::istreambuf_iterator<char>(fp), (std::istreambuf_iterator<char>()));

        // Debug output to show full text of shader
        errorLog.writeSuccess("Shader debug: %s", buffer.c_str());

        LoadFromString(whichShader, buffer);
    } 
    else 
    {
        errorLog.writeError("Could not load the shader %s", filename.c_str());
    }
}

将它加载到字符串后,它会将其发送到要加载:

void GLSLShader::LoadFromString(GLenum type, const string source) 
{
    // Create the shader
    GLuint shader = glCreateShader(type);

    // Convert the string
    const char * ptmp = source.c_str();
    glShaderSource(shader, 1, &ptmp, NULL);

    // Compile the shader
    glCompileShader(shader);

    // Check to see if the shader has loaded
    GLint status;
    glGetShaderiv (shader, GL_COMPILE_STATUS, &status);
    if (status == GL_FALSE) {
        GLint infoLogLength;
        glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
        GLchar *infoLog= new GLchar[infoLogLength];
        glGetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
        errorLog.writeError("could not compile: %s", infoLog);
        delete [] infoLog;
    }
    _shaders[_totalShaders++]=shader;
}

我得到调试输出“无法编译:”,似乎错误缓冲区为空。这让我在过去的3天里疯狂了。有人看到我的错误吗?

以下是非常简单的着色器:

#version 330

layout (location = 0) in vec4 position;
layout (location = 1) in vec4 color;

smooth out vec4 theColor;

void main()
{
    gl_Position = position;
    theColor = color;
}



#version 330

smooth in vec4 theColor;

out vec4 outputColor;

void main()
{
    outputColor = theColor;
}

更新

看起来由于某种原因,在内存中着色器的末尾会添加垃圾。我不知道为什么。我已经尝试将其读入char *和字符串。这是输出:

<-!-> Shader: #version 330

layout (location = 0) in vec4 position;
layout (location = 1) in vec4 color;

smooth out vec4 theColor;

void main()
{
    gl_Position = position;
    theColor = color;
}
n
<-!-> Shader: #version 330

smooth in vec4 theColor;

out vec4 outputColor;

void main()
{
    outputColor = theColor;
}
nts/Resources

注意第一个着色器末尾的'n'和第二个着色器末尾的'nts / Resources'。知道为什么吗?

另一个更新

最后的废话是由最后一条额外的线引起的。我删除了它,然后返回输出正确的着色器文本。编译仍然没有运气。

我在这里不知所措。 Superbible拥有广泛的库,未经优化,我不需要。似乎没有适合Mac的该死的正确着色器编译器。我真的不介意它有多简单。它只需要与着色器一起使用。有人有例子吗?

1 个答案:

答案 0 :(得分:2)

您正在调用glShaderSource而不提供sources数组中每个字符串的长度。因此,假设提供的字符串由空字节('\ 0')终止。将文件读入内存的方式不会使用额外的空字节终止着色器源。因此,GLSL编译器将超越着色器源的末尾读入随机存储器,在那里找到......垃圾。

解决方案:添加终止空字节,或提供着色器文本长度参数。

相关问题