Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.63 KB | None | 0 0
  1. //Add the instruction to include the irrlicht.h file below
  2. #include <irrlicht.h>
  3. #include "EventListener.h"
  4.  
  5. //Add the instructions to use the namespaces irr, core, video and scene below
  6. using namespace irr;
  7. using namespace core;
  8. using namespace video;
  9. using namespace scene;
  10. using namespace gui;
  11.  
  12. //Add the instructions to define the irrlicht.lib file for the linker
  13. #pragma comment(lib, "irrlicht.lib")
  14.  
  15.  
  16. //Function to duplicate and place roads on the terrain.
  17. void roadPlacement(ISceneManager *scnmgr, IVideoDriver *driver, ITerrainSceneNode *terrain, int j) {
  18. for (int i = 0; i < j; i++) {
  19. //Spawn the Road
  20. ISceneNode *road = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/roadV2.obj"));
  21. //Disable lighting for the road node in order to see object
  22. road->setMaterialFlag(EMF_LIGHTING, false);
  23. //Load the road texture and apply it to the road scene object
  24. road->setMaterialTexture(0, driver->getTexture("../media/textures/roadTexture.jpg"));
  25. road->getMaterial(0).getTextureMatrix(0).setTextureScale(0.011, 0.011);
  26. road->setPosition(vector3df(990, 800, 160 + i * 85.5));
  27. road->setScale(vector3df(25));
  28.  
  29. //Sets road height to scale with terrain height
  30. vector3df roadPosition = road->getPosition();
  31. roadPosition.Y = terrain->getHeight(roadPosition.X, roadPosition.Z) + 2;
  32. road->setPosition(roadPosition);
  33. }
  34. }
  35.  
  36.  
  37.  
  38. int main(void) {
  39.  
  40. //Function to capture keyboard events
  41. EventListener listener;
  42.  
  43. //Add the instruction to create the IrrlichtDevice and store it in a variable called device
  44. IrrlichtDevice *device = createDevice(EDT_DIRECT3D9, dimension2d<u32>(1024, 768), 16, false, false, false, & listener);
  45.  
  46. if (device == nullptr) {
  47.  
  48. //Return -1 to indicate the device was not created correctly
  49. return -1;
  50. }
  51.  
  52. ISceneManager *scnmgr = device->getSceneManager();
  53. IVideoDriver *driver = device->getVideoDriver();
  54.  
  55. scnmgr->setAmbientLight(video::SColorf(0.3, 0.3, 0.3, 1));
  56.  
  57. //Create mesh to add water effect to
  58. IAnimatedMesh* plane = scnmgr->addHillPlaneMesh("plane", // Name of mesh
  59. core::dimension2d<f32>(20, 20), // Size of a tile of the mesh. (10.0f, 10.0f) would be a good value to start, for example.
  60. core::dimension2d<u32>(40, 40), 0, 0, // Specifies how much tiles there will be. If you specifiy for example that a tile has the size (10.0f, 10.0f) and the tileCount is (10,10), than you get a field of 100 tiles which has the dimension 100.0fx100.0f.
  61. core::dimension2d<f32>(0, 0), //material
  62. core::dimension2d<f32>(10, 10)); //countHills
  63.  
  64. //mesh, waveheight = 0.5f, wave speed = 350.0f, wave length = 80.0f
  65. ISceneNode* sea = scnmgr->addWaterSurfaceSceneNode(plane->getMesh(0), 5.0f, 300.0f, 40.0f, NULL, -1, vector3df(1150, 1000, 1650));;
  66. sea->setMaterialTexture(0, driver->getTexture("../media/textures/stones.jpg"));
  67. sea->setMaterialTexture(1, driver->getTexture("../media/textures/water.jpg"));
  68. sea->setMaterialFlag(EMF_LIGHTING, true);
  69. sea->setMaterialType(video::EMT_REFLECTION_2_LAYER);
  70.  
  71.  
  72.  
  73. // SPAWN BUILDINGS //
  74.  
  75. //Spawn the Building1
  76. ISceneNode *building1 = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/building1.obj"));
  77. //Disable lighting for the building1 node in order to see object
  78. building1->setMaterialFlag(EMF_LIGHTING, false);
  79. //Load the building1 texture and apply it to the watertower scene object
  80. building1->setMaterialTexture(0, driver->getTexture("../media/textures/building1.png"));
  81. building1->setPosition(vector3df(1150, 800, 900));
  82. building1->setScale(vector3df(10));
  83.  
  84. //Spawn the Warehouse
  85. ISceneNode *warehouse = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/warehouse.obj"));
  86. //Disable lighting for the warehouse node in order to see object
  87. warehouse->setMaterialFlag(EMF_LIGHTING, false);
  88. //Load the warehouse texture and apply it to the watertower scene object
  89. warehouse->setMaterialTexture(0, driver->getTexture("../media/textures/warehouse.png"));
  90. warehouse->setPosition(vector3df(1150, 800, 600));
  91. warehouse->setScale(vector3df(10));
  92.  
  93. //Spawn the Shop
  94. ISceneNode *shop = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/shop.obj"));
  95. //Disable lighting for the Shop node in order to see object
  96. shop->setMaterialFlag(EMF_LIGHTING, false);
  97. //Load the Shop texture and apply it to the watertower scene object
  98. shop->setMaterialTexture(0, driver->getTexture("../media/textures/shop.png"));
  99. shop->setPosition(vector3df(1150, 800, 300));
  100. shop->setScale(vector3df(8));
  101.  
  102. //Spawn the Apartments
  103. ISceneNode *apartments = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/apartments.obj"));
  104. //Disable lighting for the Apartments node in order to see object
  105. apartments->setMaterialFlag(EMF_LIGHTING, false);
  106. //Load the Apartments texture and apply it to the watertower scene object
  107. apartments->setMaterialTexture(0, driver->getTexture("../media/textures/apartments.png"));
  108. apartments->setPosition(vector3df(1200, 800, 1200));
  109. apartments->setScale(vector3df(8));
  110.  
  111. //Spawn the Garage
  112. ISceneNode *garage = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/garage.obj"));
  113. //Disable lighting for the Apartments node in order to see object
  114. garage->setMaterialFlag(EMF_LIGHTING, false);
  115. //Load the Apartments texture and apply it to the watertower scene object
  116. garage->setMaterialTexture(0, driver->getTexture("../media/textures/garage.png"));
  117. garage->setPosition(vector3df(1150, 800, 1650));
  118. garage->setScale(vector3df(8));
  119.  
  120. //Spawn the House1
  121. ISceneNode *house1 = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/house1.obj"));
  122. //Disable lighting for the Apartments node in order to see object
  123. house1->setMaterialFlag(EMF_LIGHTING, false);
  124. //Load the Apartments texture and apply it to the watertower scene object
  125. house1->setMaterialTexture(0, driver->getTexture("../media/textures/house1.png"));
  126. house1->setPosition(vector3df(970, 800, 1950));
  127. house1->setScale(vector3df(10));
  128. house1->setRotation(vector3df(0, 180, 0));
  129.  
  130. //Spawn the House2
  131. ISceneNode *house2 = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/house2.obj"));
  132. //Disable lighting for the Apartments node in order to see object
  133. house2->setMaterialFlag(EMF_LIGHTING, false);
  134. //Load the Apartments texture and apply it to the watertower scene object
  135. house2->setMaterialTexture(0, driver->getTexture("../media/textures/house2.png"));
  136. house2->setPosition(vector3df(800, 800, 1800));
  137. house2->setScale(vector3df(10));
  138. house2->setRotation(vector3df(0, 120, 0));
  139.  
  140. //Spawn the House3
  141. ISceneNode *house3 = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/house3.obj"));
  142. //Disable lighting for the Apartments node in order to see object
  143. house3->setMaterialFlag(EMF_LIGHTING, false);
  144. //Load the Apartments texture and apply it to the watertower scene object
  145. house3->setMaterialTexture(0, driver->getTexture("../media/textures/house3.png"));
  146. house3->setPosition(vector3df(800, 800, 1500));
  147. house3->setScale(vector3df(8));
  148. house3->setRotation(vector3df(0, 90, 0));
  149.  
  150. //Spawn the Building Block
  151. ISceneNode *block = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/block.obj"));
  152. //Disable lighting for the Apartments node in order to see object
  153. block->setMaterialFlag(EMF_LIGHTING, false);
  154. //Load the Apartments texture and apply it to the watertower scene object
  155. block->setMaterialTexture(0, driver->getTexture("../media/textures/block.png"));
  156. block->setPosition(vector3df(970, 800, -100));
  157. block->setScale(vector3df(10));
  158.  
  159. //Spawn the Shed
  160. ISceneNode *shed = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/shed.obj"));
  161. //Disable lighting for the Apartments node in order to see object
  162. shed->setMaterialFlag(EMF_LIGHTING, false);
  163. //Load the Apartments texture and apply it to the watertower scene object
  164. shed->setMaterialTexture(0, driver->getTexture("../media/textures/shed.png"));
  165. shed->setPosition(vector3df(1120, 800, 775));
  166. shed->setScale(vector3df(7));
  167. shed->setRotation(vector3df(0, -90, 0));
  168.  
  169.  
  170.  
  171. // SPAWN OBJECTS //
  172.  
  173. //Spawn the Watertower object
  174. ISceneNode *watertower = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/Watertower.obj"));
  175. //Disable lighting for the watertower node in order to see object
  176. watertower->setMaterialFlag(EMF_LIGHTING, false);
  177. //Load the watertower texture and apply it to the watertower scene object
  178. watertower->setMaterialTexture(0, driver->getTexture("../media/textures/watertowertexture.png"));
  179. watertower->setPosition(vector3df(800, 800, 1000));
  180. watertower->setScale(vector3df(25));
  181.  
  182. //Spawn a tree object
  183. ISceneNode *tree = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/tree.obj"));
  184. //Disable lighting for the tree node in order to see object
  185. tree->setMaterialFlag(EMF_LIGHTING, false);
  186. //Load the tree texture and apply it to the watertower scene object
  187. tree->setMaterialTexture(0, driver->getTexture("../media/textures/tree.png"));
  188. tree->setPosition(vector3df(700, 800, 1650));
  189. tree->setScale(vector3df(8));
  190.  
  191. //Spawn car object
  192. ISceneNode *car = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/car.obj"));
  193. //Disable lighting for the tree node in order to see object
  194. car->setMaterialFlag(EMF_LIGHTING, false);
  195. //Load the tree texture and apply it to the watertower scene object
  196. car->setMaterialTexture(0, driver->getTexture("../media/textures/car.png"));
  197. car->setPosition(vector3df(1015, 800, 1700));
  198. car->setScale(vector3df(6));
  199. car->setRotation(vector3df(90.0f));
  200.  
  201. //Spawn dumpster object
  202. ISceneNode *bin = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/dumpster.obj"));
  203. //Disable lighting for the dumpster node in order to see object
  204. bin->setMaterialFlag(EMF_LIGHTING, false);
  205. //Load the dumpster texture and apply it to the watertower scene object
  206. bin->setMaterialTexture(0, driver->getTexture("../media/textures/dumpster.png"));
  207. bin->setPosition(vector3df(1100, 800, 1485));
  208. bin->setScale(vector3df(5));
  209. bin->setRotation(vector3df(0, 180, 0));
  210.  
  211. //Spawn a treehouse object
  212. ISceneNode *treehouse = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/treehouse.obj"));
  213. //Disable lighting for the tree node in order to see object
  214. treehouse->setMaterialFlag(EMF_LIGHTING, false);
  215. //Load the tree texture and apply it to the watertower scene object
  216. treehouse->setMaterialTexture(0, driver->getTexture("../media/textures/treehouse.png"));
  217. treehouse->setPosition(vector3df(700, 800, 800));
  218. treehouse->setScale(vector3df(8));
  219. treehouse->setRotation(vector3df(0, -90, 0));
  220.  
  221. //Spawn a busstop object
  222. ISceneNode *busstop = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/BusStop.obj"));
  223. //Disable lighting for the tree node in order to see object
  224. busstop->setMaterialFlag(EMF_LIGHTING, false);
  225. //Load the tree texture and apply it to the watertower scene object
  226. busstop->setMaterialTexture(0, driver->getTexture("../media/textures/BusStop.png"));
  227. busstop->setPosition(vector3df(900, 800, 1100));
  228. busstop->setScale(vector3df(3));
  229. busstop->setRotation(vector3df(0, 180, 0));
  230.  
  231. //Spawn a Swing object
  232. ISceneNode *swing = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/Swing.obj"));
  233. //Disable lighting for the tree node in order to see object
  234. swing->setMaterialFlag(EMF_LIGHTING, false);
  235. //Load the tree texture and apply it to the watertower scene object
  236. swing->setMaterialTexture(0, driver->getTexture("../media/textures/Swing.png"));
  237. swing->setPosition(vector3df(500, 800, 1200));
  238. swing->setScale(vector3df(5));
  239. swing->setRotation(vector3df(0, 180, 0));
  240.  
  241. //Spawn a Merrygoround object
  242. ISceneNode *merry = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/Merrygoround.obj"));
  243. //Disable lighting for the tree node in order to see object
  244. merry->setMaterialFlag(EMF_LIGHTING, false);
  245. //Load the tree texture and apply it to the watertower scene object
  246. merry->setMaterialTexture(0, driver->getTexture("../media/textures/Merrygoround.png"));
  247. merry->setPosition(vector3df(600, 800, 1300));
  248. merry->setScale(vector3df(5));
  249. merry->setRotation(vector3df(0, 180, 0));
  250.  
  251. //Spawn a Slide object
  252. ISceneNode *slide = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/Slide.obj"));
  253. //Disable lighting for the tree node in order to see object
  254. slide->setMaterialFlag(EMF_LIGHTING, false);
  255. //Load the tree texture and apply it to the watertower scene object
  256. slide->setMaterialTexture(0, driver->getTexture("../media/textures/Slide.png"));
  257. slide->setPosition(vector3df(550, 800, 1400));
  258. slide->setScale(vector3df(5));
  259. slide->setRotation(vector3df(0, 90, 0));
  260.  
  261. //Spawn a Park Bench object
  262. ISceneNode *parkbench = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/ParkBench.obj"));
  263. //Disable lighting for the tree node in order to see object
  264. parkbench->setMaterialFlag(EMF_LIGHTING, false);
  265. //Load the tree texture and apply it to the watertower scene object
  266. parkbench->setMaterialTexture(0, driver->getTexture("../media/textures/ParkBench.png"));
  267. parkbench->setPosition(vector3df(650, 800, 1450));
  268. parkbench->setScale(vector3df(4));
  269. parkbench->setRotation(vector3df(0, -60, 0));
  270.  
  271. //Spawn a Park Table object
  272. ISceneNode *parktable = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/ParkTable.obj"));
  273. //Disable lighting for the tree node in order to see object
  274. parktable->setMaterialFlag(EMF_LIGHTING, false);
  275. //Load the tree texture and apply it to the watertower scene object
  276. parktable->setMaterialTexture(0, driver->getTexture("../media/textures/ParkTable.png"));
  277. parktable->setPosition(vector3df(650, 800, 1100));
  278. parktable->setScale(vector3df(3));
  279. parktable->setRotation(vector3df(0, -130, 0));
  280.  
  281. //Spawn a Rubbish Bin object
  282. ISceneNode *rubbishbin = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/RubbishBin.obj"));
  283. //Disable lighting for the tree node in order to see object
  284. rubbishbin->setMaterialFlag(EMF_LIGHTING, false);
  285. //Load the tree texture and apply it to the watertower scene object
  286. rubbishbin->setMaterialTexture(0, driver->getTexture("../media/textures/RubbishBin.png"));
  287. rubbishbin->setPosition(vector3df(700, 800, 1150));
  288. rubbishbin->setScale(vector3df(2.5));
  289.  
  290. // CHARACTER //
  291.  
  292. ISceneNode *character = scnmgr->addMeshSceneNode(scnmgr->getMesh("../media/character.obj"));
  293. //Disable lighting for the tree node in order to see object
  294. character->setMaterialFlag(EMF_LIGHTING, false);
  295. //Load the tree texture and apply it to the watertower scene object
  296. character->setMaterialTexture(0, driver->getTexture("../media/textures/character.png"));
  297. character->setPosition(vector3df(850, 800, 1200));
  298. character->setScale(vector3df(1));
  299.  
  300.  
  301. // BILLBOARDS //
  302.  
  303. //Lamp Billboard
  304. ISceneNode *lamp = scnmgr->addBillboardSceneNode(NULL, dimension2d<f32>(15, 75), vector3df(900, 800, 1485));
  305. lamp->setMaterialFlag(EMF_LIGHTING, false);
  306. lamp->setMaterialTexture(0, driver->getTexture("../media/textures/lamp.png"));
  307. lamp->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
  308.  
  309. //Bench Billboard
  310. ISceneNode *bench = scnmgr->addBillboardSceneNode(NULL, dimension2d<f32>(60, 30), vector3df(900, 800, 950));
  311. bench->setMaterialFlag(EMF_LIGHTING, false);
  312. bench->setMaterialTexture(0, driver->getTexture("../media/textures/bench.png"));
  313. bench->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
  314.  
  315. //ATM Billboard
  316. ISceneNode *atm = scnmgr->addBillboardSceneNode(NULL, dimension2d<f32>(95, 50), vector3df(1040, 800, 1550));
  317. atm->setMaterialFlag(EMF_LIGHTING, false);
  318. atm->setMaterialTexture(0, driver->getTexture("../media/textures/atm.png"));
  319. atm->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
  320.  
  321. //Bush Billboard
  322. ISceneNode *bush = scnmgr->addBillboardSceneNode(NULL, dimension2d<f32>(40, 40), vector3df(850, 800, 1400));
  323. bush->setMaterialFlag(EMF_LIGHTING, false);
  324. bush->setMaterialTexture(0, driver->getTexture("../media/textures/bush.png"));
  325. bush->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
  326.  
  327. //Trash Bin Billboard
  328. ISceneNode *trashbin = scnmgr->addBillboardSceneNode(NULL, dimension2d<f32>(23, 30), vector3df(900, 800, 900));
  329. trashbin->setMaterialFlag(EMF_LIGHTING, false);
  330. trashbin->setMaterialTexture(0, driver->getTexture("../media/textures/trashbin.png"));
  331. trashbin->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
  332.  
  333. //Tree2 Billboard
  334. ISceneNode *tree2 = scnmgr->addBillboardSceneNode(NULL, dimension2d<f32>(150, 150), vector3df(400, 800, 800));
  335. tree2->setMaterialFlag(EMF_LIGHTING, false);
  336. tree2->setMaterialTexture(0, driver->getTexture("../media/textures/tree2.png"));
  337. tree2->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
  338.  
  339. //Tree3 Billboard
  340. ISceneNode *tree3 = scnmgr->addBillboardSceneNode(NULL, dimension2d<f32>(120, 180), vector3df(300, 800, 1000));
  341. tree3->setMaterialFlag(EMF_LIGHTING, false);
  342. tree3->setMaterialTexture(0, driver->getTexture("../media/textures/tree3.png"));
  343. tree3->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
  344.  
  345. //Fence Billboard
  346. ISceneNode *fence = scnmgr->addBillboardSceneNode(NULL, dimension2d<f32>(140, 80), vector3df(450, 800, 1300));
  347. fence->setMaterialFlag(EMF_LIGHTING, false);
  348. fence->setMaterialTexture(0, driver->getTexture("../media/textures/fence.png"));
  349. fence->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
  350.  
  351. //Playground Billboard
  352. ISceneNode *playground = scnmgr->addBillboardSceneNode(NULL, dimension2d<f32>(100, 100), vector3df(480, 800, 1300));
  353. playground->setMaterialFlag(EMF_LIGHTING, false);
  354. playground->setMaterialTexture(0, driver->getTexture("../media/textures/playground.png"));
  355. playground->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
  356.  
  357. //Tree3 Billboard
  358. ISceneNode *tree4 = scnmgr->addBillboardSceneNode(NULL, dimension2d<f32>(150, 150), vector3df(400, 800, 1600));
  359. tree4->setMaterialFlag(EMF_LIGHTING, false);
  360. tree4->setMaterialTexture(0, driver->getTexture("../media/textures/tree4.png"));
  361. tree4->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
  362.  
  363. // SKYBOX //
  364.  
  365. //Textures for each side of Skybox
  366. ITexture *top = driver->getTexture("../media/textures/skyboxTOP.png");
  367. ITexture *bottom = driver->getTexture("../media/textures/skyboxBOTTOM.png");
  368. ITexture *left = driver->getTexture("../media/textures/skyboxLEFT.png");
  369. ITexture *right = driver->getTexture("../media/textures/skyboxRIGHT.png");
  370. ITexture *front = driver->getTexture("../media/textures/skyboxFRONT.png");
  371. ITexture *veryright = driver->getTexture("../media/textures/skyboxBACK.png");
  372.  
  373. //Skybox node
  374. ISceneNode *skybox = scnmgr->addSkyBoxSceneNode(top, bottom, left, right, front, veryright);
  375.  
  376.  
  377.  
  378. // TERRAIN //
  379.  
  380. //Terrain spawn
  381. irr::io::path path = "../media/newterrainMap.bmp";
  382. ITerrainSceneNode *terrain = scnmgr->addTerrainSceneNode(path, 0, 0, vector3df(-1000, 0, -800), vector3df(0, 0, 0), vector3df(40, 1, 40), SColor(255, 255, 255, 255), 5, ETPS_17, 4);
  383.  
  384. //Terrain texture
  385. terrain->setMaterialFlag(EMF_LIGHTING, false);
  386. terrain->setMaterialTexture(0, driver->getTexture("../media/textures/terraintexture.jpg"));
  387.  
  388.  
  389.  
  390. // TERRAIN HEIGHT //
  391.  
  392. // BUILDINGS //
  393. //Set building1 height to scale with terrain height
  394. vector3df building1Position = building1->getPosition();
  395. building1Position.Y = terrain->getHeight(building1Position.X, building1Position.Z);
  396. building1->setPosition(building1Position);
  397.  
  398. //Set warehouse height to scale with terrain height
  399. vector3df warehousePosition = warehouse->getPosition();
  400. warehousePosition.Y = terrain->getHeight(warehousePosition.X, warehousePosition.Z);
  401. warehouse->setPosition(warehousePosition);
  402.  
  403. //Set Shop height to scale with terrain height
  404. vector3df shopPosition = shop->getPosition();
  405. shopPosition.Y = terrain->getHeight(shopPosition.X, shopPosition.Z);
  406. shop->setPosition(shopPosition);
  407.  
  408. //Set Apartments height to scale with terrain height
  409. vector3df apartmentsPosition = apartments->getPosition();
  410. apartmentsPosition.Y = terrain->getHeight(apartmentsPosition.X, apartmentsPosition.Z);
  411. apartments->setPosition(apartmentsPosition);
  412.  
  413. //Set Garage height to scale with terrain height
  414. vector3df garagePosition = garage->getPosition();
  415. garagePosition.Y = terrain->getHeight(garagePosition.X, garagePosition.Z);
  416. garage->setPosition(garagePosition);
  417.  
  418. //Set House1 height to scale with terrain height
  419. vector3df house1Position = house1->getPosition();
  420. house1Position.Y = terrain->getHeight(house1Position.X, house1Position.Z);
  421. house1->setPosition(house1Position);
  422.  
  423. //Set House2 height to scale with terrain height
  424. vector3df house2Position = house2->getPosition();
  425. house2Position.Y = terrain->getHeight(house2Position.X, house2Position.Z);
  426. house2->setPosition(house2Position);
  427.  
  428. //Set House3 height to scale with terrain height
  429. vector3df house3Position = house3->getPosition();
  430. house3Position.Y = terrain->getHeight(house3Position.X, house3Position.Z);
  431. house3->setPosition(house3Position);
  432.  
  433. //Set Apartment Block height to scale with terrain height
  434. vector3df blockPosition = block->getPosition();
  435. blockPosition.Y = terrain->getHeight(blockPosition.X, blockPosition.Z);
  436. block->setPosition(blockPosition);
  437.  
  438. //Set Shed height to scale with terrain height
  439. vector3df shedPosition = shed->getPosition();
  440. shedPosition.Y = terrain->getHeight(shedPosition.X, shedPosition.Z);
  441. shed->setPosition(shedPosition);
  442.  
  443. // OBJECTS //
  444.  
  445. //Set Watertower height to scale with terrain height
  446. vector3df watertowerPosition = watertower->getPosition();
  447. watertowerPosition.Y = terrain->getHeight(watertowerPosition.X, watertowerPosition.Z);
  448. watertower->setPosition(watertowerPosition);
  449.  
  450. //Set tree height to scale with terrain height
  451. vector3df treePosition = tree->getPosition();
  452. treePosition.Y = terrain->getHeight(treePosition.X, treePosition.Z);
  453. tree->setPosition(treePosition);
  454.  
  455. //Set car height to scale with terrain height
  456. vector3df carPosition = car->getPosition();
  457. carPosition.Y = terrain->getHeight(carPosition.X, carPosition.Z);
  458. car->setPosition(carPosition);
  459.  
  460. //Set bin height to scale with terrain height
  461. vector3df binPosition = bin->getPosition();
  462. binPosition.Y = terrain->getHeight(binPosition.X, binPosition.Z);
  463. bin->setPosition(binPosition);
  464.  
  465. //Treehouse object height to scale with terrain height
  466. vector3df treehousePosition = treehouse->getPosition();
  467. treehousePosition.Y = terrain->getHeight(treehousePosition.X, treehousePosition.Z);
  468. treehouse->setPosition(treehousePosition);
  469.  
  470. //Bus Stop object height to scale with terrain height
  471. vector3df busstopPosition = busstop->getPosition();
  472. busstopPosition.Y = terrain->getHeight(busstopPosition.X, busstopPosition.Z);
  473. busstop->setPosition(busstopPosition);
  474.  
  475. //Swing object height to scale with terrain height
  476. vector3df swingPosition = swing->getPosition();
  477. swingPosition.Y = terrain->getHeight(swingPosition.X, swingPosition.Z);
  478. swing->setPosition(swingPosition);
  479.  
  480. //Merrygoround object height to scale with terrain height
  481. vector3df merryPosition = merry->getPosition();
  482. merryPosition.Y = terrain->getHeight(merryPosition.X, merryPosition.Z) - 6;
  483. merry->setPosition(merryPosition);
  484.  
  485. //Slide object height to scale with terrain height
  486. vector3df slidePosition = slide->getPosition();
  487. slidePosition.Y = terrain->getHeight(slidePosition.X, slidePosition.Z);
  488. slide->setPosition(slidePosition);
  489.  
  490. //Park Bench object height to scale with terrain height
  491. vector3df parkbenchPosition = parkbench->getPosition();
  492. parkbenchPosition.Y = terrain->getHeight(parkbenchPosition.X, parkbenchPosition.Z);
  493. parkbench->setPosition(parkbenchPosition);
  494.  
  495. //Park Table object height to scale with terrain height
  496. vector3df parktablePosition = parktable->getPosition();
  497. parktablePosition.Y = terrain->getHeight(parktablePosition.X, parktablePosition.Z);
  498. parktable->setPosition(parktablePosition);
  499.  
  500. //Park Rubbish Bin object height to scale with terrain height
  501. vector3df rubbishbinPosition = rubbishbin->getPosition();
  502. rubbishbinPosition.Y = terrain->getHeight(rubbishbinPosition.X, rubbishbinPosition.Z);
  503. rubbishbin->setPosition(rubbishbinPosition);
  504.  
  505.  
  506. // BILLBOARDS //
  507.  
  508. //Lamp Billboard height to scale with terrain height
  509. vector3df lampPosition = lamp->getPosition();
  510. lampPosition.Y = terrain->getHeight(lampPosition.X, lampPosition.Z) + 30;
  511. lamp->setPosition(lampPosition);
  512.  
  513. //Bench Billboard height to scale with terrain height
  514. vector3df benchPosition = bench->getPosition();
  515. benchPosition.Y = terrain->getHeight(benchPosition.X, benchPosition.Z) + 9;
  516. bench->setPosition(benchPosition);
  517.  
  518. //ATM Billboard height to scale with terrain height
  519. vector3df atmPosition = atm->getPosition();
  520. atmPosition.Y = terrain->getHeight(atmPosition.X, atmPosition.Z) + 19;
  521. atm->setPosition(atmPosition);
  522.  
  523. //Bush Billboard height to scale with terrain height
  524. vector3df bushPosition = bush->getPosition();
  525. bushPosition.Y = terrain->getHeight(bushPosition.X, bushPosition.Z) + 11;
  526. bush->setPosition(bushPosition);
  527.  
  528. //Trash Bin Billboard height to scale with terrain height
  529. vector3df trashbinPosition = trashbin->getPosition();
  530. trashbinPosition.Y = terrain->getHeight(trashbinPosition.X, trashbinPosition.Z) + 10;
  531. trashbin->setPosition(trashbinPosition);
  532.  
  533. //Tree2 Billboard height to scale with terrain height
  534. vector3df tree2Position = tree2->getPosition();
  535. tree2Position.Y = terrain->getHeight(tree2Position.X, tree2Position.Z) + 60;
  536. tree2->setPosition(tree2Position);
  537.  
  538. //Tree3 Billboard height to scale with terrain height
  539. vector3df tree3Position = tree3->getPosition();
  540. tree3Position.Y = terrain->getHeight(tree3Position.X, tree3Position.Z) + 60;
  541. tree3->setPosition(tree3Position);
  542.  
  543. //Fence Billboard height to scale with terrain height
  544. vector3df fencePosition = fence->getPosition();
  545. fencePosition.Y = terrain->getHeight(fencePosition.X, fencePosition.Z);
  546. fence->setPosition(fencePosition);
  547.  
  548. //Playground Billboard height to scale with terrain height
  549. vector3df playgroundPosition = playground->getPosition();
  550. playgroundPosition.Y = terrain->getHeight(playgroundPosition.X, playgroundPosition.Z) + 35;
  551. playground->setPosition(playgroundPosition);
  552.  
  553. //Tree4 Billboard height to scale with terrain height
  554. vector3df tree4Position = tree4->getPosition();
  555. tree4Position.Y = terrain->getHeight(tree4Position.X, tree4Position.Z) + 60;
  556. tree4->setPosition(tree4Position);
  557.  
  558. // ROADS //
  559.  
  560. //Road duplication
  561. roadPlacement(scnmgr, driver, terrain, 21);
  562.  
  563.  
  564.  
  565. // CAMERAS //
  566.  
  567. //Sets the FPS camera position
  568. ICameraSceneNode *camera = scnmgr->addCameraSceneNodeFPS(NULL, 100.0f, 0.0f);
  569. camera->setPosition(character->getPosition());
  570.  
  571. //Sets thirdperson camera position
  572. ICameraSceneNode *thirdpcamera = scnmgr->addCameraSceneNode(character, vector3df(0, 30, -30), vector3df(0, 5, 0));
  573.  
  574. //Set initial active camera to the FPS camera
  575. scnmgr->setActiveCamera(camera);
  576.  
  577. //Store the initial FPS
  578. s32 fps_then = driver->getFPS();
  579.  
  580. //Calculate Delta Time
  581. u32 then = device->getTimer()->getTime();
  582.  
  583. bool fps = true;
  584.  
  585.  
  586. IGUIFont *font = device->getGUIEnvironment()->getFont("../media/textures/bigfont.png");
  587.  
  588. ITextSceneNode *text = scnmgr->addTextSceneNode(font, L"Normal Text", SColor(100, 255, 255, 255), NULL, vector3df(20.0f, 0.0f, 10.0f));
  589.  
  590. IBillboardTextSceneNode *billboard_text = scnmgr->addBillboardTextSceneNode(font, L"Billboard Text", NULL, dimension2d<f32>(20.0f, 10.0f), vector3df(-20.0f, 0.0f, 10.0f));
  591.  
  592. IGUIStaticText *guiText = device->getGUIEnvironment()->addStaticText(L"GUI text", rect<s32>(10, 10, 100, 30), true);
  593.  
  594.  
  595. //Main loop
  596. while (device->run()) {
  597. //Clear the backbuffer
  598. driver->beginScene(true, true, SColor(255, 100, 101, 140));
  599.  
  600. //Render scene objects to backbuffer
  601. scnmgr->drawAll();
  602.  
  603. //Show backbuffer and final rendered frame
  604. driver->endScene();
  605.  
  606. //Get the current frame rate
  607. s32 fps_now = driver->getFPS();
  608.  
  609. //Calculate the frameDeltaTime
  610. const u32 now = device->getTimer()->getTime();
  611. const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
  612. then = now;
  613.  
  614. vector3df nodePosition = character->getPosition();
  615.  
  616. //Forward & Backwards Movements
  617. if (listener.IsKeyDown(irr::KEY_KEY_W))
  618. nodePosition.Z += 80 * frameDeltaTime;
  619. else if (listener.IsKeyDown(irr::KEY_KEY_S))
  620. nodePosition.Z -= 80 *frameDeltaTime;
  621.  
  622. //Left & Right Movements
  623. if (listener.IsKeyDown(irr::KEY_KEY_A))
  624. nodePosition.X -= 80 * frameDeltaTime;
  625. else if (listener.IsKeyDown(irr::KEY_KEY_D))
  626. nodePosition.X += 80 * frameDeltaTime;
  627.  
  628. //Update position of character & cameras
  629. character->setPosition(nodePosition);
  630. nodePosition.Y += 15;
  631. nodePosition.Z += 6;
  632. camera->setPosition(nodePosition);
  633. thirdpcamera->setTarget(character->getPosition());
  634.  
  635. //Change active camera key
  636. if (listener.IsKeyDown(irr::KEY_KEY_E))
  637. {
  638. //Delay the change time
  639. listener.ChangeKeyDown(irr::KEY_KEY_E, false);
  640. //Change active camera
  641. fps = !fps;
  642. }
  643.  
  644. if (fps) {
  645. scnmgr->setActiveCamera(camera);
  646. //Hide character model in FPS camera mode
  647. character->setVisible(false);
  648. }
  649. else {
  650. //Show character model in 3rd Person Camera mode
  651. scnmgr->setActiveCamera(thirdpcamera);
  652. character->setVisible(true);
  653. }
  654.  
  655. //Update character position and height to scale with terrain.
  656. vector3df characterPosition = character->getPosition();
  657. characterPosition.Y = terrain->getHeight(characterPosition.X, characterPosition.Z) + 5;
  658. character->setPosition(characterPosition);
  659.  
  660. //If there has been a change in frame rate
  661. if (fps_now != fps_then) {
  662. //then update window caption bar
  663. stringw msg = "FPS = ";
  664.  
  665. msg += fps_now;
  666.  
  667. text->setText(msg.c_str());
  668. billboard_text->setText(msg.c_str());
  669. guiText->setText(msg.c_str());
  670.  
  671. device->getGUIEnvironment()->drawAll();
  672.  
  673. msg += camera->getPosition().Z;
  674.  
  675. device->setWindowCaption(msg.c_str());
  676.  
  677. //Update frame rate
  678. fps_then = fps_now;
  679. }
  680. }
  681.  
  682. //At this point the loop has ended so shutdown Irrlicht object
  683. device->drop();
  684.  
  685. //End of program. Return 0 to indicate successful execution
  686. return 0;
  687. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement