Item of the Day: Transparent Textures with Android Opengl es

So on my quest to make a new android app i ran into an issue. My stupid texture would not filter the black from the image so that portion could become trasparent. But never fear after much searching and me having done it with the full version of opengl i have found my solution.

1) Turn your image into a PNG.

2) If you dont have an alpha channel just add one using GIMP. Its really easy to do just search it.

3) Load your Texture (This is loading from a resource)

InputStream is = context.getResources().openRawResource(R.drawable.circle);
Bitmap bitmap = null;
try {

	BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
        bitmap = BitmapFactory.decodeStream(is, null, bitmapOptions);

	} finally {

	try {
		is.close();
		is = null;
	  } catch (IOException e) {
	  }
	}

	gl.glGenTextures(1, textures, 0);

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

	gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
	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_REPEAT);
	gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

	GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
	bitmap.recycle();

4)  Draw that texture

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

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

gl.glFrontFace(GL10.GL_CCW);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_BYTE, indexBuffer);

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

5) Done

See that was easy.

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

6 Responses to Item of the Day: Transparent Textures with Android Opengl es

Leave a Reply

Your email address will not be published.