C GLSL链接错误

我正在开发一个3D引擎,我在使用C语言的GLSL着色器时遇到了一些问题.

顶点着色器:

layout(location = 0) in vec3 vertexUP;
layout(location = 1) in vec3 vertexUV;

out vec3 UP;
out vec3 UV;

void main(){
    UP = vertexUP;//Without this line, everything works fine
    UV = vertexUV;

    gl_Position = ftransform();
 }

片段着色器:

in vec3 UP;
in vec3 UV;

out vec3 color;

uniform sampler2D rs0;
uniform sampler2D rs1;
uniform sampler2D rs2;

void main(){
    if(UV[2]==0){//Selects between different textures
        color = texture( rs0, vec2(UV[0], UV[1]) ).rgb;
    }else if(UV[2]==1){
        color = texture( rs1, vec2(UV[0], UV[1]) ).rgb;
    }else{
        color = texture( rs2, vec2(UV[0], UV[1]) ).rgb;
    }
    if(UP[0]<2){//Test thing
        color[0] = 1.0;
    }
}

我想将位置数组(位置0)解析为fragement着色器,以便我可以将它用于照明.它编译但链接顶点着色器会出错:
顶点着色器无法链接,片段着色器链接.

我究竟做错了什么?

编辑:
加载着色器的代码:

GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){
    // Create the shaders
    GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

    // Read the Vertex Shader code from the file
    std::string VertexShaderCode;
    std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
    if(VertexShaderStream.is_open())
    {
        std::string Line = "";
        while(getline(VertexShaderStream, Line))
            VertexShaderCode += "\n" + Line;
        VertexShaderStream.close();
    }

    // Read the Fragment Shader code from the file
    std::string FragmentShaderCode;
    std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
    if(FragmentShaderStream.is_open()){
        std::string Line = "";
        while(getline(FragmentShaderStream, Line))
            FragmentShaderCode += "\n" + Line;
        FragmentShaderStream.close();
    }

    GLint Result = GL_FALSE;
    int InfoLogLength;

    // Compile Vertex Shader
    printf("Compiling shader : %s\n", vertex_file_path);
    char const * VertexSourcePointer = VertexShaderCode.c_str();
    glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
    glCompileShader(VertexShaderID);

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

    // Compile Fragment Shader
    printf("Compiling shader : %s\n", fragment_file_path);
    char const * FragmentSourcePointer = FragmentShaderCode.c_str();
    glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
    glCompileShader(FragmentShaderID);

    // Check Fragment Shader
    glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
    glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);

    // Link the program
    fprintf(stdout, "Linking program\n");
    GLuint ProgramID = glCreateProgram();
    glAttachShader(ProgramID, VertexShaderID);
    glAttachShader(ProgramID, FragmentShaderID);
    glLinkProgram(ProgramID);

    // Check the program
    glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
    glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> ProgramErrorMessage(InfoLogLength);
    glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
    fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);

    glDeleteShader(VertexShaderID);
    glDeleteShader(FragmentShaderID);

    return ProgramID;
}

最佳答案 你不能这样做:

layout(location = 0) in vec3 vertexUP;
layout(location = 1) in vec3 vertexUV;
...
gl_Position = ftransform();

ftransform()使用内置的gl_Vertex,GL规范要求对属性索引0进行别名,因此GL不能在没有冲突的情况下分配属性索引.

你真的不应该将旧的,已弃用的内置组件与通用属性混合使用.如果你这样做,你应该带领GL分配属性位置.

点赞