SampleApp.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <Samples/Common/SampleApp.h>
  6. using namespace anki;
  7. Error SampleApp::init(int argc, char** argv, CString sampleName)
  8. {
  9. HeapAllocator<U32> alloc(allocAligned, nullptr);
  10. // Init the super class
  11. m_config.init(allocAligned, nullptr);
  12. m_config.setWindowFullscreen(true);
  13. #if !ANKI_OS_ANDROID
  14. StringAuto mainDataPath(alloc, ANKI_SOURCE_DIRECTORY);
  15. StringAuto assetsDataPath(alloc);
  16. assetsDataPath.sprintf("%s/Samples/%s", ANKI_SOURCE_DIRECTORY, sampleName.cstr());
  17. if(!directoryExists(assetsDataPath))
  18. {
  19. ANKI_LOGE("Cannot find directory \"%s\". Have you moved the clone of the repository? "
  20. "I'll continue but expect issues",
  21. assetsDataPath.cstr());
  22. }
  23. else
  24. {
  25. m_config.setRsrcDataPaths(StringAuto(alloc).sprintf("%s:%s", mainDataPath.cstr(), assetsDataPath.cstr()));
  26. }
  27. #endif
  28. ANKI_CHECK(m_config.setFromCommandLineArguments(argc - 1, argv + 1));
  29. ANKI_CHECK(App::init(&m_config, argv[0], allocAligned, nullptr));
  30. ANKI_CHECK(sampleExtraInit());
  31. return Error::NONE;
  32. }
  33. Error SampleApp::userMainLoop(Bool& quit, Second elapsedTime)
  34. {
  35. constexpr F32 ROTATE_ANGLE = toRad(2.5f);
  36. constexpr F32 MOUSE_SENSITIVITY = 5.0f;
  37. quit = false;
  38. SceneGraph& scene = getSceneGraph();
  39. Renderer& renderer = getMainRenderer().getOffscreenRenderer();
  40. Input& in = getInput();
  41. if(in.getKey(KeyCode::ESCAPE))
  42. {
  43. quit = true;
  44. return Error::NONE;
  45. }
  46. if(in.getKey(KeyCode::BACKQUOTE) == 1)
  47. {
  48. setDisplayDeveloperConsole(!getDisplayDeveloperConsole());
  49. }
  50. if(in.getKey(KeyCode::Y) == 1)
  51. {
  52. renderer.setCurrentDebugRenderTarget(
  53. (renderer.getCurrentDebugRenderTarget() == "GBufferNormals") ? "" : "GBufferNormals");
  54. }
  55. if(in.getKey(KeyCode::U) == 1)
  56. {
  57. renderer.setCurrentDebugRenderTarget(
  58. (renderer.getCurrentDebugRenderTarget() == "IndirectDiffuse") ? "" : "IndirectDiffuse");
  59. }
  60. if(in.getKey(KeyCode::I) == 1)
  61. {
  62. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "SSR") ? "" : "SSR");
  63. }
  64. if(in.getKey(KeyCode::O) == 1)
  65. {
  66. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "SM_resolve") ? ""
  67. : "SM_resolve");
  68. }
  69. if(in.getKey(KeyCode::P) == 1)
  70. {
  71. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "VRS") ? "" : "VRS");
  72. }
  73. if(in.getKey(KeyCode::L) == 1)
  74. {
  75. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "MotionVectorsHistoryLength")
  76. ? ""
  77. : "MotionVectorsHistoryLength");
  78. }
  79. if(in.getKey(KeyCode::H) == 1)
  80. {
  81. static U32 pressCount = 0;
  82. CString rtName;
  83. switch(pressCount)
  84. {
  85. case 0:
  86. rtName = "RtShadows";
  87. break;
  88. case 1:
  89. rtName = "RtShadows1";
  90. break;
  91. case 2:
  92. rtName = "RtShadows2";
  93. break;
  94. default:
  95. rtName = "";
  96. }
  97. renderer.setCurrentDebugRenderTarget(rtName);
  98. pressCount = (pressCount + 1) % 4;
  99. }
  100. if(in.getKey(KeyCode::J) == 1)
  101. {
  102. m_config.setRVrs(!m_config.getRVrs());
  103. }
  104. static Vec2 mousePosOn1stClick = in.getMousePosition();
  105. if(in.getMouseButton(MouseButton::RIGHT) == 1)
  106. {
  107. // Re-init mouse pos
  108. mousePosOn1stClick = in.getMousePosition();
  109. }
  110. if(in.getMouseButton(MouseButton::RIGHT) || in.hasTouchDevice())
  111. {
  112. in.hideCursor(true);
  113. // move the camera
  114. static MoveComponent* mover = &scene.getActiveCameraNode().getFirstComponentOfType<MoveComponent>();
  115. if(in.getKey(KeyCode::_1) == 1)
  116. {
  117. mover = &scene.getActiveCameraNode().getFirstComponentOfType<MoveComponent>();
  118. }
  119. if(in.getKey(KeyCode::F1) == 1)
  120. {
  121. static U mode = 0;
  122. mode = (mode + 1) % 3;
  123. if(mode == 0)
  124. {
  125. getConfig().setRDbgEnabled(false);
  126. }
  127. else if(mode == 1)
  128. {
  129. getConfig().setRDbgEnabled(true);
  130. renderer.getDbg().setDepthTestEnabled(true);
  131. renderer.getDbg().setDitheredDepthTestEnabled(false);
  132. }
  133. else
  134. {
  135. getConfig().setRDbgEnabled(true);
  136. renderer.getDbg().setDepthTestEnabled(false);
  137. renderer.getDbg().setDitheredDepthTestEnabled(true);
  138. }
  139. }
  140. if(in.getKey(KeyCode::F2) == 1)
  141. {
  142. // renderer.getDbg().flipFlags(DbgFlag::SPATIAL_COMPONENT);
  143. }
  144. if(in.getKey(KeyCode::UP))
  145. {
  146. mover->rotateLocalX(ROTATE_ANGLE);
  147. }
  148. if(in.getKey(KeyCode::DOWN))
  149. {
  150. mover->rotateLocalX(-ROTATE_ANGLE);
  151. }
  152. if(in.getKey(KeyCode::LEFT))
  153. {
  154. mover->rotateLocalY(ROTATE_ANGLE);
  155. }
  156. if(in.getKey(KeyCode::RIGHT))
  157. {
  158. mover->rotateLocalY(-ROTATE_ANGLE);
  159. }
  160. static F32 moveDistance = 0.1f;
  161. if(in.getMouseButton(MouseButton::SCROLL_UP) == 1)
  162. {
  163. moveDistance += 0.1f;
  164. moveDistance = min(moveDistance, 10.0f);
  165. }
  166. if(in.getMouseButton(MouseButton::SCROLL_DOWN) == 1)
  167. {
  168. moveDistance -= 0.1f;
  169. moveDistance = max(moveDistance, 0.1f);
  170. }
  171. if(in.getKey(KeyCode::A))
  172. {
  173. mover->moveLocalX(-moveDistance);
  174. }
  175. if(in.getKey(KeyCode::D))
  176. {
  177. mover->moveLocalX(moveDistance);
  178. }
  179. if(in.getKey(KeyCode::Q))
  180. {
  181. mover->moveLocalY(-moveDistance);
  182. }
  183. if(in.getKey(KeyCode::E))
  184. {
  185. mover->moveLocalY(moveDistance);
  186. }
  187. if(in.getKey(KeyCode::W))
  188. {
  189. mover->moveLocalZ(-moveDistance);
  190. }
  191. if(in.getKey(KeyCode::S))
  192. {
  193. mover->moveLocalZ(moveDistance);
  194. }
  195. if(in.getKey(KeyCode::F12) == 1 && ANKI_ENABLE_TRACE)
  196. {
  197. TracerSingleton::get().setEnabled(!TracerSingleton::get().getEnabled());
  198. }
  199. const Vec2 velocity = in.getMousePosition() - mousePosOn1stClick;
  200. in.moveCursor(mousePosOn1stClick);
  201. if(velocity != Vec2(0.0))
  202. {
  203. Euler angles(mover->getLocalRotation().getRotationPart());
  204. angles.x() += velocity.y() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  205. angles.x() = clamp(angles.x(), toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  206. angles.y() += -velocity.x() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  207. angles.z() = 0.0f;
  208. mover->setLocalRotation(Mat3x4(Vec3(0.0f), angles));
  209. }
  210. static TouchPointer rotateCameraTouch = TouchPointer::COUNT;
  211. static Vec2 rotateEventInitialPos = Vec2(0.0f);
  212. for(TouchPointer touch : EnumIterable<TouchPointer>())
  213. {
  214. if(rotateCameraTouch == TouchPointer::COUNT && in.getTouchPointer(touch) == 1
  215. && in.getTouchPointerNdcPosition(touch).x() > 0.1f)
  216. {
  217. rotateCameraTouch = touch;
  218. rotateEventInitialPos = in.getTouchPointerNdcPosition(touch) * getWindow().getAspectRatio();
  219. break;
  220. }
  221. }
  222. if(rotateCameraTouch != TouchPointer::COUNT && in.getTouchPointer(rotateCameraTouch) == 0)
  223. {
  224. rotateCameraTouch = TouchPointer::COUNT;
  225. }
  226. if(rotateCameraTouch != TouchPointer::COUNT && in.getTouchPointer(rotateCameraTouch) > 1)
  227. {
  228. Vec2 velocity =
  229. in.getTouchPointerNdcPosition(rotateCameraTouch) * getWindow().getAspectRatio() - rotateEventInitialPos;
  230. velocity *= 0.3f;
  231. Euler angles(mover->getLocalRotation().getRotationPart());
  232. angles.x() += velocity.y() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  233. angles.x() = clamp(angles.x(), toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  234. angles.y() += -velocity.x() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  235. angles.z() = 0.0f;
  236. mover->setLocalRotation(Mat3x4(Vec3(0.0f), angles));
  237. }
  238. static TouchPointer moveCameraTouch = TouchPointer::COUNT;
  239. static Vec2 moveEventInitialPos = Vec2(0.0f);
  240. for(TouchPointer touch : EnumIterable<TouchPointer>())
  241. {
  242. if(moveCameraTouch == TouchPointer::COUNT && in.getTouchPointer(touch) == 1
  243. && in.getTouchPointerNdcPosition(touch).x() < -0.1f)
  244. {
  245. moveCameraTouch = touch;
  246. moveEventInitialPos = in.getTouchPointerNdcPosition(touch) * getWindow().getAspectRatio();
  247. break;
  248. }
  249. }
  250. if(moveCameraTouch != TouchPointer::COUNT && in.getTouchPointer(moveCameraTouch) == 0)
  251. {
  252. moveCameraTouch = TouchPointer::COUNT;
  253. }
  254. if(moveCameraTouch != TouchPointer::COUNT && in.getTouchPointer(moveCameraTouch) > 0)
  255. {
  256. Vec2 velocity =
  257. in.getTouchPointerNdcPosition(moveCameraTouch) * getWindow().getAspectRatio() - moveEventInitialPos;
  258. velocity *= 2.0f;
  259. mover->moveLocalX(moveDistance * velocity.x());
  260. mover->moveLocalZ(moveDistance * -velocity.y());
  261. }
  262. }
  263. else
  264. {
  265. in.hideCursor(false);
  266. }
  267. return Error::NONE;
  268. }