Sign in to follow this  
Christen

Color Keying

Recommended Posts

When loading the texture, once you have a pointer to the RGB data, you will need to make an RGBA copy, looping through each pixel in the image, and if the pixel is, say, magenta as you say, set it to black, with zero alpha. Otherwise, the colour stays the same, and alpha is 255.

eg, something like:


unsigned char newImage = new unsigned char[width*height*4];

int s=0,d=0;
for (int x=0; x<width; x++)
{
for (int y=0; y<height; y++)
{
if (image[s]==255 && image[s+1]==255 && image[s+2]==0)
{
newImage[d++]=0;
newImage[d++]=0;
newImage[d++]=0;
newImage[d++]=0;
s+=3;
}
else
{
newImage[d++]=image[s++];
newImage[d++]=image[s++];
newImage[d++]=image[s++];
newImage[d++]=255;
}
}
}

load texture as GL_RGBA...


delete [] newImage;


Then when drawing, use alpha test and/or alpha blending to mask out the previously pink areas.

hope that makes some sense?

Share this post


Link to post
Share on other sites
Well, honestly I'm a beginner OpenGL programmer so I'm a bit confused on where should I put the code.

One more thing, I loaded multiple textures using mipmaps. Problem is, some of my textures are slanted when displayed. Oh, and I'm using 2d primitives.

Share this post


Link to post
Share on other sites
Post the code where you draw the vertices and their respective texture coordinates for these said slanted shapes.

As for color keying you need to learn how the data is stored in an opengl texture object. In this case you would probably want to use an RGBA format, and while loading in your data, set the alpha for any colored pixel equal to your color key to 0, while setting the alpha for all other colored pixels to 255.

Also if that explanation still boggles you, it would help if you posted the code you use a load a picture file into a texture object, so we would be better fit to help with your specific code.

Share this post


Link to post
Share on other sites
I've fixed the slanted textures now. So what's left is the color keying. Here's the code I used to load the textures.


GLuint texture[14];

AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL;

if (!Filename)
{
return NULL;
}

File=fopen(Filename,"r");

if (File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}

return NULL;
}

int LoadGLTextures()
{
int Status=FALSE;
int ctr = 0;

AUX_RGBImageRec *TextureImage[15];

memset(TextureImage,0,sizeof(void *)*15);

if ((TextureImage[0]=LoadBMP("data/textures/battle/buttonmsg.bmp")) &&
(TextureImage[1]=LoadBMP("data/textures/battle/buttonopt.bmp")) &&
(TextureImage[2]=LoadBMP("data/textures/battle/buttonphase.bmp")) &&
(TextureImage[3]=LoadBMP("data/textures/battle/buttonretire.bmp")) &&
(TextureImage[4]=LoadBMP("data/textures/battle/buttonview.bmp")) &&
(TextureImage[5]=LoadBMP("data/textures/battle/carddummy.bmp")) &&
(TextureImage[6]=LoadBMP("data/textures/battle/chip.bmp")) &&
(TextureImage[7]=LoadBMP("data/textures/battle/chipactive.bmp")) &&
(TextureImage[8]=LoadBMP("data/textures/battle/chipinactive.bmp")) &&
(TextureImage[9]=LoadBMP("data/textures/battle/enemycard.bmp")) &&
(TextureImage[10]=LoadBMP("data/textures/battle/enemychip.bmp")) &&
(TextureImage[11]=LoadBMP("data/textures/battle/fieldgrid.bmp")) &&
(TextureImage[12]=LoadBMP("data/textures/battle/fieldmenu.bmp")) &&
(TextureImage[13]=LoadBMP("data/textures/battle/fieldright.bmp")) &&
(TextureImage[14]=LoadBMP("data/textures/title.bmp")))
{
Status=TRUE;

glGenTextures(15, &texture[0]);
for (ctr=0;ctr<15;ctr++)
{
glBindTexture(GL_TEXTURE_2D, texture[ctr]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[ctr]->sizeX,
TextureImage[ctr]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[ctr]->data);
}
}

for (ctr=0;ctr<15;ctr++)
{
if (TextureImage[ctr])
{
if (TextureImage[ctr]->data)
{
free(TextureImage[ctr]->data);
}

free(TextureImage[ctr]);
}
}

return Status;
}



Share this post


Link to post
Share on other sites
Did you write the AUX_RGBImageRec class yourself? It looks like it is storing 3 bytes per pixel (RGB) now. You want it to store an alpha byte with each pixel as well (RGBA). You will have to modify the class to support this. Then when you load the bitmap from the file loop through it and assign alpha values for each pixel (255 for most pixels, 0 for the ones which match the key value).

You might also want to consider switching to a file format that supports alpha (like tga).

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