PageRenderTime 60ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/examples/16.Quake3MapShader/main.cpp

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