TerrainSample.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #include "TerrainSample.h"
  2. #include "SamplesGame.h"
  3. #if defined(ADD_SAMPLE)
  4. ADD_SAMPLE("Graphics", "Terrain", TerrainSample, 12);
  5. #endif
  6. struct TerrainHitFilter : public PhysicsController::HitFilter
  7. {
  8. TerrainHitFilter(Terrain* terrain)
  9. {
  10. terrainObject = terrain->getNode()->getCollisionObject();
  11. }
  12. bool filter(PhysicsCollisionObject* object)
  13. {
  14. // Filter out all objects but the terrain
  15. return (object != terrainObject);
  16. }
  17. PhysicsCollisionObject* terrainObject;
  18. };
  19. TerrainSample::TerrainSample()
  20. : _font(NULL), _scene(NULL), _terrain(NULL), _sky(NULL), _form(NULL), _formVisible(true),
  21. _wireframe(false), _debugPhysics(false), _snapToGround(true), _vsync(true),
  22. _mode(MODE_LOOK), _sphere(NULL), _box(NULL), _directionalLight(NULL)
  23. {
  24. }
  25. TerrainSample::~TerrainSample()
  26. {
  27. SAFE_RELEASE(_box);
  28. SAFE_RELEASE(_sphere);
  29. SAFE_RELEASE(_form);
  30. SAFE_RELEASE(_font);
  31. SAFE_RELEASE(_scene);
  32. }
  33. void TerrainSample::initialize()
  34. {
  35. // Load scene
  36. _scene = Scene::load("res/common/terrain/sample.scene");
  37. _terrain = _scene->findNode("terrain")->getTerrain();
  38. _sky = _scene->findNode("sky");
  39. _sky->setTag("lighting", "none");
  40. // Load shapes
  41. Bundle* bundle;
  42. bundle = Bundle::create("res/common/sphere.gpb");
  43. _sphere = bundle->loadNode("sphere");
  44. _sphere->getModel()->setMaterial("res/common/terrain/shapes.material#sphere", 0);
  45. SAFE_RELEASE(bundle);
  46. bundle = Bundle::create("res/common/box.gpb");
  47. _box = bundle->loadNode("box");
  48. _box->getModel()->setMaterial("res/common/terrain/shapes.material#box", 0);
  49. SAFE_RELEASE(bundle);
  50. // Load font
  51. _font = Font::create("res/ui/arial.gpb");
  52. // Setup form
  53. _form = Form::create("res/common/terrain/terrain.form");
  54. _form->getControl("plusButton")->addListener(this, Control::Listener::CLICK);
  55. _form->getControl("minusButton")->addListener(this, Control::Listener::CLICK);
  56. _form->getControl("wireframe")->addListener(this, Control::Listener::VALUE_CHANGED);
  57. _form->getControl("patches")->addListener(this, Control::Listener::VALUE_CHANGED);
  58. _form->getControl("physics")->addListener(this, Control::Listener::VALUE_CHANGED);
  59. _form->getControl("lod")->addListener(this, Control::Listener::VALUE_CHANGED);
  60. _form->getControl("culling")->addListener(this, Control::Listener::VALUE_CHANGED);
  61. _form->getControl("snapToGround")->addListener(this, Control::Listener::VALUE_CHANGED);
  62. _form->getControl("dropSphere")->addListener(this, Control::Listener::CLICK);
  63. _form->getControl("dropBox")->addListener(this, Control::Listener::CLICK);
  64. _form->getControl("clearAll")->addListener(this, Control::Listener::CLICK);
  65. Control* main = _form->getControl("main");
  66. _formSize.set(main->getWidth(), main->getHeight());
  67. // Use script camera for navigation
  68. enableScriptCamera(true);
  69. setScriptCameraSpeed(20, 80);
  70. _directionalLight = _scene->findNode("directionalLight")->getLight();
  71. }
  72. void TerrainSample::finalize()
  73. {
  74. SAFE_RELEASE(_font);
  75. SAFE_RELEASE(_scene);
  76. SAFE_RELEASE(_form);
  77. SAFE_RELEASE(_sphere);
  78. SAFE_RELEASE(_box);
  79. }
  80. void TerrainSample::update(float elapsedTime)
  81. {
  82. Node* camera = _scene->getActiveCamera()->getNode();
  83. if (_snapToGround)
  84. {
  85. // Get current camera location in world coords
  86. Vector3 pos = camera->getTranslationWorld();
  87. // Query the height of our terrain at this location
  88. float height = _terrain->getHeight(pos.x, pos.z);
  89. // Snap our camera to the ground
  90. camera->setTranslationY(height + 10);
  91. }
  92. // Keep the sky centered around the viewer
  93. _sky->setTranslationX(camera->getTranslationX());
  94. _sky->setTranslationZ(camera->getTranslationZ());
  95. // Prune dropped physics shapes that fall off the terrain
  96. for (std::list<Node*>::iterator itr = _shapes.begin(); itr != _shapes.end(); )
  97. {
  98. Node* shape = *itr;
  99. if (shape->getTranslation().y < 0)
  100. {
  101. _scene->removeNode(shape);
  102. std::list<Node*>::iterator oldItr = itr;
  103. ++itr;
  104. _shapes.erase(oldItr);
  105. }
  106. else
  107. {
  108. ++itr;
  109. }
  110. }
  111. }
  112. void TerrainSample::render(float elapsedTime)
  113. {
  114. clear(Game::CLEAR_COLOR_DEPTH, 0, 0, 0, 1, 1, 0);
  115. // Draw scene
  116. _scene->visit(this, &TerrainSample::drawScene);
  117. // Debug draw
  118. if (_debugPhysics)
  119. Game::getInstance()->getPhysicsController()->drawDebug(_scene->getActiveCamera()->getViewProjectionMatrix());
  120. // Draw form
  121. _form->draw();
  122. // Draw text
  123. char buffer[1024];
  124. sprintf(buffer, "FPS: %d", getFrameRate());
  125. _font->start();
  126. _font->drawText(buffer, 65, 18, Vector4::one(), 30);
  127. _font->finish();
  128. }
  129. bool TerrainSample::drawScene(Node* node)
  130. {
  131. Camera* camera = _scene->getActiveCamera();
  132. if (node->getModel())
  133. {
  134. if (node->getBoundingSphere().intersects(camera->getFrustum()))
  135. {
  136. node->getModel()->draw();
  137. }
  138. }
  139. else if (node->getTerrain())
  140. {
  141. Terrain* terrain = node->getTerrain();
  142. terrain->draw(_wireframe);
  143. }
  144. return true;
  145. }
  146. void TerrainSample::keyEvent(Keyboard::KeyEvent evt, int key)
  147. {
  148. // Process keyboard shortcuts for options
  149. if (evt == Keyboard::KEY_PRESS)
  150. {
  151. switch (key)
  152. {
  153. case Keyboard::KEY_ONE:
  154. {
  155. CheckBox* cb = static_cast<CheckBox*>(_form->getControl("wireframe"));
  156. cb->setChecked(!cb->isChecked());
  157. }
  158. break;
  159. case Keyboard::KEY_TWO:
  160. {
  161. CheckBox* cb = static_cast<CheckBox*>(_form->getControl("patches"));
  162. cb->setChecked(!cb->isChecked());
  163. }
  164. break;
  165. case Keyboard::KEY_THREE:
  166. {
  167. CheckBox* cb = static_cast<CheckBox*>(_form->getControl("physics"));
  168. cb->setChecked(!cb->isChecked());
  169. }
  170. break;
  171. case Keyboard::KEY_FOUR:
  172. {
  173. CheckBox* cb = static_cast<CheckBox*>(_form->getControl("lod"));
  174. cb->setChecked(!cb->isChecked());
  175. }
  176. break;
  177. case Keyboard::KEY_FIVE:
  178. {
  179. CheckBox* cb = static_cast<CheckBox*>(_form->getControl("culling"));
  180. cb->setChecked(!cb->isChecked());
  181. }
  182. break;
  183. case Keyboard::KEY_SIX:
  184. {
  185. CheckBox* cb = static_cast<CheckBox*>(_form->getControl("snapToGround"));
  186. cb->setChecked(!cb->isChecked());
  187. }
  188. break;
  189. }
  190. }
  191. }
  192. void TerrainSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  193. {
  194. if (evt == Touch::TOUCH_PRESS)
  195. {
  196. // If the FPS region is touched, toggle vsync (if platform supports it)
  197. if (x >= 65 && x < 300 && y >= 18 && y <= 48)
  198. {
  199. _vsync = !_vsync;
  200. setVsync(_vsync);
  201. }
  202. else if (_mode != MODE_LOOK)
  203. {
  204. // Ray sample
  205. Ray pickRay;
  206. _scene->getActiveCamera()->pickRay(Rectangle (0, 0, getWidth(), getHeight()), x, y, &pickRay);
  207. PhysicsController::HitResult hitResult;
  208. TerrainHitFilter hitFilter(_terrain);
  209. if (Game::getInstance()->getPhysicsController()->rayTest(pickRay, 1000000, &hitResult, &hitFilter) && hitResult.object == _terrain->getNode()->getCollisionObject())
  210. {
  211. Node* clone = NULL;
  212. PhysicsCollisionShape::Definition rbShape;
  213. switch (_mode)
  214. {
  215. case MODE_DROP_SPHERE:
  216. {
  217. clone = _sphere->clone();
  218. rbShape = PhysicsCollisionShape::sphere();
  219. }
  220. break;
  221. case MODE_DROP_BOX:
  222. {
  223. clone = _box->clone();
  224. rbShape = PhysicsCollisionShape::box();
  225. }
  226. break;
  227. }
  228. if (clone)
  229. {
  230. clone->setScale(10,10,10);
  231. clone->setTranslation(hitResult.point.x, hitResult.point.y + 50, hitResult.point.z);
  232. PhysicsRigidBody::Parameters rbParams(1);
  233. clone->setCollisionObject(PhysicsCollisionObject::RIGID_BODY, rbShape, &rbParams);
  234. _scene->addNode(clone);
  235. clone->release();
  236. _shapes.push_back(clone);
  237. _mode = MODE_LOOK;
  238. setMessage(NULL);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. bool TerrainSample::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  245. {
  246. return false;
  247. }
  248. void TerrainSample::controlEvent(Control* control, EventType evt)
  249. {
  250. if (strcmp(control->getId(), "plusButton") == 0)
  251. {
  252. control->setVisible(false);
  253. _form->getControl("minusButton")->setVisible(true);
  254. _form->getControl("settings")->setVisible(true);
  255. _form->getControl("physicsSettings")->setVisible(true);
  256. _form->getControl("main")->setSize(_formSize.x, _formSize.y);
  257. _formVisible = true;
  258. }
  259. else if (strcmp(control->getId(), "minusButton") == 0)
  260. {
  261. control->setVisible(false);
  262. _form->getControl("plusButton")->setVisible(true);
  263. _form->getControl("settings")->setVisible(false);
  264. _form->getControl("physicsSettings")->setVisible(false);
  265. _form->getControl("main")->setSize(50, 50);
  266. _formVisible = false;
  267. }
  268. else if (strcmp(control->getId(), "wireframe") == 0)
  269. {
  270. _wireframe = static_cast<CheckBox*>(control)->isChecked();
  271. }
  272. else if (strcmp(control->getId(), "patches") == 0)
  273. {
  274. _terrain->setFlag(Terrain::DEBUG_PATCHES, static_cast<CheckBox*>(control)->isChecked());
  275. }
  276. else if (strcmp(control->getId(), "physics") == 0)
  277. {
  278. _debugPhysics = static_cast<CheckBox*>(control)->isChecked();
  279. }
  280. else if (strcmp(control->getId(), "lod") == 0)
  281. {
  282. _terrain->setFlag(Terrain::LEVEL_OF_DETAIL, static_cast<CheckBox*>(control)->isChecked());
  283. }
  284. else if (strcmp(control->getId(), "snapToGround") == 0)
  285. {
  286. _snapToGround = static_cast<CheckBox*>(control)->isChecked();
  287. if (_snapToGround)
  288. setScriptCameraSpeed(20, 80);
  289. else
  290. setScriptCameraSpeed(200, 800);
  291. }
  292. else if (strcmp(control->getId(), "dropSphere") == 0)
  293. {
  294. if (_mode == MODE_DROP_SPHERE)
  295. {
  296. _mode = MODE_LOOK;
  297. setMessage(NULL);
  298. }
  299. else
  300. {
  301. _mode = MODE_DROP_SPHERE;
  302. setMessage("Click on the terrain to drop a sphere.");
  303. }
  304. }
  305. else if (strcmp(control->getId(), "dropBox") == 0)
  306. {
  307. if (_mode == MODE_DROP_BOX)
  308. {
  309. _mode = MODE_LOOK;
  310. setMessage(NULL);
  311. }
  312. else
  313. {
  314. _mode = MODE_DROP_BOX;
  315. setMessage("Click on the terrain to drop a box.");
  316. }
  317. }
  318. else if (strcmp(control->getId(), "clearAll") == 0)
  319. {
  320. for (std::list<Node*>::iterator itr = _shapes.begin(); itr != _shapes.end(); ++itr)
  321. _scene->removeNode(*itr);
  322. _shapes.clear();
  323. }
  324. }
  325. void TerrainSample::setMessage(const char* message)
  326. {
  327. Label* label = static_cast<Label*>(_form->getControl("message"));
  328. label->setText(message ? message : "");
  329. _form->getControl("messageBox")->setVisible(message ? true : false);
  330. }
  331. Vector3 TerrainSample::getLightDirection0() const
  332. {
  333. return _directionalLight->getNode()->getForwardVectorView();
  334. }
  335. Vector3 TerrainSample::getLightColor0() const
  336. {
  337. return _directionalLight->getColor();
  338. }
  339. bool TerrainSample::resolveAutoBinding(const char* autoBinding, Node* node, MaterialParameter* parameter)
  340. {
  341. if (strcmp(autoBinding, "LIGHT_DIRECTION_0") == 0)
  342. {
  343. parameter->bindValue(this, &TerrainSample::getLightDirection0);
  344. return true;
  345. }
  346. else if (strcmp(autoBinding, "LIGHT_COLOR_0") == 0)
  347. {
  348. parameter->bindValue(this, &TerrainSample::getLightColor0);
  349. return true;
  350. }
  351. return false;
  352. }