main.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /** Example 015 Loading Scenes from .irr Files
  2. Since version 1.1, Irrlicht is able to save and load
  3. the full scene graph into an .irr file, an xml based
  4. format. There is an editor available to edit
  5. those files, named irrEdit (http://www.ambiera.com/irredit)
  6. which can also be used as world and particle editor.
  7. This tutorial shows how to use .irr files.
  8. Lets start: Create an Irrlicht device and setup the window.
  9. */
  10. #include <irrlicht.h>
  11. #include "driverChoice.h"
  12. #include "exampleHelper.h"
  13. using namespace irr;
  14. #ifdef _MSC_VER
  15. #pragma comment(lib, "Irrlicht.lib")
  16. #endif
  17. int main(int argc, char** argv)
  18. {
  19. // ask user for driver
  20. video::E_DRIVER_TYPE driverType=driverChoiceConsole();
  21. if (driverType==video::EDT_COUNT)
  22. return 1;
  23. // create device and exit if creation failed
  24. IrrlichtDevice* device =
  25. createDevice(driverType, core::dimension2d<u32>(640, 480));
  26. if (device == 0)
  27. return 1; // could not create selected driver.
  28. device->setWindowCaption(L"Load .irr file example");
  29. video::IVideoDriver* driver = device->getVideoDriver();
  30. scene::ISceneManager* smgr = device->getSceneManager();
  31. /*
  32. Now load our .irr file.
  33. .irr files can store the whole scene graph including animators,
  34. materials and particle systems. And there is also the possibility to
  35. store arbitrary user data for every scene node in that file. To keep
  36. this example simple, we are simply loading the scene here. See the
  37. documentation at ISceneManager::loadScene and ISceneManager::saveScene
  38. for more information. So to load and display a complicated huge scene,
  39. we only need a single call to loadScene().
  40. */
  41. // load the scene
  42. /* You might have to work around some minor problems in current .irr loader:
  43. - It can't load meshes relative to the .irr file, but only relative to the working directory.
  44. So you might have to change your working directory to the path where the .irr file is in.
  45. - When passing a custom parent node to loadScene then irr_scene attributes will be passed to that.
  46. Usually not a problem, but for example AmbientLight will not change that way unless you create a custom
  47. SceneNode type which can interpret those attributes.
  48. */
  49. if (argc>1)
  50. smgr->loadScene(argv[1]);
  51. else
  52. smgr->loadScene(getExampleMediaPath() + "example.irr");
  53. /*
  54. Now we'll create a camera, and give it a collision response animator
  55. that's built from the mesh nodes in the scene we just loaded.
  56. */
  57. scene::ICameraSceneNode * camera = smgr->addCameraSceneNodeFPS(0, 50.f, 0.1f);
  58. // Create a meta triangle selector to hold several triangle selectors.
  59. scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();
  60. /*
  61. Now we will find all the nodes in the scene and create triangle
  62. selectors for all suitable nodes. Typically, you would want to make a
  63. more informed decision about which nodes to performs collision checks
  64. on; you could capture that information in the node name or Id.
  65. */
  66. core::array<scene::ISceneNode *> nodes;
  67. smgr->getSceneNodesFromType(scene::ESNT_ANY, nodes); // Find all nodes
  68. for (u32 i=0; i < nodes.size(); ++i)
  69. {
  70. scene::ISceneNode * node = nodes[i];
  71. scene::ITriangleSelector * selector = 0;
  72. switch(node->getType())
  73. {
  74. case scene::ESNT_CUBE:
  75. case scene::ESNT_ANIMATED_MESH:
  76. // Because the selector won't animate with the mesh,
  77. // and is only being used for camera collision, we'll just use an approximate
  78. // bounding box instead of ((scene::IAnimatedMeshSceneNode*)node)->getMesh(0)
  79. selector = smgr->createTriangleSelectorFromBoundingBox(node);
  80. break;
  81. case scene::ESNT_MESH:
  82. case scene::ESNT_SPHERE: // Derived from IMeshSceneNode
  83. selector = smgr->createTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);
  84. break;
  85. case scene::ESNT_TERRAIN:
  86. selector = smgr->createTerrainTriangleSelector((scene::ITerrainSceneNode*)node);
  87. break;
  88. case scene::ESNT_OCTREE:
  89. selector = smgr->createOctreeTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);
  90. break;
  91. default:
  92. // Don't create a selector for this node type
  93. break;
  94. }
  95. if(selector)
  96. {
  97. // Add it to the meta selector, which will take a reference to it
  98. meta->addTriangleSelector(selector);
  99. // And drop my reference to it, so that the meta selector owns it.
  100. selector->drop();
  101. }
  102. }
  103. /*
  104. Now that the mesh scene nodes have had triangle selectors created and added
  105. to the meta selector, create a collision response animator from that meta selector.
  106. */
  107. scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
  108. meta, camera, core::vector3df(5,5,5),
  109. core::vector3df(0,0,0));
  110. meta->drop(); // I'm done with the meta selector now
  111. camera->addAnimator(anim);
  112. anim->drop(); // I'm done with the animator now
  113. // And set the camera position so that it doesn't start off stuck in the geometry
  114. camera->setPosition(core::vector3df(0.f, 20.f, 0.f));
  115. // Point the camera at the cube node, by finding the first node of type ESNT_CUBE
  116. scene::ISceneNode * cube = smgr->getSceneNodeFromType(scene::ESNT_CUBE);
  117. if(cube)
  118. camera->setTarget(cube->getAbsolutePosition());
  119. /*
  120. That's it. Draw everything and finish as usual.
  121. */
  122. int lastFPS = -1;
  123. while(device->run())
  124. if (device->isWindowActive())
  125. {
  126. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(0,200,200,200));
  127. smgr->drawAll();
  128. driver->endScene();
  129. int fps = driver->getFPS();
  130. if (lastFPS != fps)
  131. {
  132. core::stringw str = L"Load Irrlicht File example - Irrlicht Engine [";
  133. str += driver->getName();
  134. str += "] FPS:";
  135. str += fps;
  136. device->setWindowCaption(str.c_str());
  137. lastFPS = fps;
  138. }
  139. }
  140. device->drop();
  141. return 0;
  142. }
  143. /*
  144. **/