TerrainSample.cpp 13 KB

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