Jump to content
  • Advertisement
Sign in to follow this  

GLSL and texture coords...

This topic is 4013 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, thanks for reading this but I have a question regarding GLSL and texture coords. Say I have the following shader program:
//note, 128.0 is the size of the texture width, hardcoded for now, needs to change
//to uniform variable.
void bilerp(in vec2 coords, out vec4 qtynew, in sampler2D qty)
  //figure out the texture coords
  vec4 st;
  st.xy = coords-1.0/128.0; //implied translation into texture simulation domain.
  st.zw = st.xy + 1.0/128.0; //top right corner of the "neighbor hood"
  //now access the texture
  vec4 t11 = texture2D(qty,st.xy);
  vec4 t21 = texture2D(qty,st.zy);
  vec4 t12 = texture2D(qty,st.xw);
  vec4 t22 = texture2D(qty,st.zw);
  //now lerp
  vec2 factor = (coords - st.xy)*128.0; //Factor is in simulation domain!
  //bilerp, such that, we compute lerp:
  //l1 = 11 to 21, l2 = 12 to 22, lerping long the x axis.
  //final = l1 to l2, lerping along the y axis. Please refer to any documentation on bilerp.
  qtynew = mix(mix(t11,t21,factor.x),mix(t12,t22,factor.x),factor.y);
Where is exactly is the "center" of the texture coord? In Mark Harris' GPU gem article, he uses CG. And he does this by adding a 0.5, which according to his comments is due to the fact that the center of a texel is 0.5, i.e 0.5, 1.5, ... Mark Harris had something like this:
float4 f4texRECTbilerp(samplerRECT tex, float2 s)


  float4 st;

  st.xy = floor(s - 0.5) + 0.5;

  st.zw = st.xy + 1;


  float2 t = s - st.xy; //interpolating factors 


  float4 tex11 = f4texRECT(tex, st.xy);

  float4 tex21 = f4texRECT(tex, st.zy);

  float4 tex12 = f4texRECT(tex, st.xw);

  float4 tex22 = f4texRECT(tex, st.zw);

  // bilinear interpolation

  return lerp(lerp(tex11, tex21, t.x), lerp(tex12, tex22, t.x), t.y);

Notice his offset of 0.5. So how does GLSL translate? I don't notice any difference from toggling between 0.5 and 1.0.

Share this post

Link to post
Share on other sites
The Cg instruction tex2DRECT was used for npot textures. Normal texture coordinates are "percentual", 0.0 = left, 1.0 = right, etc. TexRECT used real pixel coordinates. If you want the center coordinate of a 800x600 texture, the texcoords are not {0.5,0.5}, but {400,300}. So, in this context adding 0.5 really means adding half a pixel.

You are using "normal" tex2D functions, so this trick is not working here. If you shift with 0.5, you will pick a pixel 50% of the image width further. In your case, a half pixel =
0.5 / image.width , 0.5 / image.height


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!