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.

  • $\begingroup$ What is the sampler that is being used with the texture function? Anything but nearest will give an averaged result using a filter. $\endgroup$
    – pmw1234
    Commented Oct 13, 2022 at 12:00
  • 1
    $\begingroup$ Assuming that glsl int (myFloat) rounds to zero, why not pixelCoordinate = int(normalizedCoordinate * 2048) ? $\endgroup$ Commented Oct 13, 2022 at 18:17
  • $\begingroup$ @pmw1234 like I wrote in my question: the sampler is "GL_NEAREST" so color will output the color of the pixel without any interpolation... $\endgroup$
    – Thomas
    Commented Oct 14, 2022 at 6:24
  • $\begingroup$ @leoneruggiero when normalizedCoordinate.x is 1, the output would be 2048, which is outside [0 2047] $\endgroup$
    – Thomas
    Commented Oct 14, 2022 at 6:26
  • $\begingroup$ @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. $\endgroup$ Commented Oct 14, 2022 at 6:38


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.