SampleApp.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 ANKI_TRACING_ENABLED
  133. if(in.getKey(KeyCode::kF11) == 1)
  134. {
  135. g_cvarCoreTracingEnabled = !g_cvarCoreTracingEnabled;
  136. }
  137. #endif
  138. if(in.getMouseButton(MouseButton::kRight) > 0 || in.hasTouchDevice())
  139. {
  140. in.hideCursor(true);
  141. // move the camera
  142. static SceneNode* mover = &scene.getActiveCameraNode();
  143. if(in.getKey(KeyCode::k1) == 1)
  144. {
  145. mover = &scene.getActiveCameraNode();
  146. }
  147. if(in.getKey(KeyCode::k2) == 1)
  148. {
  149. mover = &scene.findSceneNode("Spot");
  150. }
  151. if(in.getKey(KeyCode::kUp) > 0)
  152. {
  153. mover->rotateLocalX(ROTATE_ANGLE);
  154. }
  155. if(in.getKey(KeyCode::kDown) > 0)
  156. {
  157. mover->rotateLocalX(-ROTATE_ANGLE);
  158. }
  159. if(in.getKey(KeyCode::kLeft) > 0)
  160. {
  161. mover->rotateLocalY(ROTATE_ANGLE);
  162. }
  163. if(in.getKey(KeyCode::kRight) > 0)
  164. {
  165. mover->rotateLocalY(-ROTATE_ANGLE);
  166. }
  167. static F32 moveDistance = 0.1f;
  168. if(in.getMouseButton(MouseButton::kScrollUp) == 1)
  169. {
  170. moveDistance += 0.1f;
  171. moveDistance = min(moveDistance, 10.0f);
  172. }
  173. if(in.getMouseButton(MouseButton::kScrollDown) == 1)
  174. {
  175. moveDistance -= 0.1f;
  176. moveDistance = max(moveDistance, 0.1f);
  177. }
  178. if(in.getKey(KeyCode::kA) > 0)
  179. {
  180. mover->moveLocalX(-moveDistance);
  181. }
  182. if(in.getKey(KeyCode::kD) > 0)
  183. {
  184. mover->moveLocalX(moveDistance);
  185. }
  186. if(in.getKey(KeyCode::kQ) > 0)
  187. {
  188. mover->moveLocalY(-moveDistance);
  189. }
  190. if(in.getKey(KeyCode::kE) > 0)
  191. {
  192. mover->moveLocalY(moveDistance);
  193. }
  194. if(in.getKey(KeyCode::kW) > 0)
  195. {
  196. mover->moveLocalZ(-moveDistance);
  197. }
  198. if(in.getKey(KeyCode::kS) > 0)
  199. {
  200. mover->moveLocalZ(moveDistance);
  201. }
  202. const Vec2 velocity = in.getMousePositionNdc() - mousePosOn1stClick;
  203. in.moveMouseNdc(mousePosOn1stClick);
  204. if(velocity != Vec2(0.0))
  205. {
  206. Euler angles(mover->getLocalRotation().getRotationPart());
  207. angles.x += velocity.y * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  208. angles.x = clamp(angles.x, toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  209. angles.y += -velocity.x * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  210. angles.z = 0.0f;
  211. mover->setLocalRotation(Mat3(angles));
  212. }
  213. static TouchPointer rotateCameraTouch = TouchPointer::kCount;
  214. static Vec2 rotateEventInitialPos = Vec2(0.0f);
  215. for(TouchPointer touch : EnumIterable<TouchPointer>())
  216. {
  217. if(rotateCameraTouch == TouchPointer::kCount && in.getTouchPointer(touch) == 1 && in.getTouchPointerNdcPosition(touch).x > 0.1f)
  218. {
  219. rotateCameraTouch = touch;
  220. rotateEventInitialPos = in.getTouchPointerNdcPosition(touch) * NativeWindow::getSingleton().getAspectRatio();
  221. break;
  222. }
  223. }
  224. if(rotateCameraTouch != TouchPointer::kCount && in.getTouchPointer(rotateCameraTouch) == 0)
  225. {
  226. rotateCameraTouch = TouchPointer::kCount;
  227. }
  228. if(rotateCameraTouch != TouchPointer::kCount && in.getTouchPointer(rotateCameraTouch) > 1)
  229. {
  230. Vec2 velocity = in.getTouchPointerNdcPosition(rotateCameraTouch) * NativeWindow::getSingleton().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(Mat3(angles));
  238. }
  239. static TouchPointer moveCameraTouch = TouchPointer::kCount;
  240. static Vec2 moveEventInitialPos = Vec2(0.0f);
  241. for(TouchPointer touch : EnumIterable<TouchPointer>())
  242. {
  243. if(moveCameraTouch == TouchPointer::kCount && in.getTouchPointer(touch) == 1 && in.getTouchPointerNdcPosition(touch).x < -0.1f)
  244. {
  245. moveCameraTouch = touch;
  246. moveEventInitialPos = in.getTouchPointerNdcPosition(touch) * NativeWindow::getSingleton().getAspectRatio();
  247. break;
  248. }
  249. }
  250. if(moveCameraTouch != TouchPointer::kCount && in.getTouchPointer(moveCameraTouch) == 0)
  251. {
  252. moveCameraTouch = TouchPointer::kCount;
  253. }
  254. if(moveCameraTouch != TouchPointer::kCount && in.getTouchPointer(moveCameraTouch) > 0)
  255. {
  256. Vec2 velocity = in.getTouchPointerNdcPosition(moveCameraTouch) * NativeWindow::getSingleton().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::kNone;
  267. }