Jump to content
  • Advertisement
Sign in to follow this  

Mapping ALPHA values to a texture

This topic is 4866 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

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net 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!