title: OpenGL ES Texture Coordinates Slightly Off
body: <p>I'm trying to draw a subregion of a texture in OpenGL by specifying the coordinates I want. What's happening though is that, depending on the size of the image, it seems there's a slight offset in the origin of where it selects the texture coordinates. The offset amount seems to be less than the size of a pixel &amp; the output is is blurred combination of neighboring pixels.</p>
  
  <p>Here's an idea of what I'm describing. In this case I'd want to select the 6x5 green/white region but what OpenGL is rendering includes a slight pink tint to the top &amp; left pixels.
  <img src="https://i.stack.imgur.com/3qQeW.png" alt="Offset Illustration"></p>
  
  <p>What the output would look like:</p>
  
  <p><img src="https://i.stack.imgur.com/tYiju.png" alt="Resulting Output Texture"></p>
  
  <p>I can fix it by adding an offset to the texture coordinates before passing them to glTexCoordPointer but the problem is that I have no way to calculate what the offset is and it seems different for different textures.</p>
  
  <p>Pseudocode:</p>
  
  <pre><code>float uFactor = regionWidth / textureWidth;   // For the example: 0.6f
  float vFactor = regionHeight / textureHeight; // For the example: 0.5f
  
  data[0].t[0] = 0.0f * uFactor;
  data[0].t[1] = 0.0f * vFactor;
  data[1].t[0] = 1.0f * uFactor;
  data[1].t[1] = 0.0f * vFactor;
  data[2].t[0] = 0.0f * uFactor;
  data[2].t[1] = 1.0f * vFactor;
  data[3].t[0] = 1.0f * uFactor;
  data[3].t[1] = 1.0f * vFactor;
  
  glPushMatrix();
  
  // translate/scale/bind operations
  
  glTexCoordPointer(2, GL_FLOAT, 0, data[0].t);
  </code></pre>