main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 file, we need to 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. // check if user presses the key F9
  59. if ((event.EventType == EET_KEY_INPUT_EVENT) &&
  60. event.KeyInput.PressedDown)
  61. {
  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%04d.jpg",
  69. FilenameTemplate.c_str(),
  70. ++Number);
  71. Device->getVideoDriver()->writeImageToFile(image, buf, 85 );
  72. image->drop();
  73. }
  74. }
  75. else
  76. 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 would 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. const char* mapname=0;
  114. if (argc>2)
  115. mapname = argv[2];
  116. else
  117. mapname = QUAKE3_MAP_NAME;
  118. /*
  119. Get a pointer to the video driver and the SceneManager so that
  120. we do not always have to write device->getVideoDriver() and
  121. device->getSceneManager().
  122. */
  123. video::IVideoDriver* driver = device->getVideoDriver();
  124. scene::ISceneManager* smgr = device->getSceneManager();
  125. gui::IGUIEnvironment* gui = device->getGUIEnvironment();
  126. const io::path mediaPath = getExampleMediaPath();
  127. //! add our private media directory to the file system
  128. device->getFileSystem()->addFileArchive(mediaPath);
  129. /*
  130. To display the Quake 3 map, we first need to load it. Quake 3 maps
  131. are packed into .pk3 files, which are nothing other than .zip files.
  132. So we add the .pk3 file to our FileSystem. After it was added,
  133. we are able to read from the files in that archive as they would
  134. directly be stored on disk.
  135. */
  136. if (argc>2)
  137. device->getFileSystem()->QUAKE3_STORAGE_FORMAT(argv[1]);
  138. else
  139. device->getFileSystem()->QUAKE3_STORAGE_FORMAT(QUAKE3_STORAGE_1);
  140. #ifdef QUAKE3_STORAGE_2
  141. device->getFileSystem()->QUAKE3_STORAGE_FORMAT(QUAKE3_STORAGE_2);
  142. #endif
  143. // Quake3 Shader controls Z-Writing
  144. smgr->getParameters()->setAttribute(scene::ALLOW_ZWRITE_ON_TRANSPARENT, true);
  145. /*
  146. Now we can load the mesh by calling getMesh(). We get a pointer returned
  147. to a IAnimatedMesh. As you know, Quake 3 maps are not really animated,
  148. they are only a huge chunk of static geometry with some materials
  149. attached. Hence the IAnimated mesh consists of only one frame,
  150. so we get the "first frame" of the "animation", which is our quake level
  151. and create an Octree scene node with it, using addOctreeSceneNode().
  152. The Octree optimizes the scene a little bit, trying to draw only geometry
  153. which is currently visible. An alternative to the Octree would be a
  154. AnimatedMeshSceneNode, which would draw always the complete geometry of
  155. the mesh, without optimization. Try it out: Write addAnimatedMeshSceneNode
  156. instead of addOctreeSceneNode and compare the primitives drawn by the
  157. video driver. (There is a getPrimitiveCountDrawed() method in the
  158. IVideoDriver class). Note that this optimization with the Octree is only
  159. useful when drawing huge meshes consisting of lots of geometry.
  160. */
  161. scene::IQ3LevelMesh* const mesh =
  162. (scene::IQ3LevelMesh*) smgr->getMesh(mapname);
  163. /*
  164. add the geometry mesh to the Scene ( polygon & patches )
  165. The Geometry mesh is optimised for faster drawing
  166. */
  167. scene::ISceneNode* node = 0;
  168. if (mesh)
  169. {
  170. scene::IMesh * const geometry = mesh->getMesh(quake3::E_Q3_MESH_GEOMETRY);
  171. node = smgr->addOctreeSceneNode(geometry, 0, -1, 4096);
  172. }
  173. // create an event receiver for making screenshots
  174. CScreenShotFactory screenshotFactory(device, mapname, node);
  175. device->setEventReceiver(&screenshotFactory);
  176. /*
  177. now construct SceneNodes for each Shader
  178. The Objects are stored in the quake mesh scene::E_Q3_MESH_ITEMS
  179. and the Shader ID is stored in the MaterialParameters
  180. mostly dark looking skulls and moving lava.. or green flashing tubes?
  181. */
  182. if ( mesh )
  183. {
  184. // the additional mesh can be quite huge and is unoptimized
  185. const scene::IMesh * const additional_mesh = mesh->getMesh(quake3::E_Q3_MESH_ITEMS);
  186. #ifdef SHOW_SHADER_NAME
  187. gui::IGUIFont *font = device->getGUIEnvironment()->getFont(mediaPath + "fontlucida.png");
  188. u32 count = 0;
  189. #endif
  190. for ( u32 i = 0; i!= additional_mesh->getMeshBufferCount(); ++i )
  191. {
  192. const IMeshBuffer* meshBuffer = additional_mesh->getMeshBuffer(i);
  193. const video::SMaterial& material = meshBuffer->getMaterial();
  194. // The ShaderIndex is stored in the material parameter
  195. const s32 shaderIndex = (s32) material.MaterialTypeParam2;
  196. // the meshbuffer can be rendered without additional support, or it has no shader
  197. const quake3::IShader *shader = mesh->getShader(shaderIndex);
  198. if (0 == shader)
  199. {
  200. continue;
  201. }
  202. // we can dump the shader to the console in its
  203. // original but already parsed layout in a pretty
  204. // printers way.. commented out, because the console
  205. // would be full...
  206. // quake3::dumpShader ( Shader );
  207. node = smgr->addQuake3SceneNode(meshBuffer, shader);
  208. #ifdef SHOW_SHADER_NAME
  209. count += 1;
  210. core::stringw name( node->getName() );
  211. node = smgr->addBillboardTextSceneNode(
  212. font, name.c_str(), node,
  213. core::dimension2d<f32>(80.0f, 8.0f),
  214. core::vector3df(0, 10, 0));
  215. #endif
  216. }
  217. }
  218. /*
  219. Now we only need a Camera to look at the Quake 3 map. And we want to
  220. create a user controlled camera. There are some different cameras
  221. available in the Irrlicht engine. For example the Maya Camera which can
  222. be controlled comparable to the camera in Maya: Rotate with left mouse
  223. button pressed, Zoom with both buttons pressed, translate with right
  224. mouse button pressed. This could be created with
  225. addCameraSceneNodeMaya(). But for this example, we want to create a
  226. camera which behaves like the ones in first person shooter games (FPS).
  227. */
  228. scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
  229. /*
  230. so we need a good starting Position in the level.
  231. we can ask the Quake3 Loader for all entities with class_name
  232. "info_player_deathmatch"
  233. we choose a random launch
  234. */
  235. if ( mesh )
  236. {
  237. quake3::tQ3EntityList &entityList = mesh->getEntityList();
  238. quake3::IEntity search;
  239. search.name = "info_player_deathmatch";
  240. s32 index = entityList.binary_search(search);
  241. if (index >= 0)
  242. {
  243. s32 notEndList;
  244. do
  245. {
  246. const quake3::SVarGroup *group = entityList[index].getGroup(1);
  247. u32 parsepos = 0;
  248. const core::vector3df pos =
  249. quake3::getAsVector3df(group->get("origin"), parsepos);
  250. parsepos = 0;
  251. const f32 angle = quake3::getAsFloat(group->get("angle"), parsepos);
  252. core::vector3df target(0.f, 0.f, 1.f);
  253. target.rotateXZBy(angle);
  254. camera->setPosition(pos);
  255. camera->setTarget(pos + target);
  256. ++index;
  257. /*
  258. notEndList = ( index < (s32) entityList.size () &&
  259. entityList[index].name == search.name &&
  260. (device->getTimer()->getRealTime() >> 3 ) & 1
  261. );
  262. */
  263. notEndList = index == 2;
  264. } while ( notEndList );
  265. }
  266. }
  267. /*
  268. The mouse cursor needs not to be visible, so we make it invisible.
  269. */
  270. device->getCursorControl()->setVisible(false);
  271. // load the engine logo
  272. gui->addImage(driver->getTexture("irrlichtlogo3.png"),
  273. core::position2d<s32>(10, 10));
  274. // show the driver logo
  275. const core::position2di pos(videoDim.Width - 128, videoDim.Height - 64);
  276. switch ( driverType )
  277. {
  278. case video::EDT_BURNINGSVIDEO:
  279. gui->addImage(driver->getTexture("burninglogo.png"), pos);
  280. break;
  281. case video::EDT_OPENGL:
  282. gui->addImage(driver->getTexture("opengllogo.png"), pos);
  283. break;
  284. case video::EDT_DIRECT3D9:
  285. gui->addImage(driver->getTexture("directxlogo.png"), pos);
  286. break;
  287. default:
  288. break;
  289. }
  290. /*
  291. We have done everything, so lets draw it. We also write the current
  292. frames per second and the drawn primitives to the caption of the
  293. window. The 'if (device->isWindowActive())' line is optional, but
  294. prevents the engine render to set the position of the mouse cursor
  295. after task switching when other program are active.
  296. */
  297. int lastFPS = -1;
  298. while(device->run())
  299. if (device->isWindowActive())
  300. {
  301. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255,20,20,40));
  302. smgr->drawAll();
  303. gui->drawAll();
  304. driver->endScene();
  305. int fps = driver->getFPS();
  306. if (1 || lastFPS != fps)
  307. {
  308. core::stringw str = L"Q3 [";
  309. str += driver->getName();
  310. str += "] FPS:";
  311. str += fps;
  312. #ifdef _IRR_SCENEMANAGER_DEBUG
  313. io::IAttributes * const attr = smgr->getParameters();
  314. str += " Cull:";
  315. str += attr->getAttributeAsInt("calls");
  316. str += "/";
  317. str += attr->getAttributeAsInt("culled");
  318. str += " Draw: ";
  319. str += attr->getAttributeAsInt("drawn_solid");
  320. str += "/";
  321. str += attr->getAttributeAsInt("drawn_transparent");
  322. str += "/";
  323. str += attr->getAttributeAsInt("drawn_transparent_effect");
  324. #endif
  325. device->setWindowCaption(str.c_str());
  326. lastFPS = fps;
  327. }
  328. }
  329. /*
  330. In the end, delete the Irrlicht device.
  331. */
  332. device->drop();
  333. return 0;
  334. }
  335. /*
  336. **/