TerrainSample.cpp 12 KB

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