Blend 2 textures (2 images = .jpg +.png)

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
jorgerosa
Gnoblar
Posts: 19
Joined: Mon Dec 07, 2009 7:50 pm
x 3
Contact:

Blend 2 textures (2 images = .jpg +.png)

Post by jorgerosa »

.

1) My issue in an image:
http://sites.google.com/site/jorgerosap ... erials.jpg

2 a) My current code is:

Code: Select all

// My material:
MaterialPtr mat = Ogre::MaterialManager::getSingleton().create("MyMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
Ogre::Pass* pass = mat->getTechnique(0)->createPass();

// 1st Layer: The .jpg image
Ogre::TextureUnitState* texture1 = pass->createTextureUnitState();
texture1->setTextureName("sky.jpg");

// 2nd Layer: The .png image
Ogre::TextureUnitState* texture2 = pass->createTextureUnitState();
texture2->setTextureName("hello.png");
texture2->setColourOperation(Ogre::LBO_ALPHA_BLEND); // <--- .png alpha is applied correctly, as shown in the 1st row, in the above image
2 b) Or... I can achieve the exactly same (correct) result, doing this in the .material file:

Code: Select all

// My material:
material MyMaterial
{
   technique
   {
      pass
      {
         texture_unit
         {
           // 1st Layer: The .jpg image 
           texture sky.jpg
         }
         texture_unit
         {            
           // 2nd Layer: The .png image 
           texture hello.png 
           colour_op alpha_blend // <--- .png alpha is applied correctly, as shown in the 1st row, in the above image              
         }
      }
   }
}

3) THE ISSUE:
Now... How can I achieve the result, as shown in the 2nd row, in the above image?...
(eg: An 20% alpha value for the entire .png) Thanks!
Last edited by jorgerosa on Sun Mar 01, 2015 3:38 am, edited 32 times in total.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Blend two images (jpg + png)

Post by Kojack »

Try something like this:

Code: Select all

texture2->setAlphaOperation(Ogre::LayerBlendOperationEx::LBX_MODULATE,Ogre::LayerBlendSource::LBS_MANUAL, Ogre::LayerBlendSource::LBS_TEXTURE, 0.2);
That should multiply the second texture's alpha by 0.2.

Although off the top of my head I'm not sure if it will work directly like that, with the alpha operation and the blending colour operation in the same texture unit, a third texture unit might be needed (unit one loads the first texture, unit 2 preserves the colour from unit 1 while loading and multiplying the alpha from the second texture, unit 3 blends the second texture's colour with the first texture using the alpha from the second unit).
User avatar
jorgerosa
Gnoblar
Posts: 19
Joined: Mon Dec 07, 2009 7:50 pm
x 3
Contact:

Re: Blend two images (jpg + png)

Post by jorgerosa »

Kojack wrote:Try something like this (...)
Thanks for the tip Kojack!
I´m still fighting with this... Instead of doing this programmatically right away, I´m trying now on a ".material" file (try/error) to see if I get any result. I´ve been searching the manual, samples, web, etc... Shouldnt be difficult, but its the first time for me on this stuff. Any other tips?... :?
User avatar
shadowfeign
Goblin
Posts: 213
Joined: Mon Jan 26, 2009 11:51 pm
x 15

Re: Blend two textures (images: jpg + png)

Post by shadowfeign »

I think this should do what you want. It's basically the same thing in the wiki. http://www.ogre3d.org/tikiwiki/Materials scroll down to "Dynamic texture transition", which also gives you the example of doing part of it in code. Can't tell you if that's the best way, but it seems to work at least with the textures I had on hand.

Code: Select all

material testing
{
   technique
   {
      pass
      {
         texture_unit
         {
           texture grass128.jpg
         }
      }
      pass
      {
         scene_blend alpha_blend
         texture_unit
         {
            texture mask128.png              
            alpha_op_ex modulate src_manual src_texture 0.2
         }
         texture_unit
         {
            texture dirt128.jpg
            colour_op_ex blend_current_alpha src_texture src_current
         }
      }
   }
}
User avatar
jorgerosa
Gnoblar
Posts: 19
Joined: Mon Dec 07, 2009 7:50 pm
x 3
Contact:

Re: Blend two textures (images: jpg + png)

Post by jorgerosa »

shadowfeign wrote:It's basically the same thing in the wiki (...)
Thanks for the tip shadowfeign!
I´m still trying to blend these 2 images... With no luck yet...
User avatar
spacegaier
OGRE Team Member
OGRE Team Member
Posts: 4304
Joined: Mon Feb 04, 2008 2:02 pm
Location: Germany
x 135
Contact:

Re: Blend two textures (images: jpg + png)

Post by spacegaier »

jorgerosa wrote:
shadowfeign wrote:It's basically the same thing in the wiki (...)
Thanks for the tip shadowfeign!
I´m still trying to blend these 2 images... With no luck yet...
Did you try the solution posted by shadowfeign? Didn't it work for you? What was the result you got?
Ogre Admin [Admin, Dev, PR, Finance, Wiki, etc.] | BasicOgreFramework | AdvancedOgreFramework
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...
User avatar
jorgerosa
Gnoblar
Posts: 19
Joined: Mon Dec 07, 2009 7:50 pm
x 3
Contact:

Re: Blend 2 textures (.jpg +.png)

Post by jorgerosa »

alpha_op_ex source1 src_manual src_texture 0.2 <--- Almost!... my custom alpha is applied... but results in a 20% opacity rectangle... not the "hello" text...
- Tried too to do this programatically... Also tried to add this in a new pass, tried with a new texture_unit as mask too... etc, etc, etc...
- I´m still doing all sort of combinations here (based on your replies, plus examples in Ogre wiki...)

Code: Select all

// My material:
material MyMaterial
{
   technique
   {
      pass
      {
         texture_unit
         {
           // 1st Layer: The .jpg image 
           texture sky.jpg
         }
         texture_unit
         {            
           // 2nd Layer: The .png image 
           texture hello.png
           alpha_op_ex source1 src_manual src_texture 0.2  // <--- Almost... my custom alpha is applied... results in a 20% rectangle... not the "hello" text...  
           // Tried too to do this programatically... Also tried to add this in a new pass, tried with a new texture_unit as mask too... etc, etc, etc...
           // I´m still doing all sort of combinations here (based on your replies, plus examples in Ogre wiki...)
         }
      }
   }
}
User avatar
spacegaier
OGRE Team Member
OGRE Team Member
Posts: 4304
Joined: Mon Feb 04, 2008 2:02 pm
Location: Germany
x 135
Contact:

Re: Blend 2 textures (.jpg +.png)

Post by spacegaier »

jorgerosa wrote:my custom alpha is applied... but results in a 20% opacity rectangle... not the "hello" text...
At least to me that is not clear. Can you perhaps show a screenshot of what you achieved now compared to those in your first post.
Ogre Admin [Admin, Dev, PR, Finance, Wiki, etc.] | BasicOgreFramework | AdvancedOgreFramework
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...
User avatar
jorgerosa
Gnoblar
Posts: 19
Joined: Mon Dec 07, 2009 7:50 pm
x 3
Contact:

Re: Blend 2 textures (.jpg +.png)

Post by jorgerosa »

SOLUTION!
Ok, FINALLY I achieved the result that I wanted to. Thanks all for the lights & tips!
I´m posting it here to help others, and so you could spend more time with your beautiful girlfriend instead of looking to ugly Ogres and Shreks... for hours!... :mrgreen:
(Improved code, snippets, ideas, etc... are still welcome)

Code: Select all

// My material:
material MyMaterial
{
   technique
   {
      pass
      {
         scene_blend alpha_blend
         depth_write off
         texture_unit
         {
           // 1st Layer: The .jpg image 
           texture sky.jpg
         }
      }

      pass
      {
         scene_blend alpha_blend
         depth_write off
         texture_unit
         {
           // 2nd Layer: The .png image 
           texture hello.png
           alpha_op_ex modulate src_manual src_texture 0.2
         }
      }
   }
}
Note: The "rectangle issue" I found that was because I must have: depth_write off
For the rest, I found that I must have 2 different "pass" with the exactly same settings. (Even one is a .jpg and the other an .png)
And only then the: alpha_op_ex.
Post Reply