LoadSceneSample.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "LoadSceneSample.h"
  2. #include "SamplesGame.h"
  3. #if defined(ADD_SAMPLE)
  4. ADD_SAMPLE("Scene", "Load Scene", LoadSceneSample, 2);
  5. #endif
  6. LoadSceneSample::LoadSceneSample()
  7. : _font(NULL), _scene(NULL), _lightNode(NULL), _wireFrame(false)
  8. {
  9. }
  10. void LoadSceneSample::initialize()
  11. {
  12. // Create the font for drawing the framerate.
  13. _font = Font::create("res/common/arial18.gpb");
  14. _scene = Scene::load("res/common/sample.scene");
  15. // Find the light node
  16. _lightNode = _scene->findNode("directionalLight");
  17. // Update the aspect ratio for our scene's camera to match the current device resolution
  18. _scene->getActiveCamera()->setAspectRatio(getAspectRatio());
  19. _scene->visit(this, &LoadSceneSample::bindLights);
  20. }
  21. void LoadSceneSample::finalize()
  22. {
  23. SAFE_RELEASE(_font);
  24. SAFE_RELEASE(_scene);
  25. }
  26. void LoadSceneSample::update(float elapsedTime)
  27. {
  28. }
  29. void LoadSceneSample::render(float elapsedTime)
  30. {
  31. // Clear the color and depth buffers
  32. clear(CLEAR_COLOR_DEPTH, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0);
  33. // Visit all the nodes in the scene, drawing the models/mesh.
  34. _scene->visit(this, &LoadSceneSample::drawScene);
  35. drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate());
  36. }
  37. void LoadSceneSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  38. {
  39. switch (evt)
  40. {
  41. case Touch::TOUCH_PRESS:
  42. if (x < 75 && y < 50)
  43. {
  44. // Toggle Vsync if the user touches the top left corner
  45. setVsync(!isVsync());
  46. return;
  47. }
  48. break;
  49. case Touch::TOUCH_RELEASE:
  50. break;
  51. case Touch::TOUCH_MOVE:
  52. break;
  53. };
  54. }
  55. void LoadSceneSample::keyEvent(Keyboard::KeyEvent evt, int key)
  56. {
  57. if (evt == Keyboard::KEY_PRESS)
  58. {
  59. switch (key)
  60. {
  61. case Keyboard::KEY_W:
  62. case Keyboard::KEY_CAPITAL_W:
  63. _wireFrame = !_wireFrame;
  64. break;
  65. }
  66. }
  67. }
  68. bool LoadSceneSample::drawScene(Node* node)
  69. {
  70. Model* model = node->getModel();
  71. if (model)
  72. model->draw(_wireFrame);
  73. return true;
  74. }
  75. bool LoadSceneSample::bindLights(Node* node)
  76. {
  77. Model* model = node->getModel();
  78. if (model)
  79. {
  80. Material* material = model->getMaterial();
  81. if (material)
  82. {
  83. MaterialParameter* ambientColorParam = material->getParameter("u_ambientColor");
  84. if (ambientColorParam)
  85. {
  86. ambientColorParam->setValue(_scene->getAmbientColor());
  87. }
  88. if (_lightNode && _lightNode->getLight())
  89. {
  90. MaterialParameter* lightDirectionParam = material->getParameter("u_lightDirection");
  91. MaterialParameter* lightColorParam = material->getParameter("u_lightColor");
  92. if (lightDirectionParam)
  93. {
  94. lightDirectionParam->bindValue(_lightNode, &Node::getForwardVectorView);
  95. }
  96. if (lightColorParam)
  97. {
  98. lightColorParam->setValue(_lightNode->getLight()->getColor());
  99. }
  100. }
  101. }
  102. }
  103. return true;
  104. }