【发布时间】:2013-05-30 19:07:09
【问题描述】:
我在 OpenGL 中清除纹理内存时遇到了这个问题。我正在使用 OpenGL 在 Android 中制作游戏。一切正常,但我所做的是在应用程序启动时将所有纹理加载到内存中。我显然减慢了游戏的速度 - 我已经到了那里,每个怪物都有 10 个动画帧 512x512 大小加上环境纹理 - 所以我想一次将它们全部加载到内存中并不是一个完美的主意。
我想做的是一个 onLoadLevel(int level) 函数,它将清除纹理内存并在当前关卡开始时加载必要的纹理。
问题是如何在 OpenGL 中清除纹理内存? 我考虑了2个选项 1 - 全部清除 - 就像删除内存中的所有纹理并加载当前级别所需的所有内容 - 但只有此级别的纹理 - 它会更慢但我想更容易做?只是为了重置纹理内存? 2 - 只删除我不再需要的纹理并加载下一个纹理。
我现在不知道如何做到这两个 - 如何一次从内存中清除所有纹理或如何从内存中删除一个 - 或者即使有可能 - 我已经看到了一些解决方案,但它是关于指向纹理 ID 似乎是个问题,因为我不知道纹理 ID 是什么?
我将在下面向您展示我是如何进行纹理加载的:
public int mTextureDataHandleSample1;
//then I use loadTexture function from my TextureHelper class
mTextureDataHandleSample1 = TextureHelper.loadTexture(mActivityContext, R.drawable.sample1);
GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
loadTexture 函数如下所示:
public static int loadTexture(final Context context, final int resourceId)
{
final int[] textureHandle = new int[1];
GLES20.glGenTextures(1, textureHandle, 0);
if (textureHandle[0] != 0)
{
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false; // No pre-scaling
// Read in the resource
final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);
// Bind to the texture in OpenGL
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
// Set filtering
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
// Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle();
}
if (textureHandle[0] == 0)
{
throw new RuntimeException("Error loading texture.");
}
return textureHandle[0];
}
因此,如果您能告诉我或至少指出一个解决方案如何清除纹理内存或删除之前加载到内存中的一些纹理,我将非常感激。
【问题讨论】:
标签: android opengl-es opengl-es-2.0 textures