SampleApp.cpp 8.2 KB

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