【问题标题】:Android: OpenGL textures too large, can't figure out whyAndroid:OpenGL纹理太大,不知道为什么
【发布时间】:2011-10-02 06:35:14
【问题描述】:

为什么我的纹理看起来占用了这么多空间?

我的应用大量使用 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, 2011 
Identifier size 32-bit 

但是,当我使用手机上的任务管理器查看我的应用程序的 ram 使用情况时,它显示我的应用程序使用了 44.42MB。堆大小使用和内存使用之间有什么关系吗?我认为这 42MB 中的大部分必须是我的开放 GL 纹理,但我不知道为什么它们会占用这么多空间,因为在磁盘上所有文件加起来只占用 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);

        }

*hprof文件使用mat分析,eclipse ddms生成相同数据

【问题讨论】:

    标签: android opengl-es


    【解决方案1】:

    PNG 是压缩图像。为了让 OpenGL 使用它们,必须解压缩 png。这种解压会增加内存大小。

    您可能希望以某种方式减小某些纹理的大小。也许不是使用 512x512 图像,而是使用 256x256 或 128x128。您使用的某些纹理可能不需要那么大,因为它们将用于屏幕尺寸有限的移动设备。

    【讨论】:

    • 解压后释放内存
    • 数据在被推送到 OpenGL 上下文后仍然驻留在内存中。虽然与位图本身的数据不同。您可以通过在使用 glTexImage2D 之前和之后输出本机内存使用情况来测试这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2016-07-13
    相关资源
    最近更新 更多