Android 安卓:OpenGL纹理太大,不知道为什么
为什么我的纹理看起来占用了这么多空间 我的应用程序大量使用opengl,在运行期间生成以下堆统计数据*:Android 安卓:OpenGL纹理太大,不知道为什么,android,opengl-es,Android,Opengl Es,为什么我的纹理看起来占用了这么多空间 我的应用程序大量使用opengl,在运行期间生成以下堆统计数据*: Used heap dump 1.8 MB Number of objects 49,447 Number of classes 2,257 Number of class loaders 4 Number of GC roots 8,551 Format hprof JVM version Time 1:08:15 AM GMT+02:00 Date Oct 2, 201
Used heap dump 1.8 MB
Number of objects 49,447
Number of classes 2,257
Number of class loaders 4
Number of GC roots 8,551
Format hprof
JVM version
Time 1:08:15 AM GMT+02:00
Date Oct 2, 2011
Identifier size 32-bit
但是,当我在手机上使用任务管理器查看应用程序的ram使用情况时,它显示我的应用程序使用了44.42MB。堆大小使用和ram使用之间有关系吗?我认为42MB中的大部分一定是我的OpenGL纹理,但我不明白为什么它们会占用这么多空间,因为在磁盘上,所有文件加起来只占用24MB,而且它们不会同时加载。我甚至在纹理加载之前通过调整位图的大小来缩小它们。我还动态创建一些纹理,但也会在使用后销毁这些纹理
我使用的是OpenGL 1.0和Android 2.2,用于加载纹理的典型代码如下所示:
static int set_gl_texture(Bitmap bitmap){
bitmap = Bitmap.createScaledBitmap(bitmap, 256, 256, true);
// generate one texture pointer
mGL.glGenTextures(1, mTextures, 0);
mGL.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]); // A bound texture is
// an active texture
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0,GL10.GL_RGBA, bitmap, 0);
// create nearest filtered texture
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR); // This is where the scaling algorithms are
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR); // This is where the scaling algorithms are
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
GL10.GL_CLAMP_TO_EDGE);
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
GL10.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
Log.v("GLSurfaceView", "Loading Texture Finished, Error Codes:"+mGL.glGetError());
return mTextures[0];
}
我用于加载位图的代码如下所示:
public int load_texture(int res_id){
if(mBitmapOpts==null){
mBitmapOpts = new BitmapFactory.Options();
mBitmapOpts.inScaled = false;
}
mBtoLoad = BitmapFactory.decodeResource(MyApplicationObject.getContext().getResources(),res_id, mBitmapOpts);
assert mBtoLoad != null;
return GraphicsOperations.set_gl_texture(mBtoLoad);
}
*使用mat分析hprof文件,eclipse ddms生成相同的数据,PNG是压缩图像。为了让OpenGL使用它们,必须对PNG进行解压缩。此解压缩将增加内存大小
您可能希望以某种方式减小某些纹理的大小。可以使用256x256或128x128代替512x512图像。您使用的某些纹理可能不需要太大,因为它们将以有限的屏幕大小进入移动设备。解压后释放内存数据推送到OpenGL上下文后仍驻留在内存中。但数据与位图本身不同。您可以通过在使用glTexImage2D之前和之后输出本机内存使用情况来测试这一点