2

I am trying to understand this code:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture1"), 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture2"), 1);

This is the related shader code:

#version 330 core
...

uniform sampler2D ourTexture1;
uniform sampler2D ourTexture2;

void main()
{
    color = mix(texture(ourTexture1, TexCoord), texture(ourTexture2, TexCoord), 0.2);
}

So, as far as I understand, after activating GL_TEXTURE0 we bind texture1 to it. My understanding is that this binds texture1 to first sampler2d.The part I dont understand is, why do we need to use glUniform call.

yasar
  • 13,158
  • 28
  • 95
  • 160
  • I tried explaining these concepts, and some more, in a detailed answer here: http://stackoverflow.com/questions/30960403/multitexturing-theory-with-texture-objects-and-samplers. – Reto Koradi Oct 27 '15 at 06:12

1 Answers1

1

It's an indirection. You choose the texture that is input at location GL_TEXTURE0 then you tell the uniform in your shader to fetch its texture from that same location. It's kind of like this (apologies for the diagram).

The first row is texture unit locations and the second row is shader uniform locations. You may want to bind texture unit 4 to shader sampler 2, for example.

enter image description here

(DatenWolf will be along in a moment to correct me :).

Robinson
  • 9,666
  • 16
  • 71
  • 115