title: OpenGL ES 2.0: What's the correct way to dynamically set texture coordinates in a GLSL vertex shader?
body: <p>I'm trying to do the following in a vertex shader:</p>
  
  <pre><code>...
  attribute vec2 tCoordIn;
  uniform vec2 tCoordScale;
  uniform vec2 tCoordOffset;
  varying vec2 tCoordOut;
  
  void main() {
      ...
      tCoordOut = (tCoordIn * tCoordScale) + tCoordOffset;
      ...
  }
  </code></pre>
  
  <p>with no luck if I use sensible values for <code>tCoordScale</code> and <code>tCoordOffset</code>.</p>
  
  <p><strike>Will this work if I do nothing else wrong, or does it go against the GLSL specification?</strike></p>
  
  <p>It only works approximately when I use some very outlandish scale numbers, and add some unexpected number to both x and y offsets. Does the GPU ever pad out the texture image so that <code>0</code> and <code>1</code> no longer refer to the beginning and end of the image which you put in?</p>
  
  <p>My texture "atlas" is 2048x2048 and the target is 75x75 pixels (it is for a quad) and is offset at (0.0, 0.75) on the texture (I've verified this on an output image).</p>
  
  <p>Therefore I presumed that <code>tCoordScale</code> should be 75 / 2048 = (0.0366, 0.0366) approx. (given that my input texture coordinates are (0.0, 0.0) (1.0, 0.0) (0.0, 1.0) (1.0, 1.0) ) and that <code>tCoordOffset</code> should simply be (0.0, 0.75).</p>
  
  <p>However it only appears approximately correct when <code>tCoordScale</code> is around (0.000045, 0.000045) (about 814 times smaller in each direction than what I would expect to need for a 75/2048 scaling) and when <code>tCoordOffset</code> is about (0.018, 0.768) (i.e. when I add about <code>0.018</code> to the value of both the <code>x</code> and <code>y</code> offset values).</p>
  
  <p>I have verified this both by sending the values in as uniforms, and hard-coding the same values in the shader itself. (I'm sending the 2048x2048 image in via <code>GLUtils.texImage2D</code> on Android).</p>
  
  <p>What is happening here? Why does it only accept these modified values? How do I ensure that I am always able to scale and translate the texture coordinates exactly correctly within my texture atlas, regardless of which device it's on?</p>
  
  <p>Any insights would be appreciated, or any experiences from anyone who has got something like this working.</p>