如何在OpenGL ES 2.0中管理多个纹理? [英] How to manage multiple textures in OpenGL ES 2.0?

查看:106
本文介绍了如何在OpenGL ES 2.0中管理多个纹理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有6种不同纹理的OpenGL ES 2.0应用程序.我需要的是同时绘制和移动它们.我能够做到这一点,但是运动很缓慢,因为我一直将位图加载到纹理中.对于每种纹理,我都会在Render方法上执行以下操作:

I have a OpenGL ES 2.0 app with 6 different textures. What I need is to draw and move them all at the same time. I was able to do it, but the movement was laggy because I was loading the bitmaps into the textures all the time. For each texture, I do the following on the Render method:

    setupImage(texture1);
    GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 0, vertexBufferT1);
    GLES20.glVertexAttribPointer(mTexCoordLoc, 2, GLES20.GL_FLOAT, false, 0, uvBufferT1);
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, indicesT1.length, GLES20.GL_UNSIGNED_SHORT, drawListBufferT1);

在setupImage方法上,我这样做:

And on the setupImage method, I do this:

    // Create the bitmap
    int id = mContext.getResources().getIdentifier("drawable/texture1", null, mContext.getPackageName());
    Bitmap bmp = BitmapFactory.decodeResource(mContext.getResources(), id);

    // Bind texture to texturename
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

    // Set filtering
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    // Set wrapping mode
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

    // Load the bitmap into the bound texture.
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bmp, 0);

    // Free bitmap
    bmp.recycle();

这显然是错误的.但是,我不知道如何将每个位图加载到不同的纹理中,然后如何在Render方法上切换活动纹理.

This is obviously wrong. However, I don't know how to load each bitmap into a different texture and then how can I switch the active texture on the Render method.

那么,如何显示和移动所有六个纹理并进行流畅的运动呢?

So, how can I achieve this, in order to display and move all the six textures and have a fluid movement?

推荐答案

您是对的,您正在做的事情非常浪费.维护纹理的一种简单方法是简单地以更聪明的方式使用该textures[]数组.

You are right, what you're doing is very wasteful. An easy way to maintain the textures is to simply use that textures[] array in a smarter way.

曾经想知道为什么该变量是大小为1的数组?好了,您可以使用它来保留1个以上的纹理!只需将其定义为更大的数组即​​可(大小为6).完成此操作后,您将能够在应用启动时将多个位图加载到纹理中,然后在要绘制它时仅绑定正确的纹理.

Ever wonder why that variable is an array of size 1? Well, you can use it to hold more than 1 texture! Simply define it as a larger array (size 6 for you). Once you do this, you will be able to load multiple bitmaps into textures when the app starts and then just bind the correct texture when you're about to draw it.

这是一个例子:

private int[] textures = new int[6];

private void loadTexture(Bitmap bitmap, int textureIndex) {
    GLES20.glGenTextures ( 1, textures, textureIndex );

    GLES20.glBindTexture ( GLES20.GL_TEXTURE_2D, textures[textureIndex] );

    //.. call glTexParameter as needed

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
}

对于要加载为纹理的每个位图,应在应用程序启动时调用一次以上代码.如果您有很多位图,则可能需要考虑某种即时纹理加载和卸载-但是对于您的需求,只需一次加载它们就足够了.

The code above should be called once, when the app starts up, for every bitmap you want to load as a texture. If you have many bitmaps, you might want to consider some sort of just-in-time texture loading, and unloading - but for your needs, it should be enough to just load them all at once.

稍后,当您要绘制某种纹理时,请确保首先绑定正确的纹理:

Later, when you're about to draw a certain texture, make sure you first bind the correct one:

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[textureIndex]);

这只会告诉OpenGL层它将用于即将进行的绘制调用的预加载纹理.确保在每次绘制之前调用此命令,以确保每次绘制正确的纹理.请注意,此时OpenGL不会重新创建整个纹理,而是仅使用已经加载的纹理,使其非常有效.

This will simply tell the OpenGL layer which pre-loaded texture it's going to be using for the up-coming draw call. Be sure to call this before each draw to ensure you draw the correct texture each time. Note that at this point, OpenGL will not re-create the whole texture, but rather, just use the already loaded one, making it very efficient.

这篇关于如何在OpenGL ES 2.0中管理多个纹理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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