Android OpenGL ES2.0 Texture Swapping

2019-02-26 05:35发布

问题:

First off I am new to OpenGL, but on my phone (Motorola Bionic) the following code works as intended.

GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTowerTextureHandle);

GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTowerNormalHandle);

GLES20.glActiveTexture(GLES20.GL_TEXTURE3);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mFrostTextureHandle);

GLES20.glActiveTexture(GLES20.GL_TEXTURE4);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mFrostNormalHandle);

GLES20.glActiveTexture(GLES20.GL_TEXTURE5);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mFireTextureHandle);

GLES20.glActiveTexture(GLES20.GL_TEXTURE6);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mFireNormalHandle);

...
for (int i =0; i < mGame.towers.length; i++)
{
switch (mGame.towers[i].type)
{
case Dart:
    GLES20.glUniform1i(mTextureUniformHandle, 1);  
    GLES20.glUniform1i(mTextureNormalUniformHandle, 2);
  break;
case Frost:
    GLES20.glUniform1i(mTextureUniformHandle, 3);  
    GLES20.glUniform1i(mTextureNormalUniformHandle, 4);
  break;
case Fire:
    GLES20.glUniform1i(mTextureUniformHandle, 5);  
    GLES20.glUniform1i(mTextureNormalUniformHandle, 6);
  break;
}
...
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6); 
}

It will display the correct texture and normal for each tower. On older phones (my friends Incredible) it just display all of the towers with the texture of the last type being drawn. I could just swap in the correct texture to Texture1 for each tower, but isn't that slower and looked down upon?

Does anyone have any information as to why it isn't working on some phones and what I need to do to make it compliant across all OpenGLES20 compatible phones?

回答1:

You're probably hitting some limit of maximum active textures, for example as docs says:

glActivateTexture - The number of texture units is implementation dependent, but must be at least two

Also you might want to check GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (with glGet).

To debug problem you might want to check for glGetError after each call glActiveTexture/glBindTexture and maybe those glUniform1i.