1
votes

Let's say I've three different texture objects with different image data on them. These three texture objects are all bound to texture unit 0.

I've three models in my scene and each uses one of these textures.

In my fragment shader I've a sampler2D variable. And all models use the same shader program for rendering.

Now I do the following operations in sequence

  1. Bind texture object 1
  2. Render model 1

  3. Bind texture object 2

  4. Render model 2

  5. Bind texture object 3

  6. Render model 3

What is want to ask is this the way you texture different models with different textures and does the sampler in fragment shader reads data from currently bound texture. What happens if a texture is bound to a different texture unit, do I need a different sampler for that.

1

1 Answers

2
votes

A sampler always reads from the currently bound texture at the texture unit it is set to. Since the default active texture unit is unit 0 and the default value of a sampler is also 0 your code should work unless you change either the sampler or the active texture unit.

If you bind the texture to another texture unit, you have to make sure that the active texture unit (glActiveTexture) matches with the value set for the sampler (glUniform1i). If you bind, For example, the texture to GL_TEXTURE2, then you have to set your sampler to glUniform1i(sampler_location, 2).