SampleApp.cpp 8.0 KB

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