main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /** Deprecated. This was Example 017 Helloworld mobile for WinCE 6.
  2. But WinCE6 support has been removed for Irrlicht 1.9.
  3. If you still need that please use Irrlicht 1.8 or svn revision 5045 which was the last one to include it.
  4. Sources still kept for now as it compiles on other platform too. And we might use this example again
  5. once we support Windows RT.
  6. */
  7. #include <irrlicht.h>
  8. #if defined ( _IRR_WINDOWS_ )
  9. #include <windows.h>
  10. #endif
  11. using namespace irr;
  12. using namespace core;
  13. using namespace scene;
  14. using namespace video;
  15. using namespace io;
  16. using namespace gui;
  17. #pragma comment(lib, "Irrlicht.lib")
  18. class EventReceiver_basic : public IEventReceiver
  19. {
  20. private:
  21. IrrlichtDevice *Device;
  22. public:
  23. EventReceiver_basic ( IrrlichtDevice *device ): Device ( device ) {}
  24. virtual bool OnEvent(const SEvent& event)
  25. {
  26. if (event.EventType == EET_GUI_EVENT)
  27. {
  28. s32 id = event.GUIEvent.Caller->getID();
  29. switch(event.GUIEvent.EventType)
  30. {
  31. case EGET_BUTTON_CLICKED:
  32. if (id == 2)
  33. {
  34. Device->closeDevice();
  35. return true;
  36. } break;
  37. }
  38. }
  39. return false;
  40. }
  41. };
  42. class CSampleSceneNode : public ISceneNode
  43. {
  44. aabbox3d<f32> Box;
  45. S3DVertex Vertices[4];
  46. SMaterial Material;
  47. public:
  48. CSampleSceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id)
  49. : ISceneNode(parent, mgr, id)
  50. {
  51. Material.Wireframe = false;
  52. Material.Lighting = false;
  53. Vertices[0] = S3DVertex(0,0,10, 1,1,0, SColor(255,0,255,255), 0, 1);
  54. Vertices[1] = S3DVertex(10,0,-10, 1,0,0, SColor(255,255,0,255), 1, 1);
  55. Vertices[2] = S3DVertex(0,20,0, 0,1,1, SColor(255,255,255,0), 1, 0);
  56. Vertices[3] = S3DVertex(-10,0,-10, 0,0,1, SColor(255,0,255,0), 0, 0);
  57. Box.reset(Vertices[0].Pos);
  58. for (s32 i=1; i<4; ++i)
  59. Box.addInternalPoint(Vertices[i].Pos);
  60. }
  61. virtual void OnRegisterSceneNode()
  62. {
  63. if (IsVisible)
  64. SceneManager->registerNodeForRendering(this);
  65. ISceneNode::OnRegisterSceneNode();
  66. }
  67. virtual void render()
  68. {
  69. u16 indices[] = { 0,2,3, 2,1,3, 1,0,3, 2,0,1 };
  70. IVideoDriver* driver = SceneManager->getVideoDriver();
  71. driver->setMaterial(Material);
  72. driver->setTransform(ETS_WORLD, AbsoluteTransformation);
  73. driver->drawIndexedTriangleList(&Vertices[0], 4, &indices[0], 4);
  74. }
  75. virtual const aabbox3d<f32>& getBoundingBox() const
  76. {
  77. return Box;
  78. }
  79. virtual u32 getMaterialCount()
  80. {
  81. return 1;
  82. }
  83. virtual SMaterial& getMaterial(u32 i)
  84. {
  85. return Material;
  86. }
  87. };
  88. /*!
  89. Startup a Windows Mobile Device
  90. */
  91. IrrlichtDevice *startup()
  92. {
  93. // both software and burnings video can be used
  94. E_DRIVER_TYPE driverType = EDT_SOFTWARE; // EDT_BURNINGSVIDEO;
  95. // create device
  96. IrrlichtDevice *device = 0;
  97. // Use window mode on PC
  98. device = createDevice(driverType, dimension2d<u32>(240, 320), 16, false );
  99. if ( 0 == device )
  100. return 0;
  101. IVideoDriver* driver = device->getVideoDriver();
  102. ISceneManager* smgr = device->getSceneManager();
  103. IGUIEnvironment* guienv = device->getGUIEnvironment();
  104. // set the filesystem relative to the executable
  105. #if defined (_IRR_WINDOWS_)
  106. {
  107. wchar_t buf[255];
  108. GetModuleFileNameW ( 0, buf, 255 );
  109. io::path base = buf;
  110. base = base.subString ( 0, base.findLast ( '\\' ) + 1 );
  111. device->getFileSystem()->addFileArchive ( base );
  112. }
  113. #endif
  114. IGUIStaticText *text = guienv->addStaticText(L"FPS: 25",
  115. rect<s32>(140,15,200,30), false, false, 0, 100 );
  116. guienv->addButton(core::rect<int>(200,10,238,30), 0, 2, L"Quit");
  117. // add irrlicht logo
  118. guienv->addImage(driver->getTexture("../../media/irrlichtlogo3.png"),
  119. core::position2d<s32>(0,-2));
  120. return device;
  121. }
  122. /*!
  123. */
  124. int run ( IrrlichtDevice *device )
  125. {
  126. while(device->run())
  127. if (device->isWindowActive())
  128. {
  129. device->getVideoDriver()->beginScene(true, true, SColor(0,100,100,100));
  130. device->getSceneManager()->drawAll();
  131. device->getGUIEnvironment()->drawAll();
  132. device->getVideoDriver()->endScene ();
  133. IGUIElement *stat = device->getGUIEnvironment()->
  134. getRootGUIElement()->getElementFromId ( 100 );
  135. if ( stat )
  136. {
  137. stringw str = L"FPS: ";
  138. str += (s32)device->getVideoDriver()->getFPS();
  139. stat->setText ( str.c_str() );
  140. }
  141. }
  142. device->drop();
  143. return 0;
  144. }
  145. /*!
  146. */
  147. int example_customscenenode()
  148. {
  149. // create device
  150. IrrlichtDevice *device = startup();
  151. if (device == 0)
  152. return 1; // could not create selected driver.
  153. // create engine and camera
  154. EventReceiver_basic receiver(device);
  155. device->setEventReceiver(&receiver);
  156. IVideoDriver* driver = device->getVideoDriver();
  157. ISceneManager* smgr = device->getSceneManager();
  158. IGUIEnvironment* guienv = device->getGUIEnvironment();
  159. smgr->addCameraSceneNode(0, vector3df(0,-40,0), vector3df(0,0,0));
  160. CSampleSceneNode *myNode =
  161. new CSampleSceneNode(smgr->getRootSceneNode(), smgr, 666);
  162. ISceneNodeAnimator* anim =
  163. smgr->createRotationAnimator(vector3df(0.8f, 0, 0.8f));
  164. if(anim)
  165. {
  166. myNode->addAnimator(anim);
  167. anim->drop();
  168. anim = 0; // As I shouldn't refer to it again, ensure that I can't
  169. }
  170. myNode->drop();
  171. myNode = 0; // As I shouldn't refer to it again, ensure that I can't
  172. return run ( device );
  173. }
  174. class EventReceiver_terrain : public IEventReceiver
  175. {
  176. public:
  177. EventReceiver_terrain(IrrlichtDevice *device, scene::ISceneNode* terrain, scene::ISceneNode* skybox, scene::ISceneNode* skydome) :
  178. Device ( device ), Terrain(terrain), Skybox(skybox), Skydome(skydome), showBox(true)
  179. {
  180. Skybox->setVisible(true);
  181. Skydome->setVisible(false);
  182. }
  183. bool OnEvent(const SEvent& event)
  184. {
  185. if (event.EventType == EET_GUI_EVENT)
  186. {
  187. s32 id = event.GUIEvent.Caller->getID();
  188. switch(event.GUIEvent.EventType)
  189. {
  190. case EGET_BUTTON_CLICKED:
  191. if (id == 2)
  192. {
  193. Device->closeDevice();
  194. return true;
  195. } break;
  196. }
  197. }
  198. // check if user presses the key 'W' or 'D'
  199. if (event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
  200. {
  201. switch (event.KeyInput.Key)
  202. {
  203. case irr::KEY_KEY_W: // switch wire frame mode
  204. Terrain->setMaterialFlag(video::EMF_WIREFRAME,
  205. !Terrain->getMaterial(0).Wireframe);
  206. Terrain->setMaterialFlag(video::EMF_POINTCLOUD, false);
  207. return true;
  208. case irr::KEY_KEY_P: // switch wire frame mode
  209. Terrain->setMaterialFlag(video::EMF_POINTCLOUD,
  210. !Terrain->getMaterial(0).PointCloud);
  211. Terrain->setMaterialFlag(video::EMF_WIREFRAME, false);
  212. return true;
  213. case irr::KEY_KEY_D: // toggle detail map
  214. Terrain->setMaterialType(
  215. Terrain->getMaterial(0).MaterialType == video::EMT_SOLID ?
  216. video::EMT_DETAIL_MAP : video::EMT_SOLID);
  217. return true;
  218. case irr::KEY_KEY_S: // toggle skies
  219. showBox=!showBox;
  220. Skybox->setVisible(showBox);
  221. Skydome->setVisible(!showBox);
  222. return true;
  223. default:
  224. break;
  225. }
  226. }
  227. return false;
  228. }
  229. private:
  230. IrrlichtDevice *Device;
  231. scene::ISceneNode* Terrain;
  232. scene::ISceneNode* Skybox;
  233. scene::ISceneNode* Skydome;
  234. bool showBox;
  235. };
  236. /*
  237. The start of the main function starts like in most other example. We ask the user
  238. for the desired renderer and start it up. This time with the advanced parameter handling.
  239. */
  240. int example_terrain()
  241. {
  242. // create device
  243. IrrlichtDevice *device = startup();
  244. if (device == 0)
  245. return 1; // could not create selected driver.
  246. /*
  247. First, we add standard stuff to the scene: A nice irrlicht engine
  248. logo, a small help text, a user controlled camera, and we disable
  249. the mouse cursor.
  250. */
  251. video::IVideoDriver* driver = device->getVideoDriver();
  252. scene::ISceneManager* smgr = device->getSceneManager();
  253. gui::IGUIEnvironment* env = device->getGUIEnvironment();
  254. //set other font
  255. //env->getSkin()->setFont(env->getFont("../../media/fontlucida.png"));
  256. // add some help text
  257. env->addStaticText(
  258. L"Press 'W' to change wireframe mode\nPress 'D' to toggle detail map\nPress 'S' to toggle skybox/skydome",
  259. core::rect<s32>(5,250,235,320), true, true, 0, -1, true);
  260. // add camera
  261. scene::ICameraSceneNode* camera =
  262. smgr->addCameraSceneNodeFPS(0,100.0f,1.2f);
  263. camera->setPosition(core::vector3df(2700*2,255*2,2600*2));
  264. camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
  265. camera->setFarValue(42000.0f);
  266. // disable mouse cursor
  267. device->getCursorControl()->setVisible(false);
  268. /*
  269. Here comes the terrain renderer scene node: We add it just like any
  270. other scene node to the scene using
  271. ISceneManager::addTerrainSceneNode(). The only parameter we use is a
  272. file name to the heightmap we use. A heightmap is simply a gray scale
  273. texture. The terrain renderer loads it and creates the 3D terrain from
  274. it.
  275. To make the terrain look more big, we change the scale factor of
  276. it to (40, 4.4, 40). Because we don't have any dynamic lights in the
  277. scene, we switch off the lighting, and we set the file
  278. terrain-texture.jpg as texture for the terrain and detailmap3.jpg as
  279. second texture, called detail map. At last, we set the scale values for
  280. the texture: The first texture will be repeated only one time over the
  281. whole terrain, and the second one (detail map) 20 times.
  282. */
  283. // add terrain scene node
  284. scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode(
  285. "../../media/terrain-heightmap.bmp",
  286. 0, // parent node
  287. -1, // node id
  288. core::vector3df(0.f, 0.f, 0.f), // position
  289. core::vector3df(0.f, 0.f, 0.f), // rotation
  290. core::vector3df(40.f, 4.4f, 40.f), // scale
  291. video::SColor ( 255, 255, 255, 255 ), // vertexColor
  292. 5, // maxLOD
  293. scene::ETPS_17, // patchSize
  294. 4 // smoothFactor
  295. );
  296. if ( terrain )
  297. {
  298. terrain->setMaterialFlag(video::EMF_LIGHTING, false);
  299. terrain->setMaterialTexture(0,
  300. driver->getTexture("../../media/terrain-texture.jpg"));
  301. terrain->setMaterialTexture(1,
  302. driver->getTexture("../../media/detailmap3.jpg"));
  303. terrain->setMaterialType(video::EMT_DETAIL_MAP);
  304. terrain->scaleTexture(1.0f, 20.0f);
  305. //terrain->setDebugDataVisible ( true );
  306. /*
  307. To be able to do collision with the terrain, we create a triangle selector.
  308. If you want to know what triangle selectors do, just take a look into the
  309. collision tutorial. The terrain triangle selector works together with the
  310. terrain. To demonstrate this, we create a collision response animator
  311. and attach it to the camera, so that the camera will not be able to fly
  312. through the terrain.
  313. */
  314. // create triangle selector for the terrain
  315. scene::ITriangleSelector* selector
  316. = smgr->createTerrainTriangleSelector(terrain, 0);
  317. terrain->setTriangleSelector(selector);
  318. // create collision response animator and attach it to the camera
  319. scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
  320. selector, camera, core::vector3df(60,100,60),
  321. core::vector3df(0,0,0),
  322. core::vector3df(0,50,0));
  323. selector->drop();
  324. camera->addAnimator(anim);
  325. anim->drop();
  326. /* If you need access to the terrain data you can also do this directly via the following code fragment.
  327. */
  328. scene::CDynamicMeshBuffer* buffer = new scene::CDynamicMeshBuffer(video::EVT_2TCOORDS, video::EIT_16BIT);
  329. terrain->getMeshBufferForLOD(*buffer, 0);
  330. video::S3DVertex2TCoords* data = (video::S3DVertex2TCoords*)buffer->getVertexBuffer().getData();
  331. // Work on data or get the IndexBuffer with a similar call.
  332. buffer->drop(); // When done drop the buffer again.
  333. }
  334. /*
  335. To make the user be able to switch between normal and wireframe mode,
  336. we create an instance of the event receiver from above and let Irrlicht
  337. know about it. In addition, we add the skybox which we already used in
  338. lots of Irrlicht examples and a skydome, which is shown mutually
  339. exclusive with the skybox by pressing 'S'.
  340. */
  341. // create skybox and skydome
  342. driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
  343. scene::ISceneNode* skybox=smgr->addSkyBoxSceneNode(
  344. driver->getTexture("../../media/irrlicht2_up.jpg"),
  345. driver->getTexture("../../media/irrlicht2_dn.jpg"),
  346. driver->getTexture("../../media/irrlicht2_lf.jpg"),
  347. driver->getTexture("../../media/irrlicht2_rt.jpg"),
  348. driver->getTexture("../../media/irrlicht2_ft.jpg"),
  349. driver->getTexture("../../media/irrlicht2_bk.jpg"));
  350. scene::ISceneNode* skydome=smgr->addSkyDomeSceneNode(driver->getTexture("../../media/skydome.jpg"),16,8,0.95f,2.0f);
  351. driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
  352. // create event receiver
  353. EventReceiver_terrain receiver( device, terrain, skybox, skydome);
  354. device->setEventReceiver(&receiver);
  355. return run ( device );
  356. }
  357. /*
  358. */
  359. int example_helloworld()
  360. {
  361. // create device
  362. IrrlichtDevice *device = startup();
  363. if (device == 0)
  364. return 1; // could not create selected driver.
  365. IVideoDriver* driver = device->getVideoDriver();
  366. ISceneManager* smgr = device->getSceneManager();
  367. IGUIEnvironment* guienv = device->getGUIEnvironment();
  368. IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2");
  369. if (!mesh)
  370. {
  371. device->drop();
  372. return 1;
  373. }
  374. IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
  375. /*
  376. To let the mesh look a little bit nicer, we change its material. We
  377. disable lighting because we do not have a dynamic light in here, and
  378. the mesh would be totally black otherwise. Then we set the frame loop,
  379. such that the predefined STAND animation is used. And last, we apply a
  380. texture to the mesh. Without it the mesh would be drawn using only a
  381. color.
  382. */
  383. if (node)
  384. {
  385. node->setMaterialFlag(EMF_LIGHTING, false);
  386. node->setMD2Animation(scene::EMAT_STAND);
  387. node->setMaterialTexture( 0, driver->getTexture("../../media/sydney.bmp") );
  388. }
  389. /*
  390. To look at the mesh, we place a camera into 3d space at the position
  391. (0, 30, -40). The camera looks from there to (0,5,0), which is
  392. approximately the place where our md2 model is.
  393. */
  394. smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
  395. EventReceiver_basic receiver(device);
  396. device->setEventReceiver(&receiver);
  397. return run ( device );
  398. }
  399. #if defined (_IRR_WINDOWS_)
  400. #pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
  401. #endif
  402. /*
  403. */
  404. int main()
  405. {
  406. example_helloworld ();
  407. example_customscenenode();
  408. //example_terrain();
  409. }
  410. /*
  411. **/