main.cpp 14 KB

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