title: Initialize GLSL layout with glEnableVertexAttribArray
body: <p>I want to pass arrays of vertices, UV's and normals to the shader and transform them using MVP matrix, so I wrote a simple shader program:</p>
  
  <pre><code>#version 330 core
  //Vertex shader
  layout(location=0)in vec3 vertexPosition_modelspace;
  layout(location=1)in vec2 vertexUV;
  out vec2 UV;
  
  uniform mat4 MVP;
  
  void main(){
      gl_Position =  MVP * vec4(vertexPosition_modelspace,1);
      UV = vertexUV;
  }
  
  #version 330
  //Fragment Shader
  in vec2 UV;
  out vec3 color;
  uniform sampler2D color_texture;
  
  void main(void) {
      color = texture(color_texture, UV).rgb;
  }
  </code></pre>
  
  <p>Then I needed to pass an array of vertices, which is being initialized like that:</p>
  
  <pre><code>glGenBuffers(1, &amp;vertex_buffer);
  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &amp;vertices[0], model_usage);
  </code></pre>
  
  <p>Same with UV's and normals, the type is still GL_ARRAY_BUFFER for them.</p>
  
  <p>Then a draw loop:</p>
  
  <pre><code>glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
          for (Model* mdl : baseShader-&gt;getModels()) {
  
              glUseProgram(baseShader-&gt;getShaderProgram());
  
              glActiveTexture(GL_TEXTURE0);
              glBindTexture(GL_TEXTURE_2D, mdl-&gt;getTextureBuffer());
              glUniform1i(texture_location, 0);
  
              glm::mat4 mvp = RootEngine-&gt;projection_matrix * RootEngine-&gt;view_matrix * mdl-&gt;getModelMatrix();
              glUniformMatrix4fv(baseShader-&gt;getMVPlocation(), 1, GL_FALSE, &amp;mvp[0][0]);
  
              glEnableVertexAttribArray(0);
              glBindBuffer(GL_ARRAY_BUFFER, mdl-&gt;getVertexBuffer());
              glVertexAttribPointer(0,  3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  
              glEnableVertexAttribArray(1); // Matches layout (location = 1)
              glBindBuffer(GL_ARRAY_BUFFER, mdl-&gt;getUVsBuffer());
              glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
  
              glDrawArrays(GL_TRIANGLES, 0, mdl-&gt;getVertices()-&gt;size());
  
              glDisableVertexAttribArray(0);
              glDisableVertexAttribArray(1);
  
          }
  
          SDL_GL_SwapWindow(RootEngine-&gt;getMainWindow());
  </code></pre>
  
  <p>BaseShader and Model are my own classes which do a simple initialization and VBO handling.</p>
  
  <p>The problem is nothing actually being rendered. I tried to add <code>glEnableClientState(GL_VERTEX_ARRAY);</code></p>
  
  <p>and</p>
  
  <pre><code>glGenVertexArrays(1, &amp;VertexArrayID);
  glBindVertexArray(VertexArrayID);
  ...
  glDeleteVertexArrays(1, &amp;VertexArrayID);
  </code></pre>
  
  <p>But still nothing. When I don't use layout and pass the data with glVertexPointer everything seems to work fine.</p>
  
  <p>UPDATE 1: I found out what prevents vertices from being rendered. It's uniform variable in the VertexShader. If it's being removed the vertices are being rendered, but then there is no way to pass a matrix to the shader.</p>