SampleApp.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. return Error::kNone;
  24. }
  25. Error SampleApp::userMainLoop(Bool& quit, Second elapsedTime)
  26. {
  27. constexpr F32 ROTATE_ANGLE = toRad(2.5f);
  28. constexpr F32 MOUSE_SENSITIVITY = 5.0f;
  29. quit = false;
  30. SceneGraph& scene = SceneGraph::getSingleton();
  31. Renderer& renderer = Renderer::getSingleton();
  32. Input& in = Input::getSingleton();
  33. if(in.getKey(KeyCode::kEscape) > 0)
  34. {
  35. quit = true;
  36. return Error::kNone;
  37. }
  38. if(in.getKey(KeyCode::kGrave) == 1)
  39. {
  40. toggleDeveloperConsole();
  41. }
  42. if(getDeveloperConsoleEnabled())
  43. {
  44. return Error::kNone;
  45. }
  46. if(in.getKey(KeyCode::kY) == 1)
  47. {
  48. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "IndirectDiffuseClipmaps") ? "" : "IndirectDiffuseClipmaps");
  49. // g_shadowMappingPcssCVar = !g_shadowMappingPcssCVar;
  50. }
  51. if(in.getKey(KeyCode::kU) == 1)
  52. {
  53. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "Reflections") ? "" : "Reflections");
  54. }
  55. if(in.getKey(KeyCode::kI) == 1)
  56. {
  57. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "RtMaterialFetchDbg") ? "" : "RtMaterialFetchDbg");
  58. }
  59. if(in.getKey(KeyCode::kO) == 1)
  60. {
  61. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "HistoryLen") ? "" : "HistoryLen");
  62. }
  63. static Bool timeOfDay = false;
  64. if(in.getKey(KeyCode::kP) == 1 || timeOfDay)
  65. {
  66. timeOfDay = true;
  67. static F32 time = 8.0f;
  68. scene.getDirectionalLight()->setDirectionFromTimeOfDay(6, 25, time);
  69. time += 0.2f * F32(elapsedTime);
  70. if(time > 19.0)
  71. {
  72. time = 8.0f;
  73. }
  74. }
  75. if(in.getKey(KeyCode::kL) == 1)
  76. {
  77. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "MotionBlur") ? "" : "MotionBlur");
  78. }
  79. if(in.getKey(KeyCode::kJ) == 1)
  80. {
  81. g_cvarGrVrs = !g_cvarGrVrs;
  82. }
  83. static Vec2 mousePosOn1stClick = in.getMousePositionNdc();
  84. if(in.getMouseButton(MouseButton::kRight) == 1)
  85. {
  86. // Re-init mouse pos
  87. mousePosOn1stClick = in.getMousePositionNdc();
  88. }
  89. #if ANKI_TRACING_ENABLED
  90. if(in.getKey(KeyCode::kF11) == 1)
  91. {
  92. g_cvarCoreTracingEnabled = !g_cvarCoreTracingEnabled;
  93. }
  94. #endif
  95. if(in.getMouseButton(MouseButton::kRight) > 0 || in.hasTouchDevice())
  96. {
  97. in.hideMouseCursor(true);
  98. // move the camera
  99. static SceneNode* mover = &scene.getActiveCameraNode();
  100. if(in.getKey(KeyCode::k1) == 1)
  101. {
  102. mover = &scene.getActiveCameraNode();
  103. }
  104. if(in.getKey(KeyCode::k2) == 1)
  105. {
  106. mover = &scene.findSceneNode("Spot");
  107. }
  108. if(in.getKey(KeyCode::kUp) > 0)
  109. {
  110. mover->rotateLocalX(ROTATE_ANGLE);
  111. }
  112. if(in.getKey(KeyCode::kDown) > 0)
  113. {
  114. mover->rotateLocalX(-ROTATE_ANGLE);
  115. }
  116. if(in.getKey(KeyCode::kLeft) > 0)
  117. {
  118. mover->rotateLocalY(ROTATE_ANGLE);
  119. }
  120. if(in.getKey(KeyCode::kRight) > 0)
  121. {
  122. mover->rotateLocalY(-ROTATE_ANGLE);
  123. }
  124. static F32 moveDistance = 0.1f;
  125. if(in.getMouseButton(MouseButton::kScrollUp) == 1)
  126. {
  127. moveDistance += 0.1f;
  128. moveDistance = min(moveDistance, 10.0f);
  129. }
  130. if(in.getMouseButton(MouseButton::kScrollDown) == 1)
  131. {
  132. moveDistance -= 0.1f;
  133. moveDistance = max(moveDistance, 0.1f);
  134. }
  135. if(in.getKey(KeyCode::kA) > 0)
  136. {
  137. mover->moveLocalX(-moveDistance);
  138. }
  139. if(in.getKey(KeyCode::kD) > 0)
  140. {
  141. mover->moveLocalX(moveDistance);
  142. }
  143. if(in.getKey(KeyCode::kQ) > 0)
  144. {
  145. mover->moveLocalY(-moveDistance);
  146. }
  147. if(in.getKey(KeyCode::kE) > 0)
  148. {
  149. mover->moveLocalY(moveDistance);
  150. }
  151. if(in.getKey(KeyCode::kW) > 0)
  152. {
  153. mover->moveLocalZ(-moveDistance);
  154. }
  155. if(in.getKey(KeyCode::kS) > 0)
  156. {
  157. mover->moveLocalZ(moveDistance);
  158. }
  159. const Vec2 velocity = in.getMousePositionNdc() - mousePosOn1stClick;
  160. in.moveMouseNdc(mousePosOn1stClick);
  161. if(velocity != Vec2(0.0))
  162. {
  163. Euler angles(mover->getLocalRotation().getRotationPart());
  164. angles.x += velocity.y * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  165. angles.x = clamp(angles.x, toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  166. angles.y += -velocity.x * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  167. angles.z = 0.0f;
  168. mover->setLocalRotation(Mat3(angles));
  169. }
  170. static TouchPointer rotateCameraTouch = TouchPointer::kCount;
  171. static Vec2 rotateEventInitialPos = Vec2(0.0f);
  172. for(TouchPointer touch : EnumIterable<TouchPointer>())
  173. {
  174. if(rotateCameraTouch == TouchPointer::kCount && in.getTouchPointer(touch) == 1 && in.getTouchPointerNdcPosition(touch).x > 0.1f)
  175. {
  176. rotateCameraTouch = touch;
  177. rotateEventInitialPos = in.getTouchPointerNdcPosition(touch) * NativeWindow::getSingleton().getAspectRatio();
  178. break;
  179. }
  180. }
  181. if(rotateCameraTouch != TouchPointer::kCount && in.getTouchPointer(rotateCameraTouch) == 0)
  182. {
  183. rotateCameraTouch = TouchPointer::kCount;
  184. }
  185. if(rotateCameraTouch != TouchPointer::kCount && in.getTouchPointer(rotateCameraTouch) > 1)
  186. {
  187. Vec2 velocity = in.getTouchPointerNdcPosition(rotateCameraTouch) * NativeWindow::getSingleton().getAspectRatio() - rotateEventInitialPos;
  188. velocity *= 0.3f;
  189. Euler angles(mover->getLocalRotation().getRotationPart());
  190. angles.x += velocity.y * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  191. angles.x = clamp(angles.x, toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  192. angles.y += -velocity.x * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  193. angles.z = 0.0f;
  194. mover->setLocalRotation(Mat3(angles));
  195. }
  196. static TouchPointer moveCameraTouch = TouchPointer::kCount;
  197. static Vec2 moveEventInitialPos = Vec2(0.0f);
  198. for(TouchPointer touch : EnumIterable<TouchPointer>())
  199. {
  200. if(moveCameraTouch == TouchPointer::kCount && in.getTouchPointer(touch) == 1 && in.getTouchPointerNdcPosition(touch).x < -0.1f)
  201. {
  202. moveCameraTouch = touch;
  203. moveEventInitialPos = in.getTouchPointerNdcPosition(touch) * NativeWindow::getSingleton().getAspectRatio();
  204. break;
  205. }
  206. }
  207. if(moveCameraTouch != TouchPointer::kCount && in.getTouchPointer(moveCameraTouch) == 0)
  208. {
  209. moveCameraTouch = TouchPointer::kCount;
  210. }
  211. if(moveCameraTouch != TouchPointer::kCount && in.getTouchPointer(moveCameraTouch) > 0)
  212. {
  213. Vec2 velocity = in.getTouchPointerNdcPosition(moveCameraTouch) * NativeWindow::getSingleton().getAspectRatio() - moveEventInitialPos;
  214. velocity *= 2.0f;
  215. mover->moveLocalX(moveDistance * velocity.x);
  216. mover->moveLocalZ(moveDistance * -velocity.y);
  217. }
  218. }
  219. else
  220. {
  221. in.hideMouseCursor(false);
  222. }
  223. return Error::kNone;
  224. }