title: Why does rendering and sampling from texture need an offset?
body: <p>I have 2 similar versions of a vertex shader that produce slightly different textures.</p>
  
  <p>The first such version is the following:</p>
  
  <pre><code>#version 450
  
  layout(location = 0) in vec3 position; //(x,y,z) coordinates of a vertex
  layout(location = 1) in vec3 norm; //a 3D vertex representing the normal to the vertex 
  layout(location = 2) in vec2 texture_coordinate; // texture coordinates
  
  layout(std430, binding = 3) buffer instance_buffer
  {
      vec4 cubes_info[];//first 3 values are position of object 
  };
  
  out vec3 normalized_pos;
  out vec3 normal; 
  out vec2 texture_coord;
  
  uniform float width = 128;
  uniform float depth = 128;
  uniform float height = 128;
  
  void main()
  {
      texture_coord = texture_coordinate; 
      vec4 pos = (vec4(position, 1.0) + vec4(vec3(cubes_info[gl_InstanceID]),0));
  
      pos+=ivec4(1,1,0,0);
  
      pos.x = (2.f*pos.x-width)/(width);
      pos.y = (2.f*pos.y-depth)/(depth);
  
      pos.z = 0.9;
  
      gl_Position = pos;
  
      normalized_pos = vec3(pos);
  
      normal = normalize(norm);
  }
  </code></pre>
  
  <p>Which produces:</p>
  
  <p><a href="https://i.stack.imgur.com/UDptG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UDptG.png" alt="enter image description here"></a></p>
  
  <p>Look at the border, especially at the bottom and notice some isolated pixels near the botom left.</p>
  
  <p>Now the following version:</p>
  
  <pre><code>#version 450
  
  layout(location = 0) in vec3 position; //(x,y,z) coordinates of a vertex
  layout(location = 1) in vec3 norm; //a 3D vertex representing the normal to the vertex 
  layout(location = 2) in vec2 texture_coordinate; // texture coordinates
  
  layout(std430, binding = 3) buffer instance_buffer
  {
      vec4 cubes_info[];//first 3 values are position of object 
  };
  
  out vec3 normalized_pos;
  out vec3 normal; 
  out vec2 texture_coord;
  
  uniform float width = 128;
  uniform float depth = 128;
  uniform float height = 128;
  
  void main()
  {
      texture_coord = texture_coordinate; 
      vec4 pos = (vec4(position, 1.0) + vec4(vec3(cubes_info[gl_InstanceID]),0));
  
     // pos+=ivec4(1,1,0,0); &lt;- Look here, we're not offsetting anymore
  
      pos.x = (2.f*pos.x-width)/(width);
      pos.y = (2.f*pos.y-depth)/(depth);
  
      pos.z = 0.9;
  
      gl_Position = pos;
  
      normalized_pos = vec3(pos);
  
      normal = normalize(norm);
  }
  </code></pre>
  
  <p>Produces the image:
  <a href="https://i.stack.imgur.com/WSr05.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WSr05.png" alt="enter image description here"></a></p>
  
  <p>Unsurprisingly when we do not add that offset the image is shifted (the pixels at the bottom are gone and we know have an empty border on the top and right edges). My question is, why is that offset necessary to begin with?</p>
  
  <p>Why doesn't the mapping </p>
  
  <pre><code>    pos.x = (2.f*pos.x-width)/(width);
      pos.y = (2.f*pos.y-depth)/(depth);
  </code></pre>
  
  <p>Directly calculate the correct texel?</p>
  
  <p>Edit:</p>
  
  <p>Some clarifications based on a comment.</p>
  
  <p>The images you are seeing are layers of 3D dimensional texture. Although it may vary we can assume that the x,y coordinates will be on the range [0,width-1] and [0,depth-1] (in this case 127 for both).</p>