Framework.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "Framework.h"
  6. using namespace anki;
  7. Error SampleApp::init(int argc, char** argv, CString sampleName)
  8. {
  9. if(!directoryExists("assets"))
  10. {
  11. ANKI_LOGE("Cannot find directory \"assets\". YOU ARE RUNNING THE SAMPLE FROM THE WRONG DIRECTORY. "
  12. "To run %s you have to navigate to the /path/to/anki/samples/%s and then execute it",
  13. argv[0],
  14. &sampleName[0]);
  15. return Error::USER_DATA;
  16. }
  17. // Init the super class
  18. Config config;
  19. config.set("window.fullscreenDesktopResolution", true);
  20. config.set("rsrc.dataPaths", ".:../..");
  21. config.set("window.debugContext", 0);
  22. ANKI_CHECK(config.setFromCommandLineArguments(argc, argv));
  23. ANKI_CHECK(App::init(config, allocAligned, nullptr));
  24. // Input
  25. getInput().lockCursor(true);
  26. getInput().hideCursor(true);
  27. getInput().moveCursor(Vec2(0.0f));
  28. // Some renderer stuff
  29. getMainRenderer().getOffscreenRenderer().getVolumetric().setFogParticleColor(Vec3(1.0f, 0.9f, 0.9f) * 0.0001f);
  30. ANKI_CHECK(sampleExtraInit());
  31. return Error::NONE;
  32. }
  33. Error SampleApp::userMainLoop(Bool& quit)
  34. {
  35. const F32 MOVE_DISTANCE = 0.1f;
  36. const F32 ROTATE_ANGLE = toRad(2.5f);
  37. const F32 MOUSE_SENSITIVITY = 9.0f;
  38. quit = false;
  39. SceneGraph& scene = getSceneGraph();
  40. Renderer& renderer = getMainRenderer().getOffscreenRenderer();
  41. Input& in = getInput();
  42. if(in.getKey(KeyCode::ESCAPE))
  43. {
  44. quit = true;
  45. return Error::NONE;
  46. }
  47. // move the camera
  48. static MoveComponent* mover = &scene.getActiveCameraNode().getComponent<MoveComponent>();
  49. if(in.getKey(KeyCode::_1) == 1)
  50. {
  51. mover = &scene.getActiveCameraNode().getComponent<MoveComponent>();
  52. }
  53. if(in.getKey(KeyCode::F1) == 1)
  54. {
  55. static U mode = 0;
  56. mode = (mode + 1) % 3;
  57. if(mode == 0)
  58. {
  59. renderer.getDbg().setEnabled(false);
  60. }
  61. else if(mode == 1)
  62. {
  63. renderer.getDbg().setEnabled(true);
  64. renderer.getDbg().setDepthTestEnabled(true);
  65. renderer.getDbg().setDitheredDepthTestEnabled(false);
  66. }
  67. else
  68. {
  69. renderer.getDbg().setEnabled(true);
  70. renderer.getDbg().setDepthTestEnabled(false);
  71. renderer.getDbg().setDitheredDepthTestEnabled(true);
  72. }
  73. }
  74. if(in.getKey(KeyCode::F2) == 1)
  75. {
  76. // renderer.getDbg().flipFlags(DbgFlag::SPATIAL_COMPONENT);
  77. }
  78. if(in.getKey(KeyCode::UP))
  79. {
  80. mover->rotateLocalX(ROTATE_ANGLE);
  81. }
  82. if(in.getKey(KeyCode::DOWN))
  83. {
  84. mover->rotateLocalX(-ROTATE_ANGLE);
  85. }
  86. if(in.getKey(KeyCode::LEFT))
  87. {
  88. mover->rotateLocalY(ROTATE_ANGLE);
  89. }
  90. if(in.getKey(KeyCode::RIGHT))
  91. {
  92. mover->rotateLocalY(-ROTATE_ANGLE);
  93. }
  94. if(in.getKey(KeyCode::A))
  95. {
  96. mover->moveLocalX(-MOVE_DISTANCE);
  97. }
  98. if(in.getKey(KeyCode::D))
  99. {
  100. mover->moveLocalX(MOVE_DISTANCE);
  101. }
  102. if(in.getKey(KeyCode::C))
  103. {
  104. mover->moveLocalY(-MOVE_DISTANCE);
  105. }
  106. if(in.getKey(KeyCode::SPACE))
  107. {
  108. mover->moveLocalY(MOVE_DISTANCE);
  109. }
  110. if(in.getKey(KeyCode::W))
  111. {
  112. mover->moveLocalZ(-MOVE_DISTANCE);
  113. }
  114. if(in.getKey(KeyCode::S))
  115. {
  116. mover->moveLocalZ(MOVE_DISTANCE);
  117. }
  118. if(in.getKey(KeyCode::Q))
  119. {
  120. mover->rotateLocalZ(ROTATE_ANGLE);
  121. }
  122. if(in.getKey(KeyCode::E))
  123. {
  124. mover->rotateLocalZ(-ROTATE_ANGLE);
  125. }
  126. if(in.getMousePosition() != Vec2(0.0))
  127. {
  128. F32 angY = -ROTATE_ANGLE * in.getMousePosition().x() * MOUSE_SENSITIVITY * getMainRenderer().getAspectRatio();
  129. mover->rotateLocalY(angY);
  130. mover->rotateLocalX(ROTATE_ANGLE * in.getMousePosition().y() * MOUSE_SENSITIVITY);
  131. }
  132. return Error::NONE;
  133. }