着色器附件错误。无效的程序或着色器

时间:2014-11-22 10:30:10

标签: c++ oop opengl glsl

我正在处理我的图形程序。我试图将着色器中的特征抽象为一个类。我测试了常规绘图代码,它的工作原理。着色器也可以运行,但是一旦我开始使用这些OpenGL着色器功能,它就会停止工作。

Shader::Shader(char c)
{
    //program = int(0);
    if (program!=0 || glIsProgram(program))
        glDeleteProgram(program);
    uniforms.clear();

    program = glCreateProgram();
}

void Shader::compileProgram()
{
    GLint Result = GL_FALSE;
    int InfoLogLength;

    glLinkProgram(program);

    glValidateProgram( program);

    glGetProgramiv(program, GL_LINK_STATUS, &Result);
    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::string ProgramErrorMessage = std::string();
    ProgramErrorMessage.resize( glm::max(InfoLogLength, int(1)));
    glGetProgramInfoLog(program, InfoLogLength, NULL, &ProgramErrorMessage[0]);
    std::cout << ProgramErrorMessage.c_str() << std::endl;
}

void Shader::addToProgram( std::string File, GLenum type)
{
    std::cout << glGetError() << std::endl;

    GLuint shader = glCreateShader(type);

    std::cout << glewGetErrorString(glGetError()) << std::endl;

    std::string ShaderCode;
    std::ifstream ShaderStream(File, std::ios::in);
    if(ShaderStream.is_open())
    {
        std::string Line = "";
        while(getline(ShaderStream, Line))
            ShaderCode += Line + "\n";
        ShaderStream.close();
    }

    GLint Result = GL_FALSE;
    int InfoLogLength;

    // Compile Vertex Shader
    std::cout << "Compiling shader : " <<  File<< std::endl;
    const char * SourcePointer = ShaderCode.c_str();
    std::cout << SourcePointer << std::endl;
    int length = ShaderCode.size();
    glShaderSource(shader, 1, &SourcePointer , &length );

    glCompileShader(shader);

    // Check Vertex Shader
    glGetShaderiv(shader, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> ShaderErrorMessage(InfoLogLength);
    glGetShaderInfoLog(shader, InfoLogLength, NULL, &ShaderErrorMessage[0]);
    printf("%s\n", &ShaderErrorMessage[0]);

    glAttachShader(program, shader);  //Invalid Operation, eighter Program or shader invalid

    std::cout << glGetError() << std::endl;

}

1 个答案:

答案 0 :(得分:0)

所以问题是程序无效。

我做的是:

if(!glIsProgram(program))
    program = glCreateProgram();

glAttachShaders(program, shader);

我在构造函数中做了同样的守护。我仍然想知道这是怎么发生的。我的意思是我检查了Debug中的值,它们似乎都是有效的。