title: C++ OpenGL Shaders not working
body: <p>I've just started learning c++ and I'm trying to make a program with shaders that draws a triangle with colors.</p>
  
  <p>This is my vertex shader:</p>
  
  <pre><code>const GLchar* vertexSource =
  "#version 150 core\n"
  "in vec4 position;"
  "in vec4 color;"
  "out vec4 Color;"
  "out vec4 gl_Position;"
  "void main(){"
  "   gl_Position = position;"
  "   Color = color;"
  "}";
  </code></pre>
  
  <p>and this is my fragment shader:</p>
  
  <pre><code>const GLchar* fragmentSource =
  "#version 150 core\n"
  "in vec4 Color;"
  "out vec4 gl_FragColor"
  "void main(){"
  "    gl_FragColor = Color;"
  "}";
  </code></pre>
  
  <p>I have a list of values for the co-ords and colors respectively:</p>
  
  <pre><code>float vertices[]{
      -1.0f, -1.0f, 0.0f,1.0f,   1.0f,0.0f,0.0f,1.0f,
      1.0f, -1.0f, 0.0f,1.0f,    0.0f,1.0f,0.0f,1.0f,
      0.0f,  1.0f, 0.0f,1.0f,    0.0f,0.0f,1.0f,1.0f,
  };
  </code></pre>
  
  <p>I initialise a 'vertex buffer object' and a 'vertex array object'</p>
  
  <pre><code>GLuint vao; //Initialise vertex array object
  glGenVertexArrays(1, &amp;vao);
  glBindVertexArray(vao);
  
  GLuint vbo; //Initialise vertex buffer object
  glGenBuffers(1,&amp;vbo);
  glBindBuffer(GL_ARRAY_BUFFER,vbo);
  glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
  </code></pre>
  
  <p>I compile a shader program:</p>
  
  <pre><code>GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  glShaderSource(vertexShader, 1, &amp;vertexSource, NULL);
  glCompileShader(vertexShader);
  
  GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  glShaderSource(fragmentShader, 1, &amp;fragmentSource, NULL);
  glCompileShader(fragmentShader);
  
  GLuint shaderProgram = glCreateProgram();
  glAttachShader(shaderProgram, vertexShader);
  glAttachShader(shaderProgram, fragmentShader);
  glLinkProgram(shaderProgram);
  
  glUseProgram(shaderProgram);
  </code></pre>
  
  <p>I define some attributes for the shaders:</p>
  
  <pre><code>glBindAttribLocation(shaderProgram, 0, "position");
  glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0);
  glEnableVertexAttribArray(0);
  
  glBindAttribLocation(shaderProgram, 1 ,"color");
  glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(4 * sizeof(float)));
  glEnableVertexAttribArray(1);
  </code></pre>
  
  <p>and finally I draw the triangle in a 'do, while' loop</p>
  
  <pre><code>glDrawArrays(GL_TRIANGLES, 0, 3);
  </code></pre>
  
  <p>However, it doesn't print any triangles, but instead just shows a black screen.</p>
  
  <p>I got most of this code from tutorials online, and I'm trying to rewrite everything out to understand how everything works.</p>
  
  <p>Could anyone see where I went wrong? Any advice would be greatly appreciated :)</p>
  
  <p>Edit:</p>
  
  <p>I've added some code to check for errors:</p>
  
  <pre><code>if (status == GL_FALSE)
  {
      GLint infoLogLength;
      glGetShaderiv(shaderProgram, GL_INFO_LOG_LENGTH, &amp;infoLogLength);
  
      GLchar* strInfoLog = new GLchar[infoLogLength + 1];
      glGetProgramInfoLog(shaderProgram, infoLogLength, NULL, strInfoLog);
  
      std::cout &lt;&lt; strInfoLog;
      delete[] strInfoLog;
  }
  </code></pre>
  
  <p>...but nothing prints</p>