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.
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.
{
typedef enum
{
NO_MANAGEMENT,
LIGHTS_NEAREST_NODE,
LIGHTS_IN_ZONE
}
LightManagementMode;
LightManagementMode Mode;
LightManagementMode RequestedMode;
public:
: 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;
{
RequestedMode = NO_MANAGEMENT;
break;
RequestedMode = LIGHTS_NEAREST_NODE;
break;
RequestedMode = LIGHTS_IN_ZONE;
break;
default:
handled = false;
break;
}
if(NO_MANAGEMENT == RequestedMode)
else
}
return handled;
}
{
Mode = RequestedMode;
SceneLightList = &lightList;
}
virtual void OnPostRender()
{
for (
u32 i = 0; i < SceneLightList->
size(); i++)
(*SceneLightList)[i]->setVisible(true);
}
{
CurrentRenderPass = renderPass;
}
{
if (scene::ESNRP_SOLID == renderPass)
{
for (
u32 i = 0; i < SceneLightList->
size(); ++i)
(*SceneLightList)[i]->setVisible(false);
}
}
{
CurrentSceneNode = node;
if (scene::ESNRP_SOLID != CurrentRenderPass)
return;
if (node->
getType() != scene::ESNT_CUBE)
return;
if (LIGHTS_NEAREST_NODE == Mode)
{
array<LightDistanceElement> sortingArray;
sortingArray.reallocate(SceneLightList->
size());
for(i = 0; i < SceneLightList->
size(); ++i)
{
sortingArray.push_back(LightDistanceElement(lightNode, distance));
}
sortingArray.sort();
for(i = 0; i < sortingArray.size(); ++i)
}
else if(LIGHTS_IN_ZONE == Mode)
{
for (
u32 i = 0; i < SceneLightList->
size(); ++i)
{
if ((*SceneLightList)[i]->getType() != scene::ESNT_LIGHT)
continue;
if (video::ELT_DIRECTIONAL != lightData.
Type)
}
if (parentZone)
turnOnZoneLights(parentZone);
}
}
{
}
private:
{
if (!node)
return 0;
if (node->
getType() == scene::ESNT_EMPTY)
return node;
}
{
child != children.
end(); ++child)
{
if ((*child)->getType() == scene::ESNT_LIGHT)
(*child)->setVisible(true);
else
turnOnZoneLights(*child);
}
}
class LightDistanceElement
{
public:
LightDistanceElement() {};
: node(n), distance(d) { }
bool operator < (const LightDistanceElement& other) const
{
return (distance < other.distance);
}
};
};
Interface of an object which can receive events.
Axis aligned bounding box in 3d dimensional space.
u32 size() const
Get number of occupied elements of the array.
List iterator for const access.
Iterator end()
Gets end node.
Iterator begin()
Gets first node.
T getDistanceFromSQ(const vector3d< T > &other) const
Returns squared distance from another point.
ILightManager provides an interface for user applications to manipulate the list of lights in the sce...
Scene node which is a dynamic light.
virtual void setVisible(bool isVisible)=0
Sets if the node should be visible or not.
virtual const video::SLight & getLightData() const =0
Gets the light data associated with this ILightSceneNode.
The Scene Manager manages scene nodes, mesh recources, cameras and all the other stuff.
virtual void setLightManager(ILightManager *lightManager)=0
Register a custom callbacks manager which gets callbacks during scene rendering.
virtual core::vector3df getAbsolutePosition() const
Gets the absolute position of the node in world coordinates.
const core::list< ISceneNode * > & getChildren() const
Returns a const reference to the list of all children.
scene::ISceneNode * getParent() const
Returns the parent of this scene node.
virtual void setVisible(bool isVisible)
Sets if the node should be visible or not.
virtual ESCENE_NODE_TYPE getType() const
Returns type of the scene node.
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
E_SCENE_NODE_RENDER_PASS
Enumeration for render passes.
unsigned int u32
32 bit unsigned variable.
double f64
64 bit floating point variable.
@ EET_KEY_INPUT_EVENT
A key input event.
SEvents hold information about an event. See irr::IEventReceiver for details on event handling.
struct SKeyInput KeyInput
structure for holding data describing a dynamic point light.
E_LIGHT_TYPE Type
Read-ONLY! Type of the light. Default: ELT_POINT.
int main(int argumentCount, char * argumentValues[])
{
if (driverType==video::EDT_COUNT)
return 1;
dimension2d<u32>(640, 480), 32);
if(!device)
return -1;
f32 const lightRadius = 60.f;
if (skin)
{
if(font)
}
The Irrlicht device. You can create it with createDevice() or createDeviceEx().
virtual scene::ISceneManager * getSceneManager()=0
Provides access to the scene manager.
virtual video::IVideoDriver * getVideoDriver()=0
Provides access to the video driver for drawing 3d and 2d geometry.
virtual gui::IGUIEnvironment * getGUIEnvironment()=0
Provides access to the 2d user interface environment.
GUI Environment. Used as factory and manager of all other GUI elements.
virtual IGUIFont * getFont(const io::path &filename)=0
Returns pointer to the font with the specified filename.
virtual IGUIStaticText * addStaticText(const wchar_t *text, const core::rect< s32 > &rectangle, bool border=false, bool wordWrap=true, IGUIElement *parent=0, s32 id=-1, bool fillBackground=false)=0
Adds a static text.
virtual IGUISkin * getSkin() const =0
Returns pointer to the current gui skin.
A skin modifies the look of the GUI elements.
virtual void setFont(IGUIFont *font, EGUI_DEFAULT_FONT which=EGDF_DEFAULT)=0
sets a default font
virtual void setColor(EGUI_DEFAULT_COLOR which, video::SColor newColor)=0
sets a default color
Interface to driver which is able to perform 2d and 3d graphics functions.
Class representing a 32 bit ARGB color.
E_DRIVER_TYPE
An enum for all types of drivers the Irrlicht Engine supports.
float f32
32 bit floating point variable.
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)
{
}
CMyLightManager * myLightManager = new CMyLightManager(smgr);
int lastFps = -1;
{
if(fps != lastFps)
{
lastFps = fps;
str += "] FPS:";
str += fps;
}
}
myLightManager->drop();
return 0;
}
bool drop() const
Drops the object. Decrements the reference counter by one.
virtual bool run()=0
Runs the device.
virtual void setWindowCaption(const wchar_t *text)=0
Sets the caption of the window.
virtual void setEventReceiver(IEventReceiver *receiver)=0
Sets a new user event receiver which will receive events from the engine.
virtual void drawAll()=0
Draws all gui elements by traversing the GUI environment starting at the root node.
A scene node displaying a static mesh.
virtual void drawAll()=0
Draws all the scene nodes.
virtual ICameraSceneNode * addCameraSceneNode(ISceneNode *parent=0, const core::vector3df &position=core::vector3df(0, 0, 0), const core::vector3df &lookat=core::vector3df(0, 0, 100), s32 id=-1, bool makeActive=true)=0
Adds a camera scene node to the scene graph and sets it as active camera.
virtual ISceneNode * addEmptySceneNode(ISceneNode *parent=0, s32 id=-1)=0
Adds an empty scene node to the scene graph.
virtual ISceneNodeAnimator * createRotationAnimator(const core::vector3df &rotationSpeed)=0
Creates a rotation animator, which rotates the attached scene node around itself.
virtual IMeshSceneNode * addCubeSceneNode(f32 size=10.0f, ISceneNode *parent=0, s32 id=-1, const core::vector3df &position=core::vector3df(0, 0, 0), const core::vector3df &rotation=core::vector3df(0, 0, 0), const core::vector3df &scale=core::vector3df(1.0f, 1.0f, 1.0f))=0
Adds a cube scene node.
virtual IBillboardSceneNode * addBillboardSceneNode(ISceneNode *parent=0, const core::dimension2d< f32 > &size=core::dimension2d< f32 >(10.0f, 10.0f), const core::vector3df &position=core::vector3df(0, 0, 0), s32 id=-1, video::SColor colorTop=0xFFFFFFFF, video::SColor colorBottom=0xFFFFFFFF)=0
Adds a billboard scene node to the scene graph.
virtual ILightSceneNode * addLightSceneNode(ISceneNode *parent=0, const core::vector3df &position=core::vector3df(0, 0, 0), video::SColorf color=video::SColorf(1.0f, 1.0f, 1.0f), f32 radius=100.0f, s32 id=-1)=0
Adds a dynamic light scene node to the scene graph.
Animates a scene node. Can animate position, rotation, material, and so on.
void setMaterialTexture(u32 textureLayer, video::ITexture *texture)
Sets the texture of the specified layer in all materials of this scene node to the new texture.
virtual void addAnimator(ISceneNodeAnimator *animator)
Adds an animator which should animate this node.
virtual void setPosition(const core::vector3df &newpos)
Sets the position of the node relative to its parent.
void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
Sets all material flags at once to a new value.
void setMaterialType(video::E_MATERIAL_TYPE newType)
Sets the material type of all materials in this scene node to a new material type.
virtual bool beginScene(bool backBuffer=true, bool zBuffer=true, SColor color=SColor(255, 0, 0, 0), const SExposedVideoData &videoData=SExposedVideoData(), core::rect< s32 > *sourceRect=0)=0
Applications must call this method before performing any rendering.
virtual ITexture * getTexture(const io::path &filename)=0
Get access to a named texture.
virtual s32 getFPS() const =0
Returns current frames per second value.
virtual const wchar_t * getName() const =0
Gets name of this video driver.
virtual bool endScene()=0
Presents the rendered image to the screen.
Class representing a color with four floats.