Advertisement Jump to content
Sign in to follow this  

i didn't understand lesson 8 (blending)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

// Insert vertices and texcoords

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
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 this post

Link to post
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.

DavidR does an excellent job with his code :P

Share this post

Link to post
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... :/

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!