# i didn't understand lesson 8 (blending)

This topic is 4807 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i didn't understand how can i render bitmaps that have a certain area transparent.

##### Share on other sites
I'm guessing that you want to make pixels which has a specific color transparent?

Well, one of the options is this:

Make a 24-bit array and a 32-bit array of the image in the memory. You should first load all the image pixels into the 24-bit array and convert that array to the 32-bit one (that will be faster than loading into a 32-bit, cause you can load the whole array at once). Then you go through the 32-bit array and set the last 8 bit of the pixel to 0 if the pixels color is (255, 255, 255) or whatever pixel color you'd want transparent. The rest of the pixels last 8 bit should be set to 255 (fully visible) to make the rest of the image as visible as it normally would be.
Then you upload the image to OpenGL (with the proper settings of course - remember that you're now using 32 bit instead of 24) and use blending before displaying the image like so:
glEnable(GL_BLENDING);glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);glBegin(GL_QUADS)// Insert vertices and texcoordsglEnd();

That will make the image look transparent at the places, where the color has a specific value.

Setting the last 8 bit can be done in a couple of ways. One of them is to make a structure for each pixel instead of bytes only:
union Color{struct{unsigned char r, g, b, a;};unsigned char index[4];};

(the union is to make the color faster to load because you can just load 3 bytes into the index array)

Another option is to use logical operators. To make the pixel (which is here an element of an array which is made up of unsigned long's - aka 4 bytes) fully transparent:
pixels[index] &= 0x00FFFFFF;
And to make it fully visible:
pixels[index] |= 0xFF000000;

I haven't tested the last piece of the code (the union and the logical operators), so there could be errors, but I hope you get the idea ;-)

EDIT: I just took the time to read NeHe's lesson 8 description.. well, he isn't doing exactly what I'm doing, because he's making the image transparent according to all the color values of each pixel. But the method seems to be somewhat the same, although I didn't want to read through the source to find out how much they are alike in terms of coding structure.
Since I have already written the above text I'll leave it.. maybe someone can benefit of it.

[Edited by - nife on June 17, 2005 10:03:38 AM]

##### Share on other sites
assuming colorkeying is what you're after, i'd also like to refer you to this thread that I posted when *I* went through that trial.

http://www.gamedev.net/community/forums/topic.asp?topic_id=324645

DavidR does an excellent job with his code :P

##### Share on other sites
You might want to take a look at Nehe's lesson 20 on masking as well...

If you are going to use nife's method you may want to consider drawing your images on top of a transparent layer or channel whatever is called...So that you don't have to compute the alpha channel of all the pixels when you load an image. You will need a file format that supports alpha channels of course... :/

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 13
• 13
• 61
• 14
• 15
• ### Forum Statistics

• Total Topics
632125
• Total Posts
3004252

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!