Solid color and alpha questions

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!
luvcraft
Posts: 23
Joined: Sun Apr 25, 2004 1:00 am
Location: Albuquerque, NM

Solid color and alpha questions

Post by luvcraft »

I have three quick questions I can't find the answers to anywhere in the Irrlicht documentation or here on the forums:

1. Is there a simple way to assign a solid color as the texture of an ISceneNode?

2. I have a texture with a gradual alpha; it is a circle that is solid at the edge, and fades to transparent in the middle. When I use it in Irrlicht, however, it is solid up to the point where the alpha<128, at which point it goes totally transparent. Is there any way to keep the gradual alpha change in Irrlicht?

3. Is there any way to adjust the overall alpha for a texture, for instance if I want a scene node to slowly fade out to total transparency?

Thanks in advance for the help!
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Hava a look at SMaterial::MaterialTypeParam.
luvcraft
Posts: 23
Joined: Sun Apr 25, 2004 1:00 am
Location: Albuquerque, NM

Post by luvcraft »

vitek wrote:Hava a look at SMaterial::MaterialTypeParam.
Adjusting that would only change how much of the texture is completely transparent or completely solid. It doesn't have anything to do with the "gradual transparency" effect that I'm after. Thanks for trying.

At this point it looks like I'll probably need to switch to a different 3D engine, like Ogre3D, especially since I'm handling all of the gameplay code myself and not using any of Irrlicht's gameplay functions.

Oh well.
luvcraft
Posts: 23
Joined: Sun Apr 25, 2004 1:00 am
Location: Albuquerque, NM

Post by luvcraft »

Well, I figured out #1 and #3: They can both be done with an IMeshManipulator, as in the following code snippet:

Code: Select all

IAnimatedMesh* mesh = smgr->getMesh("sydney.md2");
IMeshManipulator *manipulator = smgr->getMeshManipulator();

manipulator->setVertexColors(mesh->getMesh(0),SColor(128,255,0,0));
/*
this sets the alpha for the whole mesh to 128, and colors it red. If the mesh already has a texture, it will "dye" the texture red, otherwise it will just color the whole thing red
*/

int alpha=128;
manipulator->setVertexColorAlpha(mesh->getMesh(0),alpha);
the API says NOT to use this to make adjustments in your main program loop, and that it's only to be used to fix meshes before you use them, but it worked just fine when I used it within my main loop, and I can't find anything better that I'm supposed to use in my main loop.

I still have not found an answer to question #2. Any help would be greatly appreciated.
luvcraft
Posts: 23
Joined: Sun Apr 25, 2004 1:00 am
Location: Albuquerque, NM

Post by luvcraft »

vitek wrote:Hava a look at SMaterial::MaterialTypeParam.
Whoa, nevermind. I just tried that again and it did exactly what I needed for #2. I guess my texture was screwed up the first time I tried it. Thanks! :)X
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Glad you figured #1 and 3 out. I didn't know and have been to busy to look. Now I know. :)
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

SetVertexColor is too slow. I'd use the materialTypeParam for #3 and "animate" the value.
Image
luvcraft
Posts: 23
Joined: Sun Apr 25, 2004 1:00 am
Location: Albuquerque, NM

Post by luvcraft »

Spintz wrote:SetVertexColor is too slow. I'd use the materialTypeParam for #3 and "animate" the value.
If I just wanted one or two things to fade that would work, but since I need it to be more dynamic I'll just have to keep using SetVertexColor until something better gets implemented.
counter123
Posts: 6
Joined: Sat Jul 08, 2006 8:39 am

Post by counter123 »

I have exactly the same problem as your problem #2.

I want to have a texture that gradually fades out towards the edges but Irrlicht just does a hard cut after a certain alpha value, so everything above a certain alpha value is completely transparent and below that value it's completely opaque.

Can you post how exactly you solved that problem?
Emil_halim
Posts: 518
Joined: Tue Mar 29, 2005 9:02 pm
Location: Alex,Egypt
Contact:

Re: Solid color and alpha questions

Post by Emil_halim »

luvcraft wrote: 3. Is there any way to adjust the overall alpha for a texture, for instance if I want a scene node to slowly fade out to total transparency?
see Magic3D library here is a base class of Magic3D library

Code: Select all

/******************************************************

          base Magic class

          part of Magic Library
          By Emil Halim


******************************************************/


#ifndef __I_base_Magic_H_INCLUDED__
#define __I_base_Magic_H_INCLUDED__

/// Base class for doing color and alpha blending effectes,
/// fade in and out effectes, by using Shader Language.

class CBaseMagicSceneNode : public scene::ISceneNode // public IMagic3DObject
{

  protected:
     IShaderProgram*     Sprogram;    ///< pointer to a shader program
     f32                 col[4];      ///< hold the current color component and alpha as float value
     u8                  red;         ///< holds the current color red component as integer value
     u8                  green;       ///< holds the current color green component as integer value
     u8                  blue;        ///< holds the current color blue component as integer value
     f32                 alpha;       ///< holds the current alpha value
     f32                 factor;      ///< using in fading system
     u32                 oldtick;     ///< using in fading system
     u32                 flag;        ///< using in fading system

   public:
     /// constructor
     CBaseMagicSceneNode(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id);

     /// distructor
     ~CBaseMagicSceneNode();

     /// attach a shader program to this scene node
     void setShaderProgram(IShaderProgram* Sp);

     /// set the color component red,green,blue
     void setColor(u8 r,u8 g, u8 b);

     /// set the aplha value
     void setAlpha(f32 a);

     /// fade in function
     void FadeIn(f32 time);

     /// fade out function
     void FadeOut(f32 time);

     /// cycle fading function
     void FadeInOut(f32 in,u32 sty,f32 out,bool flg = false);

     /// return fade factor
     f32  getFadeFactor();

     /// set fade factor
     void setFadeFactor(f32 fctr);
};

//==============================================================================

  /// constructor of the class
  /// set the defulte value for each members
  CBaseMagicSceneNode::CBaseMagicSceneNode(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id) : ISceneNode(parent, mgr, id)
   {
           Sprogram = 0;
           red   =255;
           green =255;
           blue  =255;
           alpha =1.0f;
           flag = 1;
           setAutomaticCulling(false);
   }

  /// distructor of the class
  CBaseMagicSceneNode::~CBaseMagicSceneNode()
   {
   }

  /// allows you to create your own shader program then attach it to this node
  /// or attach the program created in IniMagic Function
  void CBaseMagicSceneNode::setShaderProgram(IShaderProgram* Sp)
   {
           Sprogram = Sp;
   }

  /// set the color component red,green,blue
  void CBaseMagicSceneNode::setColor(u8 r,u8 g, u8 b)
   {
           red   =r;
           green =g;
           blue  =b;
   }

  /// set the aplha value
  void CBaseMagicSceneNode::setAlpha(f32 a)
   {
           alpha=a;
   }

  /// fade in our scene node
  /// time is the time of fading operation
  /// it changes 50 times per second
  void CBaseMagicSceneNode::FadeIn(f32 time)
   {
         u32 tick = GetTickCount();
         if(tick-oldtick > 20)
          {
              oldtick = tick;
              factor += 20.0/time;
              if(factor>1.0)factor=1.0;
          }
         setAlpha(factor);
   }

  /// fade out our scene node
  void CBaseMagicSceneNode::FadeOut(f32 time)
   {
         u32 tick = GetTickCount();
         if(tick-oldtick > 20)
          {
              oldtick = tick;
              factor -= 20.0/time;
              if(factor<0.0)factor=0.0;
          }
         setAlpha(factor);
   }

   /// cycle fading of our scene node
   /// in is the time to fade in
   /// sty is the time befor it start to fade out
   /// in is the time to fade out
   /// flg true to cycle fading operation
  void CBaseMagicSceneNode::FadeInOut(f32 in,u32 sty,f32 out,bool flg)
   {
         u32 tick = GetTickCount();
         if((tick-oldtick > 20) && flag == 1)
          {
              oldtick = tick;
              factor += 20.0/in;
              if(factor>1.0){factor=1.0; flag = 2;}
          }
         if((tick-oldtick > sty) && flag == 2)
          {
              oldtick = tick;
              flag = 3;
          }
         if((tick-oldtick > 20) && flag == 3)
          {
              oldtick = tick;
              factor -= 20.0/out;
              if(factor<0.0)
               {
                   factor=0.0;
                   if(flg)flag=4;
               }
          }
         if((tick-oldtick > sty) && flag == 4)
          {
              oldtick = tick;
              flag = 1;
          }
         setAlpha(factor);
   }

  /// return fade factor
  f32  CBaseMagicSceneNode::getFadeFactor()
   {
         return  factor;
   }

  /// set fade factor
  void CBaseMagicSceneNode::setFadeFactor(f32 fctr)
   {
        factor= fctr;
   }

#endif
as you see , you could assign a solid color for each Obaject,and set it's
alpha value,and fading in and out it.

here is the link of Magic3D library
http://irrlicht.sourceforge.net/phpBB2/ ... &start=225
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Emil,

You posted all that code, and all it does is show how to set a flag to different values over time. It doesn't solve his problem because you don't show how you are rendering the node using the alpha value.

Travis
Emil_halim
Posts: 518
Joined: Tue Mar 29, 2005 9:02 pm
Location: Alex,Egypt
Contact:

Post by Emil_halim »

vitek wrote:Emil,

You posted all that code, and all it does is show how to set a flag to different values over time. It doesn't solve his problem because you don't show how you are rendering the node using the alpha value.

Travis
Hi vitek

yes , i post only part of magic3D library, it is not good idea to post all the code here, so if he see this part is usful he could download the library and see all the classes, and if he not like it , no problem no big code have posted.
Pazystamo
Posts: 115
Joined: Sat Dec 03, 2005 5:56 pm
Location: Lithuania
Contact:

Post by Pazystamo »

For #2 it is very easy... I alredy post it in forum somewhere:

Code: Select all

bill->setMaterialTexture(0, driver->getTexture("sun.tga"));
bill->getMaterial(0).MaterialTypeParam = 0.01f;	//this
bill->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL);//and this line
You can find it in my atmosphere source code.
0.01f is very small number,so texture has nice smooth edge, you can try to animate this value (rise it) to get sharp edge until texture disappere.
My project in forum- ATMOsphere - new dynamic sky dome for Irrlicht
counter123
Posts: 6
Joined: Sat Jul 08, 2006 8:39 am

Post by counter123 »

Pazystamo wrote:For #2 it is very easy... I alredy post it in forum somewhere:

Code: Select all

bill->setMaterialTexture(0, driver->getTexture("sun.tga"));
bill->getMaterial(0).MaterialTypeParam = 0.01f;	//this
bill->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL);//and this line
You can find it in my atmosphere source code.
0.01f is very small number,so texture has nice smooth edge, you can try to animate this value (rise it) to get sharp edge until texture disappere.
Well, it seems to work for the edges, but if I make the alpha channel all grey, it doesn't look transparent. Until now I've been using EMT_TRANSPARENT_ADD_COLOR which looks a lot better than any of my alpha channel experiments. However EMT_TRANSPARENT_ADD_COLOR doesn't work very well if the background is very bright...
Pazystamo
Posts: 115
Joined: Sat Dec 03, 2005 5:56 pm
Location: Lithuania
Contact:

Post by Pazystamo »

counter123 wrote:Well, it seems to work for the edges, but if I make the alpha channel all grey, it doesn't look transparent. Until now I've been using EMT_TRANSPARENT_ADD_COLOR which looks a lot better than any of my alpha channel experiments. However EMT_TRANSPARENT_ADD_COLOR doesn't work very well if the background is very bright...
Everything is working, maybe your textures are bad?
ImageImage
My project in forum- ATMOsphere - new dynamic sky dome for Irrlicht
Post Reply