[SOLVED] setting transparency (alpha) for spherescenenode?

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.
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

[SOLVED] setting transparency (alpha) for spherescenenode?

Post by pandoragami »

Hi All,

Sorry for asking such a noobish question considering that this has probably been answered before but I did search and found very little on how to do it, including this page

http://irrlicht.sourcearchive.com/docum ... ource.html

Anyways I have this code where I create a sphere and add a texture, what I wanted to do was to create motion trails behind it as it moves along using alpha blending methods but I can't seem to change the alpha (tansparency) of the node itself. I did find this page

http://irrlicht.sourceforge.net/forum/v ... php?t=2179

Where an animated scene node is alpha blended for each frame differently, any suggestions for just a regular sphere node?

Code: Select all

scene::ISceneNode* sphere = smgr->addSphereSceneNode( radius, 32, 0, -1, core::vector3df(0, Y, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    sphere->setMaterialFlag(video::EMF_LIGHTING, false);
    sphere->setMaterialTexture( 0, driver->getTexture("image.bmp"));
Last edited by pandoragami on Mon Mar 02, 2015 9:22 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9689
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: setting transparency (alpha) for spherescenenode?

Post by CuteAlien »

You can try to experiment with IMeshManipulator::setVertexColorAlpha. As each sphere-node has it's own mesh that should work.
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
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: setting transparency (alpha) for spherescenenode?

Post by pandoragami »

CuteAlien wrote:You can try to experiment with IMeshManipulator::setVertexColorAlpha. As each sphere-node has it's own mesh that should work.
Thanks so far, I tried this

Code: Select all

scene::IMeshManipulator* mesh = smgr->getMeshManipulator();
mesh->setVertexColorAlpha(sphere, 0);
But the compiler complains that my sphere isn't an IMesh* type?

Code: Select all

error: no matching function for call to 'irr::scene::IMeshManipulator::setVertexColorAlpha(irr::scene::ISceneNode*&, int)'|
CuteAlien
Admin
Posts: 9689
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: setting transparency (alpha) for spherescenenode?

Post by CuteAlien »

Use sphere->getMesh() to get the mesh.
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
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: setting transparency (alpha) for spherescenenode?

Post by pandoragami »

CuteAlien wrote:Use sphere->getMesh() to get the mesh.

Tried but got this error.

Code: Select all

 
scene::ISceneNode* sphere = smgr->addSphereSceneNode(radius, 32, 0, -1, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    sphere->setMaterialFlag(video::EMF_LIGHTING, false);
    sphere->setMaterialTexture( 0, driver->getTexture("graph.jpg"));
 
    scene::IMeshManipulator* mesh = smgr->getMeshManipulator();
    mesh->setVertexColorAlpha(sphere->getMesh(), 0);
 
error: 'class irr::scene::ISceneNode' has no member named 'getMesh'|
CuteAlien
Admin
Posts: 9689
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: setting transparency (alpha) for spherescenenode?

Post by CuteAlien »

Well, then take a look at the documentation which type addSphereSceneNode does return: http://irrlicht.sourceforge.net/docu/cl ... 63970cfc04

Maybe you are lucky and it's real type does have such a function ;-)
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
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: setting transparency (alpha) for spherescenenode?

Post by pandoragami »

CuteAlien wrote:Well, then take a look at the documentation which type addSphereSceneNode does return: http://irrlicht.sourceforge.net/docu/cl ... 63970cfc04

Maybe you are lucky and it's real type does have such a function ;-)
It returns a type IMeshSceneNode, but how come ISceneNode works?

Anyways this compiles,

Code: Select all

scene::IMeshSceneNode* sphere = smgr->addSphereSceneNode(radius, 32, 0, -1, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    sphere->setMaterialFlag(video::EMF_LIGHTING, false);
    sphere->setMaterialTexture( 0, driver->getTexture("graph.jpg"));
 
    scene::IMeshManipulator* mesh = smgr->getMeshManipulator();
    mesh->setVertexColorAlpha(sphere->getMesh(), 0);
But I was expecting the sphere to vanish since the second parameter is zero in mesh->setVertexColorAlpha(sphere->getMesh(), 0)

Am I misunderstanding alpha channels? Does this not work with textured meshes?
CuteAlien
Admin
Posts: 9689
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: setting transparency (alpha) for spherescenenode?

Post by CuteAlien »

IMeshSceneNode is derived from ISceneNode, meaning it has everything ISceneNode has + some additional stuff. That's why you can always cast safely to the base-classes.

Not sure right now if the alpha value means transparence or opaque, doesn't seem to be documented. Just try it with 255 maybe.
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
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: setting transparency (alpha) for spherescenenode?

Post by pandoragami »

CuteAlien wrote:IMeshSceneNode is derived from ISceneNode, meaning it has everything ISceneNode has + some additional stuff. That's why you can always cast safely to the base-classes.

Not sure right now if the alpha value means transparence or opaque, doesn't seem to be documented. Just try it with 255 maybe.
Tried both 0 and 255, no luck either way.
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: setting transparency (alpha) for spherescenenode?

Post by pandoragami »

Here's the complete code

Code: Select all

 
#include <irrlicht.h>
 
using namespace irr;
 
class MyEventReceiver : public IEventReceiver
{
public:
 
    struct SMouseState
    {
        core::position2di Position;
        bool LeftButtonDown;
        SMouseState() : LeftButtonDown(false) { }
    } MouseState;
 
    virtual bool OnEvent(const SEvent& event)
    {
        if (event.EventType == irr::EET_KEY_INPUT_EVENT)
            KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
        if(event.EventType == EET_MOUSE_INPUT_EVENT)
        {
            switch(event.MouseInput.Event)
            {
            case EMIE_LMOUSE_PRESSED_DOWN:
                MouseState.LeftButtonDown = true;
                break;
 
            case EMIE_LMOUSE_LEFT_UP:
                MouseState.LeftButtonDown = false;
                break;
 
            case EMIE_MOUSE_MOVED:
                MouseState.Position.X = event.MouseInput.X;
                MouseState.Position.Y = event.MouseInput.Y;
                break;
 
            default:
 
                break;
            }
        }
 
        return false;
    }
 
    // This is used to check whether a key is being held down
    virtual bool IsKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
 
    const SMouseState & GetMouseState(void) const
    {
        return MouseState;
    }
 
    MyEventReceiver()
    {
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
            KeyIsDown[i] = false;
    }
 
private:
    // We use this array to store the current state of each key
    bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
MyEventReceiver receiver;
IrrlichtDevice* device = 0;
const int SCRX = 640;
const int SCRY = 480;
const float radius = 5.0f;
 
int main()
{
    device = createDevice( irr::video::EDT_OPENGL, core::dimension2d<u32>( SCRX, SCRY), 16, false, false, false, &receiver);
 
    if (device == 0)
        return 1;
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
 
    driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
 
    scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS( 0, 50.0f, 0.01f, -1, 0, 0, false, 0.0f, false, true);
 
    device->getCursorControl()->setVisible(false);
    scene::IMeshSceneNode* sphere = smgr->addSphereSceneNode(radius, 32, 0, -1, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    sphere->setMaterialFlag(video::EMF_LIGHTING, false);
    sphere->setMaterialTexture( 0, driver->getTexture("graph.jpg"));
 
    scene::IMeshManipulator* mesh = smgr->getMeshManipulator();
    mesh->setVertexColorAlpha(sphere->getMesh(), 255);
 
    cam->setPosition( core::vector3df( 0, 0,-15));
    cam->setTarget( core::vector3df( 0.0f, 0.0f, 0.0f));
 
    int lastFPS = -1;
 
 
    while(device->run())
    if (device->isWindowActive())
    {
        driver->beginScene(true, true, video::SColor( 255, 0, 0, 0));
        smgr->drawAll();
        driver->endScene();
 
        int fps = driver->getFPS();
 
        if (lastFPS != fps)
        {
            core::stringw str = L"Irrlicht Text Node [";
            str += driver->getName();
            str += "] FPS:";
            str += fps;
 
            device->setWindowCaption(str.c_str());
            lastFPS = fps;
        }
 
        device->sleep(50);
    }
 
    device->drop();
 
    return 0;
}
CuteAlien
Admin
Posts: 9689
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: setting transparency (alpha) for spherescenenode?

Post by CuteAlien »

Hm, I really have to sleep, so won't test now. But I suppose it's because the material used in the mesh is solid. Try using any material supporting transparence like video::EMT_TRANSPARENT_ADD_COLOR.
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
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: setting transparency (alpha) for spherescenenode?

Post by pandoragami »

CuteAlien wrote:Hm, I really have to sleep, so won't test now. But I suppose it's because the material used in the mesh is solid. Try using any material supporting transparence like video::EMT_TRANSPARENT_ADD_COLOR.
Goodnite, thanks so far though, this code below works now except that the texture cannot be alpha blended so it's commented out.

Code: Select all

 
#include <C:\irrlicht-1.7.2\include\irrlicht.h>
 
using namespace irr;
 
class MyEventReceiver : public IEventReceiver
{
public:
 
    struct SMouseState
    {
        core::position2di Position;
        bool LeftButtonDown;
        SMouseState() : LeftButtonDown(false) { }
    } MouseState;
 
    virtual bool OnEvent(const SEvent& event)
    {
        if (event.EventType == irr::EET_KEY_INPUT_EVENT)
            KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
        if(event.EventType == EET_MOUSE_INPUT_EVENT)
        {
            switch(event.MouseInput.Event)
            {
            case EMIE_LMOUSE_PRESSED_DOWN:
                MouseState.LeftButtonDown = true;
                break;
 
            case EMIE_LMOUSE_LEFT_UP:
                MouseState.LeftButtonDown = false;
                break;
 
            case EMIE_MOUSE_MOVED:
                MouseState.Position.X = event.MouseInput.X;
                MouseState.Position.Y = event.MouseInput.Y;
                break;
 
            default:
 
                break;
            }
        }
 
        return false;
    }
 
    // This is used to check whether a key is being held down
    virtual bool IsKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
 
    const SMouseState & GetMouseState(void) const
    {
        return MouseState;
    }
 
    MyEventReceiver()
    {
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
            KeyIsDown[i] = false;
    }
 
private:
    // We use this array to store the current state of each key
    bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
MyEventReceiver receiver;
IrrlichtDevice* device = 0;
const int SCRX = 640;
const int SCRY = 480;
const float radius = 5.0f;
 
int main()
{
    device = createDevice( irr::video::EDT_OPENGL, core::dimension2d<u32>( SCRX, SCRY), 16, false, false, false, &receiver);
 
    if (device == 0)
        return 1;
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
 
    driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
 
    scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS( 0, 50.0f, 0.01f, -1, 0, 0, false, 0.0f, false, true);
 
    device->getCursorControl()->setVisible(false);
    scene::IMeshSceneNode* sphere = smgr->addSphereSceneNode(radius, 32, 0, -1, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    sphere->setMaterialFlag(video::EMF_LIGHTING, false);
    //sphere->setMaterialTexture( 0, driver->getTexture("graph.jpg"));
    sphere->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL);
 
    scene::IMeshManipulator* mesh = smgr->getMeshManipulator();
    mesh->setVertexColorAlpha(sphere->getMesh(), 155);
 
 
    cam->setPosition( core::vector3df( 0, 0,-15));
    cam->setTarget( core::vector3df( 0.0f, 0.0f, 0.0f));
 
    int lastFPS = -1;
 
    while(device->run())
    if (device->isWindowActive())
    {
        driver->beginScene(true, true, video::SColor( 255, 0, 0, 0));
        smgr->drawAll();
        driver->endScene();
 
        int fps = driver->getFPS();
 
        if (lastFPS != fps)
        {
            core::stringw str = L"Irrlicht Text Node [";
            str += driver->getName();
            str += "] FPS:";
            str += fps;
 
            device->setWindowCaption(str.c_str());
            lastFPS = fps;
        }
 
        device->sleep(50);
    }
 
    device->drop();
 
    return 0;
}
 
 
 
 
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: setting transparency (alpha) for spherescenenode?

Post by thanhle »

There's a post by an author call feelthat.
I saw a few weeks back, he did transparent even for animated mesh.
I think he fixed other issues he found as well.


Actually there's also lot of posts on the related issue if you search the forum.

http://irrlicht.sourceforge.net/forum/v ... hp?t=31400

Regards
thanh
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: setting transparency (alpha) for spherescenenode?

Post by pandoragami »

thanhle wrote:There's a post by an author call feelthat.
I saw a few weeks back, he did transparent even for animated mesh.
I think he fixed other issues he found as well.


Actually there's also lot of posts on the related issue if you search the forum.

http://irrlicht.sourceforge.net/forum/v ... hp?t=31400

Regards
thanh
Thanks for the note, I did check his code and he is using "EMT_TRANSPARENT_ALPHA_CHANNEL" just like me in the following code below, but I set the alpha to zero and still the texture doesn't allow it to be transparent? I've tried other enumerators too, if you look you'll notice several, here's all of them

Code: Select all

video::EMT_TRANSPARENT_ALPHA_CHANNEL
    video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF
    video::EMT_TRANSPARENT_VERTEX_ALPHA
    video::EMT_TRANSPARENT_ALPHA_CHANNEL

Code: Select all

 
#include <irrlicht.h>
 
using namespace irr;
 
class MyEventReceiver : public IEventReceiver
{
public:
 
    struct SMouseState
    {
        core::position2di Position;
        bool LeftButtonDown;
        SMouseState() : LeftButtonDown(false) { }
    } MouseState;
 
    virtual bool OnEvent(const SEvent& event)
    {
        if (event.EventType == irr::EET_KEY_INPUT_EVENT)
            KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
        if(event.EventType == EET_MOUSE_INPUT_EVENT)
        {
            switch(event.MouseInput.Event)
            {
            case EMIE_LMOUSE_PRESSED_DOWN:
                MouseState.LeftButtonDown = true;
                break;
 
            case EMIE_LMOUSE_LEFT_UP:
                MouseState.LeftButtonDown = false;
                break;
 
            case EMIE_MOUSE_MOVED:
                MouseState.Position.X = event.MouseInput.X;
                MouseState.Position.Y = event.MouseInput.Y;
                break;
 
            default:
 
                break;
            }
        }
 
        return false;
    }
 
    // This is used to check whether a key is being held down
    virtual bool IsKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
 
    const SMouseState & GetMouseState(void) const
    {
        return MouseState;
    }
 
    MyEventReceiver()
    {
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
            KeyIsDown[i] = false;
    }
 
private:
    // We use this array to store the current state of each key
    bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
MyEventReceiver receiver;
IrrlichtDevice* device = 0;
const int SCRX = 640;
const int SCRY = 480;
const float radius = 5.0f;
 
int main()
{
    device = createDevice( irr::video::EDT_OPENGL, core::dimension2d<u32>( SCRX, SCRY), 16, false, false, false, &receiver);
 
    if (device == 0)
        return 1;
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
 
    driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
 
    scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS( 0, 50.0f, 0.01f, -1, 0, 0, false, 0.0f, false, true);
 
    device->getCursorControl()->setVisible(false);
    scene::IMeshSceneNode* sphere = smgr->addSphereSceneNode(radius, 32, 0, -1, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    sphere->setMaterialFlag(video::EMF_LIGHTING, false);
    sphere->setMaterialTexture( 0, driver->getTexture("graph.jpg"));
    //sphere->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL);
    //sphere->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
    //sphere->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);
    sphere->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL);
    scene::IMeshManipulator* mesh = smgr->getMeshManipulator();
    mesh->setVertexColorAlpha(sphere->getMesh(), 0);
 
 
    cam->setPosition( core::vector3df( 0, 0,-15));
    cam->setTarget( core::vector3df( 0.0f, 0.0f, 0.0f));
 
    int lastFPS = -1;
 
    while(device->run())
    if (device->isWindowActive())
    {
        driver->beginScene(true, true, video::SColor( 255, 0, 0, 0));
        smgr->drawAll();
        driver->endScene();
 
        int fps = driver->getFPS();
 
        if (lastFPS != fps)
        {
            core::stringw str = L"Irrlicht Text Node [";
            str += driver->getName();
            str += "] FPS:";
            str += fps;
 
            device->setWindowCaption(str.c_str());
            lastFPS = fps;
        }
 
        device->sleep(50);
    }
 
    device->drop();
 
    return 0;
}
 
 
 
 
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: setting transparency (alpha) for spherescenenode?

Post by mongoose7 »

Code: Select all

Meshbuffer->Material.DiffuseColor.setAlpha( (s32)(value * 255) );
Meshbuffer->Material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
Post Reply