title: opengl 2.1 and textures
body: <p>I'm trying to show a texture(yes it is a pot) with opengl 2.1 and glsl 120, but i'm not sure on how to do it, all i can get is a black quad, i've been following this tutorials: <a href="http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/" rel="nofollow noreferrer">A Textured Cube</a>, <a href="https://open.gl/textures" rel="nofollow noreferrer">OpenGl - Textures</a> and what i have understood is that i need to:</p>
  
  <ol>
  <li>Specify the texture coordinates to attach to each vertex(in my case are 6 vertices, a cube without indexing)</li>
  <li>Load the texture and bind it in a texture unit(default is 0)</li>
  <li><p>call glDrawArrays</p></li>
  <li><p>Inside the shaders i need to:</p></li>
  <li>Receive the texture coords in an attribute in the vertex shader and pass it to the fragment shader through a varying variable</li>
  <li>In the fragment shader use a sampler object to sample a pixel, in the position specified by the varying variable, from the texture.</li>
  </ol>
  
  <p>Is it all correct?</p>
  
  <p>Here is how i create the texture VBO and load the texture:</p>
  
  <pre><code>void Application::onStart(){
  unsigned int format;
  SDL_Surface* img;
  
  float quadCoords[] = {
          -0.5f, -0.5f, 0.0f,
          0.5f, -0.5f, 0.0f,
          0.5f, 0.5f, 0.0f,
          0.5f, 0.5f, 0.0f,
          -0.5f, 0.5f, 0.0f,
          -0.5f, -0.5f, 0.0f};
  
  const float texCoords[] = {
          0.0f, 0.0f, 
          1.0f, 0.0f, 
          1.0f, 1.0f, 
          1.0f, 1.0f, 
          0.0f, 1.0f,
          0.0f, 0.0f};
  
  //shader loading omitted ...
  sprogram.bind(); // call glUseProgram(programId)
  //set the sampler value to 0 -&gt; use texture unit 0
  sprogram.loadValue(sprogram.getUniformLocation(SAMPLER), 0);
  
  //quad
  glGenBuffers(1, &amp;quadBuffer);
  glBindBuffer(GL_ARRAY_BUFFER, quadBuffer);
  glBufferData(GL_ARRAY_BUFFER, sizeof(float)*18, quadCoords, GL_STATIC_DRAW);
  //texture
  glGenBuffers(1, &amp;textureBuffer);
  glBindBuffer(GL_ARRAY_BUFFER, textureBuffer);
  glBufferData(GL_ARRAY_BUFFER, sizeof(float)*12, texCoords, GL_STATIC_DRAW);
  
  //load texture
  img = IMG_Load("resources/images/crate.jpg");
  
  if(img == nullptr)
      throw std::runtime_error(SDL_GetError());
  
  glGenTextures(1, &amp;this-&gt;texture);
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, this-&gt;texture);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img-&gt;w, img-&gt;h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img-&gt;pixels);
  
  SDL_FreeSurface(img);
  }
  </code></pre>
  
  <p>rendering phase:</p>
  
  <pre><code>glClear(GL_COLOR_BUFFER_BIT);
  
  glEnableVertexAttribArray(COORDS);
  glBindBuffer(GL_ARRAY_BUFFER, quadBuffer);
  glVertexAttribPointer(COORDS, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
  
  glEnableVertexAttribArray(TEX_COORDS);
  glBindBuffer(GL_ARRAY_BUFFER, textureBuffer);
  glVertexAttribPointer(TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
  
  //draw the vertices
  glDrawArrays(GL_TRIANGLES, 0, 6);
  </code></pre>
  
  <p>vertex shader:</p>
  
  <pre><code>#version 120
  
  attribute vec3 coord;
  attribute vec2 texCoord;
  
  varying vec2 UV;
  
  void main(){    
      gl_Position = vec4(coord.x, coord.y, coord.z, 1.0);
      UV = texCoord;
  }
  </code></pre>
  
  <p>fragment shader:</p>
  
  <pre><code>#version 120
  
  uniform sampler2D tex;
  varying vec2 UV;
  
  void main(){
      gl_FragColor.rgb = texture2D(tex, UV).rgb;
      gl_FragColor.a = 1.0;
  }
  </code></pre>
  
  <p>I know that the tutorials use <code>out</code> instead of <code>varying</code> so i tried to "convert" the code, also there is this tutorial: <a href="http://www.lighthouse3d.com/tutorials/glsl-12-tutorial/simple-texture/" rel="nofollow noreferrer">Simple Texture - LightHouse</a> that explain the <code>gl_MultiTexCoord0</code> attribute and <code>gl_TexCoord</code> array wich are built in, but this is almost the same thing i'm doing. I want to know if 'm doing it all right and if not, i would like to know how to show a simple 2d texture in the screen with opengl 2.1 and glsl 120</p>