title: Setting attributes in the vertex shader in openGL
body: <p>I have been learning OpenGL recently and I have got bit confused about shaders and the VBOs.
  Basically, I am confused on how my vertex shader knows about the colour and position data in my VBOs.
  To my knowledge <code>glBindAttribLocation(program, attribute, variable);</code> is what sets the attribute in the shader but when I comment out that code, the triangle still renders fine with it's colours.</p>
  <p>Here is my code:</p>
  <pre><code>public static void main(String[] args) {
      String vertexSource = &quot;#version 400 core\n&quot;
              + &quot;in vec3 position;\n&quot;
              + &quot;in vec3 colour;\n&quot;
              + &quot;out vec3 vertexColour;\n&quot;
              + &quot;uniform mat4 model;\n&quot;
              + &quot;uniform mat4 view;\n&quot;
              + &quot;uniform mat4 projection;\n&quot;
              + &quot;void main() {\n&quot;
              + &quot;vertexColour = colour;\n&quot;
              + &quot;mat4 mvp = projection * view * model;\n&quot;
              + &quot;gl_Position = vec4(position, 1.0);\n&quot;
              + &quot;}\n&quot;;
      
      String fragmentSource = &quot;#version 400 core\n&quot;
              + &quot;in vec3 vertexColour;\n&quot;
              + &quot;out vec4 colours;\n&quot;
              + &quot;void main()\n&quot;
              + &quot;{\n&quot;
              + &quot;    colours = vec4(vertexColour, 1);\n&quot;
              + &quot;}&quot;;
      
      glfwSetErrorCallback(errorCallBack);
      
      if (!glfwInit()) {
          throw new IllegalStateException(&quot;Unable to initialize GLFW&quot;);
      }
          
      glfwDefaultWindowHints();
      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
      glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
      glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
      
      glfwWindowHint(GLFW_VISIBLE, GL_TRUE);
      glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
      
      long window = glfwCreateWindow(640, 480, &quot;my window!&quot;, 0, 0);
  
      glfwSetKeyCallback(window, keyCallback);
      if (window == 0) {
          glfwTerminate();
          throw new RuntimeException(&quot;Failed to create the GLFW window&quot;);
      }
      
      glfwMakeContextCurrent(window);
      GL.createCapabilities();
      
      float[] vertexPoints = {
          0f,0.5f,0f,
          -0.5f,-0.5f,0f,
          0.5f,-0.5f,0f
      };
      float[] colours = {
              0.1f,0.5f,0.5f,
              0.3f,0.7f,0.9f,
              0.4f,0.9f,0.1f
          };
      
      ArrayList&lt;Integer&gt; vaos = new ArrayList&lt;Integer&gt;();
      ArrayList&lt;Integer&gt; vbos = new ArrayList&lt;Integer&gt;();
      
      int vaoID = glGenVertexArrays();
      vaos.add(vaoID);
      glBindVertexArray(vaoID);
      
      
      FloatBuffer vertices = BufferUtils.createFloatBuffer(vertexPoints.length);
      vertices.put(vertexPoints);
      vertices.flip();
     
      int vboID = GL33.glGenBuffers();
      vbos.add(vboID);
      glBindBuffer(GL_ARRAY_BUFFER, vboID);
      glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
      
      //add it to the vao at position 0, size 3, with data types of float, normalised = false, 
      glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
      //unbind currently bound vbo
      glBindBuffer(GL_ARRAY_BUFFER, 0);
      
      //and one for the colour data
      vboID = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vboID);
      glBufferData(GL_ARRAY_BUFFER, colours, GL_STATIC_DRAW);
      glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
      
      //finished with vao now unbind it
      glBindVertexArray(0);
  
      //vao and vbo stuff is now finished with
      //now for the shader stuff
      
      int vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vertexShaderID, vertexSource);
      glCompileShader(vertexShaderID);
      
      if (!checkForSuccess(vertexShaderID)) {
          
          glfwTerminate();
          throw new IllegalStateException(&quot;vertex shader failed: &quot; + glGetShaderInfoLog(vertexShaderID));
      }
      
      int fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(fragmentShaderID, fragmentSource);
      glCompileShader(fragmentShaderID);
      
      if (!checkForSuccess(vertexShaderID)) {
          glGetShaderInfoLog(vertexShaderID);
          glfwTerminate();
          throw new IllegalStateException(&quot;fragment shader failed&quot;);
      }
      
      //shader program
      int shaderProgramID = glCreateProgram();
      glAttachShader(shaderProgramID, vertexShaderID);
      glAttachShader(shaderProgramID, fragmentShaderID);
      glLinkProgram(shaderProgramID);
      //error test
      int status = glGetProgrami(shaderProgramID, GL_LINK_STATUS);
      if (status != GL_TRUE) {
          glfwTerminate();
          throw new RuntimeException(glGetProgramInfoLog(shaderProgramID));
      }
      
      glUseProgram(shaderProgramID);
  
      
      //setting position attribute in the shader:
      int positionAttribute = glGetAttribLocation(shaderProgramID, &quot;position&quot;);
      glEnableVertexAttribArray(positionAttribute);
      glBindAttribLocation(shaderProgramID, positionAttribute, &quot;position&quot;);
      
      //setting colour attribute in the shader:
      int colourAttribute = glGetAttribLocation(shaderProgramID, &quot;colour&quot;);
      glEnableVertexAttribArray(colourAttribute);
      glBindAttribLocation(shaderProgramID, colourAttribute, &quot;colour&quot;);
      
      
      glClearColor(0.5f,0.5f,0.5f,1f);
      
      //this will create a wire frame view
      //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
      
      glfwShowWindow(window); //optional
      
      while (!glfwWindowShouldClose(window)) {
          double time = glfwGetTime();
          glfwPollEvents();
          glClear(GL_COLOR_BUFFER_BIT);
          
          glBindVertexArray(vaoID);
          
          //enables the position attribute in the vertex shader
          glEnableVertexAttribArray(0);
          //enables the colour attribute in the vertex shader
          glEnableVertexAttribArray(1);
          
          glDrawArrays(GL_TRIANGLES, 0, vertexPoints.length);
          //disables the position and colour attribute in the vertex shader
          glDisableVertexAttribArray(0);
          glDisableVertexAttribArray(1);
          
          glfwSwapBuffers(window);
      }
      
      //clear vaos and vbos
      for (int vao : vaos) {
          glDeleteVertexArrays(vao);
      }
      for (int vbo : vbos) {
          glDeleteBuffers(vbo);
      }
      //delete shaders
      glDetachShader(shaderProgramID, vertexShaderID);
      glDetachShader(shaderProgramID, fragmentShaderID);
      glDeleteShader(vertexShaderID);
      glDeleteShader(fragmentShaderID);
      glDeleteProgram(shaderProgramID);
      
      Callbacks.glfwFreeCallbacks(window);
      
      glfwDestroyWindow(window);
      glfwTerminate();
     
  }
  </code></pre>
  <p>I have provided my full code file on github (250 lines) here: <a href="https://github.com/OneEgg42/opengl" rel="nofollow noreferrer">https://github.com/OneEgg42/opengl</a></p>
  <p>Would someone be so kind and explain to me how this works?</p>
  <p>Thanks in advance!</p>