Understanding of textures

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.

Sampler uniforms are associated with texture units by using glUniform1i() to assign the texture unit number to the uniform.

You can either bind each texture to a different texture unit, and modify the uniform to refer to the appropriate texture unit before each draw call, or use a single texture unit, binding the appropriate texture to it.

If you need to use multiple textures within a single draw call, you can use an array of samplers; however, expressions used to index the array must be dynamically-uniform, i.e. involving only uniform variables or (in the the fragment shader) [var]flat[/var]-qualified inputs, and all samplers in the array must be of the same basic type (e.g. sampler2D, usampler2D, samplerCube, etc).

Alternatively, you can use array textures (sampler2DArray etc); these don’t have the dynamically-uniform restriction, but the layers of an array must have the same type and dimensions, and sampling parameters (filters, wrap mode) are set the entire texture rather than individual layers.