装载多个纹理的openGL [英] loading multiple textures openGL

查看:131
本文介绍了装载多个纹理的openGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是用openGL我需要加载配发的位图动画的开始,我可以得到它的几帧的工作很好,但运行内存不足时,我尝试加载我怎样才能在加载只有几帧的所有帧一时间?这是code我使用加载纹理

I just started using openGL I need to load allot of bitmaps for animation I can get it to work great for a few frames but run out of memory when I try to load all the frames How can I load just a few frames at a time? This is the code I'm using to load the textures

public void loadGLTexture(GL10 gl, Context context) {


    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.r1);
    Bitmap bitmap01 = Bitmap.createScaledBitmap(bitmap, 512, 512, false);

    Bitmap bitmap2 = BitmapFactory.decodeResource(context.getResources(),R.drawable.r2);
    Bitmap bitmap02 = Bitmap.createScaledBitmap(bitmap2, 512, 512, false);

    Bitmap bitmap3 = BitmapFactory.decodeResource(context.getResources(),R.drawable.r3);
    Bitmap bitmap03 = Bitmap.createScaledBitmap(bitmap3, 512, 512, false);


    try {

    bitmap = bitmap01;
        bitmap2 = bitmap02;
        bitmap3 = bitmap03;
    } finally {
    }

    //Generate there texture pointer
    gl.glGenTextures(3, textures, 0);

    //Create  Texture and bind it to texture 0
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    //Create  Texture and bind it to texture 1
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap2, 0);

    //Create  Texture and bind it to texture 2
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[2]);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap3, 0);


    //Clean up
    bitmap.recycle();
    bitmap2.recycle();
    bitmap3.recycle();      
}   

}

推荐答案

嗯,这是许多位图提供给您的应用程序的过程中(非常有限)内存装载的经典问题。我不会进入这个,我只想说,pre蜂窝这些位图加载offheap,从蜂巢超越他们装上了一堆,但问题依然存在:的128x256像素的位图将采取4 *在内存128 * 256字节。你通常有兆可用内存16和48之间为您的应用程序(取决于设备和Android版本)。

Well, it's the classic issue of loading to many bitmaps in the (very limited) memory available to your app's process. I'm not gonna get into this, suffice to say that pre Honeycomb these bitmaps were loaded offheap, from Honeycomb and beyond they're loaded on the heap, but the issue still remains: a bitmap of 128x256 pixels will take 4*128*256 bytes in memory. And you usually have between 16 and 48 megs of available memory for your app (depending on device and Android version).

在这里好消息是,一旦你加载一个位图,并创建一个OpenGL纹理它,您不再需要该位图对象,可以回收并使其无效。所以,你可能尝试加载(和,因为我从你的code看到,缩放)第一个位图,然后创建第一个纹理对象,然后回收和取消REFFERENCE首先位图。然后去和做相同的第二个纹理对象的第二位。

Good news here is that once you load a bitmap and create an opengl texture with it, you no longer need that Bitmap object, you can recycle it and make it null. So you might try loading (and, as I see from your code, scaling) the first bitmap, then create the first texture object, then recycle and de-refference that first bitmap. Then go on and do the same with the second bitmap for the second texture object.

这篇关于装载多个纹理的openGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆