# How to use GLSL texelFetch?

I have read the OpenGl documentation of texelFetch, which is:

gvec4 texelFetch(gsampler2D sampler, ivec2 P, int lod).

I know the first argument "sampler" is just a texture. But I am very confused about the second and third arguments.

According to the documentation, the second argument "P" is a texture coordinate. But it has the type ivec2, which means that it is a vector of integers. Shouldn't a texture coordinate be in the range [0, 1]?

And for the third argument "lod", in the documentation, it said "If present, specifies the level-of-detail within the texture from which the texel will be fetched." I have no idea how to set this argument...

Could anyone explain this and give a real example of how to use texelFetch?

Thank you!

## 1 Answer

OK, I have figured out this problem.

For the texture coordinates, there are two kind of texture coordinates. One is normalized texture coordinates, which is in the range [0, 1]. The other is texel space, which is in the range [0, size), where size is the size of the texture. For texelFetch(), the texel space is used.

For lod, it means the level of detail in the mipmap. We can simply use 0 for the base level (the original size).

Thus, an example of using texelFetch() should be:

vec4 texelValue= texelFetch(texture2D, ivec2(gl_FragCoord.xy), 0);

• Many thanks to @NicolBolas who helped editting the format. I am new to StackOverflow. After doing another editting, I noticed that your name was gone... Didn't mean to do that – Yuchen Jun 5 '18 at 15:06
• Don't forget to also accept your answer! – bernie Jun 11 '18 at 14:56