Jump to content
  • Advertisement
Sign in to follow this  

(Solved)[OpenGL] Removing color key (what im doing wrong?)

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

I have the following code:
 int  keyColor[3] = { 255, 0, 255 };
ImageObject   is just a class that holds information about the image(Data,width,height, and channels).
load image "cube.tga" ..//4 channels..
for (int i=0;i<ImageObject->w*Img->h;i+=3)
if (ImageObject->data == keyColor[0]
  &&ImageObject->data[i+1] == keyColor[1]
  &&ImageObject->data[i+2] == keyColor[2])

The result , is this: imagenr2.jpg Its obvious that im doing something wrong. Can someone help me a little? Thanks. [Edited by - 3Dgonewild on November 22, 2007 10:26:05 AM]

Share this post

Link to post
Share on other sites
for (int i=0; i<ImageObject->w * Img->h; i+=3)
i+=3 should be i+=4; otherwise you're constantly reading a value you recently changed.

Also, I'm guessing ImageObject->w points to the width of the image and Img->h points to the height, in which case you'll need to multiply by the channels (4) again so the conversion doesn't stop 1/4th into the image.

for (int i=0; i<ImageObject->w*Img->h*4; i+=4)

Share this post

Link to post
Share on other sites

Well , now it shows the image normally(with the color key).
Im afraid i must be doing something wrong with the layer mask(im using gimp by the way).


Share this post

Link to post
Share on other sites
**edit : never mind , problem solved :)**

I've edited the code a little , and now it removes the black background( becomes white).

int keyColor[3] = { 0, 0, 0 }; //<---Black
unsigned char *copy=NULL;
copy = new unsigned char[Img->w*Img->h*Img->channels];
for (int x=0,y=0; x<Img->w*Img->h*3;x+=3,y+=4)
unsigned char r = Img->data[x];
unsigned char g = Img->data[x+1];
unsigned char b = Img->data[x+2];
if ( r==keyColor[0] && g==keyColor[1] && b==keyColor[2])
copy[y] = r;
copy[y+1] = g;
copy[y+2] = b;
glTexImage2D (GL_TEXTURE_2D, 0, Image_Type,Img->w, Img->h, 0, Image_Type, GL_UNSIGNED_BYTE, copy);
if(copy) delete []copy;

But if i just change glteximage2d() parameters a little :

glTexImage2D (GL_TEXTURE_2D, 0, Image_Type,Img->w, Img->h, 0, Image_Type, GL_UNSIGNED_BYTE, Img->data);

Im getting the same result (black background becomes white).

How i render the 2d texture :

glEnable( GL_BLEND );
bind texture
create quad
glDisable( GL_BLEND );

Enable2d function:

glMatrixMode( GL_PROJECTION );
glOrtho( 0, GetScreenWidth(),GetScreenHeight(), 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );

...im really stuck...

[Edited by - 3Dgonewild on November 22, 2007 10:40:31 AM]

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!