title: OpenGL, Shader Model 3.3 Texturing: Black Textures?
body: <p>I've been banging my head against this for hours now, I'm sure it's something simple, but I just can't get a result. I've had to edit this code down a bit because I've built a little library to encapsulate the OpenGL calls, but the following is an accurate description of the state of affairs.</p>
  
  <p>I'm using the following vertex shader:</p>
  
  <pre><code>#version 330
  in vec4 position;
  in vec2 uv;
  out vec2 varying_uv;
  void main(void)
  {
      gl_Position = position;
      varying_uv = uv;
  }
  </code></pre>
  
  <p>And the following fragment shader:</p>
  
  <pre><code>#version 330
  in vec2 varying_uv;
  uniform sampler2D base_texture;
  out vec4 fragment_colour;
  void main(void)
  {
      fragment_colour = texture2D(base_texture, varying_uv);
  }
  </code></pre>
  
  <p>Both shaders compile and the program links without issue.</p>
  
  <p>In my init section, I load a single texture like so:</p>
  
  <pre><code>// Check for errors.
  kt::kits::open_gl::Core&lt;QString&gt;::throw_on_error();
  
  // Load an image.
  QImage image("G:/test_image.png");
  image = image.convertToFormat(QImage::Format_RGB888);
  
  if(!image.isNull())
  {
      // Load up a single texture.
      glGenTextures(1, &amp;Texture);
      glBindTexture(GL_TEXTURE_2D, Texture);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.width(), image.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.constBits());
      glBindTexture(GL_TEXTURE_2D, 0);
  }
  
  // Check for errors.
  kt::kits::open_gl::Core&lt;QString&gt;::throw_on_error();
  </code></pre>
  
  <p>You'll observe that I'm using Qt to load the texture. The calls to ::throw_on_error() check for errors in OpenGL (by calling Error()), and throw an exception if one occurs. No OpenGL errors occur in this code, and the image loaded using Qt is valid.</p>
  
  <p>Drawing is performed as follows:</p>
  
  <pre><code>// Clear previous.
  glClear(GL_COLOR_BUFFER_BIT |
      GL_DEPTH_BUFFER_BIT |
      GL_STENCIL_BUFFER_BIT);
  
  // Use our program.
  glUseProgram(GLProgram);
  
  // Bind the vertex array.
  glBindVertexArray(GLVertexArray);
  
  /* ------------------ Setting active texture here ------------------- */
  
  // Tell the shader which textures are which.
  kt::kits::open_gl::gl_int tAddr = glGetUniformLocation(GLProgram, "base_texture");
  glUniform1i(tAddr, 0);
  
  // Activate the texture Texture(0) as texture 0.
  glActiveTexture(GL_TEXTURE0 + 0);
  glBindTexture(GL_TEXTURE_2D, Texture);
  
  /* ------------------------------------------------------------------ */
  
  // Draw vertex array as triangles.
  glDrawArrays(GL_TRIANGLES, 0, 4);
  glBindVertexArray(0);
  glUseProgram(0);
  
  // Detect errors.
  kt::kits::open_gl::Core&lt;QString&gt;::throw_on_error();
  </code></pre>
  
  <p>Similarly, no OpenGL errors occur, and a triangle is drawn to screeen. However, it looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/NTrWs.png" alt="Texturing, not as expected."></p>
  
  <p>It occurred to me the problem may be related to my texture coordinates. So, I rendered the following image using s as the 'red' component, and t as the 'green' component:</p>
  
  <p><img src="https://i.stack.imgur.com/HgOi6.png" alt="Coloring using st coordinates."></p>
  
  <p>The texture coordinates appear correct, yet I'm still receiving the black triangle of doom. What am I doing wrong?</p>