在OpenGL程序链接状态检查期间“没有定义顶点着色器中的主”

时间:2016-12-02 18:04:25

标签: c++ opengl glsl vbo

我正在使用VBO和GLSL着色器编写OpenGL / C ++插件。但是,虽然我可以成功编译着色器并将它们链接到程序,但是它与glGetProgramiv(...,GL_LINK_STATUS,...)的状态链接检查会返回“没有定义主顶点着色器”错误。

顶点着色器正在向片段着色器发送颜色。如果我删除此颜色传递,则链接已成功验证。

如果我想将颜色传递给片段着色器,我的代码有什么问题?

    const GLchar *vert_src =
    "#version 410\n"
    "layout (location = 0) in vec3 Pos;\n"
    "layout (location = 1) in vec3 vColor;\n"
    "uniform mat4 projectionMatrix;\n"
    "uniform mat4 modelViewMatrix;\n"
    "out vec3 color;\n";  // -> REMOVING THIS LINE MAKES THE VALIDATION WORK
    "void main() {\n"
    "   color=vColor;\n" // -> AND THIS LINE TOO 
    "   gl_Position = projectionMatrix*modelViewMatrix*vec4(Pos.x, Pos.y, Pos.z, 1.0);"
    "}\n";

    const GLchar *frag_src =
    "#version 410\n"
    "in vec3 color;\n";
    "out vec4 fragColor;\n"
    "void main() {\n"
     "   fragColor = vec4(color, 1.0);"
     "}\n";

    GLuint program = glCreateProgram();

    GLuint v_shader = glCreateShader(GL_VERTEX_SHADER);
    GLuint f_shader = glCreateShader(GL_FRAGMENT_SHADER);
    int len=(GLint)strlen(vert_src);
    glShaderSource(v_shader, 1, &vert_src, &len);
    glCompileShader(v_shader);

    glGetShaderiv(v_shader, GL_COMPILE_STATUS, &success); --> SUCCESS

    len=(GLint)strlen(frag_src);
    glShaderSource(f_shader, 1, &frag_src, &len);
    glCompileShader(f_shader);

    glGetShaderiv(f_shader, GL_COMPILE_STATUS, &success); --> SUCCESS

    glAttachShader(program, v_shader);  --> SUCCESS
    glAttachShader(program, f_shader);  --> SUCCESS

    glLinkProgram(program);   --> SUCCESS

    glValidateProgram(program);     --> SUCCESS
    glGetProgramiv(program, GL_LINK_STATUS, &success); --> ERROR: Program is not successfully linked
[...] rest of code

1 个答案:

答案 0 :(得分:3)

你在字符串之外的分号不应该存在:

const GLchar *vert_src =
    "#version 410\n"
    "layout (location = 0) in vec3 Pos;\n"
    "layout (location = 1) in vec3 vColor;\n"
    "uniform mat4 projectionMatrix;\n"
    "uniform mat4 modelViewMatrix;\n"
    "out vec3 color;\n";  // <- HERE YOU HAVE A SEMICOLON
    "void main() {\n"
    "   color=vColor;\n"
    "   gl_Position = projectionMatrix*modelViewMatrix*vec4(Pos.x, Pos.y, Pos.z, 1.0);"
    "}\n";

const GLchar *frag_src =
    "#version 410\n"
    "in vec3 color;\n";   // <- HERE YOU HAVE ANOTHER SEMICOLON
    "out vec4 fragColor;\n"
    "void main() {\n"
     "   fragColor = vec4(color, 1.0);"
     "}\n";