title: Can't get ids assigned to an attribute in OpenGL
body: <p>I am trying to have OpenGL automatically assign an ID to a glsl-attribute, but it is failing.</p>
  
  <p>My main program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  #include &lt;GL/glew.h&gt;
  #include &lt;GL/glfw3.h&gt;
  
  #include "test.h"
  #include "shader_utils.h"
  
  static void error_callback(int error, const char* description) {
      std::cout &lt;&lt; description &lt;&lt; std::endl;
  }
  
  static void key_callback(GLFWwindow* window, int a, int b) {
      if (a == GLFW_KEY_ESCAPE &amp;&amp; b == GLFW_PRESS) {
          glfwSetWindowShouldClose(window, GL_TRUE);
      }
  }
  
  void test()
  {
      std::cout &lt;&lt; "Starting up" &lt;&lt; std::endl;
      init();
      run();
  }
  
  GLFWwindow *window;
  GLuint shaders;
  GLuint vertexBuffer;
  
  int init() {
      glfwSetErrorCallback(error_callback);
  
      if(!glfwInit()) {
          return -1;
      }
  
      window = glfwCreateWindow(640, 480, "GLFW test", NULL, NULL);
      if (!window)  {
          glfwTerminate();
          return -1;
      }
      glfwMakeContextCurrent(window);
      glfwSetKeyCallback(window, key_callback);
  
      glewExperimental = true;
      glewInit();
  
      shaders = LoadShaders("vertex.glsl", "fragment.glsl");
  
      GLuint vao_id;
      glGenVertexArrays(1, &amp;vao_id);
      glBindVertexArray(vao_id);
  
  
      static const GLfloat vertex_data[] = {
          // Bottom
          -.5f, -.5f, -.5f,   1.f, 0.f, 0.f,
          -.5f, -.5f,  .5f,   1.f, 0.f, 0.f,
           .5f, -.5f,  .5f,   1.f, 0.f, 0.f,
      };
  
      glGenBuffers(1, &amp;vertexBuffer);
      glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW);
  }
  
  void checkE() {
      std::cout &lt;&lt; "Checking for errors: ";
      int err;
      int a = 0;
      while((err = glGetError()) != GL_NO_ERROR) {
          std::cout &lt;&lt; "Error: " &lt;&lt; err &lt;&lt; std::endl;
          a = 1;
      }
  
      if(a == 0) {
          std::cout &lt;&lt; "no errors" &lt;&lt; std::endl;
      }
      std::cout.flush();
  }
  
  int run() {
      GLfloat angle = 0;
  
      while(!glfwWindowShouldClose(window)) {
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
          glUseProgram(shaders);
  
          glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  
          GLuint attrLocation = glGetAttribLocation(shaders, "location");
          GLuint attrColor = glGetAttribLocation(shaders, "color");
  
          std::cout &lt;&lt; "AttribLocation('location'): ";
          std::cout &lt;&lt; glGetAttribLocation(shaders, "location") &lt;&lt; std::endl;
          std::cout &lt;&lt; "AttribLocation('color'): ";
          std::cout &lt;&lt; glGetAttribLocation(shaders, "color") &lt;&lt; std::endl;
  
          checkE();
          std::cout &lt;&lt; std::endl;
  
          std::cout &lt;&lt; "glEnableVertexAttribArray()" &lt;&lt; std::endl;
          glEnableVertexAttribArray(attrLocation);
          glEnableVertexAttribArray(attrColor);
  
          checkE();
          std::cout &lt;&lt; std::endl;
  
          std::cout &lt;&lt; "glVertexAttribPointer();" &lt;&lt; std::endl;
  
          glVertexAttribPointer(
                          glGetAttribLocation(shaders, "location"), // Attribute
                          3, // Size
                          GL_FLOAT, // Size
                          GL_FALSE, // Normalized
                          24, // Stride
                          (GLvoid*) 0 // Offset
                      );
  
          checkE();
          std::cout &lt;&lt; std::endl;
  
  
          std::cout &lt;&lt; "glVertexAttribPointer();" &lt;&lt; std::endl;
          glVertexAttribPointer(
                          glGetAttribLocation(shaders, "color"),
                          3,
                          GL_FLOAT,
                          GL_FALSE,
                          24,
                          (GLvoid*) (3*sizeof(GLfloat))
                      );
          checkE();
          std::cout &lt;&lt; std::endl;
  
          glDrawArrays(GL_TRIANGLES, 0, 3);
  
          checkE();
          std::cout &lt;&lt; std::endl;
  
          glDisableVertexAttribArray(attrLocation);
          glDisableVertexAttribArray(attrColor);
  
          checkE();
          std::cout &lt;&lt; std::endl;
  
          glfwSwapBuffers(window);
          glfwPollEvents();
  
          glfwSetWindowShouldClose(window, GL_TRUE);
      }
  
      glfwDestroyWindow(window);
      glfwTerminate();
  }
  </code></pre>
  
  <p>Output from program:</p>
  
  <pre><code>Starting up
  Compiling shader : vertex.glsl
  
  Compiling shader : fragment.glsl
  
  Linking program
  
  AttribLocation('location'): -1
  AttribLocation('color'): -1
  Checking for errors: no errors
  
  glEnableVertexAttribArray()
  Checking for errors: Error: 1281
  
  glVertexAttribPointer();
  Checking for errors: Error: 1281
  
  glVertexAttribPointer();
  Checking for errors: Error: 1281
  
  Checking for errors: no errors
  
  Checking for errors: Error: 1281
  </code></pre>
  
  <p>Shader loader:</p>
  
  <pre><code>#include "shader_utils.h"
  
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  
  #include "GL/glew.h"
  #include "GL/glfw3.h"
  
  
  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();
      } else {
          std::cout &lt;&lt; "could not open\n";
      }
  
      // 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, &amp;VertexSourcePointer , NULL);
      glCompileShader(VertexShaderID);
  
      // Check Vertex Shader
      glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &amp;Result);
      glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &amp;InfoLogLength);
      std::vector&lt;char&gt; VertexShaderErrorMessage(InfoLogLength);
      glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &amp;VertexShaderErrorMessage[0]);
      fprintf(stdout, "%s\n", &amp;VertexShaderErrorMessage[0]);
  
      // Compile Fragment Shader
      printf("Compiling shader : %s\n", fragment_file_path);
      char const * FragmentSourcePointer = FragmentShaderCode.c_str();
      glShaderSource(FragmentShaderID, 1, &amp;FragmentSourcePointer , NULL);
      glCompileShader(FragmentShaderID);
  
      // Check Fragment Shader
      glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &amp;Result);
      glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &amp;InfoLogLength);
      std::vector&lt;char&gt; FragmentShaderErrorMessage(InfoLogLength);
      glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &amp;FragmentShaderErrorMessage[0]);
      fprintf(stdout, "%s\n", &amp;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, &amp;Result);
      glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &amp;InfoLogLength);
      std::vector&lt;char&gt; ProgramErrorMessage( std::max(InfoLogLength, int(1)) );
      glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &amp;ProgramErrorMessage[0]);
      fprintf(stdout, "%s\n", &amp;ProgramErrorMessage[0]);
  
      glDeleteShader(VertexShaderID);
      glDeleteShader(FragmentShaderID);
  
      std::cout.flush();
  
      return ProgramID;
  }
  </code></pre>
  
  <p>Vertex shader:</p>
  
  <pre><code>#version 130
  
  in vec4 position;
  in vec3 color;
  
  out vec3 f_color;
  
  void main() {
      f_color = color;
  
      gl_Position = position * gl_ModelViewProjectionMatrix;
  }
  </code></pre>
  
  <p>Fragment shader:</p>
  
  <pre><code>#version 130
  
  in vec3 color;
  
  void main() {
      gl_FragColor = vec4(color, 1);
  }
  </code></pre>
  
  <p>For some reason I get just <code>-1</code> as the location for both the attributes. Obviously the rest of the errors are caused by invalid location indices?</p>
  
  <p>From OpenGL docs:<br>
  <code>If the named attribute variable is not an active attribute in the specified program object or if name starts with the reserved prefix "gl_", a value of -1 is returned.</code></p>
  
  <p>The names do not begin with <code>gl_</code> and they are in use in the shaders, so I shouldn't get a value of <code>-1</code>. What am I missing?</p>