How to bind the shaders with two or more textures? OpenGL ES

I know how to create a single texture, how to bind one texture with a shaders. But I don’t know how to bind two or more texture with the shaders, and for example how to show one texture over another texture.

I know how to modify the shaders for example in fragment shader I add the new code:

gl_FragColor = texture2D(a_texture, v_texCoord) + texture2D(a_texture2, v_texCoord2);

As I mentioned what I do not know is how to link the textures with the shaders or if I need to add other bytebuffer.

In this function I create my single texture:


public void CrearTextura(int[] texture) {
        glGenTextures(1, texture, 0);

        int id = mContext.getResources().getIdentifier("drawable/textureatlas", null, mContext.getPackageName());

        Bitmap bmp0 = BitmapFactory.decodeResource(mContext.getResources(), id);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture[0]);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        texImage2D(GL_TEXTURE_2D, 0, bmp0, 0);

        bmp0.recycle();
    }

This is the code that I do not know how it works and how I should modify it.


private void Render(float[] m) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        int mPositionHandle = glGetAttribLocation(Shader.program_Image, vPosition);
        glVertexAttribPointer(mPositionHandle, 3, GL_FLOAT, false, 0, vertexBuffer);
        glEnableVertexAttribArray(mPositionHandle);

        int mTexCoordLoc = glGetAttribLocation(Shader.program_Image, atexCoord);
        glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, false, 0, uvBuffer);
        glEnableVertexAttribArray(mTexCoordLoc);

        int mtrxhandle = glGetUniformLocation(Shader.program_Image, matrix);
        glUniformMatrix4fv(mtrxhandle, 1, false, m, 0);

        int mSamplerLoc = glGetUniformLocation(Shader.program_Image, aTexture);

        glUniform1i(mSamplerLoc, 0);
}

in fragment shader:

uniform sampler2D u_Texture1;
uniform sampler2D u_Texture2;
(...)
gl_FragColor = texture2D(u_Texture1, v_TexCoord1) + texture2D(u_Texture2, v_TexCoord2);

in app:


mTex1Handle  = glGetUniformLocation( ProgramHandle, "u_Texture1");
mTex2Handle  = glGetUniformLocation( ProgramHandle, "u_Texture2");
(...)
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTex1Handle);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mTex2Handle);

thats not correct

it has to be:


GLint location1  = glGetUniformLocation( ProgramHandle, "u_Texture1");
GLint location2  = glGetUniformLocation( ProgramHandle, "u_Texture2");

GLint textureunit1 = 12;
GLint textureunit2 = 19;
// just 2 random numbers within the range [0; max_texture_units)

glProgramUniform1i(the_program, location1, textureunit1);
glProgramUniform1i(the_program, location2, textureunit2);

glActiveTexture(GL_TEXTURE0 + textureunit1);
glBindTexture(GL_TEXTURE_2D, mTexture1);
glActiveTexture(GL_TEXTURE0 + textureunit2);
glBindTexture(GL_TEXTURE_2D, mTexture2);

or the other way around, setting the uniforms first (via glUniform1i(…)), then binding the textures to those explicitly set values (“texture units”)

another way is to preset the binding points (texture units) directly in the shader source code:
https://www.khronos.org/opengl/wiki/Layout_Qualifier_(GLSL)#Binding_points

glActiveTexture(…) calls can be omitted by using:
glBindTextureUnit(unit, texture)

Oh yes, sorry, I meant

mTex1Handle  = glGetUniformLocation( ProgramHandle, "u_Texture1");
mTex2Handle  = glGetUniformLocation( ProgramHandle, "u_Texture2");
(...)
glUniform1i(mTex1Handle, 0);
glUniform1i(mTex2Handle, 1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTex1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mTex2);

[QUOTE=Utumno;1291829]Oh yes, sorry, I meant

mTex1Handle  = glGetUniformLocation( ProgramHandle, "u_Texture1");
mTex2Handle  = glGetUniformLocation( ProgramHandle, "u_Texture2");
(...)
glUniform1i(mTex1Handle, 0);
glUniform1i(mTex2Handle, 1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTex1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mTex2);

[/QUOTE]

Thanks, I just have one question, how the vertex shader have to look?

The same like I wrote in post no. 2