4

I am writing an OpenGL / C++ plugin using VBO and GLSL shaders. However, while I can compile the shaders successfully, and link them to the program, its status link check with glGetProgramiv(...,GL_LINK_STATUS,...) returns a "No definition of main in vertex shader" error.

The vertex shader is sending a color to the fragment shader. If I remove this color-passing, the link is successfully validated.

What's wrong in my code if I want to pass color to fragment shader ?

    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
Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89
  • `glValidateProgram` doesn't do what you think it does. You shouldn't call it. See http://stackoverflow.com/questions/39761456/why-does-glvalidateprogram-fail-when-no-vao-is-bound. What happens if you remove it? – Yakov Galka Dec 02 '16 at 18:14
  • If I remove it I get `ERROR: No definition of main in vertex shader ERROR: No definition of main in fragment shader` message when doing `glGetProgramiv` – Laurent Crivello Dec 02 '16 at 18:21

1 Answers1

5

You have semicolons outside the strings that should not be there:

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";
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • Wow, you're my hero. I can't tell you how many times I have checked these semi columns and didn't see these ones ! Thanks ! – Laurent Crivello Dec 02 '16 at 18:31
  • You're better to write shader sources in separate files, in raw-string literals, or use a compiler that warns about such cases (Clang does, "warning: expression result unused"). – Yakov Galka Dec 02 '16 at 18:35