Framework.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (C) 2009-2020, 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. ConfigSet config = DefaultConfigSet::get();
  19. config.set("window_fullscreen", true);
  20. config.set("rsrc_dataPaths", ".:../..");
  21. config.set("gr_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().getVolumetricFog().setFogParticleColor(Vec3(1.0f, 0.9f, 0.9f));
  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. if(in.getKey(KeyCode::BACKQUOTE) == 1)
  48. {
  49. setDisplayDeveloperConsole(!getDisplayDeveloperConsole());
  50. }
  51. if(!getDisplayDeveloperConsole())
  52. {
  53. in.hideCursor(true);
  54. in.lockCursor(true);
  55. // move the camera
  56. static MoveComponent* mover = &scene.getActiveCameraNode().getComponent<MoveComponent>();
  57. if(in.getKey(KeyCode::_1) == 1)
  58. {
  59. mover = &scene.getActiveCameraNode().getComponent<MoveComponent>();
  60. }
  61. if(in.getKey(KeyCode::F1) == 1)
  62. {
  63. static U mode = 0;
  64. mode = (mode + 1) % 3;
  65. if(mode == 0)
  66. {
  67. renderer.getDbg().setEnabled(false);
  68. }
  69. else if(mode == 1)
  70. {
  71. renderer.getDbg().setEnabled(true);
  72. renderer.getDbg().setDepthTestEnabled(true);
  73. renderer.getDbg().setDitheredDepthTestEnabled(false);
  74. }
  75. else
  76. {
  77. renderer.getDbg().setEnabled(true);
  78. renderer.getDbg().setDepthTestEnabled(false);
  79. renderer.getDbg().setDitheredDepthTestEnabled(true);
  80. }
  81. }
  82. if(in.getKey(KeyCode::F2) == 1)
  83. {
  84. // renderer.getDbg().flipFlags(DbgFlag::SPATIAL_COMPONENT);
  85. }
  86. if(in.getKey(KeyCode::UP))
  87. {
  88. mover->rotateLocalX(ROTATE_ANGLE);
  89. }
  90. if(in.getKey(KeyCode::DOWN))
  91. {
  92. mover->rotateLocalX(-ROTATE_ANGLE);
  93. }
  94. if(in.getKey(KeyCode::LEFT))
  95. {
  96. mover->rotateLocalY(ROTATE_ANGLE);
  97. }
  98. if(in.getKey(KeyCode::RIGHT))
  99. {
  100. mover->rotateLocalY(-ROTATE_ANGLE);
  101. }
  102. if(in.getKey(KeyCode::A))
  103. {
  104. mover->moveLocalX(-MOVE_DISTANCE);
  105. }
  106. if(in.getKey(KeyCode::D))
  107. {
  108. mover->moveLocalX(MOVE_DISTANCE);
  109. }
  110. if(in.getKey(KeyCode::C))
  111. {
  112. mover->moveLocalY(-MOVE_DISTANCE);
  113. }
  114. if(in.getKey(KeyCode::SPACE))
  115. {
  116. mover->moveLocalY(MOVE_DISTANCE);
  117. }
  118. if(in.getKey(KeyCode::W))
  119. {
  120. mover->moveLocalZ(-MOVE_DISTANCE);
  121. }
  122. if(in.getKey(KeyCode::S))
  123. {
  124. mover->moveLocalZ(MOVE_DISTANCE);
  125. }
  126. if(in.getKey(KeyCode::Q))
  127. {
  128. mover->rotateLocalZ(ROTATE_ANGLE);
  129. }
  130. if(in.getKey(KeyCode::E))
  131. {
  132. mover->rotateLocalZ(-ROTATE_ANGLE);
  133. }
  134. if(in.getKey(KeyCode::F12) == 1)
  135. {
  136. TracerSingleton::get().setEnabled(!TracerSingleton::get().getEnabled());
  137. }
  138. if(in.getMousePosition() != Vec2(0.0))
  139. {
  140. F32 angY =
  141. -ROTATE_ANGLE * in.getMousePosition().x() * MOUSE_SENSITIVITY * getMainRenderer().getAspectRatio();
  142. mover->rotateLocalY(angY);
  143. mover->rotateLocalX(ROTATE_ANGLE * in.getMousePosition().y() * MOUSE_SENSITIVITY);
  144. }
  145. }
  146. else
  147. {
  148. in.hideCursor(false);
  149. in.lockCursor(false);
  150. }
  151. return Error::NONE;
  152. }