Alternative to texelFetch? [duplicate]

2019-02-12 22:51发布

问题:

This question already has an answer here:

  • OpenGL Texture Coordinates in Pixel Space 3 answers

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:

Using texelFetch:

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?

回答1:

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.



回答2:

I would say, use textureRect. Then you can use texture2D, and supply the actual coodinates in pixels that you would like to access.



标签: opengl glsl sfml