SampleApp.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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::kJ) == 1)
  81. {
  82. g_cvarGrVrs = !g_cvarGrVrs;
  83. }
  84. static Vec2 mousePosOn1stClick = in.getMousePositionNdc();
  85. if(in.getMouseButton(MouseButton::kRight) == 1)
  86. {
  87. // Re-init mouse pos
  88. mousePosOn1stClick = in.getMousePositionNdc();
  89. }
  90. #if ANKI_TRACING_ENABLED
  91. if(in.getKey(KeyCode::kF11) == 1)
  92. {
  93. g_cvarCoreTracingEnabled = !g_cvarCoreTracingEnabled;
  94. }
  95. #endif
  96. if(in.getMouseButton(MouseButton::kRight) > 0 || in.hasTouchDevice())
  97. {
  98. in.hideCursor(true);
  99. // move the camera
  100. static SceneNode* mover = &scene.getActiveCameraNode();
  101. if(in.getKey(KeyCode::k1) == 1)
  102. {
  103. mover = &scene.getActiveCameraNode();
  104. }
  105. if(in.getKey(KeyCode::k2) == 1)
  106. {
  107. mover = &scene.findSceneNode("Spot");
  108. }
  109. if(in.getKey(KeyCode::kUp) > 0)
  110. {
  111. mover->rotateLocalX(ROTATE_ANGLE);
  112. }
  113. if(in.getKey(KeyCode::kDown) > 0)
  114. {
  115. mover->rotateLocalX(-ROTATE_ANGLE);
  116. }
  117. if(in.getKey(KeyCode::kLeft) > 0)
  118. {
  119. mover->rotateLocalY(ROTATE_ANGLE);
  120. }
  121. if(in.getKey(KeyCode::kRight) > 0)
  122. {
  123. mover->rotateLocalY(-ROTATE_ANGLE);
  124. }
  125. static F32 moveDistance = 0.1f;
  126. if(in.getMouseButton(MouseButton::kScrollUp) == 1)
  127. {
  128. moveDistance += 0.1f;
  129. moveDistance = min(moveDistance, 10.0f);
  130. }
  131. if(in.getMouseButton(MouseButton::kScrollDown) == 1)
  132. {
  133. moveDistance -= 0.1f;
  134. moveDistance = max(moveDistance, 0.1f);
  135. }
  136. if(in.getKey(KeyCode::kA) > 0)
  137. {
  138. mover->moveLocalX(-moveDistance);
  139. }
  140. if(in.getKey(KeyCode::kD) > 0)
  141. {
  142. mover->moveLocalX(moveDistance);
  143. }
  144. if(in.getKey(KeyCode::kQ) > 0)
  145. {
  146. mover->moveLocalY(-moveDistance);
  147. }
  148. if(in.getKey(KeyCode::kE) > 0)
  149. {
  150. mover->moveLocalY(moveDistance);
  151. }
  152. if(in.getKey(KeyCode::kW) > 0)
  153. {
  154. mover->moveLocalZ(-moveDistance);
  155. }
  156. if(in.getKey(KeyCode::kS) > 0)
  157. {
  158. mover->moveLocalZ(moveDistance);
  159. }
  160. const Vec2 velocity = in.getMousePositionNdc() - mousePosOn1stClick;
  161. in.moveMouseNdc(mousePosOn1stClick);
  162. if(velocity != Vec2(0.0))
  163. {
  164. Euler angles(mover->getLocalRotation().getRotationPart());
  165. angles.x += velocity.y * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  166. angles.x = clamp(angles.x, toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  167. angles.y += -velocity.x * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  168. angles.z = 0.0f;
  169. mover->setLocalRotation(Mat3(angles));
  170. }
  171. static TouchPointer rotateCameraTouch = TouchPointer::kCount;
  172. static Vec2 rotateEventInitialPos = Vec2(0.0f);
  173. for(TouchPointer touch : EnumIterable<TouchPointer>())
  174. {
  175. if(rotateCameraTouch == TouchPointer::kCount && in.getTouchPointer(touch) == 1 && in.getTouchPointerNdcPosition(touch).x > 0.1f)
  176. {
  177. rotateCameraTouch = touch;
  178. rotateEventInitialPos = in.getTouchPointerNdcPosition(touch) * NativeWindow::getSingleton().getAspectRatio();
  179. break;
  180. }
  181. }
  182. if(rotateCameraTouch != TouchPointer::kCount && in.getTouchPointer(rotateCameraTouch) == 0)
  183. {
  184. rotateCameraTouch = TouchPointer::kCount;
  185. }
  186. if(rotateCameraTouch != TouchPointer::kCount && in.getTouchPointer(rotateCameraTouch) > 1)
  187. {
  188. Vec2 velocity = in.getTouchPointerNdcPosition(rotateCameraTouch) * NativeWindow::getSingleton().getAspectRatio() - rotateEventInitialPos;
  189. velocity *= 0.3f;
  190. Euler angles(mover->getLocalRotation().getRotationPart());
  191. angles.x += velocity.y * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  192. angles.x = clamp(angles.x, toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  193. angles.y += -velocity.x * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  194. angles.z = 0.0f;
  195. mover->setLocalRotation(Mat3(angles));
  196. }
  197. static TouchPointer moveCameraTouch = TouchPointer::kCount;
  198. static Vec2 moveEventInitialPos = Vec2(0.0f);
  199. for(TouchPointer touch : EnumIterable<TouchPointer>())
  200. {
  201. if(moveCameraTouch == TouchPointer::kCount && in.getTouchPointer(touch) == 1 && in.getTouchPointerNdcPosition(touch).x < -0.1f)
  202. {
  203. moveCameraTouch = touch;
  204. moveEventInitialPos = in.getTouchPointerNdcPosition(touch) * NativeWindow::getSingleton().getAspectRatio();
  205. break;
  206. }
  207. }
  208. if(moveCameraTouch != TouchPointer::kCount && in.getTouchPointer(moveCameraTouch) == 0)
  209. {
  210. moveCameraTouch = TouchPointer::kCount;
  211. }
  212. if(moveCameraTouch != TouchPointer::kCount && in.getTouchPointer(moveCameraTouch) > 0)
  213. {
  214. Vec2 velocity = in.getTouchPointerNdcPosition(moveCameraTouch) * NativeWindow::getSingleton().getAspectRatio() - moveEventInitialPos;
  215. velocity *= 2.0f;
  216. mover->moveLocalX(moveDistance * velocity.x);
  217. mover->moveLocalZ(moveDistance * -velocity.y);
  218. }
  219. }
  220. else
  221. {
  222. in.hideCursor(false);
  223. }
  224. return Error::kNone;
  225. }