SampleApp.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. static U32 idx = 3;
  72. ++idx;
  73. idx %= 4;
  74. if(idx == 0)
  75. {
  76. renderer.setCurrentDebugRenderTarget("IndirectDiffuseVrsSri");
  77. }
  78. else if(idx == 1)
  79. {
  80. renderer.setCurrentDebugRenderTarget("VrsSriDownscaled");
  81. }
  82. else if(idx == 2)
  83. {
  84. renderer.setCurrentDebugRenderTarget("VrsSri");
  85. }
  86. else
  87. {
  88. renderer.setCurrentDebugRenderTarget("");
  89. }
  90. }
  91. if(in.getKey(KeyCode::L) == 1)
  92. {
  93. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "MotionVectorsHistoryLength")
  94. ? ""
  95. : "MotionVectorsHistoryLength");
  96. }
  97. if(in.getKey(KeyCode::H) == 1)
  98. {
  99. static U32 pressCount = 0;
  100. CString rtName;
  101. switch(pressCount)
  102. {
  103. case 0:
  104. rtName = "RtShadows";
  105. break;
  106. case 1:
  107. rtName = "RtShadows1";
  108. break;
  109. case 2:
  110. rtName = "RtShadows2";
  111. break;
  112. default:
  113. rtName = "";
  114. }
  115. renderer.setCurrentDebugRenderTarget(rtName);
  116. pressCount = (pressCount + 1) % 4;
  117. }
  118. if(in.getKey(KeyCode::J) == 1)
  119. {
  120. m_config.setRVrs(!m_config.getRVrs());
  121. }
  122. static Vec2 mousePosOn1stClick = in.getMousePosition();
  123. if(in.getMouseButton(MouseButton::RIGHT) == 1)
  124. {
  125. // Re-init mouse pos
  126. mousePosOn1stClick = in.getMousePosition();
  127. }
  128. if(in.getMouseButton(MouseButton::RIGHT) || in.hasTouchDevice())
  129. {
  130. in.hideCursor(true);
  131. // move the camera
  132. static MoveComponent* mover = &scene.getActiveCameraNode().getFirstComponentOfType<MoveComponent>();
  133. if(in.getKey(KeyCode::_1) == 1)
  134. {
  135. mover = &scene.getActiveCameraNode().getFirstComponentOfType<MoveComponent>();
  136. }
  137. if(in.getKey(KeyCode::F1) == 1)
  138. {
  139. static U mode = 0;
  140. mode = (mode + 1) % 3;
  141. if(mode == 0)
  142. {
  143. getConfig().setRDbgEnabled(false);
  144. }
  145. else if(mode == 1)
  146. {
  147. getConfig().setRDbgEnabled(true);
  148. renderer.getDbg().setDepthTestEnabled(true);
  149. renderer.getDbg().setDitheredDepthTestEnabled(false);
  150. }
  151. else
  152. {
  153. getConfig().setRDbgEnabled(true);
  154. renderer.getDbg().setDepthTestEnabled(false);
  155. renderer.getDbg().setDitheredDepthTestEnabled(true);
  156. }
  157. }
  158. if(in.getKey(KeyCode::F2) == 1)
  159. {
  160. // renderer.getDbg().flipFlags(DbgFlag::SPATIAL_COMPONENT);
  161. }
  162. if(in.getKey(KeyCode::UP))
  163. {
  164. mover->rotateLocalX(ROTATE_ANGLE);
  165. }
  166. if(in.getKey(KeyCode::DOWN))
  167. {
  168. mover->rotateLocalX(-ROTATE_ANGLE);
  169. }
  170. if(in.getKey(KeyCode::LEFT))
  171. {
  172. mover->rotateLocalY(ROTATE_ANGLE);
  173. }
  174. if(in.getKey(KeyCode::RIGHT))
  175. {
  176. mover->rotateLocalY(-ROTATE_ANGLE);
  177. }
  178. static F32 moveDistance = 0.1f;
  179. if(in.getMouseButton(MouseButton::SCROLL_UP) == 1)
  180. {
  181. moveDistance += 0.1f;
  182. moveDistance = min(moveDistance, 10.0f);
  183. }
  184. if(in.getMouseButton(MouseButton::SCROLL_DOWN) == 1)
  185. {
  186. moveDistance -= 0.1f;
  187. moveDistance = max(moveDistance, 0.1f);
  188. }
  189. if(in.getKey(KeyCode::A))
  190. {
  191. mover->moveLocalX(-moveDistance);
  192. }
  193. if(in.getKey(KeyCode::D))
  194. {
  195. mover->moveLocalX(moveDistance);
  196. }
  197. if(in.getKey(KeyCode::Q))
  198. {
  199. mover->moveLocalY(-moveDistance);
  200. }
  201. if(in.getKey(KeyCode::E))
  202. {
  203. mover->moveLocalY(moveDistance);
  204. }
  205. if(in.getKey(KeyCode::W))
  206. {
  207. mover->moveLocalZ(-moveDistance);
  208. }
  209. if(in.getKey(KeyCode::S))
  210. {
  211. mover->moveLocalZ(moveDistance);
  212. }
  213. if(in.getKey(KeyCode::F12) == 1 && ANKI_ENABLE_TRACE)
  214. {
  215. TracerSingleton::get().setEnabled(!TracerSingleton::get().getEnabled());
  216. }
  217. const Vec2 velocity = in.getMousePosition() - mousePosOn1stClick;
  218. in.moveCursor(mousePosOn1stClick);
  219. if(velocity != Vec2(0.0))
  220. {
  221. Euler angles(mover->getLocalRotation().getRotationPart());
  222. angles.x() += velocity.y() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  223. angles.x() = clamp(angles.x(), toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  224. angles.y() += -velocity.x() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  225. angles.z() = 0.0f;
  226. mover->setLocalRotation(Mat3x4(Vec3(0.0f), angles));
  227. }
  228. static TouchPointer rotateCameraTouch = TouchPointer::COUNT;
  229. static Vec2 rotateEventInitialPos = Vec2(0.0f);
  230. for(TouchPointer touch : EnumIterable<TouchPointer>())
  231. {
  232. if(rotateCameraTouch == TouchPointer::COUNT && in.getTouchPointer(touch) == 1
  233. && in.getTouchPointerNdcPosition(touch).x() > 0.1f)
  234. {
  235. rotateCameraTouch = touch;
  236. rotateEventInitialPos = in.getTouchPointerNdcPosition(touch) * getWindow().getAspectRatio();
  237. break;
  238. }
  239. }
  240. if(rotateCameraTouch != TouchPointer::COUNT && in.getTouchPointer(rotateCameraTouch) == 0)
  241. {
  242. rotateCameraTouch = TouchPointer::COUNT;
  243. }
  244. if(rotateCameraTouch != TouchPointer::COUNT && in.getTouchPointer(rotateCameraTouch) > 1)
  245. {
  246. Vec2 velocity =
  247. in.getTouchPointerNdcPosition(rotateCameraTouch) * getWindow().getAspectRatio() - rotateEventInitialPos;
  248. velocity *= 0.3f;
  249. Euler angles(mover->getLocalRotation().getRotationPart());
  250. angles.x() += velocity.y() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  251. angles.x() = clamp(angles.x(), toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  252. angles.y() += -velocity.x() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  253. angles.z() = 0.0f;
  254. mover->setLocalRotation(Mat3x4(Vec3(0.0f), angles));
  255. }
  256. static TouchPointer moveCameraTouch = TouchPointer::COUNT;
  257. static Vec2 moveEventInitialPos = Vec2(0.0f);
  258. for(TouchPointer touch : EnumIterable<TouchPointer>())
  259. {
  260. if(moveCameraTouch == TouchPointer::COUNT && in.getTouchPointer(touch) == 1
  261. && in.getTouchPointerNdcPosition(touch).x() < -0.1f)
  262. {
  263. moveCameraTouch = touch;
  264. moveEventInitialPos = in.getTouchPointerNdcPosition(touch) * getWindow().getAspectRatio();
  265. break;
  266. }
  267. }
  268. if(moveCameraTouch != TouchPointer::COUNT && in.getTouchPointer(moveCameraTouch) == 0)
  269. {
  270. moveCameraTouch = TouchPointer::COUNT;
  271. }
  272. if(moveCameraTouch != TouchPointer::COUNT && in.getTouchPointer(moveCameraTouch) > 0)
  273. {
  274. Vec2 velocity =
  275. in.getTouchPointerNdcPosition(moveCameraTouch) * getWindow().getAspectRatio() - moveEventInitialPos;
  276. velocity *= 2.0f;
  277. mover->moveLocalX(moveDistance * velocity.x());
  278. mover->moveLocalZ(moveDistance * -velocity.y());
  279. }
  280. }
  281. else
  282. {
  283. in.hideCursor(false);
  284. }
  285. return Error::NONE;
  286. }