Alpha Transparency

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!
joy
Posts: 124
Joined: Tue Apr 27, 2004 9:15 am
Location: Germany

Alpha Transparency

Post by joy »

I know I can set the transparency of a mesh with the types EMT_TRANSPARENT_VERTEX_ALPHA or EMT_TRANSPARENT_ADD_COLOR. But I like to set the alpha value userdefined (for example to 200 or 50). So I wrote that function for that:

Code: Select all

void CElControl::setAlphaTransparancy()
{
    CScreen* pScreenOb = (CScreen*) m_pScreen;
    DWORD* pData = (DWORD*) (pScreenOb->pTexture->lock());
    DWORD* pPos = pData;
    irr::core::dimension2d<irr::s32> len = pScreenOb->pTexture->getSize();
    int alpha = 50;
    for  (irr::s32 i = 0; i<len.Width; i++)
    {
        for (irr::s32 j = 0; j<len.Height; j++)
        {
            *pPos &=0x00ffffff;
            *pPos += alpha<<24;
            pPos ++;
        }
    }
    
    pScreenOb->pTexture->unlock();
    pScreenOb->pTexture;
}
Additionally I set m_pDriver ->setTextureCreationFlag(irr::video::ETCF_ALWAYS_32_BIT, true); and the materialtype to EMT_TRANSPARENT_ALPHA_CHANNEL. I know that the API says that this type is not implemented but one guy in the bug forum says that the type is fine except of one little bug. So I set this to things to make sure to get the texture with 32bit and the Material works with the texture and its alphachannel.
I tried my function and it seems as it sets the alphabits right but nothing seems to happen to the mesh. Any ideas??
Thank you in advance
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

EMT_TRANSPARENT_ALPHA_CHANNEL is implemented in DX8/9 but not OpenGL.

Maybe (as a test) create a texture with alpha channel (TGA 32 bit) and see if it works. This might help determining if the problem is in your function or in rendering the mesh.
joy
Posts: 124
Joined: Tue Apr 27, 2004 9:15 am
Location: Germany

Post by joy »

I created a tga 32 bit . And still the mesh seems solid. But when I go throw the debug mode it seems as it sets the alpha value to the value I want. It seems as the result is not displayed though.
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

Do the other modes work? (EMT_TRANSPARENT_ADD_COLOR etc.)

Could you post your mesh/texture setup code?
joy
Posts: 124
Joined: Tue Apr 27, 2004 9:15 am
Location: Germany

Post by joy »

Here the code where I setup my meshes:

Code: Select all

 i.pElNode =  m_pDevice->getSceneManager()->addAnimatedMeshSceneNode(((irr::scene::IAnimatedMesh*)i.pElType), 
            NULL, -1, irr::core::vector3df(0,0,0), irr::core::vector3df(0.0f,0.0f,0.0f), 
            irr::core::vector3df(0.02f, 0.02f, 0.02f) ); 
        
        i.Material = irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL;

        if (i.pElNode !=NULL)
        {
            pTexture =m_pDriver->getTexture(pTexPath);
            i.pElNode->setMaterialTexture(0, pTexture);
            i.pElNode->setMaterialType(i.Material);
            i.pElNode->setMaterialFlag(irr::video::EMF_LIGHTING, false);
         }
I work with these i. because I have a struct where I "save" my attributes.
The other modes seem to work. The EMT_Transparent_ADD_COLOR for example sets the dark colors to transparent and the bright colors get a little bit transparent. They seem to get white but still transparent... But as I guess it works alright.
joy
Posts: 124
Joined: Tue Apr 27, 2004 9:15 am
Location: Germany

Post by joy »

Would be great if someone could help me out with that... please
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

Mh, strange. Ok...

1.)
joy wrote: I created a tga 32 bit . And still the mesh seems solid.
Did the tga that you created really have some greyscale alpha channel that would make certain areas transparent in your mesh? For example like the texture I used here:

http://irrlicht.sourceforge.net/phpBB2/ ... php?t=2977

(The shadow there is a flattend testscenenode with the shown texture).

2.) In your function, try to modify another color channel (like red or green) to see if it affects the texture at all.
joy
Posts: 124
Joined: Tue Apr 27, 2004 9:15 am
Location: Germany

Post by joy »

You are so right, although it seems as he does the right thing in the debug modus there is no differences when I change the R as I tried it with the alpha value. Mmmh, So I do something wrong... What the heck is it?

And I always was wondering how you created the two pictures of the shadow. I am not able to have the alpha channel as a different layer. How did you do that?

thank you
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

Maybe take a look at CVideoNull::makeColorKeyTexture(). This function is modifying the alpha channel of a texture.

I created the texture in Photoshop (6.0). First I create a regular image. Then in the Channels tab (Kanäle) (next to Layers (Ebenen)) I create a new channel (with the create button at the bottom nect to the trash bin) which is called "Alpha 1" by default. There I define the greyscale alpha info (white=solid to black = transparent). If I save it as TGA in 32 bit (important) then the alpha channel is included and also recognized by irrlicht.
joy
Posts: 124
Joined: Tue Apr 27, 2004 9:15 am
Location: Germany

Post by joy »

Ok I don't know why but I think it works now...

But I tried to create a tga as you described it, and in Photoshop it looks quite good, but in Irrlicht it does not have the transparency I created in Photoshop. Anything else I need to consider?

Thank you very much for your answers. As you see it helps even though I don't know how :wink:
joy
Posts: 124
Joined: Tue Apr 27, 2004 9:15 am
Location: Germany

Post by joy »

Oh I see...
Where did you explicit difine that black is transparent and white is solid? By just putting black and white over the image it is not done yet , is it?
joy
Posts: 124
Joined: Tue Apr 27, 2004 9:15 am
Location: Germany

Post by joy »

Ok next problem with that. Now I know why it didn't work before. If I use a tga 32bit which is solid from the beginning, my function works fine. But if I use this function on a tga 32bit with already transparent parts in it (I just have a transparent working space in Photoshop and erase some parts of the texture, so that it is transparent at certain spots), the function doesn't work at all. I couldn't set the transparency like you did, jox, so I couldn't try it with these textures.
So anyone has a guess why it won't work if there are some transparent parts in the texture already?
vermeer
Posts: 2017
Joined: Wed Jan 21, 2004 3:22 pm
Contact:

Post by vermeer »

no clues in the programing side of life, but...

in gfx, my field, I'd say...a tga can't save transparency, only alpha channel. A png does save transparency.

If you are seing the parts of image transparent in adobe before saving...does not mean it's gonna be saved in tga. reopen the tga in adobe and surely u'll see the supposedly now transparent parts as solid white.

PNG does allow it. In java is widely used as is supported. Ppl there just delete a part, and as it's seen in adobe will be seen in java code.

But for tga, the proccess in adobe (traslate the knowledge to Gimp is just the same concept) is save th eselection , selecting previously the part u want transparent, so it becomes a new channel.

U can modify it painting in quick mask mode, or modify with selection tools (lasso , etc) and save it again with same name -the selection(channel, really)-
Finally making games again!
http://www.konekogames.com
vermeer
Posts: 2017
Joined: Wed Jan 21, 2004 3:22 pm
Contact:

Post by vermeer »

ok, in adobe, several different ways to create a TGA 32 bits with alpha channel :


a) as I explained, just doing a lasso, marquee, circle, whatever selection (you can create in quick mask mode, but is more complex to explain) and go to selection top menu, save selection. That'd be then a saved selection u can recover, but also becomes and alpha channel.

or

b) top menu Window. Channels (in window menu you set the windows/pallettes u wanna see in adobe) . So u now see the channels pallete/dialog. There u press the tiny top left triangle and choose "new channel".Or click on the tiny "new" icon in the bottom of the channels window. Now u see a black screen. Now if u paint with white, white is what would be "protected" I think. Black is transparent in the future tga. Greys are for partially transparent stuff. If u for example do a circle selection here (do not save selection with this method; ur actually working directly on the channel), fill it with white, and apply a big feather, u'll see a feathered circle, useful fo rexample for glows sprites ingame.
To see the channel content as ur doing, two ways: when u creat a new channel, u see that u're put automatically in that alpha channel editing. but in case u reopen this file again, u'll be as u start, in rgb channels model, seeing only the standard texture, not the alpha channel.Then , way to switch to channels mode is, in channels window/pallete, click on the chanel icon/name that u know is ur alpha chanel("alphal 1", usually). Ok? capici?
At time to save, as always, 32 bits with alpha channel.
Finally making games again!
http://www.konekogames.com
joy
Posts: 124
Joined: Tue Apr 27, 2004 9:15 am
Location: Germany

Getting lost...

Post by joy »

I told you that the function I wrote is working? Ok it is not. I thought it would but I didn't test it so I always saw just one side of the mesh and that looked transparent. But all other sides of the mesh were not transparent at all!!!

The red one is my object, the grey around it is my background.

ImageImageImage

the second picture is my object when I set alpha = 100 and the last picture is rotated. You can see all other sides of the mesh is not transparent but the front looks like it would be transparent.

Any ideas? Is this a bug?
Post Reply