Advertisement

Taxtures with Alpha Channel

Started by December 06, 2004 05:29 PM
6 comments, last by python_regious 19 years, 11 months ago
I want to make a hud, that will be positioned infront of my camara all the time. How can i load in textures that treat, for example, true purple as an alpha value? Can i modify the nehe texture loader to do this or is it a completely different kettle of fish? Cheer.
When you load in the texture, just generate an alpha channel for it, using the criteria that true purple has an alpha of 0, and all other values are 1. I haven't seen the NeHe texture loader, but it's quite probable that you can easily modify it, what I'm talking about isn't differcult.
If at first you don't succeed, redefine success.
Advertisement
Assuming you mean textures and not taxtures [lol] it's fairly trivial to do. You need to allocate a new chunk of memory to hold the transformed RGBA values and copy the values you loaded into it, setting the alpha depending on the RGB value of each pixel. So where you have:
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

You want to do something like:
std::vector<Rgba> rgbaData(TextureImage[0]->sizeX * TextureImage[0]->sizeY);for (unsigned int index = 0; index < TextureImage[0]->sizeX * TextureImage[0]->sizeY; ++index){	// copy rgb data from TextureImage[0]->data to rgbaData	// set rgbaData[n].alpha depending on rgbaData[n].red, rgbaData[n].green & rgbaData[n].blue}glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, &rgbaData[0]);

This example uses C++. If you want pure C, just replace the vector with a raw array.

Enigma
The quickest possible answer is that you don't use colour-keyed transparency. Instead, you'll want to create your images in a format that supports an alpha channel in the image itself. The most common examples are PNG and TGA. Both of these are commonly used as textures because they can have alpha values in them. I know you can find loader code for TGAs all over the place and writing one yourself isn't too hard. Try here in the tutorial section for your API of choice to find an example of one.

Fire up an image editor and it should have the ability to set translucency for areas, so you just set all the areas you want not to show up as 0 opacity. Save as TGA/PNG. Then with the texture enabled, turn on alpha blending and draw your quad or whatever it is.

You can still use colour-keying (setting magenta as 0% opaque) but it's not worth it. It would take more work to get the identical final result.

-Auron
Quote: Original post by Auron
You can still use colour-keying (setting magenta as 0% opaque) but it's not worth it. It would take more work to get the identical final result.

-Auron


I wouldn't say that. For starters 24bit images take up 25% less space ( minus compression ), and it's really easy to do - it's not much more work at all. In my texture loader I have a series of options for various alpha generation routines, from a simple colour key, to the luminal value of the pixel determining the alpha, and cosine and cubic interpolation methods between two colours determining the alpha. Yes, having an alpha map as part of the image is great, when you can't generate its contents with an algorithm.
If at first you don't succeed, redefine success.
Cheers guys, i will try out some of the suggestions you've given me. Nice to see that i have a lot of options though! Cheer.s
Advertisement
Isn't there some kind of cost associated with 3-byte pixel textures due to memory alignment? I've often assumed there was just in case. I'm not 100% sure though :P
Quote: Original post by chawk
Isn't there some kind of cost associated with 3-byte pixel textures due to memory alignment? I've often assumed there was just in case. I'm not 100% sure though :P


They'll probably be converted to BGRA textures at loadtime anyway.
If at first you don't succeed, redefine success.

This topic is closed to new replies.

Advertisement