Coloring the nodes (in realtime)

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
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Coloring the nodes (in realtime)

Post by suliman »

Hi
Can i use a single model file (3ds or whatever) and then color them to (for example) make a blue and red tank, so i can set which team they belong to?

Thanks
Erik
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Coloring the nodes (in realtime)

Post by The_Glitch »

Yes that's easy If your using Irrlicht default shaders or your own you could just assign the color values in your app like this.

Code: Select all

 
 
video::SColorf RColor(yourcolorRED, yourcolorGREEN, yourcolorBLUE, yourcolorALPHA); // use this line If it's a Irrlicht standard shader.
        services->setPixelShaderConstant(ColorDiffuse, reinterpret_cast<f32*>(&RColor), 4);   // Use both lines in shader callback If it's a custom or non Irrlicht shader.
 
 
And some where in your app the "yourcolor's above" can be assigned and changed by some event.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Coloring the nodes (in realtime)

Post by mongoose7 »

You can also set a different texture to each instance of the model.
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Coloring the nodes (in realtime)

Post by suliman »

Thanks!
But typically i make 3ds-files and add the textures in 3dS max, and then just keep the texture files in the same folder. Is there any irrlicht guide how to handle models/meshes? I mean a REALLY noob-friendly one? (concerning lightning, different fileformats, texturing etc). Ive looked at the tutorials but they dont really help.

Erik
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Coloring the nodes (in realtime)

Post by hendu »

http://irrlicht.sourceforge.net/forum/v ... =6&t=49868

Guess what I did here ;) A shader uniform similar to The_Glitch's.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Coloring the nodes (in realtime)

Post by CuteAlien »

If it's single-color you don't need a shader. You can set the SMaterial color values. You can use example 22 to test differnt effect combinations (and yeah - the gui of that examples is still confusing which is my fault, I'll improve it some day).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Coloring the nodes (in realtime)

Post by suliman »

single color might work, i try this:

Code: Select all

    o->node= gfx.smgr->addAnimatedMeshSceneNode( rrr.unitMeshes[type],0,1,vect3d(0,0,0),vect3d(0,0,0),vect3d(scale,scale,scale));
    o->node->setMaterialFlag(video::EMF_LIGHTING,false);
    o->node->setMaterialFlag(EMF_FOG_ENABLE, true); 
    o->node->setPosition(o->pos);
 
    o->node->getMaterial(0).AmbientColor = SColor(255,255,0,0);
But it doesnt turn red. Probably im way off, could you help me with the calls? I want to "multiply" the already present colors (all textures of the model).

(i have no global light (or any lights at all for that matter right now) so i turn of lighting, is this wrong? I can see all textures fully lit all the time)
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Coloring the nodes (in realtime)

Post by mongoose7 »

You've turned off lighting, so the ambient colour will not be used. I think you should be looking at the diffuse colour.
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Coloring the nodes (in realtime)

Post by suliman »

You mean like this:

Code: Select all

    o->node->getMaterial(0).DiffuseColor.set(255,255,0,0); 
Still nothing changes. I want to be able to darken, or tint all the node (preferably without adding light sources).

I dont know if this is needed, but still doesnt work

Code: Select all

    int matCount=o->node->getMaterialCount();
    for(int i=0;i<matCount;i++)
        o->node->getMaterial(i).DiffuseColor.set(255,255,0,0); 
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Coloring the nodes (in realtime)

Post by thanhle »

Hi mate,
I'm using this function below.
You can extract and use any part below. I think it will work.

Code: Select all

 
void SetNodeColor(ISceneManager *smgr, video::IVideoDriver *driver, ISceneNode *node, video::SColor newColor)
{
 
             if (node->getType() == ESCENE_NODE_TYPE::ESNT_ANIMATED_MESH)
             {
                 driver->setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, false);
                 video::ITexture* texture = driver->addTexture(core::dimension2d<u32>(1, 1), "onTheFly", video::ECF_A8R8G8B8);
                 driver->setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, true);
                 // get pointer to texture data
                 u32* p = (u32*)texture->lock();
 
 
                 p[0] = newColor.color;
 
                 texture->unlock();
 
 
                 IAnimatedMeshSceneNode *animNode = static_cast<IAnimatedMeshSceneNode*>(node);
                 IAnimatedMesh *mesh = animNode->getMesh();
                 node->setMaterialTexture(0, texture);
 
 
             }
             if (node->getType() == ESNT_MESH
                 || node->getType() == ESNT_CUBE
                 || node->getType() == ESNT_SPHERE
                 || node->getType() == ESNT_WATER_SURFACE
                 || node->getType() == ESNT_Q3SHADER_SCENE_NODE
                 || node->getType() == ESNT_OCTREE
                 )
             {
                 IMesh *mesh = static_cast<IMeshSceneNode*>(node)->getMesh();
                 smgr->getMeshManipulator()->setVertexColors(mesh, newColor);
 
 
             }
 
             if (node->getType() == ESNT_TERRAIN)
             {
 
                 IMesh *mesh = static_cast<ITerrainSceneNode*>(node)->getMesh();
                 smgr->getMeshManipulator()->setVertexColors(mesh, newColor);
             }
 
}
 
Goodluck
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Coloring the nodes (in realtime)

Post by suliman »

Hi!
It works but it just overwrites every texture, replacing them with a single solid color (ignoring alpha of the supplied color).
Is there a way to tint the existing node, so it gets some color, but still remains the textures?
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Coloring the nodes (in realtime)

Post by thanhle »

Sorry mate,
The only way I know how to do that is with light setting. I'm just on the basic level of knowledge on lighting atm.

Maybe irrlicht should allows us to do that without light enable. I think that would be useful. The lib dev should look into this.

Or try Glitch method as above.
Cheers
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Coloring the nodes (in realtime)

Post by mongoose7 »

Have you tried just changing the vertex colours. You do realise, don't you, that thanle's code creates and sets a new texture? Try leaving that texture code out. It is possible, in the fixed-function pipeline, that the vertex colour modulates the texture. But this may depend on the current OpenGL/DX state, which depends on the material type. For that reason, I suggest you change both the vertex colours and the material diffuse.
Post Reply