【问题标题】:PNG Textures not loading on HTC desirePNG纹理没有加载到HTC的愿望上
【发布时间】:2010-05-19 18:57:34
【问题描述】:

您好,我正在使用 OpenGL es 为 android 开发游戏,但遇到了问题:

我的游戏在模拟器中加载良好(windows xp 和来自 eclipse 的 vista),它在 T-Mobile G2(HTC Hero)上也加载良好,但是当我在我的新 HTC Desire 上加载它时,似乎没有加载任何纹理正确(或根本)。我怀疑 BitmapFactory.decode 方法,尽管我没有证据表明这是问题所在。

我所有的纹理都是 2 的幂,而且 JPG 纹理似乎可以加载(尽管它们看起来质量不太好),但是任何 GIF 或 PNG 都不会加载,除了加载正常的 2x2 红色方块以及一个映射到 3d 对象但似乎用最接近的颜色填充网格的每个三角形的纹理)。

这是我加载图片的代码:

     AssetManager am = androidContext.getAssets();
  BufferedInputStream is = null;
  try {
     is = new BufferedInputStream(am.open(fileName));

     Bitmap bitmap;

     bitmap = BitmapFactory.decodeStream(is);

     GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
     bitmap.recycle();
  } catch(IOException e) {
     Logger.global.log(Level.SEVERE, e.getLocalizedMessage());
  } finally {
     try {
        is.close();
     } catch(Exception e) {
        // Ignore.
     }
  }

谢谢

【问题讨论】:

    标签: java android opengl-es textures


    【解决方案1】:

    大小实际上是纹理的导入,例如我使用了一个矩形作为 BitmapFactory.decodeStream 的 0、0、1024、1024 的参数。当然它必须是 0、0、1023、1023。对于参考看看下面的代码,我在Desire S和Galaxy S2上测试过:

        InputStream is = context.getResources().openRawResource(resource);
        Bitmap bmp;
    
        gl.glBindTexture(GL10.GL_TEXTURE_2D, texID[tex]);
    
        // Mendatory, tells openGL how to render the texture, nearest will look sharp, smooth will look blurry
        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
    
        // Not mendatory, tells openGL what to do when sprite is smaller or bigger than object
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
    
        gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
    
        try {
            BitmapFactory.Options sBitmapOptions  = new BitmapFactory.Options();
            // Set our bitmaps to 16-bit, 565 format...uhm, this looks more like 32 bit: 8888
            sBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bmp = BitmapFactory.decodeStream(is, new Rect(0, 0, 1023, 1023), sBitmapOptions);
    
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
            bmp.recycle();
        } finally {
            try {
                is.close();
            } catch(IOException e) {
                // Ignore.
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 2017-02-25
      • 2015-11-05
      • 1970-01-01
      相关资源
      最近更新 更多