前面介绍了OpenGL ES2.0 支持的压缩ETC1;下面我们来学习一下ETC2。
从opengl es3.0 开始支持ETC2纹理压缩,而这种压缩可以支持透明通道。
下面看如何使用
加载pkm文件
public static byte[] loadDataFromAssets(String fname, Resources r){
byte[] data=null;
InputStream in=null;
try
{
in = r.getAssets().open(fname);
int ch=0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while((ch=in.read())!=-1)
{
baos.write(ch);
}
data=baos.toByteArray();
baos.close();
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return data;
}
生成纹理Id
public static int initTexture(String pkmName,Resources r){
//生成纹理ID
int[] textures = new int[1];
GLES30.glGenTextures
(
1, //产生的纹理id的数量
textures, //纹理id的数组
0 //偏移量
);
int textureId=textures[0];
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureId);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER,GLES30.GL_NEAREST);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D,GLES30.GL_TEXTURE_MAG_FILTER,GLES30.GL_LINEAR);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S,GLES30.GL_CLAMP_TO_EDGE);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T,GLES30.GL_CLAMP_TO_EDGE);
byte[] data=loadDataFromAssets(pkmName,r);
ByteBuffer buffer = ByteBuffer.allocateDirect(data.length).order(ByteOrder.LITTLE_ENDIAN);
buffer.put(data).position(16);
ByteBuffer header = ByteBuffer.allocateDirect(PKM_HEADER_SIZE).order(ByteOrder.BIG_ENDIAN);
header.put(data, 0, 16).position(0);
int width = header.getShort(12);
int height = header.getShort(14);
GLES30.glCompressedTexImage2D
(
GLES30.GL_TEXTURE_2D,
0,
GLES30.GL_COMPRESSED_RGBA8_ETC2_EAC,
width,
height,
0,
data.length - 16,
buffer
);
return textureId;
}
左图为 pkm 右图为 png 看起来还差不多