title: Sampling from a texture in OpenGL is black
body: <p>I'm trying out my hand at graphics, following a tutorial at <a href="http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_06" rel="nofollow noreferrer">http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_06</a></p>
  
  <p>Problem: When I try to texture my cube, my sample is black.</p>
  
  <p>Screenshot: <a href="http://puu.sh/2JP1H.jpg" rel="nofollow noreferrer">http://puu.sh/2JP1H.jpg</a> (note: I set blue = uv.x to test my UVs)</p>
  
  <p>I looked at the threads <a href="https://stackoverflow.com/questions/4124708/opengl-textures-appear-just-black">OpenGL textures appear just black</a>, and
  <a href="https://stackoverflow.com/questions/16077448/texture-is-all-black">Texture is all black</a>, but it seemed they had different problems.</p>
  
  <p>First I load my texture using SOIL image loading library:</p>
  
  <pre><code>int w, h;
  unsigned char* img = SOIL_load_image("resources/images/4x4window.jpg",&amp;w,&amp;h,0,0);
  ErrorIf(!img, "%s", SOIL_last_result());
  glGenTextures(1, &amp;texture_id);
  glBindTexture(GL_TEXTURE, texture_id);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,w,h,0,GL_RGB,GL_UNSIGNED_BYTE,img);
  </code></pre>
  
  <p>My render function where I pass my texture to the shader program:</p>
  
  <pre><code>void onDisplay()
  {
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
    glUseProgram(program);
  
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture_id);
    glUniform1i(uniform_myTexture, /*GL_TEXTURE*/0);
  
    glEnableVertexAttribArray(attribute_coord3d); // open shader var
    glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_verts); // put stuff in buffer
    glVertexAttribPointer(attribute_coord3d, 3, GL_FLOAT, GL_FALSE, 0, 0); //send
  
    glEnableVertexAttribArray(attribute_texcoord);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_texcoords);
    glVertexAttribPointer(attribute_texcoord, 2, GL_FLOAT, GL_FALSE, 0, 0);
  
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_elements);
    int size; 
    glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &amp;size);
    glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
  
    glDisableVertexAttribArray(attribute_coord3d);
    glDisableVertexAttribArray(attribute_texcoord);
  
    glutSwapBuffers();
  }
  </code></pre>
  
  <p>Vertex Shader:</p>
  
  <pre><code>attribute vec3 coord3d;
  attribute vec2 texcoord;
  varying vec2 f_texcoord;
  uniform mat4 mvp;
  
  void main(void)
  {
    gl_Position = mvp * vec4(coord3d, 1.0);
    f_texcoord = texcoord;
  }
  </code></pre>
  
  <p>Fragment Shader:</p>
  
  <pre><code>varying vec2 f_texcoord;
  uniform sampler2D mytexture;
  
  void main(void)
  {
    vec4 result = texture2D(mytexture, f_texcoord);
  
    result.z = f_texcoord.x;
    result.w = 1;
  
    gl_FragColor = result;
  }
  </code></pre>