Alpha for 2D sprites

Hi all,

I’m creating a 2D game in OpenGL, and I’m using the following texture loader to load up my textures:

Some of you may recognise it, and I hope so! Because whenever I try to display a loaded up texture, it just comes out as whatever colour I last set glColor4f() to.

I have a 24 bit bitmap loader that loads the textures and displays them fine, however I can’t set an alpha value with that, so the backgrounds of my sprites show up, which isn’t helpful :frowning:

So, would anyone be able to either:

A) Explain how to fix the problem I’m having with Chris Leathley’s texture loader.

Or B) Explain how I can make a single colour of a 24 bit bitmap that I’ve loaded up in OpenGL completely transparent (if it’s even possible)

Any help is greatly appreciated :slight_smile:



I dont know the your code, so I can’t say anything about A. But for B: You can copy the image data in a larger buffer and set the alpha values as you wish.

Some pseudocode:

imageData = your loaded rgb data

buffer = alloc(width * height * 4); // as in RGBA
ptr = buffer; 
for ( int i = 0; i < width * height; i += 3 )
  *ptr++ = imageData[i+0];
  *ptr++ = imageData[i+1];
  *ptr++ = iamgeData[i+2];
  *ptr++ = 0; // thats your alpha value

// upload buffer etc...

Thank you very much, it worked beautifully :slight_smile: <3