<p>OK, I have figured out this problem.</p>

<p>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.</p>

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

<p>Thus, an example of using <code>texelFetch()</code> should be:</p>

<pre><code>vec4 texelValue= texelFetch(texture2D, ivec2(gl_FragCoord.xy), 0);
</code></pre>

CC BY-SA 4.0
2018-06-05T15:06:48.967Z
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
CC BY-SA 4.0
2018-06-04T16:53:46.333Z
2018-06-05T15:06:35.423Z
2018-06-05T15:06:35.423Z
yuchen
<p>I have read the OpenGl documentation of texelFetch, which is:</p>

<p>gvec4 texelFetch(gsampler2D sampler, ivec2 P, int lod).</p>

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

<p>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]? </p>

<p>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...</p>

<p>Could anyone explain this and give a real example of how to use texelFetch?</p>

<p>Thank you!</p>

<opengl><glsl>
How to use GLSL texelFetch?
yuchen
# How to use GLSL texelFetch?

(Asked by: username_0 on 2018-06-03)

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!

**Tags:** opengl, glsl


---

## 1 answers

### Answer by username_0 on 2018-06-04. Score: 21 | Accepted 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);

```

> **Comments:**
> **username_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  

---

