This topic is now archived and is closed to further replies.


Simulating colour-keying with alpha testing

Recommended Posts

I want to use alpha testing to make one colour of my textures, RGB(0,0,0), transparent. I am under the impression that the best way to do this is to change the alpha values of the black pixels in my texture during run time. Here''s what I have so far:
	//Enable alpha testing

	//Colours with an alpah value greater than 0 will be opaque - colours with alpha value of zero will be transparent

I hope it''s right? Anyway, what I don''t really know how to do is iterate through the pixels in my texture and change their alpha components. I''m not ever sure if my texture has alpha components, since it comes from an 8bpp Windows bitmap. Any thoughts?

Share this post

Link to post
Share on other sites
Assuming you know how to load and use textures, u should have a pointer to your data which you will pass to OpenGL using glTexImage2D().

Here is a function which does the work, just copy and paste it.

//*in is the pointer to your data which shld be in RGB format

//*out is a pointer to your allocated memory to hold your new Color-keyed data, before using this function you should have allocated memory for out like this,

// unsigned char* newData = new unsigned char[width * height * 4] We multiply by 4 because the new data will be RGBA

//Then you simply do this


//Your new data will be contained in the newData pointer, after sending your texture to OpenGL, remember to delete your memory

//delete[] data;

//delete[] newData;

void ColorKey(GLubyte *in,GLubyte *out,GLubyte r,GLubyte g,GLubyte b, unsigned int width,unsigned int height) {
unsigned int i,j;
for(j=0;j<width;++j) {
if(r>=in[3*(i*width+j)+0] &&
g>=in[3*(i*width+j)+1] &&

[edited by - GamerSg on April 5, 2004 9:33:22 PM]

Share this post

Link to post
Share on other sites