Sign in to follow this  

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

Recommended Posts

3Dgonewild    174
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[i] == keyColor[0]
  &&ImageObject->data[i+1] == keyColor[1]
  &&ImageObject->data[i+2] == keyColor[2])

The result , is this: [img][/img] 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
MonkeyInBlack    263
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
3Dgonewild    174
**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 );
NewRenderMode=RENDER_MODE_2D; really stuck...

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

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