简体   繁体   中英

OpenGL ES textures won't show up on some devices

I have a problem. So I just put my first game on Google Play. It's a simple bridge building game.

Myself I have Samsung Galaxy S Plus running on Android 2.3.6. The game worked well on my phone, Sony Xperia Ray, HTC Gratia and Samsung Ace.

After putting my game on the market I got few responses telling me that players see nothing but white boxes which means textures don't work for some reason. These phones were LG Optimus Black and Samsung Galaxy S running on 4.0.4. So Android 4.0.4 is custom for Samsung Galaxy S because they didn't release official one.

What to do? All my images are 24 bit PNG and all are power of 2.

Here's how I load them:

/** @return texture ID
*/
public int loadTexture(int resource)
{
    int[] temp = new int[1];
    gl.glGenTextures(1, temp, 0);

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
            resource);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, temp[0]);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    // recycle the bitmap
    bitmap.recycle();

    return temp[0];
}

And here's the link: https://play.google.com/store/apps/details?id=com.fizzicsgames.trainthemtocrosslite

Oh, I'm so stupid. So the problem was caused by placing textures in drawable folder instead of drawable-nodpi. Textures worked correcly on MDPI devices and on devices which support non power of 2 textures (like my Galaxy S Plus). Even though textures were power of 2, they got scaled depending on the screen. So always place your textures in drawable-nodpi if you don't want to create a copy for each of the resolutions.

Are you using any shaders?

If so, float precision could lead to h/w dependent troubles. eg: a float uniform with 1.0 inside it might give you 0 on int(); as if it had 0.99 instead of 1.0.

Post your shader code if you are using it.

The problem is glGenTextures . My advice is don't use it and instead implement your own system of keeping track of textures.

If you debug and check out the values glGenTextures is putting into temp[0] , you'll see on on some devices it will give you a nice sensible 1, 2, 3 each time you call it. Debug the same code on the other devices and you might see 1000056737, -31895888, etc

Edit eg:

public class Texture{

    public static int texturecount = 0;


    public void loadTexture(Context context){


        int[] textures = new int[1];
        textures[0] = Texture.texturecount;
        Texture.texturecount.texturecount++;

        //...and bind it to our array
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

        // etc

    }
}

Not a very elegant solution, also probably not thread safe, but it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM