Jin Kim
Jin Kim

Reputation: 17722

OpenGL ES: Drawing using a Texture Atlas

I'm trying to develop an Android 2D game using OpenGL ES that uses a tiled map, and I heard that it's best to store the tiles in a texture atlas (one large bitmap with multiple tiles) for performance reasons.

Does anyone have some sample code that demonstrates how to draw a tile from a texture atlas in Android OpenGL ES?

onDrawFrame(GL10 gl) {
    ...
}

Upvotes: 2

Views: 1736

Answers (1)

Jin Kim
Jin Kim

Reputation: 17722

Well I figured out how to do it.

onDrawFrame(GL10 gl) {
    ...

    int[] crop = new int[4];
    crop[0] = tileWidth * tileIndex;  // tileIndex represents the nth tile in the texture atlas
    crop[1] = tileHeight;
    crop[2] = tileWidth;
    crop[3] = -tileHeight;

    // specify the source rectangle 
    ((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, crop, 0);

    // draw the texture
    ((GL11Ext)gl).glDrawTexiOES(x, y, 0, tileWidth, tileHeight);

    ...
}

Upvotes: 2

Related Questions