Archived

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

Dalton

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

	glEnable(GL_ALPHA_TEST);
	//Colours with an alpah value greater than 0 will be opaque - colours with alpha value of zero will be transparent

	glAlphaFunc(GL_GREATER,0.0f);
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

//ColorKey(data,newData,0,0,0,width,height);

//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(i=0;i<height;++i)
for(j=0;j<width;++j) {
out[4*(i*width+j)+0]=in[3*(i*width+j)+0];
out[4*(i*width+j)+1]=in[3*(i*width+j)+1];
out[4*(i*width+j)+2]=in[3*(i*width+j)+2];
if(r>=in[3*(i*width+j)+0] &&
g>=in[3*(i*width+j)+1] &&
b>=in[3*(i*width+j)+2])
out[4*(i*width+j)+3]=0;
else
out[4*(i*width+j)+3]=255;
}
}




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

Share this post


Link to post
Share on other sites