java加载多纹理openGL
我刚开始使用openGL,我需要为动画加载位图,我可以让它在几帧内运行良好,但当我尝试加载所有帧时内存不足,我如何一次只加载几帧?这是我用来加载纹理的代码
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();
}
}
# 1 楼答案
在应用程序进程可用的(非常有限的)内存中加载许多位图是一个经典问题。我不打算讨论这个问题,只需说pre Honeycomb这些位图是从堆外加载的,从Honeycomb和beyond加载到堆上,但问题仍然存在:128x256像素的位图需要4*128*256字节的内存。你的应用程序通常有16到48兆的可用内存(取决于设备和Android版本)
好消息是,一旦加载位图并使用它创建opengl纹理,您就不再需要该位图对象,您可以回收它并使其为空。因此,您可以尝试加载(并且,正如我在代码中看到的,缩放)第一个位图,然后创建第一个纹理对象,然后回收并参考第一个位图。然后继续对第二个纹理对象的第二个位图执行相同的操作