SampleApp.cpp 8.1 KB

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