OpenGL着色器链接错误,没有错误消息

时间:2017-02-26 15:49:40

标签: c++ opengl glfw glew

我一直试图将这个问题弄清楚很长时间,但无法让事情发挥作用。我只想编译/运行我的OpenGL程序,但我的着色器没有正确链接,我没有得到任何有用的错误消息。在一种情况下,错误纯粹是空的,而在另一种情况下,由于某种原因它是??y?

要创建我的程序并编译着色器,我有以下代码:

GLuint buildProgram(const char* vertexShaderFile, const char* fragmentShaderFile)
{
    std::string vs = fileContents(vertexShaderFile);
    std::string fs = fileContents(fragmentShaderFile);
    return createProgram(vs.c_str(), fs.c_str());
}

着色器看起来像

#version 330
out vec4 out_color; 
void main()
{
    out_color = vec4(0.5, 0.5, 0.5, 0.5);
}

#version 330
in vec4 in_Position;
void main()
{
    gl_Position = in_Position;
}

使用

GLuint createProgram(const char* vertexSource, const char* fragmentSource)
{
    GLuint vertexshader = createShader(vertexSource, GL_VERTEX_SHADER);
    GLuint fragmentshader = createShader(fragmentSource, GL_FRAGMENT_SHADER);
    GLuint program = glCreateProgram();

    glAttachShader(program, vertexshader);
    glAttachShader(program, fragmentshader);

    glLinkProgram(program);
    GLint ret;
    checkShader(vertexshader, GL_LINK_STATUS, &ret, "unable to link vertex shader");
    checkShader(fragmentshader, GL_LINK_STATUS, &ret, "unable to link fragment shader");

    glUseProgram(program);
    return program;
}

GLuint createShader(const char* source, GLenum shaderType)
{
    GLuint shader = glCreateShader(shaderType);
    glShaderSource(shader, 1, &source, NULL);
    glCompileShader(shader);
    GLint isCompiled = 0;
    checkShader(shader, GL_COMPILE_STATUS, &isCompiled, "shader failed to compile");
    if (isCompiled == GL_FALSE)
    {
        glDeleteShader(shader);
        return 0;
    }
    return shader;
}

我尝试通过

从着色器生成错误消息
void checkShader(GLuint shader, GLuint type, GLint* ret, const char* onfail)
{
    switch(type) {
        case(GL_COMPILE_STATUS):
        glGetShaderiv(shader, type, ret);
        if(*ret == false) {
            int infologLength = 0;
            glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infologLength);
            GLchar buffer[infologLength];
            GLsizei charsWritten = 0;
            std::cout << onfail << std::endl;
            glGetShaderInfoLog(shader, infologLength, &charsWritten, buffer);
            std::cout << buffer << std::endl;
        }
        break;
    case(GL_LINK_STATUS):
        glGetProgramiv(shader, type, ret);
        if(*ret == false) {
            int infologLength =  0;
            glGetProgramiv(shader, GL_INFO_LOG_LENGTH, &infologLength);
            GLchar buffer[infologLength];
            GLsizei charsWritten = 0;
            std::cout << onfail << std::endl;
            glGetProgramInfoLog(shader, infologLength, &charsWritten, buffer);
            std::cout << buffer << std::endl;
        }
        break;
    default:
        break;
    };
}

运行GLuint program = buildProgram("shaders/test.vert", "shaders/test.frag");会给我以下输出:

  

无法链接顶点着色器

     

无法链接片段着色器

     

,θY

     

错误构建程序      错误502:GL_INVALID_OPERATION

有没有人猜到为什么我的着色器编译得很好但没有链接?

1 个答案:

答案 0 :(得分:5)

您正在着色器 对象上调用getProgramiv(...,GL_LINK_STATUS,...)。当你试图获取有关程序的信息时,它甚至没有任何意义甚至调用checkShader函数两次 - 每个着色器对象一次 - 其中只有一)。对GL_LINK_STATUS案例而言,以这种方式命名函数也没有意义。