Sign in to follow this  

Mapping ALPHA values to a texture

Recommended Posts

Hi, I've been trying to get this to work for a week now. I don't understand why it doesn't work. I have some mask/alpha data that I want to put un the alpha channel of a texture. The texture is RGBA. I loaded that mask data and put it into a unsigned byte array. So the texture is 1024x1024 and then mask is 1048576 bytes (1024x1024), so it only holds the alpha values. This is what I tried:
unsigned char *tgaimage = NULL;
tgaimage = TGALoader.LoadTGA11Image( "Data/mask1.tga", jpg_width, jpg_height );
if ( tgaimage == NULL )
        return false;

glBindTexture( GL_TEXTURE_2D, jpg );
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, jpg_width, jpg_height, GL_ALPHA, GL_UNSIGNED_BYTE, tgaimage );

So I load the mask data into tgaimage and I have the texture in jpg. but when I draw the texture to the screen, I get nothing but black. I've looked up the funtion glTexSubImage on the internet and it says that when using the GL_ALPHA it will create the rgb data, but does it replace the already existing rgb data in the texture? Is there another way of doing this? Brain21

Share this post

Link to post
Share on other sites
You can simply do it manually:

// just a quick hack!
#include <algorithm>

// actual colour component order isn't important - only alpha must be first/last
struct Colour {
unsigned char r, g, b, a;

// this is not the most efficient way of doing this - sets the alpha component
Colour const & set_alpha( Colour & pixel, unsigned char alpha ) {
pixel.a = alpha;
return pixel;

// apply mask just uses std::transform to do the work
void apply_mask_rgba( void * rgba, unsigned char * mask, size_t pixels ) {
std::transform( static_cast<Colour*>( rgba ), static_cast<Colour*>( rgba ) + pixels, mask, static_cast<Colour*>( rgba ), set_alpha );

// usage:
apply_mask_rgba( tgaimage, jpg, jpg_width * jpg_height );

This is just a quick hack, but you should get the idea [smile] (it should work, BTW).


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this