Sign in to follow this  

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

This topic is 3677 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[i] == keyColor[0]
  &&ImageObject->data[i+1] == keyColor[1]
  &&ImageObject->data[i+2] == keyColor[2])
  {//transparent
      ImageObject->data[i+3]=0;
  }else{//opaque
      ImageObject->data[i+3]=255;
  }
}


The result , is this: [img]http://img217.imageshack.us/img217/355/imagenr2.jpg[/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
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.


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

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+3]=0;
}else{
copy[y+3]=255;
}
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 :

Renderer.Enable2D();
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glEnable( GL_BLEND );
glEnable(GL_TEXTURE_2D);
bind texture
create quad
glDisable(GL_TEXTURE_2D);
glDisable( GL_BLEND );
Renderer.Disable2d();







Enable2d function:

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, GetScreenWidth(),GetScreenHeight(), 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
PrevRenderMode=RenderMode;
NewRenderMode=RENDER_MODE_2D;







...im really stuck...

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

Share this post


Link to post
Share on other sites

This topic is 3677 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.

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