gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:21:29 26 4
gpt4 key购买 nike

是否有一个命令可以标记/启用给定场景管理器中所有节点的阴影?

目前我可以从所有节点转换阴影,但我必须为每个场景节点一遍又一遍地调用相同的函数 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));,这是一个全局设置。

关于c++ - Irrlicht:如何在场景管理器中启用所有节点的阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52059906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com