Search code examples
opengl-estextures

Multiple active Textures opengl


I have one background texture and transparent layer texture for that. When I loaded these texture only _layer3 final texture is active.

I want all the texture should be active so that I know the layer to be proceed further.

   glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture[0]);//texture _floorTexture
    glUniform1i(_textureUniform, 0);
           //glUniform1i(glPrograms[currentProgram].glUniforms[_textureUniform],_floorTexture);

    glActiveTexture(GL_TEXTURE0 + 1);
    glBindTexture(GL_TEXTURE_2D, texture[1]); //_layer0
    glUniform1i(_textureUniform, 1);

    glActiveTexture(GL_TEXTURE0 + 2);
    glBindTexture(GL_TEXTURE_2D, texture[2]); //_layer1
    glUniform1i(_textureUniform, 2);

    glActiveTexture(GL_TEXTURE0 + 3);
    glBindTexture(GL_TEXTURE_2D, texture[3]); //_layer2
    glUniform1i(_textureUniform, 3);

    glActiveTexture(GL_TEXTURE0 + 4);
    glBindTexture(GL_TEXTURE_2D, texture[4]); //_layer3
    glUniform1i(_textureUniform, 4); 

Solution

  • When using multiple textures, one also has to use multiple samplers in the shader. This could look for example like this:

    in vec2 uv;
    
    out vec3 color;
    
    uniform sampler2D floor_texture;
    uniform sampler2D layer0_texture;
    
    void main()
    {
        vec4 f = texture(floor_texture, uv);
        vec4 l0 = texture(layer0_texture, uv);
    
        color = mix(f.rgb, l0.rgb, 1 - l0.a);
    }
    

    Note that the composition depends on what you want to achieve and is just an example here.