Reputation: 282
I'm dealing with texture in fragment shader and I use a sampler2D
to get the 2D texture but I do not pass through the value in OpenGL program. However, the value of that sampler2D is correct.
I feel really confused about this and any explanation about this?
Upvotes: 2
Views: 1509
Reputation: 54592
The default value of a sampler variable is 0. From the GLSL 3.30 spec, section "4.3.5 Uniforms":
The link time initial value is either the value of the variable's initializer, if present, or 0 if no initializer is present. Sampler types cannot have initializers.
Since a value of 0 means that it's sampling from texture unit 0, it will work without ever setting the value as long as you bind your textures to unit 0. This is well defined behavior.
Since texture unit 0 is also the default until you call glActiveTexture()
with a value other than GL_TEXTURE0
, it's very common to always use unit 0 as long as shaders do not need more than one texture. Which means that often times, setting the sampler uniforms is redundant for simple applications.
I would still prefer to always set the values. If nothing else, it makes it clear to anybody reading your code that you really mean to sample from texture unit 0, and did not just forget to set the value.
Upvotes: 5