2
0

Framework.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. HeapAllocator<U32> alloc(allocAligned, nullptr);
  10. StringAuto mainDataPath(alloc, ANKI_SOURCE_DIRECTORY);
  11. StringAuto assetsDataPath(alloc);
  12. assetsDataPath.sprintf("%s/samples/%s", ANKI_SOURCE_DIRECTORY, sampleName.cstr());
  13. if(!directoryExists(assetsDataPath))
  14. {
  15. ANKI_LOGE("Cannot find directory \"%s\". Have you moved the clone of the repository?", assetsDataPath.cstr());
  16. return Error::USER_DATA;
  17. }
  18. printf("%s\n", StringAuto(alloc).sprintf("%s:%s", mainDataPath.cstr(), assetsDataPath.cstr()).cstr());
  19. // Init the super class
  20. ConfigSet config = DefaultConfigSet::get();
  21. config.set("window_fullscreen", true);
  22. config.set("rsrc_dataPaths", StringAuto(alloc).sprintf("%s:%s", mainDataPath.cstr(), assetsDataPath.cstr()));
  23. config.set("gr_debugContext", 0);
  24. ANKI_CHECK(config.setFromCommandLineArguments(argc, argv));
  25. ANKI_CHECK(App::init(config, allocAligned, nullptr));
  26. // Input
  27. getInput().lockCursor(true);
  28. getInput().hideCursor(true);
  29. getInput().moveCursor(Vec2(0.0f));
  30. // Some renderer stuff
  31. getMainRenderer().getOffscreenRenderer().getVolumetricFog().setFogParticleColor(Vec3(1.0f, 0.9f, 0.9f));
  32. ANKI_CHECK(sampleExtraInit());
  33. return Error::NONE;
  34. }
  35. Error SampleApp::userMainLoop(Bool& quit)
  36. {
  37. const F32 MOVE_DISTANCE = 0.1f;
  38. const F32 ROTATE_ANGLE = toRad(2.5f);
  39. const F32 MOUSE_SENSITIVITY = 9.0f;
  40. quit = false;
  41. SceneGraph& scene = getSceneGraph();
  42. Renderer& renderer = getMainRenderer().getOffscreenRenderer();
  43. Input& in = getInput();
  44. if(in.getKey(KeyCode::ESCAPE))
  45. {
  46. quit = true;
  47. return Error::NONE;
  48. }
  49. if(in.getKey(KeyCode::BACKQUOTE) == 1)
  50. {
  51. setDisplayDeveloperConsole(!getDisplayDeveloperConsole());
  52. }
  53. if(in.getKey(KeyCode::U) == 1)
  54. {
  55. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "SSGI") ? "" : "SSGI");
  56. }
  57. if(in.getKey(KeyCode::I) == 1)
  58. {
  59. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "SSR") ? "" : "SSR");
  60. }
  61. if(in.getKey(KeyCode::O) == 1)
  62. {
  63. renderer.setCurrentDebugRenderTarget(
  64. (renderer.getCurrentDebugRenderTarget() == "SM_resolve") ? "" : "SM_resolve");
  65. }
  66. if(!getDisplayDeveloperConsole())
  67. {
  68. in.hideCursor(true);
  69. in.lockCursor(true);
  70. // move the camera
  71. static MoveComponent* mover = &scene.getActiveCameraNode().getComponent<MoveComponent>();
  72. if(in.getKey(KeyCode::_1) == 1)
  73. {
  74. mover = &scene.getActiveCameraNode().getComponent<MoveComponent>();
  75. }
  76. if(in.getKey(KeyCode::F1) == 1)
  77. {
  78. static U mode = 0;
  79. mode = (mode + 1) % 3;
  80. if(mode == 0)
  81. {
  82. renderer.getDbg().setEnabled(false);
  83. }
  84. else if(mode == 1)
  85. {
  86. renderer.getDbg().setEnabled(true);
  87. renderer.getDbg().setDepthTestEnabled(true);
  88. renderer.getDbg().setDitheredDepthTestEnabled(false);
  89. }
  90. else
  91. {
  92. renderer.getDbg().setEnabled(true);
  93. renderer.getDbg().setDepthTestEnabled(false);
  94. renderer.getDbg().setDitheredDepthTestEnabled(true);
  95. }
  96. }
  97. if(in.getKey(KeyCode::F2) == 1)
  98. {
  99. // renderer.getDbg().flipFlags(DbgFlag::SPATIAL_COMPONENT);
  100. }
  101. if(in.getKey(KeyCode::UP))
  102. {
  103. mover->rotateLocalX(ROTATE_ANGLE);
  104. }
  105. if(in.getKey(KeyCode::DOWN))
  106. {
  107. mover->rotateLocalX(-ROTATE_ANGLE);
  108. }
  109. if(in.getKey(KeyCode::LEFT))
  110. {
  111. mover->rotateLocalY(ROTATE_ANGLE);
  112. }
  113. if(in.getKey(KeyCode::RIGHT))
  114. {
  115. mover->rotateLocalY(-ROTATE_ANGLE);
  116. }
  117. if(in.getKey(KeyCode::A))
  118. {
  119. mover->moveLocalX(-MOVE_DISTANCE);
  120. }
  121. if(in.getKey(KeyCode::D))
  122. {
  123. mover->moveLocalX(MOVE_DISTANCE);
  124. }
  125. if(in.getKey(KeyCode::C))
  126. {
  127. mover->moveLocalY(-MOVE_DISTANCE);
  128. }
  129. if(in.getKey(KeyCode::SPACE))
  130. {
  131. mover->moveLocalY(MOVE_DISTANCE);
  132. }
  133. if(in.getKey(KeyCode::W))
  134. {
  135. mover->moveLocalZ(-MOVE_DISTANCE);
  136. }
  137. if(in.getKey(KeyCode::S))
  138. {
  139. mover->moveLocalZ(MOVE_DISTANCE);
  140. }
  141. if(in.getKey(KeyCode::Q))
  142. {
  143. mover->rotateLocalZ(ROTATE_ANGLE);
  144. }
  145. if(in.getKey(KeyCode::E))
  146. {
  147. mover->rotateLocalZ(-ROTATE_ANGLE);
  148. }
  149. if(in.getKey(KeyCode::F12) == 1 && ANKI_ENABLE_TRACE)
  150. {
  151. TracerSingleton::get().setEnabled(!TracerSingleton::get().getEnabled());
  152. }
  153. if(in.getMousePosition() != Vec2(0.0))
  154. {
  155. F32 angY =
  156. -ROTATE_ANGLE * in.getMousePosition().x() * MOUSE_SENSITIVITY * getMainRenderer().getAspectRatio();
  157. mover->rotateLocalY(angY);
  158. mover->rotateLocalX(ROTATE_ANGLE * in.getMousePosition().y() * MOUSE_SENSITIVITY);
  159. }
  160. }
  161. else
  162. {
  163. in.hideCursor(false);
  164. in.lockCursor(false);
  165. }
  166. return Error::NONE;
  167. }