Modify the color of a mesh

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
Charlie111
Kobold
Posts: 26
Joined: Sun Sep 16, 2007 2:38 pm
Location: France

Modify the color of a mesh

Post by Charlie111 »

Hi,

I would like to change the color of my mesh during the game.
What is the most efficient way to do it??
I think I have to play with the material of the entity but I don't know if it's the must.

Thanks

Charlie
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: Modify the color of a mesh

Post by spacegaier »

Yes, the visual appearance of a mesh is defined by its material script. Those can be modified at runtime.

Code: Select all

m_pMat = m_pEntity->getSubEntity(0)->getMaterial();
m_pMat->getTechnique(0)->getPass(0)->setAmbient(1, 0, 0);
m_pMat->getTechnique(0)->getPass(0)->setDiffuse(1, 0, 0, 0);
m_pEntity->setMaterialName(m_pMat->getName());
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...
Charlie111
Kobold
Posts: 26
Joined: Sun Sep 16, 2007 2:38 pm
Location: France

Re: Modify the color of a mesh

Post by Charlie111 »

Ok thanks for your answer.
Is it time consuming??
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: Modify the color of a mesh

Post by spacegaier »

I assume you mean the performance: I never had any problems with it. I even used to make this call once per frame in order to constantly change the alpha value so that my objects fade in and out all the time.
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...
Charlie111
Kobold
Posts: 26
Joined: Sun Sep 16, 2007 2:38 pm
Location: France

Re: Modify the color of a mesh

Post by Charlie111 »

Yeah that's what I meant, sorry for my english :wink:
User avatar
wacom
Gnome
Posts: 350
Joined: Sun Feb 10, 2008 2:07 pm

Re: Modify the color of a mesh

Post by wacom »

Be aware that changing an entity's material changes all entities which share this material.
Charlie111
Kobold
Posts: 26
Joined: Sun Sep 16, 2007 2:38 pm
Location: France

Re: Modify the color of a mesh

Post by Charlie111 »

Ok so I have to create a new material each time... but I will quickly have some memory issues... any advice?

I want each entity to be independant from all the others.

Anyway I have a problem... I've changed the color but nothing happen on screen (I checked in debug).
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: Modify the color of a mesh

Post by spacegaier »

Did you stick to the example I posted above, meaning that you also applied the altered material to the Ogre::Entity? Just chaning it in the code without reapplying won't make anything happen visually.

Yes, you will need an own material for each Ogre::Entity if you want them to look different. As the materials are nothing big, I would not bother about the RAM.

You can therefore clone one exisiting materal, if you only have to change small things (of course you can also define new ones everytime).

Code: Select all

Ogre::MaterialPtr* m_pMat = m_pExistingMat->clone("NewMatName");
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
ekt
Gremlin
Posts: 150
Joined: Thu Apr 01, 2004 5:55 pm
x 4
Contact:

Re: Modify the color of a mesh

Post by ekt »

<shameless plug>
this could be your solution :)
<shameless plug>
Post Reply