Need the simplest possible way to draw textures with alpha

Started by
2 comments, last by sayuke 16 years, 2 months ago
Hi, i have a simple 2d game i am making, I am using ortho mode, and have the depth buffer disabled. I also have several 2d sprites, all squares, but i want to be able to draw them ontop of each other. The pixels are either transparent, ie alpha = 0, or completely opaque, alpha = 255 (as sourced from the bitmap image). What is the easiest possible way to draw these ontop of each other. Right now drawing one ontop of the other will result in black being draw to where the alpha, transparent, pixels should have been.
Advertisement
You should be able to use alpha testing for this:
glEnable(GL_ALPHA_TEST);glAlphaFunc(GL_GREATER, 0.f);
(Note: Syntax not guaranteed to be correct.)

You might also want to look into alpha blending, in case you want to have translucent sprites at some point in the future.
I recommend Lonesocks library, SOIL. http://www.lonesock.net/soil.html

It loads and applies textures for you. I currently use .tga files, since they support transparency, and GIMP can export them well.

Then, just set the correct mode as JYK said and tada alpha blended textures.


REALLY IMPORTANT NOTE:

you need to re-order your sprites so they are drawn in the order of their distance from the camera, as OPenGL won't automatically do this, and will cause lots of problems whereby two overlapping semitransparent polygons have the wrong thing behind them. Luckily you are doing it in 2d and so its far easier. Turn off depth testing, then draw your sprites starting with the background and so on in descending order of their distance from the screen.

Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!
thnx guys it works.

This topic is closed to new replies.

Advertisement