# GLSL normalized texture coordinate to pixel coordinate

I've a texture (2048 x 2048) Pixels. I am looking for an algorithm to transform the normalized texture coordinate [0 1] to pixel coordinate [0 2047]. This should be a perfect fit...

So when the texture sampler has a GL_NEAREST interpolation the following code should output the same:

ivec2 texSize = textureSize(tex);
vec2 normalizedCoordinate = someValue;
ivec2 pixelCoordinate = AlgorithmIamLookingFor;
vec4 color = texture(tex, normalizedCoordinate);
vec4 color2 = texelFetch(tex, pixelCoordinate, 0);


So color and color2 should have the same value.

I've tried the following:

pixelCoordinate.x = int(normalizedCoordinate * 2047)


But the last pixel (2047) will not show up or be extremely small.

• What is the sampler that is being used with the texture function? Anything but nearest will give an averaged result using a filter. Oct 13, 2022 at 12:00
• Assuming that glsl int (myFloat) rounds to zero, why not pixelCoordinate = int(normalizedCoordinate * 2048) ? Oct 13, 2022 at 18:17
• @pmw1234 like I wrote in my question: the sampler is "GL_NEAREST" so color will output the color of the pixel without any interpolation... Oct 14, 2022 at 6:24
• @leoneruggiero when normalizedCoordinate.x is 1, the output would be 2048, which is outside [0 2047] Oct 14, 2022 at 6:26
• @Thomas Have you tried? As far as I know if a primitive ends up in a pixel it means it crosses the pixel center and texture coordinates are evaluated at the pixel center for that primitive, so the only way for them to be exactly 1 is that the vertex with texCoords=1 is exactly at the center of a pixel...in that case clamp should solve the issue. Oct 14, 2022 at 6:38