Irrlicht:如何在场景管理器中启用所有节点的阴影?

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

是否有单个命令可以 flag/enable 给定场景管理器中所有节点的阴影?

目前我可以从所有节点投射阴影,但我必须为每个场景节点一遍又一遍地调用相同的函数 addShadowVolumeSceneNode();。这对于小玩具代码来说没问题,但随着代码复杂性和大小的增加,它可能并不理想。

这是我正在玩的代码:

#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;
}

谢谢!

创建工厂函数:

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;
}

然后在需要新节点时调用它:

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));

也不需要为每个节点调用 smgr->setShadowColor(video::SColor(150,0,0,0));,这是一个全局设置。