开发者

How to load animation frames as textures in OpenGL ES?

开发者 https://www.devze.com 2023-03-18 06:49 出处:网络
I\'m new to OpenGL ES and developing a simple 2D game.However, I\'m confused as to how I can go about loading multiple animation frames as textures (for the player character).I\'ve tried to load a dif

I'm new to OpenGL ES and developing a simple 2D game. However, I'm confused as to how I can go about loading multiple animation frames as textures (for the player character). I've tried to load a different image every time the character is rendered, but that's too slow.

Here is my texture loading code thus far:

public void loadGLTexture(GL10 gl, Context context) {
    InputStream[] is=new InputStream[3];


    is[0]= context.getResources().openRawResource(R.drawable.r1);
    is[1]= context.getResources().openRawResource(R.drawable.r2);
    is[2]= context.getResources().openRawResource(R.drawable.r3);

    try {
        bitmap[0]= BitmapFactory.decodeStream(is[0]);
        bitmap[1]= BitmapFactory.decodeStream(is[1]);
        bitmap[2]= BitmapFactory.decodeStream(is[2]);
    } finally {
        try {
            is[0].close();
            is[1].close();
            is[2].close();
            is = null;
        } catch (IOException e) {
        }
    }

    gl.glGenTextures(3, textures,0);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap[0], 0);
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap[1], 0);
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap[2], 0);
    bitmap[0].recycle();
    bitmap[1].recycle();
    bitmap[2].recycle();
}

How can I make all three images accessible through an array?开发者_如何学Go


You need to call glBindTexture before every texImage2D. Currently you are loading all three images into textures[0].

Don't try to load all textures at once. Change your function to load only one texture and just call it three times. You should be able to do:

textures[0]=loadGLTexture(GL10,context,R.drawable.r1);
textures[1]=loadGLTexture(GL10,context,R.drawable.r2);
textures[2]=loadGLTexture(GL10,context,R.drawable.r3);


You can place all the frames of animation on a single texture and use texture coordinates to select which one to use

0

精彩评论

暂无评论...
验证码 换一张
取 消