Written by Colin MacDonald. This tutorial explains the use of the Light Manager of Irrlicht. It enables the use of more dynamic light sources than the actual hardware supports. Further applications of the Light Manager, such as per scene node callbacks, are left out for simplicity of the example.
using namespace core;
#if defined(_MSC_VER)
#pragma comment(lib, "Irrlicht.lib")
#endif
Main header file of the irrlicht, the only file needed to include.
Everything in the Irrlicht Engine can be found in this namespace.
Normally, you are limited to 8 dynamic lights per scene: this is a hardware limit. If you want to use more dynamic lights in your scene, then you can register an optional light manager that allows you to to turn lights on and off at specific point during rendering. You are still limited to 8 lights, but the limit is per scene node.
This is completely optional: if you do not register a light manager, then a default distance-based scheme will be used to prioritise hardware lights based on their distance from the active camera.
NO_MANAGEMENT disables the light manager and shows Irrlicht's default light behaviour. The 8 lights nearest to the camera will be turned on, and other lights will be turned off. In this example, this produces a funky looking but incoherent light display.
LIGHTS_NEAREST_NODE shows an implementation that turns on a limited number of lights per mesh scene node. If finds the 3 lights that are nearest to the node being rendered, and turns them on, turning all other lights off. This works, but as it operates on every light for every node, it does not scale well with many lights. The flickering you can see in this demo is due to the lights swapping their relative positions from the cubes (a deliberate demonstration of the limitations of this technique).
LIGHTS_IN_ZONE shows a technique for turning on lights based on a 'zone'. Each empty scene node is considered to be the parent of a zone. When nodes are rendered, they turn off all lights, then find their parent 'zone' and turn on all lights that are inside that zone, i.e. are descendents of it in the scene graph. This produces true 'local' lighting for each cube in this example. You could use a similar technique to locally light all meshes in (e.g.) a room, without the lights spilling out to other rooms.
This light manager is also an event receiver; this is purely for simplicity in this example, it's neither necessary nor recommended for a real application.
class CMyLightManager : public scene::ILightManager, public IEventReceiver
{
typedef enum
{
NO_MANAGEMENT,
LIGHTS_NEAREST_NODE,
LIGHTS_IN_ZONE
}
LightManagementMode;
LightManagementMode Mode;
LightManagementMode RequestedMode;
scene::ISceneManager * SceneManager;
core::array<scene::ISceneNode*> * SceneLightList;
scene::ISceneNode * CurrentSceneNode;
public:
CMyLightManager(scene::ISceneManager* sceneManager)
: Mode(NO_MANAGEMENT), RequestedMode(NO_MANAGEMENT),
SceneManager(sceneManager), SceneLightList(0),
CurrentRenderPass(scene::
ESNRP_NONE), CurrentSceneNode(0)
{ }
bool OnEvent(const SEvent & event)
{
bool handled = false;
{
handled = true;
switch(event.KeyInput.Key)
{
RequestedMode = NO_MANAGEMENT;
break;
RequestedMode = LIGHTS_NEAREST_NODE;
break;
RequestedMode = LIGHTS_IN_ZONE;
break;
default:
handled = false;
break;
}
if(NO_MANAGEMENT == RequestedMode)
SceneManager->setLightManager(0);
else
SceneManager->setLightManager(this);
}
return handled;
}
virtual void OnPreRender(core::array<scene::ISceneNode*> & lightList)
{
Mode = RequestedMode;
SceneLightList = &lightList;
}
virtual void OnPostRender()
{
for (
u32 i = 0; i < SceneLightList->size(); i++)
(*SceneLightList)[i]->setVisible(true);
}
{
CurrentRenderPass = renderPass;
}
{
{
for (
u32 i = 0; i < SceneLightList->size(); ++i)
(*SceneLightList)[i]->setVisible(false);
}
}
virtual void OnNodePreRender(scene::ISceneNode* node)
{
CurrentSceneNode = node;
return;
return;
if (LIGHTS_NEAREST_NODE == Mode)
{
const vector3df nodePosition = node->getAbsolutePosition();
array<LightDistanceElement> sortingArray;
sortingArray.reallocate(SceneLightList->size());
for(i = 0; i < SceneLightList->size(); ++i)
{
scene::ISceneNode* lightNode = (*SceneLightList)[i];
const f64 distance = lightNode->getAbsolutePosition().getDistanceFromSQ(nodePosition);
sortingArray.push_back(LightDistanceElement(lightNode, distance));
}
sortingArray.sort();
for(i = 0; i < sortingArray.size(); ++i)
sortingArray[i].node->setVisible(i < 3);
}
else if(LIGHTS_IN_ZONE == Mode)
{
for (
u32 i = 0; i < SceneLightList->size(); ++i)
{
continue;
scene::ILightSceneNode* lightNode = static_cast<scene::ILightSceneNode*>((*SceneLightList)[i]);
video::SLight & lightData = lightNode->getLightData();
lightNode->setVisible(false);
}
scene::ISceneNode * parentZone = findZone(node);
if (parentZone)
turnOnZoneLights(parentZone);
}
}
virtual void OnNodePostRender(scene::ISceneNode* node)
{
}
private:
scene::ISceneNode * findZone(scene::ISceneNode * node)
{
if (!node)
return 0;
return node;
return findZone(node->getParent());
}
void turnOnZoneLights(scene::ISceneNode * node)
{
core::list<scene::ISceneNode*> const & children = node->getChildren();
for (core::list<scene::ISceneNode*>::ConstIterator child = children.begin();
child != children.end(); ++child)
{
(*child)->setVisible(true);
else
turnOnZoneLights(*child);
}
}
class LightDistanceElement
{
public:
LightDistanceElement() {};
LightDistanceElement(scene::ISceneNode* n,
f64 d)
: node(n), distance(d) { }
scene::ISceneNode* node;
bool operator < (const LightDistanceElement& other) const
{
return (distance < other.distance);
}
};
};
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
E_SCENE_NODE_RENDER_PASS
Enumeration for render passes.
@ ESNRP_NONE
No pass currently active.
@ ESNRP_SOLID
Solid scene nodes or special scene nodes without materials.
@ ESNT_CUBE
simple cube scene node
@ ESNT_LIGHT
Light Scene Node.
@ ESNT_EMPTY
Empty Scene Node.
@ ELT_DIRECTIONAL
directional light, coming from a direction from an infinite distance
unsigned int u32
32 bit unsigned variable.
double f64
64 bit floating point variable.
@ EET_KEY_INPUT_EVENT
A key input event.
int main(int argumentCount, char * argumentValues[])
{
return 1;
dimension2d<u32>(640, 480), 32);
if(!device)
return -1;
f32 const lightRadius = 60.f;
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
gui::IGUIEnvironment* guienv = device->getGUIEnvironment();
gui::IGUISkin* skin = guienv->getSkin();
if (skin)
{
gui::IGUIFont* font = guienv->getFont("../../media/fontlucida.png");
if(font)
skin->setFont(font);
}
guienv->addStaticText(L"1 - No light management", core::rect<s32>(10,10,200,30));
guienv->addStaticText(L"2 - Closest 3 lights", core::rect<s32>(10,30,200,50));
guienv->addStaticText(L"3 - Lights in zone", core::rect<s32>(10,50,200,70));
@ EGDC_BUTTON_TEXT
Text on a button.
E_DRIVER_TYPE
An enum for all types of drivers the Irrlicht Engine supports.
@ EDT_COUNT
No driver, just for counting the elements.
float f32
32 bit floating point variable.
IRRLICHT_API IrrlichtDevice *IRRCALLCONV createDevice(video::E_DRIVER_TYPE deviceType=video::EDT_SOFTWARE, const core::dimension2d< u32 > &windowSize=(core::dimension2d< u32 >(640, 480)), u32 bits=16, bool fullscreen=false, bool stencilbuffer=false, bool vsync=false, IEventReceiver *receiver=0)
Creates an Irrlicht device. The Irrlicht device is the root object for using the engine.
Add several "zones". You could use this technique to light individual rooms, for example.
for(
f32 zoneX = -100.f; zoneX <= 100.f; zoneX += 50.f)
for(
f32 zoneY = -60.f; zoneY <= 60.f; zoneY += 60.f)
{
scene::ISceneNode * zoneRoot = smgr->addEmptySceneNode();
zoneRoot->setPosition(
vector3df(zoneX, zoneY, 0));
scene::IMeshSceneNode * node = smgr->addCubeSceneNode(15, zoneRoot);
scene::ISceneNodeAnimator * rotation = smgr->createRotationAnimator(
vector3df(0.25f, 0.5f, 0.75f));
node->addAnimator(rotation);
rotation->drop();
scene::IBillboardSceneNode * billboard = smgr->addBillboardSceneNode(node);
billboard->setPosition(
vector3df(0, -14, 30));
billboard->setMaterialTexture(0, driver->getTexture("../../media/particle.bmp"));
smgr->addLightSceneNode(billboard,
vector3df(0, 0, 0), video::SColorf(1, 0, 0), lightRadius);
billboard = smgr->addBillboardSceneNode(node);
billboard->setPosition(
vector3df(-21, -14, -21));
billboard->setMaterialTexture(0, driver->getTexture("../../media/particle.bmp"));
smgr->addLightSceneNode(billboard,
vector3df(0, 0, 0), video::SColorf(0, 1, 0), lightRadius);
billboard = smgr->addBillboardSceneNode(node);
billboard->setPosition(
vector3df(21, -14, -21));
billboard->setMaterialTexture(0, driver->getTexture("../../media/particle.bmp"));
smgr->addLightSceneNode(billboard,
vector3df(0, 0, 0), video::SColorf(0, 0, 1), lightRadius);
node = smgr->addCubeSceneNode(5, node);
}
CMyLightManager * myLightManager = new CMyLightManager(smgr);
smgr->setLightManager(0);
device->setEventReceiver(myLightManager);
int lastFps = -1;
while(device->run())
{
driver->beginScene(true, true, video::SColor(255,100,101,140));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
int fps = driver->getFPS();
if(fps != lastFps)
{
lastFps = fps;
str += driver->getName();
str += "] FPS:";
str += fps;
device->setWindowCaption(str.c_str());
}
}
myLightManager->drop();
device->drop();
return 0;
}
string< wchar_t > stringw
Typedef for wide character strings.
@ EMF_LIGHTING
Will this material be lighted? Default: true.
@ EMT_TRANSPARENT_ADD_COLOR
A transparent material.