title: Why am I getting a black screen with this opengl application?
body: <p>I must be missing something really simple here, I've been staring at my code for the last hour and just can't see it. The code should just create a texture with random pure color or pure black pixels, then draw it to the screen. Right now though it's just producing a black screen...</p>
  
  <p>My guess is it has something to with my vertex or uv positions.</p>
  
  <pre><code>#include &lt;time.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;GL/glew.h&gt;
  #include &lt;GLFW/glfw3.h&gt;
  
  const char TITLE[] = "GL TEST";
  const int WIDTH = 800;
  const int HEIGHT = 600;
  
  const char BASIC_VERT_SHADER[] =
  "#version 330\n"
  "in vec2 vs_position;"
  "in vec2 vs_uvCoord;"
  "out vec2 fs_uvCoord;"
  "void main()"
  "{"
    "fs_uvCoord = vs_uvCoord;"
    "gl_Position = vec4(vs_position, 1.f, 1.f);"
  "}";
  
  const char BASIC_FRAG_SHADER[] =
  "#version 330\n"
  "uniform sampler2D texture;"
  "in vec2 fs_uvCoord;"
  "out vec4 frag_color;"
  "void main()"
  "{"
    "frag_color = texture2D(texture, fs_uvCoord);"
  "}";
  
  const float TEXTURE_UV[] =
  {
    0.f, 0.f,
    1.f, 0.f,
    1.f, 1.f,
    0.f, 1.f
  };
  
  const float VERT_POS[] =
  {
    -1.f, -1.f,
     1.f, -1.f,
     1.f,  1.f,
    -1.f,  1.f
  };
  
  GLFWwindow* setupGraphics()
  {
    GLFWwindow* window;
  
    if ( !glfwInit() )
    {
      fputs("failed to initialize glfw!\n", stderr);
      exit(1);
    }
  
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // opengl 3.x
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // opengl 3.3
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // needed for osx
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    window = glfwCreateWindow(WIDTH, HEIGHT, TITLE, NULL, NULL);
  
    if (!window)
    {
      fputs("failed to create window!\n", stderr);
      exit(1);
    }
  
    glfwMakeContextCurrent(window);
  
    glewExperimental = true;
  
    if ( glewInit() != GLEW_OK )
    {
      fputs("failed to initialize glew!\n", stderr);
      exit(1);
    }
  
    if ( !GLEW_VERSION_3_3 )
    {
      fputs("opengl 3.3 support is required!\n", stderr);
      exit(1);
    }
  
    return window;
  };
  
  unsigned int compileShader(const char* shaderSource, GLenum type)
  {
    unsigned int shaderID = glCreateShader(type);
    glShaderSource(shaderID, 1, &amp;shaderSource , NULL);
    glCompileShader(shaderID);
  
    int status;
    glGetShaderiv(shaderID, GL_COMPILE_STATUS, &amp;status);
    if ( status == GL_FALSE )
    {
      int errorLength;
      glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &amp;errorLength);
      char errorString[errorLength+1];
      glGetShaderInfoLog(shaderID, errorLength, NULL, errorString);
      fprintf(stderr, "glsl compile error: %s", errorString);
      exit(1);
    }
  
    return shaderID;
  }
  
  unsigned int compileShaderProgram(const char* vsSource, const char* fsSource)
  {
    unsigned int programID = glCreateProgram();
    unsigned int vsID = compileShader(vsSource, GL_VERTEX_SHADER);
    unsigned int fsID = compileShader(fsSource, GL_FRAGMENT_SHADER);
    glAttachShader(programID, vsID);
    glAttachShader(programID, fsID);
    glLinkProgram(programID);
  
    int status;
    glGetProgramiv(programID, GL_LINK_STATUS, &amp;status);
    if ( status == GL_FALSE )
    {
      int errorLength;
      glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &amp;errorLength);
      char errorString[errorLength+1];
      glGetProgramInfoLog(programID, errorLength, NULL, errorString);
      fprintf(stderr, "glsl linking error: %s", errorString);
      exit(1);
    }
  
    glDetachShader(programID, vsID);
    glDetachShader(programID, fsID);
    glDeleteShader(vsID);
    glDeleteShader(fsID);
  
    return programID;
  }
  
  unsigned int randomTexture(int width, int height)
  {
    unsigned int textureID;
    int size = width * height;
    unsigned char* buffer = malloc( size * sizeof(unsigned char) );
  
    for (int i = 0; i &lt; size; i++)
    {
      if ( rand() &amp; 1 )
      {
        buffer[i] = 0x00;
      }
      else
      {
        buffer[i] = 0xFF;
      }
    }
  
    glGenTextures(1, &amp;textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glTexImage2D(
      GL_TEXTURE_2D, 0, GL_R8,
      width, height, 0, GL_RED,
      GL_UNSIGNED_BYTE, buffer
    );
  
    free(buffer);
  
    return textureID;
  }
  
  int main()
  {
    GLenum glError;
    GLFWwindow* window;
    unsigned int shader;
    unsigned int texture;
    unsigned int vertPosBuffer;
    unsigned int uvCoordBuffer;
    unsigned int vertexArray;
  
    int textureUniform;
    int vertPosAttribute;
    int uvCoordAttribute;
  
    srand( time(NULL) );
  
    window = setupGraphics();
  
    shader = compileShaderProgram(BASIC_VERT_SHADER, BASIC_FRAG_SHADER);
    textureUniform = glGetUniformLocation(shader, "texture");
    vertPosAttribute = glGetAttribLocation(shader, "vs_position");
    uvCoordAttribute = glGetAttribLocation(shader, "vs_uvCoord");
  
    texture = randomTexture(WIDTH, HEIGHT);
  
    glGenVertexArrays(1, &amp;vertexArray);
    glBindVertexArray(vertexArray);
  
    glGenBuffers(1, &amp;vertPosBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertPosBuffer);
    glBufferData(GL_ARRAY_BUFFER, 8*sizeof(float), VERT_POS, GL_STATIC_DRAW);
    glVertexAttribPointer(vertPosAttribute, 2, GL_FLOAT, GL_FALSE, 0, 0);
  
    glGenBuffers(1, &amp;uvCoordBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, uvCoordBuffer);
    glBufferData(GL_ARRAY_BUFFER, 8*sizeof(float), TEXTURE_UV, GL_STATIC_DRAW);
    glVertexAttribPointer(uvCoordAttribute, 2, GL_FLOAT, GL_FALSE, 0, 0);
  
    while ( !glfwWindowShouldClose(window) )
    {
      glClear( GL_COLOR_BUFFER_BIT );
      glClearColor(0.f, 0.f, 0.f, 1.f);
  
      glUseProgram(shader);
  
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_2D, texture);
      glUniform1i(textureUniform, 0);
  
      glBindVertexArray(vertexArray);
      glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  
      glfwSwapBuffers(window);
      glfwPollEvents();
  
      while((glError = glGetError()) != GL_NO_ERROR)
      {
        fprintf(stderr, "gl error: %d\n", glError);
        exit(1);
      }
    }
  
    glDeleteVertexArrays(1, &amp;vertexArray);
    glDeleteBuffers(1, &amp;vertPosBuffer);
    glDeleteBuffers(1, &amp;uvCoordBuffer);
    glDeleteTextures(1, &amp;texture);
    glDeleteProgram(shader);
  
    return 0;
  }
  </code></pre>