An example of texturing using Texture atlas and openGL 1.1 in android

I am having an issue with texturing objects in OpenGL using texture atlases, I am creating a 2d game and I know how to texture a POT bitmap for an object, but I cannot find a tutorial on converting my code to use a texture atlas for performance reasons. here is my code for my current work object creation and texturing implementation.

public void createTexture(Bitmap bmp, GL10 gls, int texturename)
    {
        this.gl = (GL11) gls;

        this.TextureName = texturename;
        bombBmp = bmp;

        VertexBuffer = null;
        TextureBuffer = null;
        IndexBuffer = null;

        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(12 * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        VertexBuffer = byteBuffer.asFloatBuffer();
        VertexBuffer.put(new float[] { 0, 0, 0.0f, 0, -h, 0.0f, w, 0, 0.0f, w, -h, 0.0f });
        VertexBuffer.position(0);

        byteBuffer = ByteBuffer.allocateDirect(8 * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        TextureBuffer = byteBuffer.asFloatBuffer();
        TextureBuffer.put(new float[] { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f });
        TextureBuffer.position(0);

        byteBuffer = ByteBuffer.allocateDirect(6 * 2);
        byteBuffer.order(ByteOrder.nativeOrder());
        IndexBuffer = byteBuffer.asShortBuffer();
        IndexBuffer.put(new short[] { 0, 1, 2, 1, 3, 2 });
        IndexBuffer.position(0);

        gl.glBindTexture(GL10.GL_TEXTURE_2D, this.TextureName);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bombBmp, 0);
        bombBmp.recycle();
        bombBmp = null;
    }

      

I am generating my Texturename from genTexture and passing the POT bitmap to this function

gl.glGenTextures(1, textures, 0);
bomb.createTexture(bombBmp, gl, textures[0]);

      

here is my intended texture bitmap

bomb_atlas.png

+3


source to share


1 answer


In response to your comment, you are not binding subImages. glTexSubImage is only used to replace pixels in the saved texture. If your bitmap already exists as an atlas, as you showed, you simply load the entire texture via glTexImage2d and link it.

If you want to access a specific part of this image (for example, the yellow round 3 bomb to the left of the first line), you simply change the texture coordinates that you use to access it. If this circle were in its own image, you would just use (0,0) - (1,1) coordinates, but since it is part of another image, you only need to select a certain part of it. In your photo there would be texcoords:



(.50,.75) //bottom left
(.75,.75) //bottom right
(.75,1.0) //top right
(.50,1.0) //top left

      

If you draw an ATV with these texcoords, it should look exactly as if the yellow round bomb had its own image, but you don't have to constantly regenerate textures (hence the performance gain).

+1


source







All Articles