main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /** Example 016 Quake3 Map Shader Support
  2. This tutorial shows how to load a Quake 3 map into the
  3. engine, create a SceneNode for optimizing the speed of
  4. rendering and how to create a user controlled camera.
  5. Lets start like the HelloWorld example: We include
  6. the irrlicht header files and an additional file to be able
  7. to ask the user for a driver type using the console.
  8. */
  9. #include <irrlicht.h>
  10. #include "driverChoice.h"
  11. #include "exampleHelper.h"
  12. /*
  13. define which Quake3 Level should be loaded
  14. */
  15. #define IRRLICHT_QUAKE3_ARENA
  16. //#define ORIGINAL_QUAKE3_ARENA
  17. //#define CUSTOM_QUAKE3_ARENA
  18. //#define SHOW_SHADER_NAME
  19. #ifdef ORIGINAL_QUAKE3_ARENA
  20. #define QUAKE3_STORAGE_FORMAT addFolderFileArchive
  21. #define QUAKE3_STORAGE_1 "/baseq3/"
  22. #ifdef CUSTOM_QUAKE3_ARENA
  23. #define QUAKE3_STORAGE_2 "/cf/"
  24. #define QUAKE3_MAP_NAME "maps/cf.bsp"
  25. #else
  26. #define QUAKE3_MAP_NAME "maps/q3dm8.bsp"
  27. #endif
  28. #endif
  29. #ifdef IRRLICHT_QUAKE3_ARENA
  30. #define QUAKE3_STORAGE_FORMAT addFileArchive
  31. #define QUAKE3_STORAGE_1 getExampleMediaPath() + "map-20kdm2.pk3"
  32. #define QUAKE3_MAP_NAME "maps/20kdm2.bsp"
  33. #endif
  34. using namespace irr;
  35. using namespace scene;
  36. /*
  37. Again, to be able to use the Irrlicht.dll on Windows, we link with the
  38. Irrlicht.lib. We could set this option in the project settings, but
  39. to make it easy, we use a pragma comment lib:
  40. */
  41. #ifdef _MSC_VER
  42. #pragma comment(lib, "Irrlicht.lib")
  43. #endif
  44. /*
  45. A class to produce a series of screenshots
  46. */
  47. class CScreenShotFactory : public IEventReceiver
  48. {
  49. public:
  50. CScreenShotFactory( IrrlichtDevice *device, const c8 * templateName, ISceneNode* node )
  51. : Device(device), Number(0), FilenameTemplate(templateName), Node(node)
  52. {
  53. FilenameTemplate.replace ( '/', '_' );
  54. FilenameTemplate.replace ( '\\', '_' );
  55. }
  56. bool OnEvent(const SEvent& event)
  57. {
  58. if ((event.EventType == EET_KEY_INPUT_EVENT) &&
  59. event.KeyInput.PressedDown)
  60. {
  61. // check if user presses the key F9 for making a screenshot
  62. if (event.KeyInput.Key == KEY_F9)
  63. {
  64. video::IImage* image = Device->getVideoDriver()->createScreenShot();
  65. if (image)
  66. {
  67. c8 buf[256];
  68. snprintf_irr(buf, 256, "%s_shot%04u.jpg",
  69. FilenameTemplate.c_str(),
  70. ++Number);
  71. Device->getVideoDriver()->writeImageToFile(image, buf, 85 );
  72. image->drop();
  73. }
  74. }
  75. // Check for F8 - enabling/disabling display of bounding box for the map
  76. else if (event.KeyInput.Key == KEY_F8)
  77. {
  78. if (Node->isDebugDataVisible())
  79. Node->setDebugDataVisible(scene::EDS_OFF);
  80. else
  81. Node->setDebugDataVisible(scene::EDS_BBOX_ALL);
  82. }
  83. }
  84. return false;
  85. }
  86. private:
  87. IrrlichtDevice *Device;
  88. u32 Number;
  89. core::stringc FilenameTemplate;
  90. ISceneNode* Node;
  91. };
  92. /*
  93. Ok, lets start.
  94. */
  95. int IRRCALLCONV main(int argc, char* argv[])
  96. {
  97. /*
  98. Like in the HelloWorld example, we create an IrrlichtDevice with
  99. createDevice(). The difference now is that we ask the user to select
  100. which hardware accelerated driver to use. The Software device might be
  101. too slow to draw a huge Quake 3 map, but just for the fun of it, we make
  102. this decision possible too.
  103. */
  104. // ask user for driver
  105. video::E_DRIVER_TYPE driverType=driverChoiceConsole();
  106. if (driverType==video::EDT_COUNT)
  107. return 1;
  108. // create device and exit if creation failed
  109. const core::dimension2du videoDim(800,600);
  110. IrrlichtDevice *device = createDevice(driverType, videoDim, 32, false );
  111. if (device == 0)
  112. return 1; // could not create selected driver.
  113. // We allow passing a map name as command line parameter
  114. const char* mapname=0;
  115. if (argc>2)
  116. mapname = argv[2];
  117. else
  118. mapname = QUAKE3_MAP_NAME;
  119. /*
  120. Get a pointer to the video driver and the SceneManager so that
  121. we do not always have to write device->getVideoDriver() and
  122. device->getSceneManager().
  123. */
  124. video::IVideoDriver* driver = device->getVideoDriver();
  125. scene::ISceneManager* smgr = device->getSceneManager();
  126. gui::IGUIEnvironment* gui = device->getGUIEnvironment();
  127. const io::path mediaPath = getExampleMediaPath();
  128. //! add our private media directory to the file system
  129. device->getFileSystem()->addFileArchive(mediaPath);
  130. /*
  131. To display the Quake 3 map, we first need to load it. Quake 3 maps
  132. are packed into .pk3 files, which are nothing other than .zip files.
  133. So we add the .pk3 file to our FileSystem. After it was added,
  134. we are able to read from the files in that archive as they would
  135. directly be stored on disk.
  136. */
  137. if (argc>2)
  138. device->getFileSystem()->QUAKE3_STORAGE_FORMAT(argv[1]);
  139. else
  140. device->getFileSystem()->QUAKE3_STORAGE_FORMAT(QUAKE3_STORAGE_1);
  141. #ifdef QUAKE3_STORAGE_2
  142. device->getFileSystem()->QUAKE3_STORAGE_FORMAT(QUAKE3_STORAGE_2);
  143. #endif
  144. // Quake3 Shader controls Z-Writing
  145. smgr->getParameters()->setAttribute(scene::ALLOW_ZWRITE_ON_TRANSPARENT, true);
  146. /*
  147. Now we can load the mesh by calling getMesh(). We get a pointer returned
  148. to an IAnimatedMesh. As you know, Quake 3 maps are not really animated,
  149. they are only a huge chunk of static geometry with some materials
  150. attached. Hence the IAnimated mesh consists of only one frame,
  151. so we get the "first frame" of the "animation", which is our quake level
  152. and create an Octree scene node with it, using addOctreeSceneNode().
  153. The Octree optimizes the scene a little bit, trying to draw only geometry
  154. which is currently visible. An alternative to the Octree would be a
  155. AnimatedMeshSceneNode, which would draw always the complete geometry of
  156. the mesh, without optimization. Try it out: Write addAnimatedMeshSceneNode
  157. instead of addOctreeSceneNode and compare the primitives drawn by the
  158. video driver. (There is a getPrimitiveCountDrawed() method in the
  159. IVideoDriver class). Note that this optimization with the Octree is only
  160. useful when drawing huge meshes consisting of lots of geometry.
  161. */
  162. scene::IQ3LevelMesh* const mesh =
  163. (scene::IQ3LevelMesh*) smgr->getMesh(mapname);
  164. /*
  165. add the geometry mesh to the Scene ( polygon & patches )
  166. The Geometry mesh is optimised for faster drawing
  167. */
  168. scene::ISceneNode* node = 0;
  169. if (mesh)
  170. {
  171. scene::IMesh * const geometry = mesh->getMesh(quake3::E_Q3_MESH_GEOMETRY);
  172. node = smgr->addOctreeSceneNode(geometry, 0, -1, 4096);
  173. }
  174. // create an event receiver for making screenshots
  175. CScreenShotFactory screenshotFactory(device, mapname, node);
  176. device->setEventReceiver(&screenshotFactory);
  177. /*
  178. now construct SceneNodes for each shader
  179. The objects are stored in the quake mesh scene::E_Q3_MESH_ITEMS
  180. and the shader ID is stored in the MaterialParameters
  181. mostly dark looking skulls and moving lava.. or green flashing tubes?
  182. */
  183. if ( mesh )
  184. {
  185. // the additional mesh can be quite huge and is unoptimized
  186. const scene::IMesh * const additional_mesh = mesh->getMesh(quake3::E_Q3_MESH_ITEMS);
  187. #ifdef SHOW_SHADER_NAME
  188. gui::IGUIFont *font = device->getGUIEnvironment()->getFont(mediaPath + "fontlucida.png");
  189. u32 count = 0;
  190. #endif
  191. for ( u32 i = 0; i!= additional_mesh->getMeshBufferCount(); ++i )
  192. {
  193. const IMeshBuffer* meshBuffer = additional_mesh->getMeshBuffer(i);
  194. const video::SMaterial& material = meshBuffer->getMaterial();
  195. // The ShaderIndex is stored in the material parameter
  196. const s32 shaderIndex = (s32) material.MaterialTypeParam2;
  197. // the meshbuffer can be rendered without additional support, or it has no shader
  198. const quake3::IShader *shader = mesh->getShader(shaderIndex);
  199. if (0 == shader)
  200. {
  201. continue;
  202. }
  203. // we can dump the shader to the console in its
  204. // original but already parsed layout in a pretty
  205. // printers way.. commented out, because the console
  206. // would be full...
  207. // quake3::dumpShader ( Shader );
  208. node = smgr->addQuake3SceneNode(meshBuffer, shader);
  209. #ifdef SHOW_SHADER_NAME
  210. count += 1;
  211. core::stringw name( node->getName() );
  212. node = smgr->addBillboardTextSceneNode(
  213. font, name.c_str(), node,
  214. core::dimension2d<f32>(80.0f, 8.0f),
  215. core::vector3df(0, 10, 0));
  216. #endif
  217. }
  218. }
  219. /*
  220. Now we only need a camera to look at the Quake 3 map. And we want to
  221. create a user controlled camera. There are some different cameras
  222. available in the Irrlicht engine. For example the Maya camera which can
  223. be controlled comparable to the camera in Maya: Rotate with left mouse
  224. button pressed, Zoom with both buttons pressed, translate with right
  225. mouse button pressed. This could be created with
  226. addCameraSceneNodeMaya(). But for this example, we want to create a
  227. camera which behaves like the ones in first person shooter games (FPS).
  228. */
  229. scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
  230. /*
  231. so we need a good starting position in the level.
  232. we can ask the Quake3 loader for all entities with class_name
  233. "info_player_deathmatch"
  234. we choose a random launch
  235. */
  236. if ( mesh )
  237. {
  238. quake3::tQ3EntityList &entityList = mesh->getEntityList();
  239. quake3::IEntity search;
  240. search.name = "info_player_deathmatch";
  241. s32 index = entityList.binary_search(search);
  242. if (index >= 0)
  243. {
  244. s32 notEndList;
  245. do
  246. {
  247. const quake3::SVarGroup *group = entityList[index].getGroup(1);
  248. u32 parsepos = 0;
  249. const core::vector3df pos =
  250. quake3::getAsVector3df(group->get("origin"), parsepos);
  251. parsepos = 0;
  252. const f32 angle = quake3::getAsFloat(group->get("angle"), parsepos);
  253. core::vector3df target(0.f, 0.f, 1.f);
  254. target.rotateXZBy(angle);
  255. camera->setPosition(pos);
  256. camera->setTarget(pos + target);
  257. ++index;
  258. /*
  259. notEndList = ( index < (s32) entityList.size () &&
  260. entityList[index].name == search.name &&
  261. (device->getTimer()->getRealTime() >> 3 ) & 1
  262. );
  263. */
  264. notEndList = index == 2;
  265. } while ( notEndList );
  266. }
  267. }
  268. /*
  269. The mouse cursor needs not to be visible, so we make it invisible.
  270. */
  271. device->getCursorControl()->setVisible(false);
  272. // load the engine logo
  273. gui->addImage(driver->getTexture("irrlichtlogo3.png"),
  274. core::position2d<s32>(10, 10));
  275. // show the driver logo
  276. const core::position2di pos(videoDim.Width - 128, videoDim.Height - 64);
  277. switch ( driverType )
  278. {
  279. case video::EDT_BURNINGSVIDEO:
  280. gui->addImage(driver->getTexture("burninglogo.png"), pos);
  281. break;
  282. case video::EDT_OPENGL:
  283. gui->addImage(driver->getTexture("opengllogo.png"), pos);
  284. break;
  285. case video::EDT_DIRECT3D9:
  286. gui->addImage(driver->getTexture("directxlogo.png"), pos);
  287. break;
  288. default:
  289. break;
  290. }
  291. /*
  292. We have done everything, so lets draw it. We also write the current
  293. frames per second and the drawn primitives to the caption of the
  294. window. The 'if (device->isWindowActive())' line is optional, but
  295. prevents the engine render to set the position of the mouse cursor
  296. after task switching when other program are active.
  297. */
  298. int lastFPS = -1;
  299. while(device->run())
  300. if (device->isWindowActive())
  301. {
  302. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255,20,20,40));
  303. smgr->drawAll();
  304. gui->drawAll();
  305. driver->endScene();
  306. // Display some info
  307. // Setting window caption can be rather slow, so usually shouldn't be done each frame.
  308. int fps = driver->getFPS();
  309. if (1 || lastFPS != fps)
  310. {
  311. core::stringw str = L"Q3 [";
  312. str += driver->getName();
  313. str += "] FPS:";
  314. str += fps;
  315. #ifdef _IRR_SCENEMANAGER_DEBUG
  316. io::IAttributes * const attr = smgr->getParameters();
  317. str += " Cull:";
  318. str += attr->getAttributeAsInt("calls");
  319. str += "/";
  320. str += attr->getAttributeAsInt("culled");
  321. str += " Draw: ";
  322. str += attr->getAttributeAsInt("drawn_solid");
  323. str += "/";
  324. str += attr->getAttributeAsInt("drawn_transparent");
  325. str += "/";
  326. str += attr->getAttributeAsInt("drawn_transparent_effect");
  327. #endif
  328. device->setWindowCaption(str.c_str());
  329. lastFPS = fps;
  330. }
  331. }
  332. /*
  333. In the end, delete the Irrlicht device.
  334. */
  335. device->drop();
  336. return 0;
  337. }
  338. /*
  339. **/