title: Strange behavior of glVertexAttrib3f on one machine
body: <p>I have a very weird problem with my simple OpenGL application not working on my girlfriend's machine. At first I thought of all kinds of problems with my code, but I could finally break it down to this code not working (not working means: the triangle is drawn correctly, but black, that is, without the correct color):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string&gt;
  
  #define GLEW_STATIC
  #include &lt;GL/glew.h&gt;
  #include &lt;GLFW/glfw3.h&gt;
  #include &lt;glm/glm.hpp&gt;
  #include &lt;glm/gtx/transform.hpp&gt;
  
  #include "../common/shader.h"
  
  
  const GLfloat reticle_vertices[] =
  {
          -1.0f, -1.0f, 0.0f,
          1.0f, -1.0f, 0.0f,
          0.0f, 1.0f, 0.0f
  };
  
  
  glm::mat4 MVP;
  glm::mat4 vp;
  
  
  const char *vertexSource =
          "#version 150\n"
          "layout(location = 0) in vec3 position;"
          "layout(location = 1) in vec3 color;"
          "out vec3 fragColor;"
          "uniform mat4 MVP;"
          "void main() {"
          "gl_Position = MVP * vec4(position, 1.0);"
          "fragColor = color;"
          "}";
  
  const char *fragmentSource =
          "#version 150\n"
          "in vec3 fragColor;"
          "out vec4 outColor;"
          "void main() {"
          "outColor = vec4(fragColor, 1.0);"
          "}";
  
  
  // Key callback: exit on ESCAPE
  static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
  {
          if (key == GLFW_KEY_ESCAPE &amp;&amp; action == GLFW_PRESS)
                  glfwSetWindowShouldClose(window, GL_TRUE);
  }
  
  
  // Initialize and return a window with GLFW
  GLFWwindow *initGLWindow(int width, int height, int samples, const char *title, GLFWkeyfun keycb)
  {
          if (!glfwInit())
          {
                  fprintf(stderr, "Failed to initialize GLFW\n");
                  return NULL;
          }
          glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
          glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
          glfwWindowHint(GLFW_SAMPLES, samples);
          glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  
          GLFWwindow *w = glfwCreateWindow(width, height, title, NULL, NULL);
          if (!w)
          {
                  glfwTerminate();
                  return NULL;
          }
  
          glfwMakeContextCurrent(w);
          glfwSetKeyCallback(w, keycb);
  
          glewExperimental = true;
          if (glewInit() != GLEW_OK)
          {
                  fprintf(stderr, "Failed to initialize GLEW\n");
                  return NULL;
          }
  
          return w;
  }
  
  
  int main()
  {
          // Initialize the OpenGL window
          GLFWwindow *window = initGLWindow(1024, 768, 2, "Schnuff", key_callback);
  
          // Load and use the shaders
          GLuint programID = LoadShaders(vertexSource, fragmentSource);
          glUseProgram(programID);
  
          // Get the attribute locations
          GLuint posAttrib = glGetAttribLocation(programID, "position");
          printf("posAttrib = %d\n", posAttrib);
          GLuint colAttrib = glGetAttribLocation(programID, "color");
          printf("colAttrib = %d\n", colAttrib);
          //GLint normAttrib = glGetAttribLocation(programID, "normal");
          //printf("normAttrib = %d\n", normAttrib);
          GLuint mvpID = glGetUniformLocation(programID, "MVP");
  
          // Vertex array object
          GLuint vao;
          glGenVertexArrays(1, &amp;vao);
          glBindVertexArray(vao);
  
          // Vertex buffer object
          GLuint vbo;
          glGenBuffers(1, &amp;vbo);
          glBindBuffer(GL_ARRAY_BUFFER, vbo);
          glBufferData(GL_ARRAY_BUFFER, sizeof(reticle_vertices), reticle_vertices, GL_STATIC_DRAW);
  
          glEnableVertexAttribArray(posAttrib);
          glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  
          // Hide the cursor
          glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  
          glEnable(GL_DEPTH_TEST);
          glDepthFunc(GL_LESS);
          glEnable(GL_CULL_FACE);
  
          glClearColor(0.0f, 0.0f, 0.2f, 1.0f);
          glm::mat4 m = glm::mat4(1.0f);
  
          // Main loop
          do
          {
                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
                  glUniformMatrix4fv(mvpID, 1, GL_FALSE, &amp;m[0][0]);
                  glVertexAttrib3f(colAttrib, 1.0f, 0.0f, 0.0f);
                  glDrawArrays(GL_TRIANGLES, 0, 3);
  
                  glfwSwapBuffers(window);
                  glfwPollEvents();
          }
          while (!glfwWindowShouldClose(window));
  
          glDeleteProgram(programID);
          glfwDestroyWindow(window);
          glfwTerminate();
          return 0;
  }
  </code></pre>
  
  <p>As far as I could figure it out, the problem <strong>only</strong> occurs because of the following: on her machine, <code>posAttrib = 1, colAttrib = 0</code>, while on mine it is the other way round. I tried using</p>
  
  <pre><code>layout(location = 0) in vec3 position;
  layout(location = 1) in vec3 color;
  </code></pre>
  
  <p>and then it works fine! Although once I exchange these numbers (I have yet to test it with other values and <code>#version 330 core\n</code>, but her laptop is offline at the moment), it stops working again...</p>
  
  <p>I even found the following unanswered question <a href="https://stackoverflow.com/questions/17608643/strange-glvertexattrib-behavior">here on SO</a>, which seems very closely related.</p>
  
  <p>Did anyone of you encounter a similar behavior, or knows how to fix the whole situation without having to use <code>layout(location = x)</code>? Since I think in a larger project, setting every location by hand could be tedious or unwanted (correct me if I'm wrong there and this is actually the preferred way to do it!).</p>
  
  <p>Yet to test: other values than 0 or 1, and the <code>#version</code> stuff, since on my machine I can't even use <code>layout(location = x)</code> without <code>#version 330 core</code> (which is again strange, because it apparently does <em>something</em> on her machine which makes it work).</p>
  
  <p>I'm a newbie at OpenGL and thus totally confused here! :-)</p>