main.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /** Example 009 Mesh Viewer
  2. This tutorial show how to create a more complex application with the engine.
  3. We construct a simple mesh viewer using the user interface API and the
  4. scene management of Irrlicht. The tutorial show how to create and use Buttons,
  5. Windows, Toolbars, Menus, ComboBoxes, Tabcontrols, Editboxes, Images,
  6. MessageBoxes, SkyBoxes, and how to parse XML files with the integrated XML
  7. reader of the engine.
  8. We start like in most other tutorials: Include all necessary header files, add
  9. a comment to let the engine be linked with the correct .lib file in Visual
  10. Studio, and declare some global variables. We also add two 'using namespace'
  11. statements, so we do not need to write the whole names of all classes. In this
  12. tutorial, we use a lot of stuff from the gui namespace.
  13. */
  14. #include <irrlicht.h>
  15. #include "driverChoice.h"
  16. #include "exampleHelper.h"
  17. using namespace irr;
  18. using namespace gui;
  19. #ifdef _MSC_VER
  20. #pragma comment(lib, "Irrlicht.lib")
  21. #endif
  22. /*
  23. Some global variables used later on
  24. */
  25. IrrlichtDevice *Device = 0;
  26. io::path StartUpModelFile;
  27. core::stringw MessageText;
  28. core::stringw Caption;
  29. scene::ISceneNode* Model = 0;
  30. scene::ISceneNode* SkyBox = 0;
  31. bool Octree=false;
  32. bool UseLight=false;
  33. scene::ICameraSceneNode* Camera[2] = {0, 0};
  34. // Values used to identify individual GUI elements
  35. enum
  36. {
  37. GUI_ID_DIALOG_ROOT_WINDOW = 0x10000,
  38. GUI_ID_X_SCALE,
  39. GUI_ID_Y_SCALE,
  40. GUI_ID_Z_SCALE,
  41. GUI_ID_OPEN_MODEL,
  42. GUI_ID_SET_MODEL_ARCHIVE,
  43. GUI_ID_LOAD_AS_OCTREE,
  44. GUI_ID_SKY_BOX_VISIBLE,
  45. GUI_ID_TOGGLE_DEBUG_INFO,
  46. GUI_ID_DEBUG_OFF,
  47. GUI_ID_DEBUG_BOUNDING_BOX,
  48. GUI_ID_DEBUG_NORMALS,
  49. GUI_ID_DEBUG_SKELETON,
  50. GUI_ID_DEBUG_WIRE_OVERLAY,
  51. GUI_ID_DEBUG_HALF_TRANSPARENT,
  52. GUI_ID_DEBUG_BUFFERS_BOUNDING_BOXES,
  53. GUI_ID_DEBUG_ALL,
  54. GUI_ID_MODEL_MATERIAL_SOLID,
  55. GUI_ID_MODEL_MATERIAL_TRANSPARENT,
  56. GUI_ID_MODEL_MATERIAL_REFLECTION,
  57. GUI_ID_CAMERA_MAYA,
  58. GUI_ID_CAMERA_FIRST_PERSON,
  59. GUI_ID_POSITION_TEXT,
  60. GUI_ID_ABOUT,
  61. GUI_ID_QUIT,
  62. GUI_ID_TEXTUREFILTER,
  63. GUI_ID_SKIN_TRANSPARENCY,
  64. GUI_ID_SKIN_ANIMATION_FPS,
  65. GUI_ID_BUTTON_SET_SCALE,
  66. GUI_ID_BUTTON_SCALE_MUL10,
  67. GUI_ID_BUTTON_SCALE_DIV10,
  68. GUI_ID_BUTTON_OPEN_MODEL,
  69. GUI_ID_BUTTON_SHOW_ABOUT,
  70. GUI_ID_BUTTON_SHOW_TOOLBOX,
  71. GUI_ID_BUTTON_SELECT_ARCHIVE,
  72. GUI_ID_ANIMATION_INFO,
  73. // And some magic numbers
  74. MAX_FRAMERATE = 80,
  75. DEFAULT_FRAMERATE = 30
  76. };
  77. /*
  78. Toggle between various cameras
  79. */
  80. void setActiveCamera(scene::ICameraSceneNode* newActive)
  81. {
  82. if (0 == Device)
  83. return;
  84. scene::ICameraSceneNode * active = Device->getSceneManager()->getActiveCamera();
  85. active->setInputReceiverEnabled(false);
  86. newActive->setInputReceiverEnabled(true);
  87. Device->getSceneManager()->setActiveCamera(newActive);
  88. }
  89. /*
  90. Set the skin transparency by changing the alpha values of all skin-colors
  91. */
  92. void setSkinTransparency(s32 alpha, irr::gui::IGUISkin * skin)
  93. {
  94. for (s32 i=0; i<irr::gui::EGDC_COUNT ; ++i)
  95. {
  96. video::SColor col = skin->getColor((EGUI_DEFAULT_COLOR)i);
  97. col.setAlpha(alpha);
  98. skin->setColor((EGUI_DEFAULT_COLOR)i, col);
  99. }
  100. }
  101. /*
  102. Update the display of the model scaling
  103. */
  104. void updateScaleInfo(scene::ISceneNode* model)
  105. {
  106. IGUIElement* toolboxWnd = Device->getGUIEnvironment()->getRootGUIElement()->getElementFromId(GUI_ID_DIALOG_ROOT_WINDOW, true);
  107. if (!toolboxWnd)
  108. return;
  109. if (!model)
  110. {
  111. toolboxWnd->getElementFromId(GUI_ID_X_SCALE, true)->setText( L"-" );
  112. toolboxWnd->getElementFromId(GUI_ID_Y_SCALE, true)->setText( L"-" );
  113. toolboxWnd->getElementFromId(GUI_ID_Z_SCALE, true)->setText( L"-" );
  114. }
  115. else
  116. {
  117. core::vector3df scale = model->getScale();
  118. toolboxWnd->getElementFromId(GUI_ID_X_SCALE, true)->setText( core::stringw(scale.X).c_str() );
  119. toolboxWnd->getElementFromId(GUI_ID_Y_SCALE, true)->setText( core::stringw(scale.Y).c_str() );
  120. toolboxWnd->getElementFromId(GUI_ID_Z_SCALE, true)->setText( core::stringw(scale.Z).c_str() );
  121. }
  122. }
  123. /*
  124. Function showAboutText() displays a messagebox with a caption and
  125. a message text. The texts will be stored in the MessageText and Caption
  126. variables at startup.
  127. */
  128. void showAboutText()
  129. {
  130. // create modal message box with the text
  131. // loaded from the xml file.
  132. Device->getGUIEnvironment()->addMessageBox(
  133. Caption.c_str(), MessageText.c_str());
  134. }
  135. /*
  136. Function loadModel() loads a model and displays it using an
  137. addAnimatedMeshSceneNode and the scene manager. Nothing difficult. It also
  138. displays a short message box, if the model could not be loaded.
  139. */
  140. void loadModel(const io::path& filename)
  141. {
  142. io::path extension;
  143. core::getFileNameExtension(extension, filename);
  144. extension.make_lower();
  145. // if a texture is loaded apply it to the current model..
  146. if (extension == ".jpg" || extension == ".pcx" ||
  147. extension == ".png" || extension == ".ppm" ||
  148. extension == ".pgm" || extension == ".pbm" ||
  149. extension == ".psd" || extension == ".tga" ||
  150. extension == ".bmp" || extension == ".wal" ||
  151. extension == ".rgb" || extension == ".rgba")
  152. {
  153. // Ensure reloading texture by clearing old one out of cache
  154. video::ITexture * texture = Device->getVideoDriver()->findTexture( filename );
  155. if ( texture )
  156. Device->getVideoDriver()->removeTexture(texture);
  157. // Load the new one and put int on the model
  158. texture = Device->getVideoDriver()->getTexture( filename );
  159. if ( texture && Model )
  160. {
  161. Model->setMaterialTexture(0, texture);
  162. }
  163. return;
  164. }
  165. // if a archive is loaded add it to the FileArchive..
  166. else if (extension == ".pk3" || extension == ".zip" || extension == ".pak" || extension == ".npk")
  167. {
  168. Device->getFileSystem()->addFileArchive(filename.c_str());
  169. return;
  170. }
  171. // Remove old model
  172. if (Model)
  173. {
  174. Model->remove();
  175. Model = 0;
  176. }
  177. // .irr is a scene format, so load as scene and set Model pointer to first object in the scene
  178. if (extension==".irr")
  179. {
  180. core::array<scene::ISceneNode*> outNodes;
  181. Device->getSceneManager()->loadScene(filename);
  182. Device->getSceneManager()->getSceneNodesFromType(scene::ESNT_ANIMATED_MESH, outNodes);
  183. if (outNodes.size())
  184. Model = outNodes[0];
  185. return;
  186. }
  187. // load a model into the engine. Also log the time it takes to load it.
  188. u32 then = Device->getTimer()->getRealTime();
  189. scene::IAnimatedMesh* mesh = Device->getSceneManager()->getMesh( filename.c_str() );
  190. Device->getLogger()->log("Loading time (ms): ", core::stringc(Device->getTimer()->getRealTime() - then).c_str());
  191. if (!mesh)
  192. {
  193. // model could not be loaded
  194. if (StartUpModelFile != filename)
  195. Device->getGUIEnvironment()->addMessageBox(
  196. Caption.c_str(), L"The model could not be loaded. " \
  197. L"Maybe it is not a supported file format.");
  198. return;
  199. }
  200. // set default material properties
  201. if (Octree)
  202. Model = Device->getSceneManager()->addOctreeSceneNode(mesh->getMesh(0));
  203. else
  204. {
  205. scene::IAnimatedMeshSceneNode* animModel = Device->getSceneManager()->addAnimatedMeshSceneNode(mesh);
  206. Model = animModel;
  207. }
  208. Model->setMaterialFlag(video::EMF_LIGHTING, UseLight);
  209. Model->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, UseLight);
  210. // Model->setMaterialFlag(video::EMF_BACK_FACE_CULLING, false);
  211. Model->setDebugDataVisible(scene::EDS_OFF);
  212. // we need to uncheck the menu entries. would be cool to fake a menu event, but
  213. // that's not so simple. so we do it brute force
  214. gui::IGUIContextMenu* menu = (gui::IGUIContextMenu*)Device->getGUIEnvironment()->getRootGUIElement()->getElementFromId(GUI_ID_TOGGLE_DEBUG_INFO, true);
  215. if (menu)
  216. for(int item = 1; item < 7; ++item)
  217. menu->setItemChecked(item, false);
  218. updateScaleInfo(Model);
  219. }
  220. /*
  221. Function createToolBox() creates a toolbox window. In this simple mesh
  222. viewer, this toolbox only contains a controls to change the scale
  223. and animation speed of the model and a control to set the transparency
  224. of the GUI-elements.
  225. */
  226. void createToolBox()
  227. {
  228. // remove tool box if already there
  229. IGUIEnvironment* env = Device->getGUIEnvironment();
  230. IGUIElement* root = env->getRootGUIElement();
  231. IGUIElement* e = root->getElementFromId(GUI_ID_DIALOG_ROOT_WINDOW, true);
  232. if (e)
  233. e->remove();
  234. // create the toolbox window
  235. IGUIWindow* wnd = env->addWindow(core::rect<s32>(600,45,800,480),
  236. false, L"Toolset", 0, GUI_ID_DIALOG_ROOT_WINDOW);
  237. // create tab control and tabs
  238. IGUITabControl* tab = env->addTabControl(
  239. core::rect<s32>(2,20,800-602,480-7), wnd, true, true);
  240. IGUITab* t1 = tab->addTab(L"Config");
  241. // add some edit boxes and a button to tab one
  242. env->addStaticText(L"Scale:",
  243. core::rect<s32>(10,20,60,45), false, false, t1);
  244. env->addStaticText(L"X:", core::rect<s32>(22,48,40,66), false, false, t1);
  245. env->addEditBox(L"1.0", core::rect<s32>(40,46,130,66), true, t1, GUI_ID_X_SCALE);
  246. env->addStaticText(L"Y:", core::rect<s32>(22,82,40,96), false, false, t1);
  247. env->addEditBox(L"1.0", core::rect<s32>(40,76,130,96), true, t1, GUI_ID_Y_SCALE);
  248. env->addStaticText(L"Z:", core::rect<s32>(22,108,40,126), false, false, t1);
  249. env->addEditBox(L"1.0", core::rect<s32>(40,106,130,126), true, t1, GUI_ID_Z_SCALE);
  250. env->addButton(core::rect<s32>(10,134,85,165), t1, GUI_ID_BUTTON_SET_SCALE, L"Set");
  251. // quick scale buttons
  252. env->addButton(core::rect<s32>(65,20,95,40), t1, GUI_ID_BUTTON_SCALE_MUL10, L"* 10");
  253. env->addButton(core::rect<s32>(100,20,130,40), t1, GUI_ID_BUTTON_SCALE_DIV10, L"* 0.1");
  254. updateScaleInfo(Model);
  255. // add transparency control
  256. env->addStaticText(L"GUI Transparency Control:",
  257. core::rect<s32>(10,200,150,225), true, false, t1);
  258. IGUIScrollBar* scrollbar = env->addScrollBar(true,
  259. core::rect<s32>(10,225,150,240), t1, GUI_ID_SKIN_TRANSPARENCY);
  260. scrollbar->setMax(255);
  261. scrollbar->setPos(255);
  262. // add framerate control
  263. env->addStaticText(L":", core::rect<s32>(10,240,150,265), true, false, t1);
  264. env->addStaticText(L"Framerate:",
  265. core::rect<s32>(12,240,75,265), false, false, t1);
  266. // current frame info
  267. env->addStaticText(L"", core::rect<s32>(75,240,200,265), false, false, t1,
  268. GUI_ID_ANIMATION_INFO);
  269. scrollbar = env->addScrollBar(true,
  270. core::rect<s32>(10,265,150,280), t1, GUI_ID_SKIN_ANIMATION_FPS);
  271. scrollbar->setMax(MAX_FRAMERATE);
  272. scrollbar->setMin(-MAX_FRAMERATE);
  273. scrollbar->setPos(DEFAULT_FRAMERATE);
  274. scrollbar->setSmallStep(1);
  275. }
  276. /*
  277. Function updateToolBox() is called each frame to update dynamic information in
  278. the toolbox.
  279. */
  280. void updateToolBox()
  281. {
  282. IGUIEnvironment* env = Device->getGUIEnvironment();
  283. IGUIElement* root = env->getRootGUIElement();
  284. IGUIElement* dlg = root->getElementFromId(GUI_ID_DIALOG_ROOT_WINDOW, true);
  285. if (!dlg )
  286. return;
  287. // update the info we have about the animation of the model
  288. IGUIStaticText * aniInfo = (IGUIStaticText *)(dlg->getElementFromId(GUI_ID_ANIMATION_INFO, true));
  289. if (aniInfo)
  290. {
  291. if ( Model && scene::ESNT_ANIMATED_MESH == Model->getType() )
  292. {
  293. scene::IAnimatedMeshSceneNode* animatedModel = (scene::IAnimatedMeshSceneNode*)Model;
  294. core::stringw str( (s32)core::round_(animatedModel->getAnimationSpeed()) );
  295. str += L" Frame: ";
  296. str += core::stringw((s32)animatedModel->getFrameNr());
  297. aniInfo->setText(str.c_str());
  298. }
  299. else
  300. aniInfo->setText(L"");
  301. }
  302. }
  303. void onKillFocus()
  304. {
  305. // Avoid that the FPS-camera continues moving when the user presses alt-tab while
  306. // moving the camera.
  307. const core::list<scene::ISceneNodeAnimator*>& animators = Camera[1]->getAnimators();
  308. core::list<irr::scene::ISceneNodeAnimator*>::ConstIterator iter = animators.begin();
  309. while ( iter != animators.end() )
  310. {
  311. if ( (*iter)->getType() == scene::ESNAT_CAMERA_FPS )
  312. {
  313. // we send a key-down event for all keys used by this animator
  314. scene::ISceneNodeAnimatorCameraFPS * fpsAnimator = static_cast<scene::ISceneNodeAnimatorCameraFPS*>(*iter);
  315. const core::array<SKeyMap>& keyMap = fpsAnimator->getKeyMap();
  316. for ( irr::u32 i=0; i< keyMap.size(); ++i )
  317. {
  318. irr::SEvent event;
  319. event.EventType = EET_KEY_INPUT_EVENT;
  320. event.KeyInput.Key = keyMap[i].KeyCode;
  321. event.KeyInput.PressedDown = false;
  322. fpsAnimator->OnEvent(event);
  323. }
  324. }
  325. ++iter;
  326. }
  327. }
  328. /*
  329. Function hasModalDialog() checks if we currently have a modal dialog open.
  330. */
  331. bool hasModalDialog()
  332. {
  333. if ( !Device )
  334. return false;
  335. IGUIEnvironment* env = Device->getGUIEnvironment();
  336. IGUIElement * focused = env->getFocus();
  337. while ( focused )
  338. {
  339. if ( focused->isVisible() && focused->hasType(EGUIET_MODAL_SCREEN) )
  340. return true;
  341. focused = focused->getParent();
  342. }
  343. return false;
  344. }
  345. /*
  346. To get all the events sent by the GUI Elements, we need to create an event
  347. receiver. This one is really simple. If an event occurs, it checks the id of
  348. the caller and the event type, and starts an action based on these values. For
  349. example, if a menu item with id GUI_ID_OPEN_MODEL was selected, it opens a file-open-dialog.
  350. */
  351. class MyEventReceiver : public IEventReceiver
  352. {
  353. public:
  354. virtual bool OnEvent(const SEvent& event)
  355. {
  356. // Key events
  357. if (event.EventType == EET_KEY_INPUT_EVENT &&
  358. event.KeyInput.PressedDown == false)
  359. {
  360. if ( OnKeyUp(event.KeyInput.Key) )
  361. return true;
  362. }
  363. // GUI events
  364. if (event.EventType == EET_GUI_EVENT)
  365. {
  366. s32 id = event.GUIEvent.Caller->getID();
  367. IGUIEnvironment* env = Device->getGUIEnvironment();
  368. switch(event.GUIEvent.EventType)
  369. {
  370. case EGET_MENU_ITEM_SELECTED:
  371. // a menu item was clicked
  372. OnMenuItemSelected( (IGUIContextMenu*)event.GUIEvent.Caller );
  373. break;
  374. case EGET_FILE_SELECTED:
  375. {
  376. // load the model file, selected in the file open dialog
  377. IGUIFileOpenDialog* dialog =
  378. (IGUIFileOpenDialog*)event.GUIEvent.Caller;
  379. loadModel(dialog->getFileNameP());
  380. }
  381. break;
  382. case EGET_SCROLL_BAR_CHANGED:
  383. // control skin transparency
  384. if (id == GUI_ID_SKIN_TRANSPARENCY)
  385. {
  386. const s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
  387. setSkinTransparency(pos, env->getSkin());
  388. }
  389. // control animation speed
  390. else if (id == GUI_ID_SKIN_ANIMATION_FPS)
  391. {
  392. const s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
  393. if (scene::ESNT_ANIMATED_MESH == Model->getType())
  394. ((scene::IAnimatedMeshSceneNode*)Model)->setAnimationSpeed((f32)pos);
  395. }
  396. break;
  397. case EGET_COMBO_BOX_CHANGED:
  398. // control anti-aliasing/filtering
  399. if (id == GUI_ID_TEXTUREFILTER)
  400. {
  401. OnTextureFilterSelected( (IGUIComboBox*)event.GUIEvent.Caller );
  402. }
  403. break;
  404. case EGET_BUTTON_CLICKED:
  405. switch(id)
  406. {
  407. case GUI_ID_BUTTON_SET_SCALE:
  408. {
  409. // set model scale
  410. gui::IGUIElement* root = env->getRootGUIElement();
  411. core::vector3df scale;
  412. core::stringc s;
  413. s = root->getElementFromId(GUI_ID_X_SCALE, true)->getText();
  414. scale.X = (f32)atof(s.c_str());
  415. s = root->getElementFromId(GUI_ID_Y_SCALE, true)->getText();
  416. scale.Y = (f32)atof(s.c_str());
  417. s = root->getElementFromId(GUI_ID_Z_SCALE, true)->getText();
  418. scale.Z = (f32)atof(s.c_str());
  419. if (Model)
  420. Model->setScale(scale);
  421. updateScaleInfo(Model);
  422. }
  423. break;
  424. case GUI_ID_BUTTON_SCALE_MUL10:
  425. if (Model)
  426. Model->setScale(Model->getScale()*10.f);
  427. updateScaleInfo(Model);
  428. break;
  429. case GUI_ID_BUTTON_SCALE_DIV10:
  430. if (Model)
  431. Model->setScale(Model->getScale()*0.1f);
  432. updateScaleInfo(Model);
  433. break;
  434. case GUI_ID_BUTTON_OPEN_MODEL:
  435. env->addFileOpenDialog(L"Please select a model file to open");
  436. break;
  437. case GUI_ID_BUTTON_SHOW_ABOUT:
  438. showAboutText();
  439. break;
  440. case GUI_ID_BUTTON_SHOW_TOOLBOX:
  441. createToolBox();
  442. break;
  443. case GUI_ID_BUTTON_SELECT_ARCHIVE:
  444. env->addFileOpenDialog(L"Please select your game archive/directory");
  445. break;
  446. }
  447. break;
  448. default:
  449. break;
  450. }
  451. }
  452. return false;
  453. }
  454. /*
  455. Handle key-up events
  456. */
  457. bool OnKeyUp(irr::EKEY_CODE keyCode)
  458. {
  459. // Don't handle keys if we have a modal dialog open as it would lead
  460. // to unexpected application behaviour for the user.
  461. if ( hasModalDialog() )
  462. return false;
  463. // Escape swaps Camera Input
  464. if (keyCode == irr::KEY_ESCAPE)
  465. {
  466. if (Device)
  467. {
  468. scene::ICameraSceneNode * camera =
  469. Device->getSceneManager()->getActiveCamera();
  470. if (camera)
  471. {
  472. camera->setInputReceiverEnabled( !camera->isInputReceiverEnabled() );
  473. }
  474. return true;
  475. }
  476. }
  477. else if (keyCode == irr::KEY_F1)
  478. {
  479. // Swap display of position information about the camera
  480. if (Device)
  481. {
  482. IGUIElement* elem = Device->getGUIEnvironment()->getRootGUIElement()->getElementFromId(GUI_ID_POSITION_TEXT);
  483. if (elem)
  484. elem->setVisible(!elem->isVisible());
  485. }
  486. }
  487. else if (keyCode == irr::KEY_KEY_M)
  488. {
  489. if (Device)
  490. Device->minimizeWindow();
  491. }
  492. else if (keyCode == irr::KEY_KEY_L)
  493. {
  494. UseLight=!UseLight;
  495. if (Model)
  496. {
  497. Model->setMaterialFlag(video::EMF_LIGHTING, UseLight);
  498. Model->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, UseLight);
  499. }
  500. }
  501. return false;
  502. }
  503. /*
  504. Handle "menu item clicked" events.
  505. */
  506. void OnMenuItemSelected( IGUIContextMenu* menu )
  507. {
  508. s32 id = menu->getItemCommandId(menu->getSelectedItem());
  509. IGUIEnvironment* env = Device->getGUIEnvironment();
  510. switch(id)
  511. {
  512. case GUI_ID_OPEN_MODEL: // File -> Open Model File & Texture
  513. env->addFileOpenDialog(L"Please select a model file to open");
  514. break;
  515. case GUI_ID_SET_MODEL_ARCHIVE: // File -> Set Model Archive
  516. env->addFileOpenDialog(L"Please select your game archive/directory");
  517. break;
  518. case GUI_ID_LOAD_AS_OCTREE: // File -> LoadAsOctree
  519. Octree = !Octree;
  520. menu->setItemChecked(menu->getSelectedItem(), Octree);
  521. break;
  522. case GUI_ID_QUIT: // File -> Quit
  523. Device->closeDevice();
  524. break;
  525. case GUI_ID_SKY_BOX_VISIBLE: // View -> Skybox
  526. menu->setItemChecked(menu->getSelectedItem(), !menu->isItemChecked(menu->getSelectedItem()));
  527. SkyBox->setVisible(!SkyBox->isVisible());
  528. break;
  529. case GUI_ID_DEBUG_OFF: // View -> Debug Information
  530. menu->setItemChecked(menu->getSelectedItem()+1, false);
  531. menu->setItemChecked(menu->getSelectedItem()+2, false);
  532. menu->setItemChecked(menu->getSelectedItem()+3, false);
  533. menu->setItemChecked(menu->getSelectedItem()+4, false);
  534. menu->setItemChecked(menu->getSelectedItem()+5, false);
  535. menu->setItemChecked(menu->getSelectedItem()+6, false);
  536. if (Model)
  537. Model->setDebugDataVisible(scene::EDS_OFF);
  538. break;
  539. case GUI_ID_DEBUG_BOUNDING_BOX: // View -> Debug Information
  540. menu->setItemChecked(menu->getSelectedItem(), !menu->isItemChecked(menu->getSelectedItem()));
  541. if (Model)
  542. Model->setDebugDataVisible((scene::E_DEBUG_SCENE_TYPE)(Model->isDebugDataVisible()^scene::EDS_BBOX));
  543. break;
  544. case GUI_ID_DEBUG_NORMALS: // View -> Debug Information
  545. menu->setItemChecked(menu->getSelectedItem(), !menu->isItemChecked(menu->getSelectedItem()));
  546. if (Model)
  547. Model->setDebugDataVisible((scene::E_DEBUG_SCENE_TYPE)(Model->isDebugDataVisible()^scene::EDS_NORMALS));
  548. break;
  549. case GUI_ID_DEBUG_SKELETON: // View -> Debug Information
  550. menu->setItemChecked(menu->getSelectedItem(), !menu->isItemChecked(menu->getSelectedItem()));
  551. if (Model)
  552. Model->setDebugDataVisible((scene::E_DEBUG_SCENE_TYPE)(Model->isDebugDataVisible()^scene::EDS_SKELETON));
  553. break;
  554. case GUI_ID_DEBUG_WIRE_OVERLAY: // View -> Debug Information
  555. menu->setItemChecked(menu->getSelectedItem(), !menu->isItemChecked(menu->getSelectedItem()));
  556. if (Model)
  557. Model->setDebugDataVisible((scene::E_DEBUG_SCENE_TYPE)(Model->isDebugDataVisible()^scene::EDS_MESH_WIRE_OVERLAY));
  558. break;
  559. case GUI_ID_DEBUG_HALF_TRANSPARENT: // View -> Debug Information
  560. menu->setItemChecked(menu->getSelectedItem(), !menu->isItemChecked(menu->getSelectedItem()));
  561. if (Model)
  562. Model->setDebugDataVisible((scene::E_DEBUG_SCENE_TYPE)(Model->isDebugDataVisible()^scene::EDS_HALF_TRANSPARENCY));
  563. break;
  564. case GUI_ID_DEBUG_BUFFERS_BOUNDING_BOXES: // View -> Debug Information
  565. menu->setItemChecked(menu->getSelectedItem(), !menu->isItemChecked(menu->getSelectedItem()));
  566. if (Model)
  567. Model->setDebugDataVisible((scene::E_DEBUG_SCENE_TYPE)(Model->isDebugDataVisible()^scene::EDS_BBOX_BUFFERS));
  568. break;
  569. case GUI_ID_DEBUG_ALL: // View -> Debug Information
  570. menu->setItemChecked(menu->getSelectedItem()-1, true);
  571. menu->setItemChecked(menu->getSelectedItem()-2, true);
  572. menu->setItemChecked(menu->getSelectedItem()-3, true);
  573. menu->setItemChecked(menu->getSelectedItem()-4, true);
  574. menu->setItemChecked(menu->getSelectedItem()-5, true);
  575. menu->setItemChecked(menu->getSelectedItem()-6, true);
  576. if (Model)
  577. Model->setDebugDataVisible(scene::EDS_FULL);
  578. break;
  579. case GUI_ID_ABOUT: // Help->About
  580. showAboutText();
  581. break;
  582. case GUI_ID_MODEL_MATERIAL_SOLID: // View -> Material -> Solid
  583. if (Model)
  584. Model->setMaterialType(video::EMT_SOLID);
  585. break;
  586. case GUI_ID_MODEL_MATERIAL_TRANSPARENT: // View -> Material -> Transparent
  587. if (Model)
  588. Model->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
  589. break;
  590. case GUI_ID_MODEL_MATERIAL_REFLECTION: // View -> Material -> Reflection
  591. if (Model)
  592. Model->setMaterialType(video::EMT_SPHERE_MAP);
  593. break;
  594. case GUI_ID_CAMERA_MAYA:
  595. setActiveCamera(Camera[0]);
  596. break;
  597. case GUI_ID_CAMERA_FIRST_PERSON:
  598. setActiveCamera(Camera[1]);
  599. break;
  600. }
  601. }
  602. /*
  603. Handle the event that one of the texture-filters was selected in the corresponding combobox.
  604. */
  605. void OnTextureFilterSelected( IGUIComboBox* combo )
  606. {
  607. s32 pos = combo->getSelected();
  608. switch (pos)
  609. {
  610. case 0:
  611. if (Model)
  612. {
  613. Model->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
  614. Model->setMaterialFlag(video::EMF_TRILINEAR_FILTER, false);
  615. Model->setMaterialFlag(video::EMF_ANISOTROPIC_FILTER, false);
  616. }
  617. break;
  618. case 1:
  619. if (Model)
  620. {
  621. Model->setMaterialFlag(video::EMF_BILINEAR_FILTER, true);
  622. Model->setMaterialFlag(video::EMF_TRILINEAR_FILTER, false);
  623. }
  624. break;
  625. case 2:
  626. if (Model)
  627. {
  628. Model->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
  629. Model->setMaterialFlag(video::EMF_TRILINEAR_FILTER, true);
  630. }
  631. break;
  632. case 3:
  633. if (Model)
  634. {
  635. Model->setMaterialFlag(video::EMF_ANISOTROPIC_FILTER, true);
  636. }
  637. break;
  638. case 4:
  639. if (Model)
  640. {
  641. Model->setMaterialFlag(video::EMF_ANISOTROPIC_FILTER, false);
  642. }
  643. break;
  644. }
  645. }
  646. };
  647. /*
  648. Most of the hard work is done. We only need to create the Irrlicht Engine
  649. device and all the buttons, menus and toolbars. We start up the engine as
  650. usual, using createDevice(). To make our application catch events, we set our
  651. eventreceiver as parameter. As you can see, there is also a call to
  652. IrrlichtDevice::setResizeable(). This makes the render window resizeable, which
  653. is quite useful for a mesh viewer.
  654. */
  655. int main(int argc, char* argv[])
  656. {
  657. // ask user for driver
  658. video::E_DRIVER_TYPE driverType=driverChoiceConsole();
  659. if (driverType==video::EDT_COUNT)
  660. return 1;
  661. // create device and exit if creation failed
  662. MyEventReceiver receiver;
  663. Device = createDevice(driverType, core::dimension2d<u32>(800, 600),
  664. 16, false, false, false, &receiver);
  665. if (Device == 0)
  666. return 1; // could not create selected driver.
  667. Device->setResizable(true);
  668. Device->setWindowCaption(L"Irrlicht Engine - Loading...");
  669. video::IVideoDriver* driver = Device->getVideoDriver();
  670. IGUIEnvironment* env = Device->getGUIEnvironment();
  671. scene::ISceneManager* smgr = Device->getSceneManager();
  672. smgr->getParameters()->setAttribute(scene::COLLADA_CREATE_SCENE_INSTANCES, true);
  673. driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
  674. smgr->addLightSceneNode(0, core::vector3df(200,200,200),
  675. video::SColorf(1.0f,1.0f,1.0f),2000);
  676. smgr->setAmbientLight(video::SColorf(0.3f,0.3f,0.3f));
  677. // add our media directory as "search path"
  678. Device->getFileSystem()->addFileArchive(getExampleMediaPath());
  679. /*
  680. The next step is to read the configuration file. It is stored in the xml
  681. format and looks a little bit like this:
  682. @verbatim
  683. <?xml version="1.0"?>
  684. <config>
  685. <startUpModel file="some filename" />
  686. <messageText caption="Irrlicht Engine Mesh Viewer">
  687. Hello!
  688. </messageText>
  689. </config>
  690. @endverbatim
  691. We need the data stored in there to be written into the global variables
  692. StartUpModelFile, MessageText and Caption. This is now done using the
  693. Irrlicht Engine integrated XML parser:
  694. */
  695. // read configuration from xml file
  696. io::IXMLReader* xml = Device->getFileSystem()->createXMLReader( L"config.xml");
  697. while(xml && xml->read())
  698. {
  699. switch(xml->getNodeType())
  700. {
  701. case io::EXN_TEXT:
  702. // in this xml file, the only text which occurs is the
  703. // messageText
  704. MessageText = xml->getNodeData();
  705. break;
  706. case io::EXN_ELEMENT:
  707. {
  708. if (core::stringw("startUpModel") == xml->getNodeName())
  709. StartUpModelFile = xml->getAttributeValue(L"file");
  710. else
  711. if (core::stringw("messageText") == xml->getNodeName())
  712. Caption = xml->getAttributeValue(L"caption");
  713. }
  714. break;
  715. default:
  716. break;
  717. }
  718. }
  719. if (xml)
  720. xml->drop(); // don't forget to delete the xml reader
  721. // We can pass a model to load per command line parameter
  722. if (argc > 1)
  723. StartUpModelFile = argv[1];
  724. // set a nicer font
  725. IGUISkin* skin = env->getSkin();
  726. IGUIFont* font = env->getFont("fonthaettenschweiler.bmp");
  727. if (font)
  728. skin->setFont(font);
  729. /*
  730. Now create the Menu.
  731. It is possible to create submenus for every menu item. The call
  732. menu->addItem(L"File", -1, true, true); for example adds a new menu
  733. Item with the name "File" and the id -1. The following parameter says
  734. that the menu item should be enabled, and the last one says, that there
  735. should be a submenu. The submenu can now be accessed with
  736. menu->getSubMenu(0), because the "File" entry is the menu item with
  737. index 0.
  738. */
  739. gui::IGUIContextMenu* menu = env->addMenu();
  740. menu->addItem(L"File", -1, true, true);
  741. menu->addItem(L"View", -1, true, true);
  742. menu->addItem(L"Camera", -1, true, true);
  743. menu->addItem(L"Help", -1, true, true);
  744. gui::IGUIContextMenu* submenu;
  745. submenu = menu->getSubMenu(0);
  746. submenu->addItem(L"Open Model File & Texture...", GUI_ID_OPEN_MODEL);
  747. submenu->addItem(L"Set Model Archive...", GUI_ID_SET_MODEL_ARCHIVE);
  748. submenu->addItem(L"Load as Octree", GUI_ID_LOAD_AS_OCTREE);
  749. submenu->addSeparator();
  750. submenu->addItem(L"Quit", GUI_ID_QUIT);
  751. submenu = menu->getSubMenu(1);
  752. submenu->addItem(L"sky box visible", GUI_ID_SKY_BOX_VISIBLE, true, false, true);
  753. submenu->addItem(L"toggle model debug information", GUI_ID_TOGGLE_DEBUG_INFO, true, true);
  754. submenu->addItem(L"model material", -1, true, true );
  755. submenu = submenu->getSubMenu(1);
  756. submenu->addItem(L"Off", GUI_ID_DEBUG_OFF);
  757. submenu->addItem(L"Bounding Box", GUI_ID_DEBUG_BOUNDING_BOX);
  758. submenu->addItem(L"Normals", GUI_ID_DEBUG_NORMALS);
  759. submenu->addItem(L"Skeleton", GUI_ID_DEBUG_SKELETON);
  760. submenu->addItem(L"Wire overlay", GUI_ID_DEBUG_WIRE_OVERLAY);
  761. submenu->addItem(L"Half-Transparent", GUI_ID_DEBUG_HALF_TRANSPARENT);
  762. submenu->addItem(L"Buffers bounding boxes", GUI_ID_DEBUG_BUFFERS_BOUNDING_BOXES);
  763. submenu->addItem(L"All", GUI_ID_DEBUG_ALL);
  764. submenu = menu->getSubMenu(1)->getSubMenu(2);
  765. submenu->addItem(L"Solid", GUI_ID_MODEL_MATERIAL_SOLID);
  766. submenu->addItem(L"Transparent", GUI_ID_MODEL_MATERIAL_TRANSPARENT);
  767. submenu->addItem(L"Reflection", GUI_ID_MODEL_MATERIAL_REFLECTION);
  768. submenu = menu->getSubMenu(2);
  769. submenu->addItem(L"Maya Style", GUI_ID_CAMERA_MAYA);
  770. submenu->addItem(L"First Person", GUI_ID_CAMERA_FIRST_PERSON);
  771. submenu = menu->getSubMenu(3);
  772. submenu->addItem(L"About", GUI_ID_ABOUT);
  773. /*
  774. Below the menu we want a toolbar, onto which we can place colored
  775. buttons and important looking stuff like a senseless combobox.
  776. */
  777. // create toolbar
  778. gui::IGUIToolBar* bar = env->addToolBar();
  779. video::ITexture* image = driver->getTexture("open.png");
  780. bar->addButton(GUI_ID_BUTTON_OPEN_MODEL, 0, L"Open a model",image, 0, false, true);
  781. image = driver->getTexture("tools.png");
  782. bar->addButton(GUI_ID_BUTTON_SHOW_TOOLBOX, 0, L"Open Toolset",image, 0, false, true);
  783. image = driver->getTexture("zip.png");
  784. bar->addButton(GUI_ID_BUTTON_SELECT_ARCHIVE, 0, L"Set Model Archive",image, 0, false, true);
  785. image = driver->getTexture("help.png");
  786. bar->addButton(GUI_ID_BUTTON_SHOW_ABOUT, 0, L"Open Help", image, 0, false, true);
  787. // create a combobox for texture filters
  788. gui::IGUIComboBox* box = env->addComboBox(core::rect<s32>(250,4,350,23), bar, GUI_ID_TEXTUREFILTER);
  789. box->addItem(L"No filtering");
  790. box->addItem(L"Bilinear");
  791. box->addItem(L"Trilinear");
  792. box->addItem(L"Anisotropic");
  793. box->addItem(L"Isotropic");
  794. /*
  795. To make the editor look a little bit better, we disable transparent gui
  796. elements, and add an Irrlicht Engine logo. In addition, a text showing
  797. the current frames per second value is created and the window caption is
  798. changed.
  799. */
  800. // disable alpha
  801. for (s32 i=0; i<gui::EGDC_COUNT ; ++i)
  802. {
  803. video::SColor col = env->getSkin()->getColor((gui::EGUI_DEFAULT_COLOR)i);
  804. col.setAlpha(255);
  805. env->getSkin()->setColor((gui::EGUI_DEFAULT_COLOR)i, col);
  806. }
  807. // add a tabcontrol
  808. createToolBox();
  809. // create fps text
  810. IGUIStaticText* fpstext = env->addStaticText(L"",
  811. core::rect<s32>(400,4,570,23), true, false, bar);
  812. IGUIStaticText* postext = env->addStaticText(L"",
  813. core::rect<s32>(10,50,470,80),false, false, 0, GUI_ID_POSITION_TEXT);
  814. postext->setVisible(false);
  815. // set window caption
  816. Caption += " - [";
  817. Caption += driver->getName();
  818. Caption += "]";
  819. Device->setWindowCaption(Caption.c_str());
  820. /*
  821. Now we show the about message box at start up, and load the first model.
  822. To make everything look better a skybox is created. We also add a user
  823. controlled camera, to make the application more interactive.
  824. Finally, everything is drawn in a standard drawing loop.
  825. */
  826. // show about message box and load default model
  827. if (argc==1)
  828. showAboutText();
  829. loadModel(StartUpModelFile.c_str());
  830. // add skybox
  831. SkyBox = smgr->addSkyBoxSceneNode(
  832. driver->getTexture("irrlicht2_up.jpg"),
  833. driver->getTexture("irrlicht2_dn.jpg"),
  834. driver->getTexture("irrlicht2_lf.jpg"),
  835. driver->getTexture("irrlicht2_rt.jpg"),
  836. driver->getTexture("irrlicht2_ft.jpg"),
  837. driver->getTexture("irrlicht2_bk.jpg"));
  838. // add a camera scene node
  839. Camera[0] = smgr->addCameraSceneNodeMaya();
  840. Camera[0]->setFarValue(20000.f);
  841. // Maya cameras reposition themselves relative to their target, so target the location
  842. // where the mesh scene node is placed.
  843. Camera[0]->setTarget(core::vector3df(0,30,0));
  844. Camera[1] = smgr->addCameraSceneNodeFPS();
  845. Camera[1]->setFarValue(20000.f);
  846. Camera[1]->setPosition(core::vector3df(0,0,-70));
  847. Camera[1]->setTarget(core::vector3df(0,30,0));
  848. setActiveCamera(Camera[0]);
  849. // load the irrlicht engine logo
  850. IGUIImage *img =
  851. env->addImage(driver->getTexture("irrlichtlogo3.png"),
  852. core::position2d<s32>(10, driver->getScreenSize().Height - 128));
  853. // lock the logo's edges to the bottom left corner of the screen
  854. img->setAlignment(EGUIA_UPPERLEFT, EGUIA_UPPERLEFT,
  855. EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT);
  856. // remember state so we notice when the window does lose the focus
  857. bool hasFocus = Device->isWindowFocused();
  858. // draw everything
  859. while(Device->run() && driver)
  860. {
  861. // Catch focus changes (workaround until Irrlicht has events for this)
  862. bool focused = Device->isWindowFocused();
  863. if ( hasFocus && !focused )
  864. onKillFocus();
  865. hasFocus = focused;
  866. if (Device->isWindowActive())
  867. {
  868. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(150,50,50,50));
  869. smgr->drawAll();
  870. env->drawAll();
  871. driver->endScene();
  872. // update information about current frame-rate
  873. core::stringw str(L"FPS: ");
  874. str.append(core::stringw(driver->getFPS()));
  875. str += L" Tris: ";
  876. str.append(core::stringw(driver->getPrimitiveCountDrawn()));
  877. fpstext->setText(str.c_str());
  878. // update information about the active camera
  879. scene::ICameraSceneNode* cam = Device->getSceneManager()->getActiveCamera();
  880. str = L"Pos: ";
  881. str.append(core::stringw(cam->getPosition().X));
  882. str += L" ";
  883. str.append(core::stringw(cam->getPosition().Y));
  884. str += L" ";
  885. str.append(core::stringw(cam->getPosition().Z));
  886. str += L" Tgt: ";
  887. str.append(core::stringw(cam->getTarget().X));
  888. str += L" ";
  889. str.append(core::stringw(cam->getTarget().Y));
  890. str += L" ";
  891. str.append(core::stringw(cam->getTarget().Z));
  892. postext->setText(str.c_str());
  893. // update the tool dialog
  894. updateToolBox();
  895. }
  896. else
  897. Device->yield();
  898. }
  899. Device->drop();
  900. return 0;
  901. }
  902. /*
  903. **/