Search code examples
c++opengl-esglslglsles

GLSL shaders compiling, but program not linking


I've been not into OpenGL for a long time, and never used OpenGLES at all. For my project I must use OpenGLES, the version printed by glGetString(GL_VERSION) is 3.0, but since I'm doing a collage of tutorials is perfectly possible that I'm mixing code which can't work on this version (that is why the #version tag is commented on the shaders code, but it did not fix the problem anyway).

This is the code of the vertex shader:

        const GLchar * vertex_shader_source =
//                "#version 100\n                                                             "
//                "                                                                           "
                " attribute vec2 a_TexCoordinate; /* Per-vertex texture coordinate */       "
                "                                 /* information we will pass in.  */       "
                " attribute vec2 coord2d;                                                   "
                "                                                                           "
                "                                                                           "
                " varying vec2 v_TexCoordinate;   // This will be passed into the fragment  "
                "                                                                           "
                " void main(){                                                              "
                "                                                                           "
                "       gl_Position = vec4(coord2d, 0.0, 1.0);                              "
                "                                                                           "
                "     // Pass through the texture coordinate.                               "
                "        v_TexCoordinate = a_TexCoordinate;                                 "
                " }                                                                         ";

        glShaderSource(vertex_shader_index, 1, &vertex_shader_source, nullptr);
        glCompileShader(vertex_shader_index);
        glGetShaderiv(vertex_shader_index, GL_COMPILE_STATUS, &compile_ok);

And this is the code of the fragment shader:

        const GLchar * fragment_shader_source =
//                "#version 100\n                                                     "
//                "                                                                   "
                "uniform sampler2D u_Texture;    // The input texture.              "
                "                                                                   "
                "varying vec2 v_TexCoordinate; /* Interpolated texture    */        "
                "                              /* coordinate per fragment */        "
                "                                                                   "
                " void main(){                                                      "
                "                                                                   "
                "     // Output color = color of the texture at the specified UV    "
                "     gl_FragColor = texture2D(u_Texture, v_TexCoordinate);         "
                " }                                                                 ";

        glShaderSource(fragment_shader_index, 1, &fragment_shader_source, nullptr);
        glCompileShader(fragment_shader_index);
        glGetShaderiv(fragment_shader_index, GL_COMPILE_STATUS, &compile_ok);

This is the code of the program:

m_program = glCreateProgram( );
        glAttachShader(m_program, vertex_shader_index);
        glAttachShader(m_program, fragment_shader_index);
        glLinkProgram(m_program);
        glGetProgramiv(m_program, GL_LINK_STATUS, &link_ok);

The variable link_ok is false, while the variables compile_ok are both true. The extended error from the linking of the program says:

(0) Vertex info: C3001 no program defined (0) Fragment info: C3001 no program defined


Solution

  • Your individual shaders compile just fine. The specific error code you are getting means that no main functions could be found in the complete program. And for good reason - you commented them. Your shader strings do not contain any new lines, so the entire shader is on one line.

    For example:

    const GLchar * var = "uniform value; // a value"
                         "main () {}               ";
    

    Is actually

    const GLchar * var = "uniform value; // a valuemain() {}";
    

    Any commenting done with // comments out the rest of the line - hence the rest of your shader. Removing the // comments, replacing them with /**/ or adding \n at the end of each line and it worked fine for me.

    Also, it might be safe to null-terminate your strings with \0.