<p>Yes, the <code>0</code> in there represents the <strong>texture unit that the sampler references</strong>. So in order for a texture to be referenced by <code>textureVar</code> in the shader, it should be bound to texture unit 0 (e.g. by <code>glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, ...)</code> or any other means you can bind a texture to a texture unit.</p>

<p>However, note that it <strong>doesn't actually matter which texture unit is active when setting that uniform, nor what texture is bound to that unit</strong>. Effectively to the outside world the sampler uniform is just an integer value specifying the texture unit to take its texture and sampler information from. It can be set to that texture unit whenever you want and the texture can be bound to that unit whenever you want (in fact in GLSL 4.20+ you can actually initialize the uniform right in the shader with e.g. <code>layout(binding=0) uniform sampler2D textureVar</code>). All that matters is that at the point the shader gets invoked (i.e. when actually <code>glDraw...</code>ing something) the sampler is set to the proper unit and the texture is bound to that unit.</p>

<p>For more information on the matter, take a look at the <a href="https://www.khronos.org/opengl/wiki/Sampler_(GLSL)#Binding_textures_to_samplers" rel="nofollow noreferrer">corresponding section in the OpenGL Wiki</a>.</p>

CC BY-SA 3.0
2017-05-06T19:04:47.600Z
2017-05-07T07:51:13.510Z
2017-05-07T07:51:13.510Z
Christian Rau
<p>As was stated by @PaulHK, the last parameter represents the Texture Unit the texture is bound to.</p>

<p>For example:</p>

<pre><code>GLuint unit = 0;

glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(shaderRef, "texture1"), unit);

unit = 1;

glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, texture2);
glUniform1i(glGetUniformLocation(shaderRef, "texture2"), unit);
</code></pre>

<p>The above code binds Texture Unit 0 to the fragment shader and then sends it texture1. Then it binds Texture Unit 1 to the shader and sends it texture2. The <code>unit</code> var represents the Texture Unit we are binding to the <code>sampler</code> in the shader to.</p>

CC BY-SA 3.0
2017-05-06T16:33:54.397Z
@NicolBolas https://open.gl/textures states that samplers in the fragment shader are bound to texture units so wouldn't it make sense to use GL_TEXTURE0 and so on to make the code more readable
Archmede
CC BY-SA 3.0
2017-05-06T17:34:26.883Z
@NicolBolas Does my answer look correct now?
Archmede
CC BY-SA 3.0
2017-05-05T15:35:38.160Z
2017-05-06T16:53:38.740Z
2017-05-06T16:53:38.740Z
Archmede
<p>I've been following the <a href="https://learnopengl.com/code_viewer.php?code=advanced-lighting/normal_mapping" rel="nofollow noreferrer">LearnOpenGL</a> tutorials and I've come across a piece of code I don't understand.</p>

<pre><code>glUniform1i(glGetUniformLocation(ShaderID, "textureVar"), 0);
</code></pre>

<p>In the above code <em>ShaderID</em> is a <em>GLuint</em> which references a shader.</p>

<p><em>textureVar</em> is a <em>sampler2D</em> in the shader.</p>

<p>I don't understand the last parameter in that line of code. Does the <em>0</em> represent <strong>GL_TEXTURE0</strong> or what exactly does the last parameter mean?</p>

CC BY-SA 3.0
2017-05-05T07:07:10.187Z
Yes the 0 is for GL_TEXTURE0. Don't forget to bind the texture object to texture unit 0 also.
PaulHK
CC BY-SA 3.0
2017-05-05T15:11:09.260Z
This is all well-covered in the [first tutorial on texturing on that very site](https://learnopengl.com/#!Getting-started/Textures).
Nicol Bolas
<opengl><shader><texture><glsl>
Send Texture to Shader
Archmede
# Send Texture to Shader

(Asked by: username_0 on 2017-05-05)

I've been following the [LearnOpenGL](https://learnopengl.com/code_viewer.php?code=advanced-lighting/normal_mapping) tutorials and I've come across a piece of code I don't understand.

```
glUniform1i(glGetUniformLocation(ShaderID, "textureVar"), 0);

```

In the above code *ShaderID* is a *GLuint* which references a shader.

*textureVar* is a *sampler2D* in the shader.

I don't understand the last parameter in that line of code. Does the *0* represent **GL\_TEXTURE0** or what exactly does the last parameter mean?

**Tags:** opengl, shader, texture, glsl

> **Comments:**
> **username_1**: Yes the 0 is for GL_TEXTURE0. Don't forget to bind the texture object to texture unit 0 also.  
> **username_2**: This is all well-covered in the [first tutorial on texturing on that very site](https://learnopengl.com/#!Getting-started/Textures).  

---

## 2 answers

### Answer by username_3 on 2017-05-06. Score: 2 | Accepted answer

Yes, the `0` in there represents the **texture unit that the sampler references**. So in order for a texture to be referenced by `textureVar` in the shader, it should be bound to texture unit 0 (e.g. by `glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, ...)` or any other means you can bind a texture to a texture unit.

However, note that it **doesn't actually matter which texture unit is active when setting that uniform, nor what texture is bound to that unit**. Effectively to the outside world the sampler uniform is just an integer value specifying the texture unit to take its texture and sampler information from. It can be set to that texture unit whenever you want and the texture can be bound to that unit whenever you want (in fact in GLSL 4.20+ you can actually initialize the uniform right in the shader with e.g. `layout(binding=0) uniform sampler2D textureVar`). All that matters is that at the point the shader gets invoked (i.e. when actually `glDraw...`ing something) the sampler is set to the proper unit and the texture is bound to that unit.

For more information on the matter, take a look at the [corresponding section in the OpenGL Wiki](https://www.khronos.org/opengl/wiki/Sampler_(GLSL)#Binding_textures_to_samplers).


---

### Answer by username_0 on 2017-05-05. Score: 1

As was stated by @username_1, the last parameter represents the Texture Unit the texture is bound to.

For example:

```
GLuint unit = 0;

glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(shaderRef, "texture1"), unit);

unit = 1;

glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, texture2);
glUniform1i(glGetUniformLocation(shaderRef, "texture2"), unit);

```

The above code binds Texture Unit 0 to the fragment shader and then sends it texture1. Then it binds Texture Unit 1 to the shader and sends it texture2. The `unit` var represents the Texture Unit we are binding to the `sampler` in the shader to.

> **Comments:**
> **username_0**: @username_2 https://open.gl/textures states that samplers in the fragment shader are bound to texture units so wouldn't it make sense to use GL_TEXTURE0 and so on to make the code more readable  
> **username_0**: @username_2 Does my answer look correct now?  

---

