Search code examples
c++irrlicht

Irrlicht: how to enable shadows of all nodes in scene manager?


Is there a single command to flag/enable shadows to all nodes in a given scenery manager?

At the moment I can cast shadows from all nodes but I have to keep calling the same function addShadowVolumeSceneNode(); over and over again for each of the scene nodes. This is ok for a small toy code, but as your code increases in complexity and size, it may well not be ideal.

Here is the code I am playing with:

#include <irrlicht.h>
#include "driverChoice.h"

using namespace irr;
using namespace scene;
using namespace core;
using namespace video;

int main()
{
    /* create device */
    IrrlichtDevice *device = createDevice(video::EDT_OPENGL, core::dimension2d<u32>(640, 480), 16, false, true);

    if (device == 0)
        return 1; 

    /* create engine and camera */
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();

    smgr->addCameraSceneNode(0, core::vector3df(0,-100,0), core::vector3df(0,0,0));
    smgr->addLightSceneNode(0, core::vector3df(0,0,400), video::SColorf(1.0f, 1.0f, 0.0f, 0.0f), 800.0f);

    /* Add cube */
    scene::IMeshSceneNode* node_cube = smgr->addCubeSceneNode(30);
    smgr->setShadowColor(video::SColor(150,0,0,0));
    node_cube->setPosition(core::vector3df(0,0,-80));
    node_cube->setMaterialFlag(video::EMF_LIGHTING, true);
    node_cube->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
    node_cube->addShadowVolumeSceneNode();

    /* Another cube */
    scene::IMeshSceneNode* node_cube2 = smgr->addCubeSceneNode(20);
    smgr->setShadowColor(video::SColor(150,0,0,0));
    node_cube2->setPosition(core::vector3df(0,0,-45));
    node_cube2->setMaterialFlag(video::EMF_LIGHTING, true);
    node_cube2->setMaterialFlag(video::EMF_BILINEAR_FILTER, true);
    node_cube2->addShadowVolumeSceneNode();

    /* And another cube */
    scene::IMeshSceneNode* node_cube3 = smgr->addCubeSceneNode(10);
    smgr->setShadowColor(video::SColor(150,0,0,0));
    node_cube3->setPosition(core::vector3df(0,0,-20));
    node_cube3->setMaterialFlag(video::EMF_LIGHTING, true);
    node_cube3->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
    node_cube3->addShadowVolumeSceneNode();

    /* Now draw everything and finish */
    u32 frames=0;
    while(device->run())
    {
        driver->beginScene(true, true, video::SColor(0,100,100,100));

        smgr->drawAll();

        driver->endScene();
        if (++frames==100)
        {
            core::stringw str = L"Irrlicht Engine [";
            str += driver->getName();
            str += L"] FPS: ";
            str += (s32)driver->getFPS();

            device->setWindowCaption(str.c_str());
            frames=0;
        }
    }
    device->drop();
    return 0;
}

Thanks!


Solution

  • Create a factory function:

    scene::IMeshSceneNode* CreateCubeWithShadow(scene::ISceneManager* smgr, const core::vector3df &pos)
    {
        scene::IMeshSceneNode* node_cube = smgr->addCubeSceneNode(10);
        node_cube->setPosition(pos);
        node_cube->setMaterialFlag(video::EMF_LIGHTING, true);
        node_cube->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
        node_cube->addShadowVolumeSceneNode();
        return node_cube;
    }
    

    Then call it whenever you want a new node:

    scene::IMeshSceneNode* node_cube1 = CreateCubeWithShadow(smgr, core::vector3df(0,0,-80));
    scene::IMeshSceneNode* node_cube2 = CreateCubeWithShadow(smgr, core::vector3df(0,0,-45));
    scene::IMeshSceneNode* node_cube3 = CreateCubeWithShadow(smgr, core::vector3df(0,0,-20));
    

    Also no need to call smgr->setShadowColor(video::SColor(150,0,0,0)); for every node, that's a global setting.