SceneLoadSample.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "SceneLoadSample.h"
  2. #include "SamplesGame.h"
  3. #if defined(ADD_SAMPLE)
  4. ADD_SAMPLE("Graphics", "Scene Loading", SceneLoadSample, 16);
  5. #endif
  6. SceneLoadSample::SceneLoadSample()
  7. : _font(NULL), _scene(NULL), _wireFrame(false)
  8. {
  9. }
  10. void SceneLoadSample::initialize()
  11. {
  12. // Create the font for drawing the framerate.
  13. _font = Font::create("res/ui/arial.gpb");
  14. _scene = Scene::load("res/common/sample.scene");
  15. // Update the aspect ratio for our scene's camera to match the current device resolution
  16. _scene->getActiveCamera()->setAspectRatio(getAspectRatio());
  17. // Visit all the nodes in the scene, drawing the models/mesh.
  18. _scene->visit(this, &SceneLoadSample::initializeMaterials);
  19. }
  20. bool SceneLoadSample::initializeMaterials(Node* node)
  21. {
  22. Model* model = node->getModel();
  23. if (model)
  24. {
  25. Material* material = model->getMaterial();
  26. // For this sample we will only bind a single light to each object in the scene.
  27. MaterialParameter* colorParam = material->getParameter("u_directionalLightColor[0]");
  28. colorParam->setValue(Vector3(0.75f, 0.75f, 0.75f));
  29. MaterialParameter* directionParam = material->getParameter("u_directionalLightDirection[0]");
  30. directionParam->setValue(Vector3(0, -1, 0));
  31. }
  32. return true;
  33. }
  34. void SceneLoadSample::finalize()
  35. {
  36. SAFE_RELEASE(_font);
  37. SAFE_RELEASE(_scene);
  38. }
  39. void SceneLoadSample::update(float elapsedTime)
  40. {
  41. }
  42. void SceneLoadSample::render(float elapsedTime)
  43. {
  44. // Clear the color and depth buffers
  45. clear(CLEAR_COLOR_DEPTH, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0);
  46. // Visit all the nodes in the scene, drawing the models/mesh.
  47. _scene->visit(this, &SceneLoadSample::drawScene);
  48. drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate());
  49. }
  50. void SceneLoadSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  51. {
  52. switch (evt)
  53. {
  54. case Touch::TOUCH_PRESS:
  55. if (x < 75 && y < 50)
  56. {
  57. // Toggle Vsync if the user touches the top left corner
  58. setVsync(!isVsync());
  59. return;
  60. }
  61. break;
  62. case Touch::TOUCH_RELEASE:
  63. break;
  64. case Touch::TOUCH_MOVE:
  65. break;
  66. };
  67. }
  68. void SceneLoadSample::keyEvent(Keyboard::KeyEvent evt, int key)
  69. {
  70. if (evt == Keyboard::KEY_PRESS)
  71. {
  72. switch (key)
  73. {
  74. case Keyboard::KEY_W:
  75. case Keyboard::KEY_CAPITAL_W:
  76. _wireFrame = !_wireFrame;
  77. break;
  78. }
  79. }
  80. }
  81. bool SceneLoadSample::drawScene(Node* node)
  82. {
  83. Model* model = node->getModel();
  84. if (model)
  85. model->draw(_wireFrame);
  86. return true;
  87. }