着色器中的编译错误

时间:2016-02-01 07:34:31

标签: c++ opengl glsl shader

您好我正在使用着色器编写程序并在编译着色器时出现错误。以下是我的着色器字符串

const GLchar* vertexShaderCode =                               \
    "#verison 140                                      \n" \
    "in vec4 a_Position;                               \n" \
    "uniform   mat4 u_MVPMatrix;                       \n" \
    "void main()                                       \n" \
    "{                                                 \n" \
    "    gl_Position = u_MVPMatrix * a_Position;       \n" \
    "}                                                   ";\

const GLchar* fragmentShaderCode =                             \
    "#verison 140                                      \n" \
    "uniform vec4 u_Color;                             \n" \
    "out vec4 fragmentColor;                           \n" \
    "void main()                                       \n" \
    " {                                                \n" \
    "  fragmentColor = u_Color;                        \n" \
    " }      

以下是加载初始化着色器对象的代码

GLuint MtShader::LoadShader(int type, const GLchar* shaderCode)
{
    GLuint shader = glCreateShader(type);

    // Add the source code to the shader and compile it
    glShaderSource(shader, 1, &shaderCode, NULL);
    glCompileShader(shader);

    GLint  compiled;
    glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
    if ( !compiled ) {
        std::cerr << " failed to compile:" << std::endl;
        GLint  logSize;
        glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize );
        char* logMsg = new char[logSize];
        glGetShaderInfoLog( shader, logSize, NULL, logMsg );
        std::cerr << logMsg << std::endl;
        delete [] logMsg;

        exit( EXIT_FAILURE );
    }

    return shader;
}


void MtShader::CompileShader(const GLchar* vertexShaderCode, const GLchar* fragmentShaderCode)
{
    // Load the shaders.
    int vertexShader      = LoadShader(GL_VERTEX_SHADER,   vertexShaderCode);
    int fragmentShader    = LoadShader(GL_FRAGMENT_SHADER, fragmentShaderCode);

    // Create the program.
    m_Program = glCreateProgram();             // Create empty shader Program.
    glAttachShader(m_Program, vertexShader);   // Add the vertex shader to program.
    glAttachShader(m_Program, fragmentShader); // Add the fragment shader to program.
    glLinkProgram (m_Program);                 // links the shader program.

    GLint  linked;
    glGetProgramiv( m_Program, GL_LINK_STATUS, &linked );
    if ( !linked ) {
        std::cerr << "Shader program failed to link" << std::endl;
        GLint  logSize;
        glGetProgramiv( m_Program, GL_INFO_LOG_LENGTH, &logSize);
        char* logMsg = new char[logSize];
        glGetProgramInfoLog( m_Program, logSize, NULL, logMsg );
        std::cerr << logMsg << std::endl;
        delete [] logMsg;

        exit( EXIT_FAILURE );
    }

    glUseProgram(m_Program);
}

编译此着色器时,它会成功编译。我使用glGetShaderInfoLog检查着色器日志。它出现以下错误

ERROR: 0:1: '' :   Invalid Directive verison
ERROR: 0:1: '' : syntax error: unexpected tokens following preprocessor directive - expected a newline
ERROR: 0:6: 'u_MVPMatrix' : undeclared identifier 
ERROR: 0:6: 'a_Position' : undeclared identifier 
ERROR: 0:6: 'assign' :  cannot convert from 'float' to 'Position 4-component vector of float'

PS。我正在创建opengl 3.1上下文。任何帮助?

0 个答案:

没有答案