Search code examples
androidopengl-estextures

Loading compressed texture ETC1 in Android


I know there is the CompressedTextureActivity example, and I based my code on it, but i can't make this work. Needing some help here:

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 is throwing an IOException, and the result is blackness...


Update: I had pkm files with no headers. For some reason I thought that they should be encoded that way, but now I encoded them with headers and don't get the IOException error anymore. By the way, I'm using etc1tool, and the source png file is 2048x2048.

However, I keep getting blackness...

By the way, this is the code I use to load normal textures, which display just fine:

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];
}

Solution

  • Even thought I had set the mipmaps like this:

    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);
    

    And was not generating mipmaps, these 2 lines of code were, god knows why, in the draw method of my object.

    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);
    

    Taking them out solved the issue!