main.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /** Example 002 Quake3Map
  2. This tutorial shows how to load a Quake 3 map into the engine, create a
  3. SceneNode for optimizing the speed of rendering, and how to create a user
  4. controlled camera.
  5. Please note that you should know the basics of the engine before starting this
  6. tutorial. Just take a short look at the first tutorial, if you haven't done
  7. this yet: http://irrlicht.sourceforge.net/docu/example001.html
  8. Lets start like the HelloWorld example: We include the irrlicht header files
  9. and an additional file to be able to ask the user for a driver type using the
  10. console.
  11. */
  12. #include <irrlicht.h>
  13. #include "driverChoice.h"
  14. #include "exampleHelper.h"
  15. /*
  16. As already written in the HelloWorld example, in the Irrlicht Engine everything
  17. can be found in the namespace 'irr'. To get rid of the irr:: in front of the
  18. name of every class, we tell the compiler that we use that namespace from now
  19. on, and we will not have to write that 'irr::'. There are 5 other sub
  20. namespaces 'core', 'scene', 'video', 'io' and 'gui'. Unlike in the HelloWorld
  21. example, we do not call 'using namespace' for these 5 other namespaces, because
  22. in this way you will see what can be found in which namespace. But if you like,
  23. you can also include the namespaces like in the previous example.
  24. */
  25. using namespace irr;
  26. /*
  27. Again, to be able to use the Irrlicht.DLL file, we need to link with the
  28. Irrlicht.lib. We could set this option in the project settings, but to make it
  29. easy, we use a pragma comment lib:
  30. */
  31. #ifdef _MSC_VER
  32. #pragma comment(lib, "Irrlicht.lib")
  33. #endif
  34. /*
  35. OK, lets start. Again, we use the main() method as start, not the WinMain().
  36. */
  37. int main()
  38. {
  39. /*
  40. Like in the HelloWorld example, we create an IrrlichtDevice with
  41. createDevice(). The difference now is that we ask the user to select
  42. which video driver to use. The Software device might be
  43. too slow to draw a huge Quake 3 map, but just for the fun of it, we make
  44. this decision possible, too.
  45. */
  46. // ask user for driver
  47. video::E_DRIVER_TYPE driverType=driverChoiceConsole(true);
  48. if (driverType==video::EDT_COUNT)
  49. return 1;
  50. // create device and exit if creation failed
  51. IrrlichtDevice *device =
  52. createDevice(driverType, core::dimension2d<u32>(640, 480));
  53. if (device == 0)
  54. return 1; // could not create selected driver.
  55. /*
  56. Get a pointer to the video driver and the SceneManager so that
  57. we do not always have to call irr::IrrlichtDevice::getVideoDriver() and
  58. irr::IrrlichtDevice::getSceneManager().
  59. */
  60. video::IVideoDriver* driver = device->getVideoDriver();
  61. scene::ISceneManager* smgr = device->getSceneManager();
  62. /*
  63. To display the Quake 3 map, we first need to load it. Quake 3 maps
  64. are packed into .pk3 files which are nothing else than .zip files.
  65. So we add the .pk3 file to our irr::io::IFileSystem. After it was added,
  66. we can read from the files in that archive as if they were stored on disk.
  67. */
  68. device->getFileSystem()->addFileArchive(getExampleMediaPath() + "map-20kdm2.pk3");
  69. /*
  70. Now we can load the mesh by calling irr::scene::ISceneManager::getMesh().
  71. We get a pointer returned to an irr::scene::IAnimatedMesh. Quake 3 maps are
  72. not really animated, they are only a chunk of static geometry with
  73. some materials attached. Hence the IAnimatedMesh consists of only one
  74. frame, so we get the "first frame" of the "animation", which is our
  75. quake level and create an Octree scene node with it, using
  76. irr::scene::ISceneManager::addOctreeSceneNode().
  77. The Octree optimizes the scene a little bit, trying to draw only geometry
  78. which is currently visible. An alternative to the Octree would be a
  79. irr::scene::IMeshSceneNode, which would always draw the complete
  80. geometry of the mesh, without optimization. Try it: Use
  81. irr::scene::ISceneManager::addMeshSceneNode() instead of
  82. addOctreeSceneNode() and compare the primitives drawn by the video
  83. driver. (There is a irr::video::IVideoDriver::getPrimitiveCountDrawn()
  84. method in the irr::video::IVideoDriver class). Note that this
  85. optimization with the Octree is only useful when drawing huge meshes
  86. consisting of lots of geometry and if users can't see the whole scene at
  87. once.
  88. */
  89. scene::IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
  90. scene::ISceneNode* node = 0;
  91. if (mesh)
  92. node = smgr->addOctreeSceneNode(mesh->getMesh(0), 0, -1, 1024);
  93. // node = smgr->addMeshSceneNode(mesh->getMesh(0));
  94. /*
  95. Because the level was not modeled around the origin (0,0,0), we
  96. translate the whole level a little bit. This is done on
  97. irr::scene::ISceneNode level using the methods
  98. irr::scene::ISceneNode::setPosition() (in this case),
  99. irr::scene::ISceneNode::setRotation(), and
  100. irr::scene::ISceneNode::setScale().
  101. */
  102. if (node)
  103. node->setPosition(core::vector3df(-1300,-144,-1249));
  104. /*
  105. Now we need a camera to look at the Quake 3 map.
  106. We want to create a user controlled camera. There are some
  107. cameras available in the Irrlicht engine. For example the
  108. MayaCamera which can be controlled like the camera in Maya:
  109. Rotate with left mouse button pressed, Zoom with both buttons pressed,
  110. translate with right mouse button pressed. This could be created with
  111. irr::scene::ISceneManager::addCameraSceneNodeMaya(). But for this
  112. example, we want to create a camera which behaves like the ones in
  113. first person shooter games (FPS) and hence use
  114. irr::scene::ISceneManager::addCameraSceneNodeFPS().
  115. */
  116. smgr->addCameraSceneNodeFPS();
  117. /*
  118. The mouse cursor needs not be visible, so we hide it via the
  119. irr::IrrlichtDevice::ICursorControl.
  120. */
  121. device->getCursorControl()->setVisible(false);
  122. /*
  123. Everything is set up, so lets draw it. We also write the current
  124. frames per second and the primitives drawn into the caption of the
  125. window. The test for irr::IrrlichtDevice::isWindowActive() is optional,
  126. but prevents the engine to grab the mouse cursor after task switching
  127. when other programs are active. The call to irr::IrrlichtDevice::yield()
  128. will avoid the busy loop to eat up all CPU cycles when the window is not
  129. active.
  130. */
  131. int lastFPS = -1;
  132. while(device->run())
  133. {
  134. if (device->isWindowActive())
  135. {
  136. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255,200,200,200));
  137. smgr->drawAll();
  138. driver->endScene();
  139. int fps = driver->getFPS();
  140. if (lastFPS != fps)
  141. {
  142. core::stringw str = L"Irrlicht Engine - Quake 3 Map example [";
  143. str += driver->getName();
  144. str += "] FPS:";
  145. str += fps;
  146. device->setWindowCaption(str.c_str());
  147. lastFPS = fps;
  148. }
  149. device->yield();
  150. }
  151. else
  152. device->yield();
  153. }
  154. /*
  155. In the end, delete the Irrlicht device.
  156. */
  157. device->drop();
  158. return 0;
  159. }
  160. /*
  161. That's it. Compile and play around with the program.
  162. **/