Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top