1

I am using code from How can I pass multiple textures to a single shader?. It works fine until I load and bind new textures after my bumpmap image. I only can get this to work if my bumpmap is the last bound and loaded texture. Even if all my images are the same size. Here's my code...

public int[] textureIDs = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //texture image ID's
//load bitmap and bind texture (done 10 times) textureIndex is 1-10

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imageiD);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIDs[textureIndex]);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);  
bitmap.recycle();

//render

shaderProgram = GraphicTools.sp_ImageBump;  
GLES20.glUseProgram(shaderProgram);

t1 = GLES20.glGetUniformLocation(shaderProgram, "u_texture");
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, globals.textureIDs[1]);//textureIndex
GLES20.glUniform1i(t1, 0);

t2 = GLES20.glGetUniformLocation(shaderProgram, "u_bumptex");
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, globals.textureIDs[2]);//bumpMapIndex);
GLES20.glUniform1i(t2, 1);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);//added this, it allows me to pass 2 textures to the shaders, otherwise TEXTURE1 is black

//fragment shader

precision mediump float;

uniform sampler2D u_bumptex;
uniform sampler2D u_texture;

varying vec2 v_texCoord;

void main()
{
    vec4 bumpColor = texture2D(u_bumptex, v_texCoord);//v_bumpCoord);// get bump map color, just use green channel for brightness

    gl_FragColor = texture2D(u_texture, v_texCoord) * bumpColor.g;
}
Normfly
  • 11
  • 3
  • public int[] textureIDs = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; //texture image ID's GLES20.glGenTextures(globals.textureIDs.length, globals.textureIDs, 0); // Generate texture-ID array thx – Normfly Oct 21 '18 at 17:43
  • I can switch textures successfully when passing a single texture to the shader. I can only pass the last texture loaded to the second shader texture(bump map). – Normfly Oct 21 '18 at 17:46
  • Ya, thanks. I changed it to GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIDs[textureIndex]. But, that's not my issue. I can't change TEXTURE1 to anything but the last loaded texture. I ActivateTexture(TEXTURE1) bindTexture then ActivateTexture(TEXTURE0) bindTexture. The second bindTexture changes the first binding, and my shader sampler2D variable is all black. Thanks again for input, this is so frustrating. – Normfly Oct 21 '18 at 21:03
  • Sorry, but I can't find any obvious issue in your code. What happens if you bind the bump texture second, but activate texture unit 0 after binding both textures? - I mean a single `GLES20.glActiveTexture(GLES20.GL_TEXTURE0)` after binding both textures. – Rabbid76 Oct 21 '18 at 21:18
  • Thanks again. That helps. It allows me to pass 2 textures to the shader. But if I load more textures after my bump map texture, black screen. I changed the code above. – Normfly Oct 21 '18 at 22:03
  • You are a genius. That did it. I don't understand why that works. But, I added GLES20.glActiveTexture(GLES20.GL_TEXTURE0) at the end of my onDrawFrame and wala. – Normfly Oct 22 '18 at 00:08
  • Do you generate, bind and/or upload the textures every frame? Note OpenGL is a state machine. If you change a state like the active texture unit, it is kept even till the next frame. So if you "leave" a frame with active texture unit 1 the state in the next frame is texture unit 1. Probably you have to switch to texture unit 0 at the begin of the frame. But I recommend to generate and load the textures only once. – Rabbid76 Oct 22 '18 at 04:58

0 Answers0