TerrainSample.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 = dynamic_cast<Terrain*>(_scene->findNode("terrain")->getDrawable());
  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. dynamic_cast<Model*>(_sphere->getDrawable())->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. dynamic_cast<Model*>(_box->getDrawable())->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. Drawable* drawable = node->getDrawable();
  133. if (dynamic_cast<Model*>(drawable))
  134. {
  135. if (!node->getBoundingSphere().intersects(camera->getFrustum()))
  136. return true;
  137. }
  138. if (drawable)
  139. drawable->draw(_wireframe);
  140. return true;
  141. }
  142. void TerrainSample::keyEvent(Keyboard::KeyEvent evt, int key)
  143. {
  144. // Process keyboard shortcuts for options
  145. if (evt == Keyboard::KEY_PRESS)
  146. {
  147. switch (key)
  148. {
  149. case Keyboard::KEY_ONE:
  150. {
  151. CheckBox* cb = static_cast<CheckBox*>(_form->getControl("wireframe"));
  152. cb->setChecked(!cb->isChecked());
  153. }
  154. break;
  155. case Keyboard::KEY_TWO:
  156. {
  157. CheckBox* cb = static_cast<CheckBox*>(_form->getControl("patches"));
  158. cb->setChecked(!cb->isChecked());
  159. }
  160. break;
  161. case Keyboard::KEY_THREE:
  162. {
  163. CheckBox* cb = static_cast<CheckBox*>(_form->getControl("physics"));
  164. cb->setChecked(!cb->isChecked());
  165. }
  166. break;
  167. case Keyboard::KEY_FOUR:
  168. {
  169. CheckBox* cb = static_cast<CheckBox*>(_form->getControl("lod"));
  170. cb->setChecked(!cb->isChecked());
  171. }
  172. break;
  173. case Keyboard::KEY_FIVE:
  174. {
  175. CheckBox* cb = static_cast<CheckBox*>(_form->getControl("culling"));
  176. cb->setChecked(!cb->isChecked());
  177. }
  178. break;
  179. case Keyboard::KEY_SIX:
  180. {
  181. CheckBox* cb = static_cast<CheckBox*>(_form->getControl("snapToGround"));
  182. cb->setChecked(!cb->isChecked());
  183. }
  184. break;
  185. }
  186. }
  187. }
  188. void TerrainSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  189. {
  190. if (evt == Touch::TOUCH_PRESS)
  191. {
  192. // If the FPS region is touched, toggle vsync (if platform supports it)
  193. if (x >= 65 && x < 300 && y >= 18 && y <= 48)
  194. {
  195. _vsync = !_vsync;
  196. setVsync(_vsync);
  197. }
  198. else if (_mode != MODE_LOOK)
  199. {
  200. // Ray sample
  201. Ray pickRay;
  202. _scene->getActiveCamera()->pickRay(Rectangle (0, 0, getWidth(), getHeight()), x, y, &pickRay);
  203. PhysicsController::HitResult hitResult;
  204. TerrainHitFilter hitFilter(_terrain);
  205. if (Game::getInstance()->getPhysicsController()->rayTest(pickRay, 1000000, &hitResult, &hitFilter) && hitResult.object == _terrain->getNode()->getCollisionObject())
  206. {
  207. Node* clone = NULL;
  208. PhysicsCollisionShape::Definition rbShape;
  209. switch (_mode)
  210. {
  211. case MODE_DROP_SPHERE:
  212. {
  213. clone = _sphere->clone();
  214. rbShape = PhysicsCollisionShape::sphere();
  215. }
  216. break;
  217. case MODE_DROP_BOX:
  218. {
  219. clone = _box->clone();
  220. rbShape = PhysicsCollisionShape::box();
  221. }
  222. break;
  223. }
  224. if (clone)
  225. {
  226. clone->setScale(10,10,10);
  227. clone->setTranslation(hitResult.point.x, hitResult.point.y + 50, hitResult.point.z);
  228. PhysicsRigidBody::Parameters rbParams(1);
  229. clone->setCollisionObject(PhysicsCollisionObject::RIGID_BODY, rbShape, &rbParams);
  230. _scene->addNode(clone);
  231. clone->release();
  232. _shapes.push_back(clone);
  233. _mode = MODE_LOOK;
  234. setMessage(NULL);
  235. }
  236. }
  237. }
  238. }
  239. }
  240. bool TerrainSample::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  241. {
  242. return false;
  243. }
  244. void TerrainSample::controlEvent(Control* control, EventType evt)
  245. {
  246. if (strcmp(control->getId(), "plusButton") == 0)
  247. {
  248. control->setVisible(false);
  249. _form->getControl("minusButton")->setVisible(true);
  250. _form->getControl("settings")->setVisible(true);
  251. _form->getControl("physicsSettings")->setVisible(true);
  252. _form->getControl("main")->setSize(_formSize.x, _formSize.y);
  253. _formVisible = true;
  254. }
  255. else if (strcmp(control->getId(), "minusButton") == 0)
  256. {
  257. control->setVisible(false);
  258. _form->getControl("plusButton")->setVisible(true);
  259. _form->getControl("settings")->setVisible(false);
  260. _form->getControl("physicsSettings")->setVisible(false);
  261. _form->getControl("main")->setSize(50, 50);
  262. _formVisible = false;
  263. }
  264. else if (strcmp(control->getId(), "wireframe") == 0)
  265. {
  266. _wireframe = static_cast<CheckBox*>(control)->isChecked();
  267. }
  268. else if (strcmp(control->getId(), "patches") == 0)
  269. {
  270. _terrain->setFlag(Terrain::DEBUG_PATCHES, static_cast<CheckBox*>(control)->isChecked());
  271. }
  272. else if (strcmp(control->getId(), "physics") == 0)
  273. {
  274. _debugPhysics = static_cast<CheckBox*>(control)->isChecked();
  275. }
  276. else if (strcmp(control->getId(), "lod") == 0)
  277. {
  278. _terrain->setFlag(Terrain::LEVEL_OF_DETAIL, static_cast<CheckBox*>(control)->isChecked());
  279. }
  280. else if (strcmp(control->getId(), "snapToGround") == 0)
  281. {
  282. _snapToGround = static_cast<CheckBox*>(control)->isChecked();
  283. if (_snapToGround)
  284. setScriptCameraSpeed(20, 80);
  285. else
  286. setScriptCameraSpeed(200, 800);
  287. }
  288. else if (strcmp(control->getId(), "dropSphere") == 0)
  289. {
  290. if (_mode == MODE_DROP_SPHERE)
  291. {
  292. _mode = MODE_LOOK;
  293. setMessage(NULL);
  294. }
  295. else
  296. {
  297. _mode = MODE_DROP_SPHERE;
  298. setMessage("Click on the terrain to drop a sphere.");
  299. }
  300. }
  301. else if (strcmp(control->getId(), "dropBox") == 0)
  302. {
  303. if (_mode == MODE_DROP_BOX)
  304. {
  305. _mode = MODE_LOOK;
  306. setMessage(NULL);
  307. }
  308. else
  309. {
  310. _mode = MODE_DROP_BOX;
  311. setMessage("Click on the terrain to drop a box.");
  312. }
  313. }
  314. else if (strcmp(control->getId(), "clearAll") == 0)
  315. {
  316. for (std::list<Node*>::iterator itr = _shapes.begin(); itr != _shapes.end(); ++itr)
  317. _scene->removeNode(*itr);
  318. _shapes.clear();
  319. }
  320. }
  321. void TerrainSample::setMessage(const char* message)
  322. {
  323. Label* label = static_cast<Label*>(_form->getControl("message"));
  324. label->setText(message ? message : "");
  325. _form->getControl("messageBox")->setVisible(message ? true : false);
  326. }
  327. Vector3 TerrainSample::getLightDirection0() const
  328. {
  329. return _directionalLight->getNode()->getForwardVectorView();
  330. }
  331. Vector3 TerrainSample::getLightColor0() const
  332. {
  333. return _directionalLight->getColor();
  334. }
  335. bool TerrainSample::resolveAutoBinding(const char* autoBinding, Node* node, MaterialParameter* parameter)
  336. {
  337. if (strcmp(autoBinding, "LIGHT_DIRECTION_0") == 0)
  338. {
  339. parameter->bindValue(this, &TerrainSample::getLightDirection0);
  340. return true;
  341. }
  342. else if (strcmp(autoBinding, "LIGHT_COLOR_0") == 0)
  343. {
  344. parameter->bindValue(this, &TerrainSample::getLightColor0);
  345. return true;
  346. }
  347. return false;
  348. }