Alpha blending

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Schakal
Posts: 3
Joined: Tue Jan 27, 2009 6:31 pm

Alpha blending

Post 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
Bear_130278
Posts: 237
Joined: Mon Jan 16, 2006 1:18 pm
Location: Odessa,Russian Federation

Post 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)
Do you like VODKA???
Image
Image
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post 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.
Dark_Kilauea
Posts: 368
Joined: Tue Aug 21, 2007 1:43 am
Location: The Middle of Nowhere

Post 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.
rogerborg wrote:Every time someone learns to use a debugger, an angel gets their wings.
Schakal
Posts: 3
Joined: Tue Jan 27, 2009 6:31 pm

Post 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!!
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

You need a pixel shader that does-

Code: Select all

destination = (source1*mask) + (source2*(1-mask));
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
grafikrobot
Posts: 44
Joined: Wed Dec 26, 2007 11:42 pm

Post 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.
Schakal
Posts: 3
Joined: Tue Jan 27, 2009 6:31 pm

Post 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* ??
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post 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.
Pyritie
Posts: 120
Joined: Fri Jan 16, 2009 12:59 pm
Contact:

Post 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?
Hive Workshop | deviantART

I've moved to Ogre.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post 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 :!:
Post Reply