Loading...
Searching...
No Matches
Tutorial 15: Loading Scenes from .irr Files

Since version 1.1, Irrlicht is able to save and load the full scene graph into an .irr file, an xml based format. There is an editor available to edit those files, named irrEdit (http://www.ambiera.com/irredit) which can also be used as world and particle editor. This tutorial shows how to use .irr files.

Lets start: Create an Irrlicht device and setup the window.

#include <irrlicht.h>
#include "driverChoice.h"
using namespace irr;
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
int main(int argc, char** argv)
{
// ask user for driver
video::E_DRIVER_TYPE driverType=driverChoiceConsole();
if (driverType==video::EDT_COUNT)
return 1;
// create device and exit if creation failed
IrrlichtDevice* device =
createDevice(driverType, core::dimension2d<u32>(640, 480));
if (device == 0)
return 1; // could not create selected driver.
device->setWindowCaption(L"Load .irr file example");
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
irr::IrrlichtDevice
The Irrlicht device. You can create it with createDevice() or createDeviceEx().
Definition IrrlichtDevice.h:44
irr::IrrlichtDevice::setWindowCaption
virtual void setWindowCaption(const wchar_t *text)=0
Sets the caption of the window.
irr::IrrlichtDevice::getSceneManager
virtual scene::ISceneManager * getSceneManager()=0
Provides access to the scene manager.
irr::IrrlichtDevice::getVideoDriver
virtual video::IVideoDriver * getVideoDriver()=0
Provides access to the video driver for drawing 3d and 2d geometry.
irr::core::dimension2d
Specifies a 2 dimensional size.
Definition dimension2d.h:21
irr::scene::ISceneManager
The Scene Manager manages scene nodes, mesh recources, cameras and all the other stuff.
Definition ISceneManager.h:151
irr::video::IVideoDriver
Interface to driver which is able to perform 2d and 3d graphics functions.
Definition IVideoDriver.h:257
driverChoice.h
irrlicht.h
Main header file of the irrlicht, the only file needed to include.
irr::video::E_DRIVER_TYPE
E_DRIVER_TYPE
An enum for all types of drivers the Irrlicht Engine supports.
Definition EDriverTypes.h:15
irr::video::EDT_COUNT
@ EDT_COUNT
No driver, just for counting the elements.
Definition EDriverTypes.h:56
irr
Everything in the Irrlicht Engine can be found in this namespace.
Definition aabbox3d.h:13
irr::createDevice
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.

Now load our .irr file. .irr files can store the whole scene graph including animators, materials and particle systems. And there is also the possibility to store arbitrary user data for every scene node in that file. To keep this example simple, we are simply loading the scene here. See the documentation at ISceneManager::loadScene and ISceneManager::saveScene for more information. So to load and display a complicated huge scene, we only need a single call to loadScene().

// load the scene
if (argc>1)
smgr->loadScene(argv[1]);
else
smgr->loadScene("../../media/example.irr");
irr::scene::ISceneManager::loadScene
virtual bool loadScene(const io::path &filename, ISceneUserDataSerializer *userDataSerializer=0, ISceneNode *rootNode=0)=0
Loads a scene. Note that the current scene is not cleared before.

Now we'll create a camera, and give it a collision response animator that's built from the mesh nodes in the scene we just loaded.

scene::ICameraSceneNode * camera = smgr->addCameraSceneNodeFPS(0, 50.f, 0.1f);
// Create a meta triangle selector to hold several triangle selectors.
scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();
irr::scene::ICameraSceneNode
Scene Node which is a (controlable) camera.
Definition ICameraSceneNode.h:24
irr::scene::IMetaTriangleSelector
Interface for making multiple triangle selectors work as one big selector.
Definition IMetaTriangleSelector.h:21
irr::scene::ISceneManager::createMetaTriangleSelector
virtual IMetaTriangleSelector * createMetaTriangleSelector()=0
Creates a meta triangle selector.
irr::scene::ISceneManager::addCameraSceneNodeFPS
virtual ICameraSceneNode * addCameraSceneNodeFPS(ISceneNode *parent=0, f32 rotateSpeed=100.0f, f32 moveSpeed=0.5f, s32 id=-1, SKeyMap *keyMapArray=0, s32 keyMapSize=0, bool noVerticalMovement=false, f32 jumpSpeed=0.f, bool invertMouse=false, bool makeActive=true)=0
Adds a camera scene node with an animator which provides mouse and keyboard control appropriate for f...

Now we will find all the nodes in the scene and create triangle selectors for all suitable nodes. Typically, you would want to make a more informed decision about which nodes to performs collision checks on; you could capture that information in the node name or Id.

core::array<scene::ISceneNode *> nodes;
smgr->getSceneNodesFromType(scene::ESNT_ANY, nodes); // Find all nodes
for (u32 i=0; i < nodes.size(); ++i)
{
scene::ISceneNode * node = nodes[i];
scene::ITriangleSelector * selector = 0;
switch(node->getType())
{
case scene::ESNT_CUBE:
case scene::ESNT_ANIMATED_MESH:
// Because the selector won't animate with the mesh,
// and is only being used for camera collision, we'll just use an approximate
// bounding box instead of ((scene::IAnimatedMeshSceneNode*)node)->getMesh(0)
selector = smgr->createTriangleSelectorFromBoundingBox(node);
break;
case scene::ESNT_MESH:
case scene::ESNT_SPHERE: // Derived from IMeshSceneNode
selector = smgr->createTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);
break;
case scene::ESNT_TERRAIN:
selector = smgr->createTerrainTriangleSelector((scene::ITerrainSceneNode*)node);
break;
case scene::ESNT_OCTREE:
selector = smgr->createOctreeTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);
break;
default:
// Don't create a selector for this node type
break;
}
if(selector)
{
// Add it to the meta selector, which will take a reference to it
meta->addTriangleSelector(selector);
// And drop my reference to it, so that the meta selector owns it.
selector->drop();
}
}
irr::IReferenceCounted::drop
bool drop() const
Drops the object. Decrements the reference counter by one.
Definition IReferenceCounted.h:116
irr::core::array
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.h:23
irr::core::array::size
u32 size() const
Get number of occupied elements of the array.
Definition irrArray.h:368
irr::scene::IMeshSceneNode
A scene node displaying a static mesh.
Definition IMeshSceneNode.h:21
irr::scene::IMetaTriangleSelector::addTriangleSelector
virtual void addTriangleSelector(ITriangleSelector *toAdd)=0
Adds a triangle selector to the collection of triangle selectors.
irr::scene::ISceneManager::createTriangleSelector
virtual ITriangleSelector * createTriangleSelector(IMesh *mesh, ISceneNode *node)=0
Creates a simple ITriangleSelector, based on a mesh.
irr::scene::ISceneManager::createTerrainTriangleSelector
virtual ITriangleSelector * createTerrainTriangleSelector(ITerrainSceneNode *node, s32 LOD=0)=0
Creates a triangle selector which can select triangles from a terrain scene node.
irr::scene::ISceneManager::createTriangleSelectorFromBoundingBox
virtual ITriangleSelector * createTriangleSelectorFromBoundingBox(ISceneNode *node)=0
Creates a simple dynamic ITriangleSelector, based on a axis aligned bounding box.
irr::scene::ISceneManager::createOctreeTriangleSelector
virtual ITriangleSelector * createOctreeTriangleSelector(IMesh *mesh, ISceneNode *node, s32 minimalPolysPerNode=32)=0
Creates a Triangle Selector, optimized by an octree.
irr::scene::ISceneManager::getSceneNodesFromType
virtual void getSceneNodesFromType(ESCENE_NODE_TYPE type, core::array< scene::ISceneNode * > &outNodes, ISceneNode *start=0)=0
Get scene nodes by type.
irr::scene::ISceneNode
Scene node interface.
Definition ISceneNode.h:41
irr::scene::ISceneNode::getType
virtual ESCENE_NODE_TYPE getType() const
Returns type of the scene node.
Definition ISceneNode.h:674
irr::scene::ITerrainSceneNode
A scene node for displaying terrain using the geo mip map algorithm.
Definition ITerrainSceneNode.h:50
irr::scene::ITriangleSelector
Interface to return triangles with specific properties.
Definition ITriangleSelector.h:29
irr::scene::ESNT_ANIMATED_MESH
@ ESNT_ANIMATED_MESH
Animated Mesh Scene Node.
Definition ESceneNodeTypes.h:70
irr::scene::ESNT_OCTREE
@ ESNT_OCTREE
Octree Scene Node.
Definition ESceneNodeTypes.h:49
irr::scene::ESNT_MESH
@ ESNT_MESH
Mesh Scene Node.
Definition ESceneNodeTypes.h:52
irr::scene::ESNT_CUBE
@ ESNT_CUBE
simple cube scene node
Definition ESceneNodeTypes.h:25
irr::scene::ESNT_SPHERE
@ ESNT_SPHERE
Sphere scene node.
Definition ESceneNodeTypes.h:28
irr::scene::ESNT_TERRAIN
@ ESNT_TERRAIN
Terrain Scene Node.
Definition ESceneNodeTypes.h:37
irr::scene::ESNT_ANY
@ ESNT_ANY
Will match with any scene node when checking types.
Definition ESceneNodeTypes.h:96
irr::u32
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58

Now that the mesh scene nodes have had triangle selectors created and added to the meta selector, create a collision response animator from that meta selector.

scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
meta, camera, core::vector3df(5,5,5),
core::vector3df(0,0,0));
meta->drop(); // I'm done with the meta selector now
camera->addAnimator(anim);
anim->drop(); // I'm done with the animator now
// And set the camera position so that it doesn't start off stuck in the geometry
camera->setPosition(core::vector3df(0.f, 20.f, 0.f));
// Point the camera at the cube node, by finding the first node of type ESNT_CUBE
scene::ISceneNode * cube = smgr->getSceneNodeFromType(scene::ESNT_CUBE);
if(cube)
camera->setTarget(cube->getAbsolutePosition());
irr::scene::ICameraSceneNode::setTarget
virtual void setTarget(const core::vector3df &pos)=0
Sets the look at target of the camera.
irr::scene::ISceneManager::getSceneNodeFromType
virtual ISceneNode * getSceneNodeFromType(scene::ESCENE_NODE_TYPE type, ISceneNode *start=0)=0
Get the first scene node with the specified type.
irr::scene::ISceneManager::createCollisionResponseAnimator
virtual ISceneNodeAnimatorCollisionResponse * createCollisionResponseAnimator(ITriangleSelector *world, ISceneNode *sceneNode, const core::vector3df &ellipsoidRadius=core::vector3df(30, 60, 30), const core::vector3df &gravityPerSecond=core::vector3df(0,-10.0f, 0), const core::vector3df &ellipsoidTranslation=core::vector3df(0, 0, 0), f32 slidingValue=0.0005f)=0
Creates a special scene node animator for doing automatic collision detection and response.
irr::scene::ISceneNodeAnimator
Animates a scene node. Can animate position, rotation, material, and so on.
Definition ISceneNodeAnimator.h:31
irr::scene::ISceneNode::getAbsolutePosition
virtual core::vector3df getAbsolutePosition() const
Gets the absolute position of the node in world coordinates.
Definition ISceneNode.h:522
irr::scene::ISceneNode::addAnimator
virtual void addAnimator(ISceneNodeAnimator *animator)
Adds an animator which should animate this node.
Definition ISceneNode.h:348
irr::scene::ISceneNode::setPosition
virtual void setPosition(const core::vector3df &newpos)
Sets the position of the node relative to its parent.
Definition ISceneNode.h:507
irr::core::vector3df
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition vector3d.h:445

That's it. Draw everything and finish as usual.

int lastFPS = -1;
while(device->run())
if (device->isWindowActive())
{
driver->beginScene(true, true, video::SColor(0,200,200,200));
smgr->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw str = L"Load Irrlicht File example - Irrlicht Engine [";
str += driver->getName();
str += "] FPS:";
str += fps;
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
device->drop();
return 0;
}
irr::IrrlichtDevice::run
virtual bool run()=0
Runs the device.
irr::IrrlichtDevice::isWindowActive
virtual bool isWindowActive() const =0
Returns if the window is active.
irr::core::string::c_str
const T * c_str() const
Returns character string.
Definition irrString.h:495
irr::scene::ISceneManager::drawAll
virtual void drawAll()=0
Draws all the scene nodes.
irr::video::IVideoDriver::beginScene
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.
irr::video::IVideoDriver::getFPS
virtual s32 getFPS() const =0
Returns current frames per second value.
irr::video::IVideoDriver::getName
virtual const wchar_t * getName() const =0
Gets name of this video driver.
irr::video::IVideoDriver::endScene
virtual bool endScene()=0
Presents the rendered image to the screen.
irr::video::SColor
Class representing a 32 bit ARGB color.
Definition SColor.h:202
irr::core::stringw
string< wchar_t > stringw
Typedef for wide character strings.
Definition irrString.h:1361
- Irrlicht Engine Documentation © 2003-2012 by Nikolaus Gebhardt. Generated on for Irrlicht 3D Engine by Doxygen 1.15.0