SampleApp.cpp 12 KB

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