Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Now, we are going to load and display a mesh in our application. Let's take the template application, we made in Chapter 2, Creating a Basic Template Application and take it a bit further:
Open the media folder of your extracted Irrlicht package.
Copy sydney.md2 and sydney.bmp to where the executable of your new application is:
Note for Xcode users:
Right-click on the Xcode project and Add | Existing Files... and select sydney.md2 and sydney.bmp. Check the option Copy items into destination group's folder (if needed). Make sure those two files are listed in Copy Bundle Resources in your build target.
Add the line ISceneManager* smgr = device->getSceneManager(); directly after where we create our instance to the video driver.
To actually load a mesh, insert IMesh* mesh = smgr->getMesh("sydney.md2").
Now that our mesh is loaded, we still can't see our mesh. We need a node to display the mesh in our scene. Let's create one by adding this code: IMeshSceneNode* node = smgr->addMeshSceneNode(mesh).
Our node is added to the scene, but we need to adjust the camera to be able to actually see the mesh. Add this line: smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0)).
Add the line smgr->drawAll(); into our "game loop" between the beginScene() and endScene() function.
Your complete source code should look like this
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace video;
using namespace scene;
#if defined(_MSC_VER)
#pragma comment(lib, "Irrlicht.lib")
#endif
int main()
{
IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d<u32>(640, 480), 16, false, false, false, 0);
if (!device)
return 1;
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IMesh* mesh = smgr->getMesh("sydney.md2");
IMeshSceneNode* node = smgr->addMeshSceneNode(mesh);
smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0));
while (device->run())
{
driver->beginScene(true, true, SColor(255, 255, 255, 255));
smgr->drawAll();
driver->endScene();
}
device->drop();
return 0;
}