title: OpenGL 3.3 texture mapping 3D objects
body: <p>I'm trying to texture a 3D pool table with two different textures, one for the table top and one for the legs and the walls. The table top is basically a 2D rectangle on which I applied some green texture and everything worked fine. On the other hand when I try to texture the cubes composing the table I run into some sampling or mapping problems.<br>
  This is my code:<br>
  create the vaos and map</p>
  
  <pre><code>  // Cube vertex data
  float cubeVertices[ CUBE_NUM_VERTICES * VALS_PER_VERT ] = {
    -1.0, -1.0,  1.0,
    1.0, -1.0,  1.0,
    1.0,  1.0,  1.0,
    -1.0,  1.0,  1.0,
    -1.0, -1.0, -1.0,
    1.0, -1.0, -1.0,
    1.0,  1.0, -1.0,
    -1.0,  1.0, -1.0 };
  
    // 12 triangles - 2 per face of the cube
  unsigned int legWallIndices[CUBE_NUM_TRIS*3] = {
    0,1,2, 2,3,0,
    1,5,6, 6,2,1,
    5,4,7, 7,6,5,
    4,0,3, 3,7,4,
    3,2,6, 6,7,3,
    4,5,1, 1,0,4
  };
  
  float tableTopVertices[4 * VALS_PER_VERT] = {
    -2.5f, 0.26f, -5.0f,
    2.5f, 0.26f, -5.0f,
    2.5f, 0.26f, 5.0f,
    -2.5f, 0.26f, 5.0f,
  };
  
  unsigned int tableTopIndices[2*3] = {
    0,1,2, 2,3,0,
  };
  
  float tabletTopTexCoord[4 * VALS_PER_TEX] = {
      0.0f, 0.0f,  1.0f, 0.0f,  1.0f, 1.0f,  0.0f, 1.0f
  };
  
  float legWallTexCoord[8 * VALS_PER_TEX] = {
      1.0f, 0.0f,
      0.0f, 0.0f,
      0.0f, 1.0f,
      1.0f, 1.0f,
      // not working properly idk why
      1.0f, 0.0f,
      0.0f, 0.0f,
      0.0f, 1.0f,
      1.0f, 1.0f,
  };
  </code></pre>
  
  <p>vertex shader</p>
  
  <pre><code>in vec2 green_tex_coord;
  in vec2 wood_tex_coord;
  
  out vec2 st1;
  out vec2 st2;
  
  void main(void)
  {
    st1 = green_tex_coord;
    st2 = wood_tex_coord;
  }
  </code></pre>
  
  <p>fragment shader</p>
  
  <pre><code>in vec2 st1;
  in vec2 st2;
  
  uniform sampler2D greenTexMap;
  uniform sampler2D woodTexMap;
  
  void main(void)
  {
    vec4 topClr = vec4(0.04, 0.42, 0.1, 1.0);
    vec4 sideClr = vec4(0.5, 0.35, 0.05, 1.0);
  
    if (predicate){
        fragColour = topClr * texture(greenTexMap, st1);
    }else{
        fragColour = sideClr * texture(woodTexMap, st2);
    }
  }
  </code></pre>
  
  <p>This is the result (the green texture is working but the wood one is not) <a href="https://i.stack.imgur.com/xfwB6.png" rel="nofollow noreferrer">table</a></p>
  
  <p>If I remove <code>* texture(woodTexMap, st2)</code> the colour changes (it shows sideClr), this means the texture reaches the fragment but it is badly sampled or mapped. If I remove <code>sideClr *</code> the colour changes again showing a plain colour and not a texture (actually it looks like it samples the bottom left corner of my picture).  </p>
  
  <p>Am I mapping it the wrong way or is it a sampling problem?</p>