简体   繁体   English

Android OpenGL ES 2.0纹理无法在某些尺寸下使用

[英]Android OpenGL ES 2.0 Textures not working at some sizes

I just discovered a really bizarre problem with my app. 我刚刚发现我的应用程序存在一个非常奇怪的问题。

This problem occurs when it is run on my phone. 在我的手机上运行时会出现此问题。

I was loading a bitmap (to be used as an Open GL Texture) like so: 我正在像这样加载位图(用作Open GL Texture):

 tiles = BitmapFactory.decodeResource(view.getResources(), R.drawable.tilespic, BMFOptions);

BMFOptions is setting the following line: BMFOptions设置以下行:

BMFOptions.inPreferredConfig = Bitmap.Config.RGB_565;

When I ran my app, what happened initially was that the texture appeared as just a black square. 当我运行我的应用程序时,最初发生的是纹理显示为一个黑色正方形。 So, I tried it on another device (a tablet) and it ran perfectly. 所以,我在另一台设备(平板电脑)上尝试过,它运行得很好。

I know that if the bitmap has an alpha channel, BitmapFactory will just load it as ARGB_8888 if Bitmap.Config.RGB_565 has been specified and I have confirmed this with other bitmaps (by calling getConfig). 我知道,如果位图具有alpha通道,如果已指定Bitmap.Config.RGB_565,并且已与其他位图(通过调用getConfig)确认,则BitmapFactory会将其加载为ARGB_8888。 This bitmap does have Alpha. 此位图确实具有Alpha。

However, when I call getConfig on it, it returns null (only when RGB_565 is specified and only on this one bitmap/texture - on this device). 但是,当我调用getConfig时,它返回null (仅当指定了RGB_565且仅在此设备上的此位图/纹理上)。

What I've tried 我试过的

If I set it to ARGB_8888 or just omit BMFOptions for this bitmap, then everything displays correctly. 如果我将其设置为ARGB_8888或只是省略此位图的BMFOptions,则所有内容都会正确显示。

If I set it to ARGB_4444 (just for testing), then it does display but the textures are crooked/at an angle, (again just this bitmap/texture). 如果我将其设置为ARGB_4444(仅用于测试),则它会显示,但纹理是弯曲的/倾斜的(再次是该位图/纹理)。

So, what I did was resize the original bitmap (original size - 100 x 909). 所以,我所做的是调整原始位图的大小(原始大小 - 100 x 909)。

I resized it to 505 x 201, 506 x 201 and a multitude of other sizes and it still didn't work (Note these are sprite sheets so, these sizes are just how to they worked out). 我将其调整为505 x 201、506 x 201以及其他多种尺寸,但仍然无法正常工作(请注意,这些是Sprite工作表,因此,这些尺寸只是他们的工作方式)。

I then resized it to 506 x 204 and it did work. 然后,我将其尺寸调整为506 x 204,它确实起作用了。 I then tried it at 1880 x 921 and it worked again. 然后,我以1880 x 921进行了尝试,然后再次工作。

I'm not using NPOT textures and have never had a problem before, bear in mind that I also have other textures with odd sizes such as 1880 x 921 and 1000 x 885 and these behave perfectly on this phone (but obviously their pixel data / layout is different). 我没有使用NPOT纹理,以前也从未遇到过问题,请记住,我还具有其他尺寸奇怪的纹理,例如1880 x 921和1000 x 885,这些在手机上表现完美(但显然它们的像素数据/布局不同)。

As this bitmap has alpha (which I need), I am simply removing the BMFOptions for this particular bitmap for now and allowing it to load as ARGB_8888, however, I would like to know any possible reasons for this odd behaviour. 由于此位图具有Alpha(我需要),因此我现在只是删除该特定位图的BMFOptions并允许将其加载为ARGB_8888,但是,我想知道这种奇怪行为的任何可能原因。

If it helps, this is the code I use to load my textures.... 如果它有帮助,这是我用来加载纹理的代码....

    public static int LoadTexture(GLSurfaceView view, Bitmap imgTex){

        int textures[] = new int[1];
        try {

            GLES20.glGenTextures(1, textures, 0);
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);

            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, imgTex, 0);

            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);

        } catch (Exception e){
          }
        textureCount++;     
        return textures[0];
    }

Some phones can only use textures with power of 2 sizes. 某些手机​​只能使用2大小幂的纹理。
eg 256x256, 512x512, 1024x128 will work 例如256x256,512x512,1024x128将起作用
300x300, 512x100, etc. won't work. 300x300、512x100等无法正常工作。

This is a requirement of the OpenGL ES 2.0 standard (afaik 3.0 relaxes this). 这是OpenGL ES 2.0标准的要求(afaik 3.0放宽了这一点)。 Some phones will work with non-pow-of-2 textures, but it's not mandatory to do so. 有些手机可以使用2-pow-of-2纹理,但并非必须这样做。 Sticking to pow-of-2 textures is the safe way to go if you wish to target as much phones as possible. 如果您希望尽可能多地定位手机,那么坚持使用2-pow-of2纹理是一种安全的方法。

If this is not the problem, see if you get any GLErrors . 如果这不是问题,请查看是否有任何GLErrors。

Also i just noticed: you are using GL10.GL_TEXTURE_2D, while in the other lines you are using GLES20.*. 我也注意到:你正在使用GL10.GL_TEXTURE_2D,而在其他行中你正在使用GLES20。*。 I looked it up and GL_TEXTURE_2D is defined as 3553 in both places, but still it would look better if you used GLES20 there as well. 我查了一下,GL_TEXTURE_2D在这两个地方被定义为3553,但如果你在那里使用GLES20,它看起来会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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