Page 1 of 1

Alpha blending

Posted: Tue Jan 27, 2009 7:09 pm
by Schakal
Hi all,
i am new to Irrlicht :)
And soon I got my first problem:
I want to make an isometric game.
I made two pictures, the one is grass and the other shall be a mask with a isometric shape:

Image

Image

And my goal is to lay the mask over the grass so that i get grass in isometric shape :)
So far so good, ... but how can I do it with Irrlicht?
I guess something like that has been often talked about in this forum,
but my keywords weren't good enough to find a solution =(

Thx for help!
David

Posted: Wed Jan 28, 2009 6:55 am
by Bear_130278
One of the ways:
1.Make a texture with alpha channel, tga for example
2.Make a plane and use this texture as the map for desired polygon.
3.Set the MaterialType = EMT_TRANSPARENT_ALPHA_CHANNEL_REF

Voila! You got what you wanted 8)
But this is stupid brute force , a fake 2D graphics 8)))

It is much wiser to set up the camera of the engine, to work as an orthographic one 8)))
So make any landscape you want, place it on the scene, and make ortho camera - you got what you need in a 3D fashion 8)

Posted: Wed Jan 28, 2009 9:38 am
by arras
If you want to do it yourself you can access grass texture loaded in to Irrlicht via lock/unlock function and manually edit pixels alpha based on mask. Or use IImage interface for the same purpose.

Easier way is to load your texture with mask already.
Two ways:
Use tga as suggested since it supports alpha.
Or use makeColorKeyTexture() function from videodriver ...look at description for more info.

Posted: Thu Jan 29, 2009 1:05 am
by Dark_Kilauea
Or you could do that step in the art pipeline, using a program like GIMP. Prevents you from having to do it later in the program, increasing the time to render each frame or load the application.

Posted: Thu Jan 29, 2009 6:34 pm
by Schakal
@Bear: Sounds reasonable! :)

@arras:
I tried this code:

Code: Select all


void MaskImage(video::ITexture* image,video::ITexture* mask,core::rect<s32> type)
{
   s32* ibuf = (s32*)image->lock();
   s32* mbuf = (s32*)mask->lock();


   for(int x = type.UpperLeftCorner.X;x<type.LowerRightCorner.X;x++)
   {
      for(int y = type.UpperLeftCorner.Y;y<type.LowerRightCorner.Y;y++)
      {
         *((u8*)ibuf+image->getPitch()*y+x*4+3)=255; //*((u8*)mbuf+mask->getPitch()*y+x*4+3);
      }
   }
   image->unlock();
   mask->unlock();
}

But I get some kind of memory error there =/

@Dark_Kilauea:
I prefer to render it at the start of the application,
because otherwise I would need X*Y images (X=tile sorts, Y=masks)
And that would be really much after some time =)


Thx for your help!!

Posted: Thu Jan 29, 2009 9:52 pm
by bitplane
You need a pixel shader that does-

Code: Select all

destination = (source1*mask) + (source2*(1-mask));

Posted: Fri Jan 30, 2009 12:56 am
by grafikrobot
bitplane wrote:You need a pixel shader that does-

Code: Select all

destination = (source1*mask) + (source2*(1-mask));
Or you could do it with a multi-texture op.

Posted: Fri Jan 30, 2009 11:06 am
by Schakal
Ok guys,
I got a bit further with my problem:

I just wrote "x*2" instead of "x*4".
The result was a blue version of my grass :D
So x*2 would mean (because each single pixel is changed),
that my texture has a 8-bit format (4bit*2)
And it seems that
I cant change the alpha value.
So I guess that I have to change the format of the texture?!
And how to do that?!

Edit:
Ok, to change the bit format I have used
driver->setTextureCreationFlag(ETCF_ALWAYS_32_BIT,true);
But it seems that nothing has changed now?! :(

Well, I think that won't ever work, so new run:

Code: Select all


void MaskImage(video::ITexture* image,video::ITexture* mask,core::rect<s32> type)
{
   SMaterial* mymat  = new SMaterial;
   mymat->setTexture(1,image);
   mymat->setTexture(2,mask);
   mymat->MaterialType = EMT_TRANSPARENT_ALPHA_CHANNEL_REF;

   // ....

Inspired by Bear_130278 i try this. But do I need now barely a plane or can I make SMaterial into ITexture* ??

Posted: Fri Jan 30, 2009 12:20 pm
by hybrid
Be careful with 'new', this one looks incorrect at this place...
Anyway, there's no way to p a material into a texture, it's the other way round. Still, you cannot use a material to do 2d rendering, because the 2d methods have their own set of parameters. You have to use those, there's no other way. And 2d just uses 1 texture, so no way to use an alpha mask texture. Anyway, Irrlicht doesn't support alpha maps at all.
Ok, final point, try to check the texture format before writing into arbitrary memory positions.

Posted: Fri Jan 30, 2009 1:15 pm
by Pyritie
hybrid wrote:Be careful with 'new', this one looks incorrect at this place...
Anyway, there's no way to p a material into a texture, it's the other way round. Still, you cannot use a material to do 2d rendering, because the 2d methods have their own set of parameters. You have to use those, there's no other way. And 2d just uses 1 texture, so no way to use an alpha mask texture. Anyway, Irrlicht doesn't support alpha maps at all.
Ok, final point, try to check the texture format before writing into arbitrary memory positions.
What do we use for transparent textures then? PNGs, TGAs with alpha channel (which after this I assume is a no), or something else?

Posted: Fri Jan 30, 2009 2:56 pm
by hybrid
Irrlicht supports the alpha channel, but not mixing one diffuse texture with another channel from a second texture. But once you check the 2d methods you'll find a hint for alpha channel and vertex alpha, and both are fully functional! We even have examples showing how to do this :!: