3

my question regards how a texturecoordinate exactly maps to texel in a texture (not taking into consideration filtering).

does (0,0) for example refere to the top left corner of the top left pixel or does it refere to the center of the top left pixel?

if using near-filtering, what pixel is fetched at the texture coordinate (0.5,0.5)?

what about (1.0,1.0), does it refere to the bottom-right corner of the bottom-right pixel or rather the top-left corner of the bottom-right pixel? (i can recall that some years ago this differed between some ATI and NVIDIA cards. is it now standardized?)

if using texelFetch() instead of textureLod(), what's the correct way to access the pixel?

is it

ivec2 size = textureSize(sampler,0);
ivec2 iCoords = ivec2(int(uv.x*size.x),int(uv.y*size.y));
vec4 col = texelFetch(sampler,iCoords);

or is maybe

ivec2 iCoords = ivec2(int(uv.x*size.x+0.5f),int(uv.y*size.y+0.5f));

correct?

or even

ivec2 iCoords = ivec2(int(uv.x*(size.x+1)),int(uv.y*(size.y+1)));

?

maybe someone can clarify how the mapping exactly works?

genpfault
  • 51,148
  • 11
  • 85
  • 139
matthias_buehlmann
  • 4,641
  • 6
  • 34
  • 76
  • This is a FAQ. I did answer it several times here on StackOverflow.com http://stackoverflow.com/a/7272871/524368 http://stackoverflow.com/a/5879551/524368 – datenwolf Mar 22 '12 at 12:33

1 Answers1

6

If your question is simply posed out of curiosity: AFAIK there is no standard answer to that question, since your texture coordinates don't necessesary have to end at 1.0. Also the grafic cards will process the values in a different manner (some graphic cards today support 64bit FP-Ops, for example). I think you should always try to code on the safe side, and maybe use 0.999 instead of 1.0, if applicable.

Otherwise, if you are looking for a way to read back pixels from a rendered texture, you can define your viewport as described on the website of dominik göddeke on gpgpu. If you set the viewport to the size of the texture you can simply grab back the pixels at their original coordinates. I really recommend the site, it helped me a lot.

UPDATE: The texture coordinates are interpolated between the first and the last pixel in the texture when using GL_REPEAT. So the center of the first pixel in a 4 texel texture is at 0.125, because each pixel has the width of 0.25 or 1/4.

This explains it more visually: Texture interpolation on 4 texels

As you can see, the texel at [0,0] is the linear mix of [0.125,0] and [0.875,0] and equal to [1,1] in this example.

All that is very well explained on the DelphiGL Wiki (english translation)

I'm sorry for the confusion at first.

devsnd
  • 7,382
  • 3
  • 42
  • 50
  • so, if it referes to the center of the pixel, then for example (-0.0001,-0.0001) will still refere to the topLeftPixel? (assuming that texturewrapping is activated and the texture is small) – matthias_buehlmann Mar 22 '12 at 11:40
  • mmmh - but if this was true, and u would create a quad with uv coordinates (0,0) to (1,1) on the corner vertices and texture it, then u would see only half of the texture's border texels.... – matthias_buehlmann Mar 22 '12 at 11:44
  • It dependson the `GL_TEXTURE_WRAP_S/T` parameter. If you have enabled `GL_REPEAT` and `GL_LINEAR`, then (-0.0001,-0.0001) would be minimally interpolated with (texsize_x, texsize_y). If you have enabled `GL_NEAREST` it will still return the texture color at (0,0) – devsnd Mar 22 '12 at 11:48
  • okay - so - if i create a 3x3 texture and texture a quad with it and set interpolation to GL_Linear, then i will always see one full texel in the middle and arround it texels that are cut in half? – matthias_buehlmann Mar 22 '12 at 11:53
  • You are right, I will correct my answer in a sec. – devsnd Mar 22 '12 at 11:59