TerrainSample.cpp 13 KB

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