Search code examples
openglglslsfml

Alternative to texelFetch?


I'm getting into GLSL and need some help with texture lookups. I'm trying to use a texture for storage but I cannot get "proper" texture lookups. I would prefer using the usual texture2D method (using GLSL 1.2) but the results are not good enough.

Using texture2D: one

Using texelFetch: two

Now obviously something is wrong with the first one. Here is what I am trying to do (yes sizes are correct unless there is something I don't know about):

vec4 texelFetch(sampler2D tex, ivec2 size, ivec2 coord)
{
    return texture2D(tex, vec2(float(coord.x) / float(size.x), 
                               float(coord.y) / float(size.y)));
}

So, how would this be done properly?


Solution

  • This has become some sort of FAQ:

    I answered it here

    https://stackoverflow.com/a/5879551/524368

    and here

    https://stackoverflow.com/a/7272871/524368

    and in a few other places as well.

    texture2D(tex, (2 * uv + vec2(1.))/2 * u_texsize);