繁体   English   中英

顶点着色器和片段着色器在调用 gllinkProgram() 之前编译失败,

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

顶点着色器和片段着色器都是小苍蝇(文件),用于出现在蓝色背景上的小红色框。

他们来了:

第一个是 colorShading.vert

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

第二个是 colorShading.frag

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

这是错误:

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

我使用控制台输出它使用

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

我用来编译着色器的函数如下

而且我把filepath和shaderID传给函数,而不是复制粘贴,方便吧?

    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;
    }
 }

我该如何解决我遇到的这个错误?

您的代码中至少有两个问题:

编译码

检查编译成功的if语句包含一个赋值,而不是一个比较:

if (success = GL_FALSE) {

你真正需要的是(不是两个等号):

if (success == GL_FALSE) {

我认识的每个编译器至少会对此发出警告。

着色器

在片段着色器中,还缺少一个 = 符号:

color vec3(1.0, 0.0, 0.0);

这不是有效的 glsl 代码。 很可能你想分配这种颜色,因此代码应该是

color = vec3(1.0, 0.0, 0.0);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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