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

  1. Jazlynn says:

    It’s really great that people are sarhnig this information.

  2. natural weight loss says:

    You made some decent points there. I regarded on the internet for the problem and found most people will go together with with your website.

  3. extreme diets says:

    I used to be suggested this website by way of my cousin. I’m not sure whether this post is written by means of him as nobody else understand such distinct approximately my problem. You’re amazing! Thanks!

  4. Alene Funkhouser says:

    This is very interesting, You’re a very skilled blogger. I have joined your feed and look forward to seeking more of your excellent post. Also, I have shared your web site in my social networks!

  5. software development Romania says:

    Thanks and good blog.

Leave a Reply to Gytha Cancel reply

Your email address will not be published. Required fields are marked *