简体   繁体   English

C ++ GLSL链接错误

[英]C++ GLSL Linking error

I'm developing a 3d Engine and I got some trouble with the GLSL shader in C++. 我正在开发一个3D引擎,我在C ++中使用GLSL着色器遇到了一些麻烦。

Vertex shader: 顶点着色器:

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

Fragment shader: 片段着色器:

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

I want to parse the position array (Location 0) to the fragement shader so I can use it for lighting. 我想将位置数组(位置0)解析为fragement着色器,以便我可以将它用于照明。 It Compiles but linking the vertex shader gives a error: Vertex shader(s) failed to link, Fragment shader(s) linked. 它编译但链接顶点着色器会产生错误:顶点着色器无法链接,片段着色器链接。

What am I doing wrong? 我究竟做错了什么?

EDIT: The code to load the shaders: 编辑:加载着色器的代码:

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

调试

You can't do that: 你不能这样做:

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

ftransform() uses the builtin gl_Vertex , and the GL spec requires that to alias attribute index 0, so the GL cannot assign the attribute indices without collision. ftransform()使用内置gl_Vertex和GL规范要求,以别名属性索引0,所以GL离不开碰撞分配属性索引。

You really shouldn't mix the old, deprecated builtins with generic attributes. 你真的不应该将旧的,已弃用的内置组件与通用属性混合使用。 And if you do, you should led the GL assign the attribute locations. 如果你这样做,你应该带领GL分配属性位置。

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

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