SampleApp.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // Copyright (C) 2009-present, 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::userPreInit()
  8. {
  9. // Init the super class
  10. g_cvarWindowFullscreen = 1;
  11. #if !ANKI_OS_ANDROID
  12. String assetsDataPath;
  13. assetsDataPath.sprintf("%s/Samples/%s", ANKI_SOURCE_DIRECTORY, getApplicationName().cstr());
  14. if(!directoryExists(assetsDataPath))
  15. {
  16. ANKI_LOGE("Cannot find directory \"%s\". Have you moved the clone of the repository? I'll continue but expect issues", assetsDataPath.cstr());
  17. }
  18. else
  19. {
  20. g_cvarRsrcDataPaths = String().sprintf("%s|.anki,lua", assetsDataPath.cstr());
  21. }
  22. #endif
  23. ANKI_CHECK(CVarSet::getSingleton().setFromCommandLineArguments(m_argc - 1, m_argv + 1));
  24. return Error::kNone;
  25. }
  26. Error SampleApp::userMainLoop(Bool& quit, Second elapsedTime)
  27. {
  28. constexpr F32 ROTATE_ANGLE = toRad(2.5f);
  29. constexpr F32 MOUSE_SENSITIVITY = 5.0f;
  30. quit = false;
  31. SceneGraph& scene = SceneGraph::getSingleton();
  32. Renderer& renderer = Renderer::getSingleton();
  33. Input& in = Input::getSingleton();
  34. if(in.getKey(KeyCode::kEscape) > 0)
  35. {
  36. quit = true;
  37. return Error::kNone;
  38. }
  39. if(in.getKey(KeyCode::kGrave) == 1)
  40. {
  41. toggleDeveloperConsole();
  42. }
  43. if(getDeveloperConsoleEnabled())
  44. {
  45. return Error::kNone;
  46. }
  47. if(in.getKey(KeyCode::kY) == 1)
  48. {
  49. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "IndirectDiffuseClipmaps") ? "" : "IndirectDiffuseClipmaps");
  50. // g_shadowMappingPcssCVar = !g_shadowMappingPcssCVar;
  51. }
  52. if(in.getKey(KeyCode::kU) == 1)
  53. {
  54. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "Reflections") ? "" : "Reflections");
  55. }
  56. if(in.getKey(KeyCode::kI) == 1)
  57. {
  58. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "RtMaterialFetchDbg") ? "" : "RtMaterialFetchDbg");
  59. }
  60. if(in.getKey(KeyCode::kO) == 1)
  61. {
  62. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "HistoryLen") ? "" : "HistoryLen");
  63. }
  64. static Bool timeOfDay = false;
  65. if(in.getKey(KeyCode::kP) == 1 || timeOfDay)
  66. {
  67. timeOfDay = true;
  68. static F32 time = 8.0f;
  69. scene.getDirectionalLight()->setDirectionFromTimeOfDay(6, 25, time);
  70. time += 0.2f * F32(elapsedTime);
  71. if(time > 19.0)
  72. {
  73. time = 8.0f;
  74. }
  75. }
  76. if(in.getKey(KeyCode::kL) == 1)
  77. {
  78. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "MotionBlur") ? "" : "MotionBlur");
  79. }
  80. if(in.getKey(KeyCode::kH) == 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::kJ) == 1)
  102. {
  103. g_cvarGrVrs = !g_cvarGrVrs;
  104. }
  105. static Vec2 mousePosOn1stClick = in.getMousePositionNdc();
  106. if(in.getMouseButton(MouseButton::kRight) == 1)
  107. {
  108. // Re-init mouse pos
  109. mousePosOn1stClick = in.getMousePositionNdc();
  110. }
  111. if(in.getKey(KeyCode::kF1) == 1)
  112. {
  113. DbgOption options = renderer.getDbg().getOptions();
  114. static U mode = 0;
  115. mode = (mode + 1) % 3;
  116. if(mode == 0)
  117. {
  118. options &= ~DbgOption::kBoundingBoxes;
  119. }
  120. else if(mode == 1)
  121. {
  122. options |= DbgOption::kBoundingBoxes;
  123. options |= DbgOption::kDepthTest;
  124. }
  125. else
  126. {
  127. options |= DbgOption::kBoundingBoxes;
  128. options &= ~DbgOption::kDepthTest;
  129. }
  130. renderer.getDbg().setOptions(options);
  131. }
  132. if(in.getKey(KeyCode::kF11) == 1 && ANKI_TRACING_ENABLED)
  133. {
  134. g_cvarCoreTracingEnabled = !g_cvarCoreTracingEnabled;
  135. }
  136. if(in.getMouseButton(MouseButton::kRight) > 0 || in.hasTouchDevice())
  137. {
  138. in.hideCursor(true);
  139. // move the camera
  140. static SceneNode* mover = &scene.getActiveCameraNode();
  141. if(in.getKey(KeyCode::k1) == 1)
  142. {
  143. mover = &scene.getActiveCameraNode();
  144. }
  145. if(in.getKey(KeyCode::k2) == 1)
  146. {
  147. mover = &scene.findSceneNode("Spot");
  148. }
  149. if(in.getKey(KeyCode::kUp) > 0)
  150. {
  151. mover->rotateLocalX(ROTATE_ANGLE);
  152. }
  153. if(in.getKey(KeyCode::kDown) > 0)
  154. {
  155. mover->rotateLocalX(-ROTATE_ANGLE);
  156. }
  157. if(in.getKey(KeyCode::kLeft) > 0)
  158. {
  159. mover->rotateLocalY(ROTATE_ANGLE);
  160. }
  161. if(in.getKey(KeyCode::kRight) > 0)
  162. {
  163. mover->rotateLocalY(-ROTATE_ANGLE);
  164. }
  165. static F32 moveDistance = 0.1f;
  166. if(in.getMouseButton(MouseButton::kScrollUp) == 1)
  167. {
  168. moveDistance += 0.1f;
  169. moveDistance = min(moveDistance, 10.0f);
  170. }
  171. if(in.getMouseButton(MouseButton::kScrollDown) == 1)
  172. {
  173. moveDistance -= 0.1f;
  174. moveDistance = max(moveDistance, 0.1f);
  175. }
  176. if(in.getKey(KeyCode::kA) > 0)
  177. {
  178. mover->moveLocalX(-moveDistance);
  179. }
  180. if(in.getKey(KeyCode::kD) > 0)
  181. {
  182. mover->moveLocalX(moveDistance);
  183. }
  184. if(in.getKey(KeyCode::kQ) > 0)
  185. {
  186. mover->moveLocalY(-moveDistance);
  187. }
  188. if(in.getKey(KeyCode::kE) > 0)
  189. {
  190. mover->moveLocalY(moveDistance);
  191. }
  192. if(in.getKey(KeyCode::kW) > 0)
  193. {
  194. mover->moveLocalZ(-moveDistance);
  195. }
  196. if(in.getKey(KeyCode::kS) > 0)
  197. {
  198. mover->moveLocalZ(moveDistance);
  199. }
  200. const Vec2 velocity = in.getMousePositionNdc() - mousePosOn1stClick;
  201. in.moveMouseNdc(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(Mat3(angles));
  210. }
  211. static TouchPointer rotateCameraTouch = TouchPointer::kCount;
  212. static Vec2 rotateEventInitialPos = Vec2(0.0f);
  213. for(TouchPointer touch : EnumIterable<TouchPointer>())
  214. {
  215. if(rotateCameraTouch == TouchPointer::kCount && in.getTouchPointer(touch) == 1 && in.getTouchPointerNdcPosition(touch).x() > 0.1f)
  216. {
  217. rotateCameraTouch = touch;
  218. rotateEventInitialPos = in.getTouchPointerNdcPosition(touch) * NativeWindow::getSingleton().getAspectRatio();
  219. break;
  220. }
  221. }
  222. if(rotateCameraTouch != TouchPointer::kCount && in.getTouchPointer(rotateCameraTouch) == 0)
  223. {
  224. rotateCameraTouch = TouchPointer::kCount;
  225. }
  226. if(rotateCameraTouch != TouchPointer::kCount && in.getTouchPointer(rotateCameraTouch) > 1)
  227. {
  228. Vec2 velocity = in.getTouchPointerNdcPosition(rotateCameraTouch) * NativeWindow::getSingleton().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(Mat3(angles));
  236. }
  237. static TouchPointer moveCameraTouch = TouchPointer::kCount;
  238. static Vec2 moveEventInitialPos = Vec2(0.0f);
  239. for(TouchPointer touch : EnumIterable<TouchPointer>())
  240. {
  241. if(moveCameraTouch == TouchPointer::kCount && in.getTouchPointer(touch) == 1 && in.getTouchPointerNdcPosition(touch).x() < -0.1f)
  242. {
  243. moveCameraTouch = touch;
  244. moveEventInitialPos = in.getTouchPointerNdcPosition(touch) * NativeWindow::getSingleton().getAspectRatio();
  245. break;
  246. }
  247. }
  248. if(moveCameraTouch != TouchPointer::kCount && in.getTouchPointer(moveCameraTouch) == 0)
  249. {
  250. moveCameraTouch = TouchPointer::kCount;
  251. }
  252. if(moveCameraTouch != TouchPointer::kCount && in.getTouchPointer(moveCameraTouch) > 0)
  253. {
  254. Vec2 velocity = in.getTouchPointerNdcPosition(moveCameraTouch) * NativeWindow::getSingleton().getAspectRatio() - moveEventInitialPos;
  255. velocity *= 2.0f;
  256. mover->moveLocalX(moveDistance * velocity.x());
  257. mover->moveLocalZ(moveDistance * -velocity.y());
  258. }
  259. }
  260. else
  261. {
  262. in.hideCursor(false);
  263. }
  264. return Error::kNone;
  265. }