2
0

SampleApp.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. #include <AnKi/Scene/Events/AnimationEvent.h>
  7. using namespace anki;
  8. class ButtonsUiNode : public SceneNode
  9. {
  10. public:
  11. FontPtr m_font;
  12. Renderer* m_renderer = nullptr;
  13. ConfigSet* m_config = nullptr;
  14. Bool m_vrs = true;
  15. Bool m_lsVis = false;
  16. Bool m_giVis = false;
  17. Bool m_reflVis = false;
  18. ButtonsUiNode(SceneGraph* scene, CString name)
  19. : SceneNode(scene, name)
  20. {
  21. SpatialComponent* spatialc = newComponent<SpatialComponent>();
  22. spatialc->setAlwaysVisible(true);
  23. UiComponent* uic = newComponent<UiComponent>();
  24. uic->init(
  25. [](CanvasPtr& canvas, void* ud) {
  26. static_cast<ButtonsUiNode*>(ud)->draw(canvas);
  27. },
  28. this);
  29. ANKI_CHECK_AND_IGNORE(getSceneGraph().getUiManager().newInstance(m_font, "EngineAssets/UbuntuMonoRegular.ttf",
  30. Array<U32, 1>{46}));
  31. }
  32. void draw(CanvasPtr& canvas)
  33. {
  34. if(!m_renderer->getGrManager().getDeviceCapabilities().m_vrs)
  35. {
  36. return;
  37. }
  38. const Vec4 oldWindowColor = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
  39. ImGui::GetStyle().Colors[ImGuiCol_WindowBg].w = 0.3f;
  40. ImGui::Begin("VrsButtons", nullptr,
  41. ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove
  42. | ImGuiWindowFlags_AlwaysAutoResize);
  43. canvas->pushFont(m_font, 46);
  44. ImGui::SetWindowPos(Vec2(canvas->getWidth() - ImGui::GetWindowWidth() - 195.0f, 55.0f));
  45. ImGui::SetWindowSize(Vec2(230.0f, 450.0f));
  46. if(m_config->getRVrs())
  47. {
  48. //ImGui::Text("VRS visualization: ");
  49. //ImGui::SameLine();
  50. if(ImGui::Checkbox("Light Shading", &m_lsVis))
  51. {
  52. m_giVis = m_reflVis = false;
  53. }
  54. ImGui::SameLine();
  55. ImGui::Spacing();
  56. ImGui::SameLine();
  57. ImGui::Spacing();
  58. ImGui::SameLine();
  59. if(ImGui::Checkbox("Global Illumination", &m_giVis))
  60. {
  61. m_lsVis = m_reflVis = false;
  62. }
  63. ImGui::SameLine();
  64. ImGui::Spacing();
  65. ImGui::SameLine();
  66. ImGui::Spacing();
  67. ImGui::SameLine();
  68. if(ImGui::Checkbox("Reflections", &m_reflVis))
  69. {
  70. m_lsVis = m_giVis = false;
  71. }
  72. ImGui::SameLine();
  73. ImGui::Spacing();
  74. ImGui::SameLine();
  75. ImGui::Spacing();
  76. ImGui::SameLine();
  77. }
  78. if(ImGui::Checkbox("VRS", &m_vrs))
  79. {
  80. }
  81. m_config->setRVrs(m_vrs);
  82. if(m_config->getRVrs() && m_lsVis)
  83. {
  84. m_renderer->setCurrentDebugRenderTarget("VrsSri");
  85. }
  86. else if(m_config->getRVrs() && m_giVis)
  87. {
  88. m_renderer->setCurrentDebugRenderTarget("IndirectDiffuseVrsSri");
  89. }
  90. else if(m_config->getRVrs() && m_reflVis)
  91. {
  92. m_renderer->setCurrentDebugRenderTarget("VrsSriDownscaled");
  93. }
  94. else
  95. {
  96. m_renderer->setCurrentDebugRenderTarget("");
  97. }
  98. canvas->popFont();
  99. ImGui::End();
  100. ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = oldWindowColor;
  101. }
  102. };
  103. Error SampleApp::init(int argc, char** argv, CString sampleName)
  104. {
  105. HeapMemoryPool pool(allocAligned, nullptr);
  106. // Init the super class
  107. m_config.init(allocAligned, nullptr);
  108. m_config.setWindowFullscreen(true);
  109. #if !ANKI_OS_ANDROID
  110. StringRaii mainDataPath(&pool, ANKI_SOURCE_DIRECTORY);
  111. StringRaii assetsDataPath(&pool);
  112. assetsDataPath.sprintf("%s/Samples/%s", ANKI_SOURCE_DIRECTORY, sampleName.cstr());
  113. if(!directoryExists(assetsDataPath))
  114. {
  115. ANKI_LOGE("Cannot find directory \"%s\". Have you moved the clone of the repository? "
  116. "I'll continue but expect issues",
  117. assetsDataPath.cstr());
  118. }
  119. else
  120. {
  121. m_config.setRsrcDataPaths(StringRaii(&pool).sprintf("%s:%s", mainDataPath.cstr(), assetsDataPath.cstr()));
  122. }
  123. #endif
  124. ANKI_CHECK(m_config.setFromCommandLineArguments(argc - 1, argv + 1));
  125. ANKI_CHECK(App::init(&m_config, allocAligned, nullptr));
  126. ANKI_CHECK(sampleExtraInit());
  127. ButtonsUiNode* node;
  128. getSceneGraph().newSceneNode("VrsButtons", node);
  129. node->m_renderer = &getMainRenderer().getOffscreenRenderer();
  130. node->m_config = &m_config;
  131. node->m_vrs = m_config.getRVrs();
  132. return Error::kNone;
  133. }
  134. Error SampleApp::userMainLoop(Bool& quit, Second elapsedTime)
  135. {
  136. constexpr F32 ROTATE_ANGLE = toRad(2.5f);
  137. constexpr F32 MOUSE_SENSITIVITY = 5.0f;
  138. quit = false;
  139. SceneGraph& scene = getSceneGraph();
  140. Renderer& renderer = getMainRenderer().getOffscreenRenderer();
  141. Input& in = getInput();
  142. if(in.getKey(KeyCode::kEscape))
  143. {
  144. quit = true;
  145. return Error::kNone;
  146. }
  147. if(in.getKey(KeyCode::kBackquote) == 1)
  148. {
  149. setDisplayDeveloperConsole(!getDisplayDeveloperConsole());
  150. }
  151. if(in.getKey(KeyCode::kY) == 1)
  152. {
  153. renderer.setCurrentDebugRenderTarget(
  154. (renderer.getCurrentDebugRenderTarget() == "GBufferNormals") ? "" : "GBufferNormals");
  155. }
  156. if(in.getKey(KeyCode::kU) == 1)
  157. {
  158. renderer.setCurrentDebugRenderTarget(
  159. (renderer.getCurrentDebugRenderTarget() == "IndirectDiffuse") ? "" : "IndirectDiffuse");
  160. }
  161. if(in.getKey(KeyCode::kI) == 1)
  162. {
  163. renderer.setCurrentDebugRenderTarget((renderer.getCurrentDebugRenderTarget() == "SSR") ? "" : "SSR");
  164. }
  165. if(in.getKey(KeyCode::kO) == 1)
  166. {
  167. renderer.setCurrentDebugRenderTarget(
  168. (renderer.getCurrentDebugRenderTarget() == "ResolvedShadows") ? "" : "ResolvedShadows");
  169. }
  170. if(in.getKey(KeyCode::kP) == 1)
  171. {
  172. static U32 idx = 3;
  173. ++idx;
  174. idx %= 4;
  175. if(idx == 0)
  176. {
  177. renderer.setCurrentDebugRenderTarget("IndirectDiffuseVrsSri");
  178. }
  179. else if(idx == 1)
  180. {
  181. renderer.setCurrentDebugRenderTarget("VrsSriDownscaled");
  182. }
  183. else if(idx == 2)
  184. {
  185. renderer.setCurrentDebugRenderTarget("VrsSri");
  186. }
  187. else
  188. {
  189. renderer.setCurrentDebugRenderTarget("");
  190. }
  191. }
  192. if(in.getKey(KeyCode::kL) == 1)
  193. {
  194. renderer.setCurrentDebugRenderTarget(
  195. (renderer.getCurrentDebugRenderTarget() == "LightShading") ? "" : "LightShading");
  196. }
  197. if(in.getKey(KeyCode::kH) == 1)
  198. {
  199. static U32 pressCount = 0;
  200. CString rtName;
  201. switch(pressCount)
  202. {
  203. case 0:
  204. rtName = "RtShadows";
  205. break;
  206. case 1:
  207. rtName = "RtShadows1";
  208. break;
  209. case 2:
  210. rtName = "RtShadows2";
  211. break;
  212. default:
  213. rtName = "";
  214. }
  215. renderer.setCurrentDebugRenderTarget(rtName);
  216. pressCount = (pressCount + 1) % 4;
  217. }
  218. if(in.getKey(KeyCode::kJ) == 1)
  219. {
  220. m_config.setRVrs(!m_config.getRVrs());
  221. }
  222. static Vec2 mousePosOn1stClick = in.getMousePosition();
  223. if(in.getMouseButton(MouseButton::kRight) == 1)
  224. {
  225. // Re-init mouse pos
  226. mousePosOn1stClick = in.getMousePosition();
  227. }
  228. if(in.getMouseButton(MouseButton::kRight) || in.hasTouchDevice())
  229. {
  230. in.hideCursor(true);
  231. // move the camera
  232. static MoveComponent* mover = &scene.getActiveCameraNode().getFirstComponentOfType<MoveComponent>();
  233. if(in.getKey(KeyCode::k1) == 1)
  234. {
  235. mover = &scene.getActiveCameraNode().getFirstComponentOfType<MoveComponent>();
  236. }
  237. if(in.getKey(KeyCode::kF1) == 1)
  238. {
  239. static U mode = 0;
  240. mode = (mode + 1) % 3;
  241. if(mode == 0)
  242. {
  243. getConfig().setRDbgEnabled(false);
  244. }
  245. else if(mode == 1)
  246. {
  247. getConfig().setRDbgEnabled(true);
  248. renderer.getDbg().setDepthTestEnabled(true);
  249. renderer.getDbg().setDitheredDepthTestEnabled(false);
  250. }
  251. else
  252. {
  253. getConfig().setRDbgEnabled(true);
  254. renderer.getDbg().setDepthTestEnabled(false);
  255. renderer.getDbg().setDitheredDepthTestEnabled(true);
  256. }
  257. }
  258. if(in.getKey(KeyCode::kF2) == 1)
  259. {
  260. // renderer.getDbg().flipFlags(DbgFlag::SPATIAL_COMPONENT);
  261. }
  262. if(in.getKey(KeyCode::kUp))
  263. {
  264. mover->rotateLocalX(ROTATE_ANGLE);
  265. }
  266. if(in.getKey(KeyCode::kDown))
  267. {
  268. mover->rotateLocalX(-ROTATE_ANGLE);
  269. }
  270. if(in.getKey(KeyCode::kLeft))
  271. {
  272. mover->rotateLocalY(ROTATE_ANGLE);
  273. }
  274. if(in.getKey(KeyCode::kRight))
  275. {
  276. mover->rotateLocalY(-ROTATE_ANGLE);
  277. }
  278. static F32 moveDistance = 0.1f;
  279. if(in.getMouseButton(MouseButton::kScrollUp) == 1)
  280. {
  281. moveDistance += 0.1f;
  282. moveDistance = min(moveDistance, 10.0f);
  283. }
  284. if(in.getMouseButton(MouseButton::kScrollDown) == 1)
  285. {
  286. moveDistance -= 0.1f;
  287. moveDistance = max(moveDistance, 0.1f);
  288. }
  289. if(in.getKey(KeyCode::kA))
  290. {
  291. mover->moveLocalX(-moveDistance);
  292. }
  293. if(in.getKey(KeyCode::kD))
  294. {
  295. mover->moveLocalX(moveDistance);
  296. }
  297. if(in.getKey(KeyCode::kQ))
  298. {
  299. mover->moveLocalY(-moveDistance);
  300. }
  301. if(in.getKey(KeyCode::kE))
  302. {
  303. mover->moveLocalY(moveDistance);
  304. }
  305. if(in.getKey(KeyCode::kW))
  306. {
  307. mover->moveLocalZ(-moveDistance);
  308. }
  309. if(in.getKey(KeyCode::kS))
  310. {
  311. mover->moveLocalZ(moveDistance);
  312. }
  313. if(in.getKey(KeyCode::kF12) == 1 && ANKI_ENABLE_TRACE)
  314. {
  315. TracerSingleton::get().setEnabled(!TracerSingleton::get().getEnabled());
  316. }
  317. const Vec2 velocity = in.getMousePosition() - mousePosOn1stClick;
  318. in.moveCursor(mousePosOn1stClick);
  319. if(velocity != Vec2(0.0))
  320. {
  321. Euler angles(mover->getLocalRotation().getRotationPart());
  322. angles.x() += velocity.y() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  323. angles.x() = clamp(angles.x(), toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  324. angles.y() += -velocity.x() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  325. angles.z() = 0.0f;
  326. mover->setLocalRotation(Mat3x4(Vec3(0.0f), angles));
  327. }
  328. static TouchPointer rotateCameraTouch = TouchPointer::kCount;
  329. static Vec2 rotateEventInitialPos = Vec2(0.0f);
  330. for(TouchPointer touch : EnumIterable<TouchPointer>())
  331. {
  332. if(rotateCameraTouch == TouchPointer::kCount && in.getTouchPointer(touch) == 1
  333. && in.getTouchPointerNdcPosition(touch).x() > 0.1f && in.getTouchPointerNdcPosition(touch).y() < 0.6f)
  334. {
  335. rotateCameraTouch = touch;
  336. rotateEventInitialPos = in.getTouchPointerNdcPosition(touch) * getWindow().getAspectRatio();
  337. break;
  338. }
  339. }
  340. if(rotateCameraTouch != TouchPointer::kCount && in.getTouchPointer(rotateCameraTouch) == 0)
  341. {
  342. rotateCameraTouch = TouchPointer::kCount;
  343. }
  344. if(rotateCameraTouch != TouchPointer::kCount && in.getTouchPointer(rotateCameraTouch) > 1)
  345. {
  346. Vec2 velocity =
  347. in.getTouchPointerNdcPosition(rotateCameraTouch) * getWindow().getAspectRatio() - rotateEventInitialPos;
  348. velocity *= 0.1f;
  349. Euler angles(mover->getLocalRotation().getRotationPart());
  350. angles.x() += velocity.y() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  351. angles.x() = clamp(angles.x(), toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  352. angles.y() += -velocity.x() * toRad(360.0f) * F32(elapsedTime) * MOUSE_SENSITIVITY;
  353. angles.z() = 0.0f;
  354. mover->setLocalRotation(Mat3x4(Vec3(0.0f), angles));
  355. }
  356. static TouchPointer moveCameraTouch = TouchPointer::kCount;
  357. static Vec2 moveEventInitialPos = Vec2(0.0f);
  358. for(TouchPointer touch : EnumIterable<TouchPointer>())
  359. {
  360. if(moveCameraTouch == TouchPointer::kCount && in.getTouchPointer(touch) == 1
  361. && in.getTouchPointerNdcPosition(touch).x() < -0.1f && in.getTouchPointerNdcPosition(touch).y() < 0.6f)
  362. {
  363. moveCameraTouch = touch;
  364. moveEventInitialPos = in.getTouchPointerNdcPosition(touch) * getWindow().getAspectRatio();
  365. break;
  366. }
  367. }
  368. if(moveCameraTouch != TouchPointer::kCount && in.getTouchPointer(moveCameraTouch) == 0)
  369. {
  370. moveCameraTouch = TouchPointer::kCount;
  371. }
  372. if(moveCameraTouch != TouchPointer::kCount && in.getTouchPointer(moveCameraTouch) > 0)
  373. {
  374. Vec2 velocity =
  375. in.getTouchPointerNdcPosition(moveCameraTouch) * getWindow().getAspectRatio() - moveEventInitialPos;
  376. velocity *= 2.0f;
  377. mover->moveLocalX(moveDistance * velocity.x());
  378. mover->moveLocalZ(moveDistance * -velocity.y());
  379. }
  380. // Camera fly mode
  381. if(moveCameraTouch != TouchPointer::kCount || rotateCameraTouch != TouchPointer::kCount)
  382. {
  383. m_timesOfLastTouchEvent = kIdleTime;
  384. if(m_cameraAnimationEvent)
  385. {
  386. m_cameraAnimationEvent->setMarkedForDeletion();
  387. m_cameraAnimationEvent = nullptr;
  388. }
  389. }
  390. m_timesOfLastTouchEvent -= elapsedTime;
  391. if(m_timesOfLastTouchEvent < 0.0 && m_cameraAnimationEvent == nullptr)
  392. {
  393. scene.getEventManager().newEvent(m_cameraAnimationEvent,
  394. "Assets/Camera.001Action.003_67159cf18109479.ankianim", "Camera.001",
  395. &scene.getActiveCameraNode());
  396. }
  397. }
  398. else
  399. {
  400. in.hideCursor(false);
  401. }
  402. return Error::kNone;
  403. }