在Android中加载压缩纹理ETC1

时间:2012-09-28 20:15:13

标签: android opengl-es textures

我知道有CompressedTextureActivity示例,我将代码基于它,但我无法使其工作。 需要一些帮助:

public static int loadCompressedTexture(final Context context, final int resourceId){
        final int[] textureHandle = new int[1];
        GLES20.glGenTextures(1, textureHandle, 0);

        if (textureHandle[0] != 0){
            InputStream input = context.getResources().openRawResource(resourceId);

            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

            try{
                ETC1Util.loadTexture(GLES20.GL_TEXTURE_2D, 0, 0,GLES20.GL_RGB, GLES20.GL_UNSIGNED_SHORT_5_6_5, input);
            }
            catch(IOException e){
                System.out.println("DEBUG! IOException"+e.getMessage());
            }
            finally{
                try {
                    input.close();
                } catch (IOException e) {
                    // ignore exception thrown from close.
                }
            }       
            //GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);

            checkGlError("");
        }
        else
            throw new RuntimeException("Error loading texture.");

        return textureHandle[0];
    }

loadTexture抛出IOException,结果是黑度......


更新:我有没有标题的pkm文件。出于某种原因,我认为它们应该以这种方式编码,但现在我用头编码它们并且不再出现IOException错误。顺便说一句,我使用的是etc1tool,源png文件是2048x2048。

然而,我一直在变黑......

顺便说一句,这是我用来加载普通纹理的代码,它显示得很好:

public static int loadTexture(final Context context, final int resourceId)
{
    final int[] textureHandle = new int[1];

    GLES20.glGenTextures(1, textureHandle, 0);

    if (textureHandle[0] != 0)
    {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;   // No pre-scaling
        final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_NEAREST);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

        GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);

        logHeap();

        bitmap.recycle();                       
    }

    if (textureHandle[0] == 0)
    {
        throw new RuntimeException("Error loading texture.");
    }

    return textureHandle[0];
}

1 个答案:

答案 0 :(得分:0)

即使我认为我已经设置了这样的mipmap:

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

并没有生成mipmap,这两行代码是,天知道为什么,在我的对象的draw方法中。

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,  GLES20.GL_LINEAR_MIPMAP_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,  GLES20.GL_LINEAR);

把它们拿出来解决了这个问题!