Color Keying

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
rabbit
Posts: 15
Joined: Wed Sep 02, 2009 12:39 pm
Location: Sydney, Australia

Color Keying

Post by rabbit »

Definition: A color key is a color on a texture which is not drawn.

I am loading directx meshes and I would like to set color(0,0,0,0) as a color key. How is this done in Irrlicht?

I am baking models in c4d and all I have 2 do is save textures in the same folder as the mesh. Textures are loaded automatically.

If possible I would also like to know how to set make make a model transparent.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You can use the video driver method makeColorKeyTexture. This sets the chosen color to full transparency. You have to set the material type to TRANSPARENT_ALPHA_CHANNEL to make it actually work.
Sundar
Posts: 84
Joined: Mon Jun 05, 2006 11:05 am

Post by Sundar »

To make model transparent you will need to do this

Code: Select all

Model->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
where model is your scenenode.


If you want to make a 2d image transparent

Code: Select all

video::ITexture* images = driver->getTexture("image.bmp");
driver->makeColorKeyTexture(images, core::position2d<s32>(0,0));
rabbit
Posts: 15
Joined: Wed Sep 02, 2009 12:39 pm
Location: Sydney, Australia

Post by rabbit »

What about magenta? r=255, g=0, b =255. I am probably approaching this problem incorrectly but I am having trouble using any other colour than white or black.

Here is the code I am using.

unsigned int meshBufferCount = mesh->getMeshBufferCount();
std::cout << "Material Count: " << meshBufferCount << std::endl;
for(unsigned int i = 0; i < meshBufferCount; i++)
{
irr::scene::IMeshBuffer* meshBuffer = mesh->getMeshBuffer(i);
irr::video::SMaterial& material = meshBuffer->getMaterial();
material.MaterialType = irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;

irr::video::ITexture* texture = material.getTexture(0);
if(texture != NULL)
{
irr::video::SColor colourKey;
colourKey.setAlpha(0); // I change this value to 255, even when using black/white, and it doesnt seem to matter.
colourKey.setRed(255);
colourKey.setGreen(0);
colourKey.setBlue(255);
// works when red =0 or 255 and green = 0 or 255 and blue = 0 or 255
pDevice->getVideoDriver()->makeColorKeyTexture(texture, colourKey);
}
}
rabbit
Posts: 15
Joined: Wed Sep 02, 2009 12:39 pm
Location: Sydney, Australia

Post by rabbit »

I believe that Irrlicht's implementation of colour keying may be quite broken. I found some information about this problem for those of you who are interested.

http://irrlicht.sourceforge.net/docu.ne ... oad_2.html

IVideoDriver.MakeColorKeyTexture Method (ITexture, Color)

"Color key color. Every pixel with this color will get transparent like described above. Please note that the colors of a texture may get converted when loading it, so the color values may not be exactly the same in the engine and for example in picture edit programs. To avoid this problem, you could use the makeColorKeyTexture method, which takes the position of a pixel instead a color value."

Since absolute black/white isn't changed by Irrlicht keying works on them. Of course my issue is that if I bake some 3d models and there is black shadows or white area highlights, then that area will not be rendered at all. Making black and white unsuitable for my purposes. I wanted a project universal colour key (magenta) so that I could simply paint regions I didn't want to render. I do not want to have to hard code or supply xml tags etc to enable or disable black/white colour keys for each object.

Also consider that if colours are changed by Irrlicht and the other method is used of sampling a pixel from the texture, who is to say that that pixel will not be already altered when you sample it and hence that pixel may no longer be your colour key.

I would like to know if anyone has found a reasonably good solution to this problem.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, the text refers to things like JPEG compression. Colors could be altered in this process. If you use, e.g., bmp or png, there are no problems. Just have alook at example 6 where exactly this process is used.
rabbit
Posts: 15
Joined: Wed Sep 02, 2009 12:39 pm
Location: Sydney, Australia

Post by rabbit »

"Well, the text refers to things like JPEG compression. Colors could be altered in this process. If you use, e.g., bmp or png, there are no problems. Just have alook at example 6 where exactly this process is used."

I am certainly getting some weird results then because my colorkeys are not working very well. Any speculation at this point would be welcomed ':D'

I am using Irrlicht 1.5. In my case I am using a 24bit bmp file for the texture. My 24 bit bmp clearly lacks an alpha channel value hence varying the alpha of the colour key made no difference at all. Changing the other values of the colourkey to anything other than (0,0,0,0) or (0,255,255,255) seems to work. But colour tones are not desirable as colour keys due to baking the light sources in the models.

I looked at example 6 and it also uses black as the colourkey; rather than a specific colour such as magenta. I am finding that only black and white work, but inconsistently throughout the scene. This is very confusing. There is something unusual going on and I don't know why this is happening.

As an unrelated issue when I substitute the line
"material.MaterialType = irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; " with "material.MaterialType = irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL; " my zbuffer gets inverted so objects further away are drawn on top of objects infront of me, which is quite wierd. :D
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Irrlicht 1.5 and older versions have had a very broken implementation of the colorkey texture function. I don't recall all problems anymore, but it's probably an idea to update.
Post Reply