简体   繁体   中英

Vertex Shader and Fragment Shader(s) failed to compile before gllinkProgram() was called,

Both the vertex shader and fragment shader are small flies ( files ) for a small red box to appear on a blue background.

Here they are:

1st one is colorShading.vert

#version 130
in vec2 vertexPosition;
void main(){
gl_Positiion.xy = vertexPosition;
gl_Positiion.z = 0;
gl_Positiion.w = 1;
}

2nd One is colorShading.frag

#version 130
out vec3 color;
void main(){
color vec3(1.0, 0.0, 0.0);
}

and Here is the error:

Vertex and Fragment shader(s) were not successfully compiled 
before glLinkProgram() was called.  Link failed.
Shaders failed to link!
Enter any key to quit...

I used a console to output it using

std::vector<GLchar> errorLog(maxLength);
glGetProgramInfoLog(_programID, maxLength, &maxLength, &errorLog[0]);
std::printf("%s\n", &(errorLog[0]));
fatalError("Shaders failed to link!");`

The function I used to compile the Shaders is below

and I pass the filepath and shaderID to the function, rather than copying and pasting, Convenient right?

    void GLSLProgram::compileShader(const std::string& filePath, GLuint& id)
{
    std::ifstream shaderFile(filePath);
    if (shaderFile.fail()) {
        perror(filePath.c_str());
        fatalError("Failed to Open  " + filePath);
    }
    std::string fileContents = "";
    std::string line;
    while (std::getline(shaderFile, line)) {
        fileContents += line + "\n";
    }
    shaderFile.close();
    const char* contentsPtr = fileContents.c_str();
    glShaderSource(id, 1, &contentsPtr, nullptr);
    glCompileShader(id);
    GLint success = 0;
    glGetShaderiv(id, GL_COMPILE_STATUS, &success);
    if (success = GL_FALSE) {
        GLint maxLength = 0;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &maxLength);
        //The maxLength include the NULL character
        std::vector<char> errorLog(maxLength);
        glGetShaderInfoLog(id, maxLength, &maxLength, &errorLog[0]);
        //Exit with failure
        glDeleteShader(id);
        std::printf("%s\n", &(errorLog[0]));
        fatalError(" Shader " +filePath +" failed to compile!");
        return;
    }
 }

How do I go about this error I am getting?

There are at least two problems in your code:

Compilation Code

The if statement that checks for successful compilation contains an assignment, not a comparison:

if (success = GL_FALSE) {

what you actually need is (not the two equal signs):

if (success == GL_FALSE) {

Every compiler I know would at least give a warning on this.

Shader

In the fragment shader, there is also a = sign missing:

color vec3(1.0, 0.0, 0.0);

This is not valid glsl code. Most probably you wanted to assign this color, thus the code should be

color = vec3(1.0, 0.0, 0.0);

您的第一个代码示例将gl_Position拼错为gl_Positiion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM