i didn't understand lesson 8 (blending)

Started by
2 comments, last by setaglib 18 years, 10 months ago
i didn't understand how can i render bitmaps that have a certain area transparent.
Advertisement
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]
Killers don't end up in jailThey end up on a high-score!
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
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... :/

This topic is closed to new replies.

Advertisement