Getting to know the Irrlicht 3D engine - Loading a 3D Model

Dec 9th, 2009 by mcasperson

In this tutorial we load and display a 3D model in our Irrlicht application.

DOWNLOAD THE DEMO AND SOURCE CODE

RETURN TO THE TUTORIAL INDEX

One thing we want to do is to keep the logic that relates to the Irrlicht engine, like opening a window and managing the render loop, separate from the logic of the application itself, which in our case here is to display a 3D model. To do this we create a new class called ApplicationManager.

ApplicationManager.h / ApplicationManager.cpp

Just like the IrrlichtEngineManager class,  the ApplicationManager is designed as a singleton, meaning there is only one instance of the object. This single object is accessed via the Instance function, or by using the APPLICATIONMANAGER definition.

In the Startup function we being by adding a FPS style camera to the scene by calling ENGINEMANAGER.GetSceneManager()->addCameraSceneNodeFPS(). This camera is controlled by the mouse and arrow keys, just like the controls for a standard FPS game.

The FPS camera records the movement of the mouse each frame, and then centres it in the middle of the screen again. This effectively locks the mouse in position, making it fairly useless, not to mention annoying, so we hide it by calling ENGINEMANAGER.GetIrrlichtDevice()->getCursorControl()->setVisible(false).

Now we load a 3D model of a ninja by calling ENGINEMANAGER.GetSceneManager()->getMesh("../../media/ninja.b3d"). Irrlicht can directly load a wide range of 3D model formats, and here we are loading a Blitz3d model.

If the file could not be found, mesh will be NULL, and we close the application by stopping the render loop through a call to ENGINEMANAGER.EndRenderLoop().

Otherwise we then add the loaded mesh to the scene by calling ENGINEMANAGER.GetSceneManager()->addAnimatedMeshSceneNode(mesh). We store the returned scene node pointer in a variable called model.

This scene node is then positioned so it is in front of the camera when the application starts.

We also disable the lighting by setting the EMF_LIGHTING flag to false. Because there are no lights in the scene, if we didn’t disable the lighting the model would appear black.

In the Shutdown function we remove the 3D model from the scene by calling remove(). Notice that we don’t call drop() because the object was not created with a function that started with “create”.

Main.cpp

The last change we need to make is to call the ApplicationManager Startup and Shutdown functions from the WinMain function. This gives the ApplicationManager a chance to set itself up before the render loop is started, and to clean itself up before the Irrlicht 3D engine is shutdown.

mcasperson

Written by mcasperson

Rate this Article:

Be the first to rate me.

Add new comment

* You must be logged in order to leave comments, please Sign in or join us.

Comments

MuratA, over a year ago
Report comment

These 3D Digital Motion Animation and modeling tutorials are perfect for artists and designers, 3D Model, 3D Animation, Architectural Presentation

http://www.mcmajans.com

Related Content