Main.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include "anki/input/Input.h"
  5. #include "anki/Math.h"
  6. #include "anki/renderer/Renderer.h"
  7. #include "anki/core/App.h"
  8. #include "anki/resource/Mesh.h"
  9. #include "anki/resource/Material.h"
  10. #include "anki/resource/SkelAnim.h"
  11. #include "anki/physics/Character.h"
  12. #include "anki/renderer/Renderer.h"
  13. #include "anki/renderer/MainRenderer.h"
  14. #include "anki/physics/Character.h"
  15. #include "anki/physics/RigidBody.h"
  16. #include "anki/script/ScriptManager.h"
  17. #include "anki/core/StdinListener.h"
  18. #include "anki/resource/Model.h"
  19. #include "anki/core/Logger.h"
  20. #include "anki/util/Util.h"
  21. #include "anki/resource/Skin.h"
  22. #include "anki/event/EventManager.h"
  23. #include "anki/event/MainRendererPpsHdrEvent.h"
  24. #include "anki/resource/ShaderProgramPrePreprocessor.h"
  25. #include "anki/resource/Material.h"
  26. #include "anki/core/ThreadPool.h"
  27. #include "anki/core/Timestamp.h"
  28. #include "anki/core/NativeWindow.h"
  29. #include "anki/scene/Scene.h"
  30. #include "anki/event/LightEvent.h"
  31. #include "anki/event/MovableEvent.h"
  32. using namespace anki;
  33. ModelNode* horse;
  34. PerspectiveCamera* cam;
  35. NativeWindow* win;
  36. //==============================================================================
  37. void initPhysics()
  38. {
  39. SceneGraph& scene = SceneGraphSingleton::get();
  40. scene.getPhysics().setDebugDrawer(
  41. new PhysicsDebugDrawer(
  42. &MainRendererSingleton::get().getDbg().getDebugDrawer()));
  43. btCollisionShape* groundShape = new btBoxShape(
  44. btVector3(btScalar(50.), btScalar(50.), btScalar(50.)));
  45. Transform groundTransform;
  46. groundTransform.setIdentity();
  47. groundTransform.setOrigin(Vec3(0, -50, 0));
  48. RigidBody::Initializer init;
  49. init.mass = 0.0;
  50. init.shape = groundShape;
  51. init.startTrf = groundTransform;
  52. init.group = PhysWorld::CG_MAP;
  53. init.mask = PhysWorld::CG_ALL;
  54. new RigidBody(&SceneGraphSingleton::get().getPhysics(), init);
  55. #if 1
  56. btCollisionShape* colShape = new btBoxShape(
  57. btVector3(1, 1, 1));
  58. init.startTrf.setOrigin(Vec3(0.0, 15.0, 0.0));
  59. init.mass = 20;
  60. init.shape = colShape;
  61. init.group = PhysWorld::CG_PARTICLE;
  62. init.mask = PhysWorld::CG_MAP | PhysWorld::CG_PARTICLE;
  63. const I ARRAY_SIZE_X = 5;
  64. const I ARRAY_SIZE_Y = 5;
  65. const I ARRAY_SIZE_Z = 5;
  66. const I START_POS_X = -5;
  67. const I START_POS_Y = 35;
  68. const I START_POS_Z = -3;
  69. float start_x = START_POS_X - ARRAY_SIZE_X / 2;
  70. float start_y = START_POS_Y;
  71. float start_z = START_POS_Z - ARRAY_SIZE_Z / 2;
  72. for(I k = 0; k < ARRAY_SIZE_Y; k++)
  73. {
  74. for(I i = 0; i < ARRAY_SIZE_X; i++)
  75. {
  76. for(I j = 0; j < ARRAY_SIZE_Z; j++)
  77. {
  78. std::string name = std::string("crate0") + std::to_string(i)
  79. + std::to_string(j) + std::to_string(k);
  80. ModelNode* mnode = new ModelNode(
  81. "data/models/crate0/crate0.mdl",
  82. name.c_str(),
  83. &SceneGraphSingleton::get(), Movable::MF_NONE, nullptr);
  84. init.movable = mnode;
  85. ANKI_ASSERT(init.movable);
  86. Transform trf(
  87. Vec3(2.0 * i + start_x, 2.0 * k + start_y,
  88. 2.0 * j + start_z), Mat3::getIdentity(), 1.0);
  89. init.startTrf = trf;
  90. new RigidBody(&SceneGraphSingleton::get().getPhysics(), init);
  91. }
  92. }
  93. }
  94. #endif
  95. }
  96. //==============================================================================
  97. void init()
  98. {
  99. ANKI_LOGI("Other init...");
  100. SceneGraph& scene = SceneGraphSingleton::get();
  101. #if 0
  102. painter = new UiPainter(Vec2(AppSingleton::get().getWindowWidth(),
  103. AppSingleton::get().getWindowHeight()));
  104. painter->setFont("engine-rsrc/ModernAntiqua.ttf", 25, 25);
  105. #endif
  106. // camera
  107. cam = new PerspectiveCamera("main-camera", &scene,
  108. Movable::MF_NONE, nullptr);
  109. const F32 ang = 45.0;
  110. cam->setAll(
  111. MainRendererSingleton::get().getAspectRatio() * toRad(ang),
  112. toRad(ang), 0.5, 500.0);
  113. cam->setLocalTransform(Transform(Vec3(20.0, 5.0, 0.0),
  114. Mat3(Euler(toRad(-10.0), toRad(90.0), toRad(0.0))),
  115. 1.0));
  116. scene.setActiveCamera(cam);
  117. // lights
  118. #if 1
  119. Vec3 lpos(-24.0, 0.1, -10.0);
  120. for(int i = 0; i < 50; i++)
  121. {
  122. for(int j = 0; j < 10; j++)
  123. {
  124. std::string name = "plight" + std::to_string(i) + std::to_string(j);
  125. PointLight* point = new PointLight(name.c_str(), &scene,
  126. Movable::MF_NONE, nullptr);
  127. point->setRadius(0.5);
  128. point->setDiffuseColor(Vec4(randFloat(6.0) - 2.0,
  129. randFloat(6.0) - 2.0, randFloat(6.0) - 2.0, 0.0));
  130. point->setSpecularColor(Vec4(randFloat(6.0) - 3.0,
  131. randFloat(6.0) - 3.0, randFloat(6.0) - 3.0, 0.0));
  132. point->setLocalTranslation(lpos);
  133. lpos.z() += 2.0;
  134. }
  135. lpos.x() += 0.93;
  136. lpos.z() = -10;
  137. }
  138. #endif
  139. #if 1
  140. SpotLight* spot = new SpotLight("spot0", &scene, Movable::MF_NONE, nullptr);
  141. spot->setOuterAngle(toRad(45.0));
  142. spot->setInnerAngle(toRad(15.0));
  143. spot->setLocalTransform(Transform(Vec3(1.3, 4.3, 3.0),
  144. Mat3::getIdentity(), 1.0));
  145. spot->setDiffuseColor(Vec4(2.0));
  146. spot->setSpecularColor(Vec4(-1.0));
  147. spot->loadTexture("gfx/lights/flashlight.tga");
  148. spot->setDistance(30.0);
  149. spot->setShadowEnabled(true);
  150. spot = new SpotLight("spot1", &scene, Movable::MF_NONE, nullptr);
  151. spot->setOuterAngle(toRad(45.0));
  152. spot->setInnerAngle(toRad(15.0));
  153. spot->setLocalTransform(Transform(Vec3(5.3, 4.3, 3.0),
  154. Mat3::getIdentity(), 1.0));
  155. spot->setDiffuseColor(Vec4(3.0, 0.0, 0.0, 0.0));
  156. spot->setSpecularColor(Vec4(3.0, 3.0, 0.0, 0.0));
  157. spot->loadTexture("gfx/lights/flashlight.tga");
  158. spot->setDistance(30.0);
  159. spot->setShadowEnabled(true);
  160. #endif
  161. #if 1
  162. // Vase point lights
  163. F32 x = 8.5;
  164. F32 y = 2.25;
  165. F32 z = 2.49;
  166. Array<Vec3, 4> vaseLightPos = {{Vec3(x, y, -z - 1.4), Vec3(x, y, z),
  167. Vec3(-x - 2.3, y, z), Vec3(-x - 2.3, y, -z - 1.4)}};
  168. for(U i = 0; i < vaseLightPos.getSize(); i++)
  169. {
  170. Vec3 lightPos = vaseLightPos[i];
  171. PointLight* point =
  172. new PointLight(("vase_plight" + std::to_string(i)).c_str(),
  173. &scene, Movable::MF_NONE, nullptr);
  174. point->setRadius(2.0);
  175. point->setLocalTranslation(lightPos);
  176. point->setDiffuseColor(Vec4(3.0, 0.2, 0.0, 0.0));
  177. point->setSpecularColor(Vec4(1.0, 1.0, 0.0, 0.0));
  178. LightEventData eventData;
  179. eventData.light = point;
  180. eventData.radiusMultiplier = 0.2;
  181. eventData.intensityMultiplier = Vec4(-1.2, 0.0, 0.0, 0.0);
  182. eventData.specularIntensityMultiplier = Vec4(0.1, 0.1, 0.0, 0.0);
  183. auto event = scene.getEventManager().newLightEvent(0.0, 0.8, eventData);
  184. event->enableBits(Event::EF_REANIMATE);
  185. MovableEventData moveData;
  186. moveData.movableSceneNode = point;
  187. moveData.posMin = Vec3(-0.5, 0.0, -0.5);
  188. moveData.posMax = Vec3(0.5, 0.0, 0.5);
  189. auto mevent = scene.getEventManager().newMovableEvent(0.0, 2.0, moveData);
  190. mevent->enableBits(Event::EF_REANIMATE);
  191. ParticleEmitter* pe = new ParticleEmitter(
  192. "data/particles/smoke.particles",
  193. ("pe" + std::to_string(i)).c_str(), &scene,
  194. Movable::MF_NONE, nullptr);
  195. pe->setLocalTranslation(lightPos);
  196. pe = new ParticleEmitter(
  197. "data/particles/fire.particles",
  198. ("pef" + std::to_string(i)).c_str(), &scene,
  199. Movable::MF_NONE, nullptr);
  200. pe->setLocalTranslation(lightPos);
  201. }
  202. #endif
  203. #if 1
  204. // horse
  205. horse = new ModelNode("data/models/horse/horse.mdl", "horse", &scene,
  206. Movable::MF_NONE, nullptr);
  207. horse->setLocalTransform(Transform(Vec3(-2, 0, 0), Mat3::getIdentity(),
  208. 0.7));
  209. // barrel
  210. ModelNode* redBarrel = new ModelNode("data/models/red_barrel/red_barrel.mdl",
  211. "red_barrel", &scene, Movable::MF_NONE, nullptr);
  212. redBarrel->setLocalTransform(Transform(Vec3(+2, 0, 0), Mat3::getIdentity(),
  213. 0.7));
  214. #endif
  215. #if 1
  216. StaticGeometryNode* sponzaModel = new StaticGeometryNode(
  217. //"data/maps/sponza/sponza_no_bmeshes.mdl",
  218. //"data/maps/sponza/sponza.mdl",
  219. "data/maps/sponza/static_geometry.mdl",
  220. "sponza", &scene);
  221. (void)sponzaModel;
  222. #endif
  223. //initPhysics();
  224. // Sectors
  225. SectorGroup& sgroup = scene.getSectorGroup();
  226. Sector* sectorA = sgroup.createNewSector(
  227. Aabb(Vec3(-38, -3, -20), Vec3(38, 27, 20)));
  228. Sector* sectorB = sgroup.createNewSector(Aabb(Vec3(-5), Vec3(5)));
  229. sgroup.createNewPortal(sectorA, sectorB, Obb(Vec3(0.0, 3.0, 0.0),
  230. Mat3::getIdentity(), Vec3(1.0, 2.0, 2.0)));
  231. Sector* sectorC = sgroup.createNewSector(
  232. Aabb(Vec3(-30, -10, -35), Vec3(30, 10, -25)));
  233. sgroup.createNewPortal(sectorA, sectorC, Obb(Vec3(-1.1, 2.0, -11.0),
  234. Mat3::getIdentity(), Vec3(1.3, 1.8, 0.5)));
  235. // Path
  236. /*Path* path = new Path("todo", "path", &scene, Movable::MF_NONE, nullptr);
  237. (void)path;
  238. const F32 distPerSec = 2.0;
  239. scene.getEventManager().newFollowPathEvent(-1.0,
  240. path->getDistance() / distPerSec,
  241. cam, path, distPerSec);*/
  242. }
  243. //==============================================================================
  244. /// The func pools the stdinListener for string in the console, if
  245. /// there are any it executes them with scriptingEngine
  246. void execStdinScpripts()
  247. {
  248. while(1)
  249. {
  250. std::string cmd = StdinListenerSingleton::get().getLine();
  251. if(cmd.length() < 1)
  252. {
  253. break;
  254. }
  255. try
  256. {
  257. ScriptManagerSingleton::get().evalString(cmd.c_str());
  258. }
  259. catch(Exception& e)
  260. {
  261. ANKI_LOGE(e.what());
  262. }
  263. }
  264. }
  265. //==============================================================================
  266. void mainLoopExtra()
  267. {
  268. F32 dist = 0.2;
  269. F32 ang = toRad(3.0);
  270. F32 scale = 0.01;
  271. F32 mouseSensivity = 9.0;
  272. // move the camera
  273. static Movable* mover = SceneGraphSingleton::get().getActiveCamera().getMovable();
  274. Input& in = InputSingleton::get();
  275. if(in.getKey(KC_1))
  276. {
  277. mover = &SceneGraphSingleton::get().getActiveCamera();
  278. }
  279. if(in.getKey(KC_2))
  280. {
  281. mover = SceneGraphSingleton::get().findSceneNode("horse").getMovable();
  282. }
  283. if(in.getKey(KC_3))
  284. {
  285. mover = SceneGraphSingleton::get().findSceneNode("spot0").getMovable();
  286. }
  287. if(in.getKey(KC_4))
  288. {
  289. mover = SceneGraphSingleton::get().findSceneNode("spot1").getMovable();
  290. }
  291. if(in.getKey(KC_5))
  292. {
  293. mover = SceneGraphSingleton::get().findSceneNode("pe").getMovable();
  294. }
  295. if(in.getKey(KC_6))
  296. {
  297. mover = SceneGraphSingleton::get().findSceneNode("vase_plight0").getMovable();
  298. }
  299. if(in.getKey(KC_7))
  300. {
  301. mover = SceneGraphSingleton::get().findSceneNode("sponza").getMovable();
  302. std::cout << mover->getWorldTransform() << std::endl;
  303. }
  304. if(in.getKey(KC_L) == 1)
  305. {
  306. Light* l = SceneGraphSingleton::get().findSceneNode("point1").getLight();
  307. static_cast<PointLight*>(l)->setRadius(10.0);
  308. }
  309. if(in.getKey(KC_F1) == 1)
  310. {
  311. MainRendererSingleton::get().getDbg().setEnabled(
  312. !MainRendererSingleton::get().getDbg().getEnabled());
  313. }
  314. if(in.getKey(KC_F2) == 1)
  315. {
  316. MainRendererSingleton::get().getDbg().switchBits(
  317. Dbg::DF_SPATIAL);
  318. }
  319. if(in.getKey(KC_F3) == 1)
  320. {
  321. MainRendererSingleton::get().getDbg().switchBits(
  322. Dbg::DF_PHYSICS);
  323. }
  324. if(in.getKey(KC_F4) == 1)
  325. {
  326. MainRendererSingleton::get().getDbg().switchBits(
  327. Dbg::DF_SECTOR);
  328. }
  329. if(in.getKey(KC_F5) == 1)
  330. {
  331. MainRendererSingleton::get().getDbg().switchBits(
  332. Dbg::DF_OCTREE);
  333. }
  334. if(in.getKey(KC_F12) == 1)
  335. {
  336. MainRendererSingleton::get().getDbg().switchDepthTestEnabled();
  337. }
  338. if(in.getKey(KC_UP)) mover->rotateLocalX(ang);
  339. if(in.getKey(KC_DOWN)) mover->rotateLocalX(-ang);
  340. if(in.getKey(KC_LEFT)) mover->rotateLocalY(ang);
  341. if(in.getKey(KC_RIGHT)) mover->rotateLocalY(-ang);
  342. if(in.getKey(KC_A)) mover->moveLocalX(-dist);
  343. if(in.getKey(KC_D)) mover->moveLocalX(dist);
  344. if(in.getKey(KC_Z)) mover->moveLocalY(dist);
  345. if(in.getKey(KC_SPACE)) mover->moveLocalY(-dist);
  346. if(in.getKey(KC_W)) mover->moveLocalZ(-dist);
  347. if(in.getKey(KC_S)) mover->moveLocalZ(dist);
  348. if(in.getKey(KC_Q)) mover->rotateLocalZ(ang);
  349. if(in.getKey(KC_E)) mover->rotateLocalZ(-ang);
  350. if(in.getKey(KC_PAGEUP))
  351. {
  352. mover->scale(scale);
  353. }
  354. if(in.getKey(KC_PAGEDOWN))
  355. {
  356. mover->scale(-scale);
  357. }
  358. if(in.getKey(KC_P) == 1)
  359. {
  360. std::cout << "{Vec3(" << mover->getWorldTransform().getOrigin()
  361. << "), Quat(" << Quat(mover->getWorldTransform().getRotation())
  362. << ")}," << std::endl;
  363. }
  364. if(in.getMousePosition() != Vec2(0.0))
  365. {
  366. mover->rotateLocalY(-ang * in.getMousePosition().x() * mouseSensivity *
  367. MainRendererSingleton::get().getAspectRatio());
  368. mover->rotateLocalX(ang * in.getMousePosition().y() * mouseSensivity);
  369. }
  370. execStdinScpripts();
  371. }
  372. //==============================================================================
  373. void mainLoop()
  374. {
  375. ANKI_LOGI("Entering main loop");
  376. HighRezTimer mainLoopTimer;
  377. mainLoopTimer.start();
  378. HighRezTimer::Scalar prevUpdateTime = HighRezTimer::getCurrentTime();
  379. HighRezTimer::Scalar crntTime = prevUpdateTime;
  380. while(1)
  381. {
  382. HighRezTimer timer;
  383. timer.start();
  384. prevUpdateTime = crntTime;
  385. crntTime = HighRezTimer::getCurrentTime();
  386. // Update
  387. //
  388. InputSingleton::get().handleEvents();
  389. InputSingleton::get().moveMouse(Vec2(0.0));
  390. mainLoopExtra();
  391. SceneGraphSingleton::get().update(
  392. prevUpdateTime, crntTime, MainRendererSingleton::get());
  393. //EventManagerSingleton::get().updateAllEvents(prevUpdateTime, crntTime);
  394. MainRendererSingleton::get().render(SceneGraphSingleton::get());
  395. if(InputSingleton::get().getKey(KC_ESCAPE))
  396. {
  397. break;
  398. }
  399. win->swapBuffers();
  400. // Sleep
  401. //
  402. #if 1
  403. timer.stop();
  404. if(timer.getElapsedTime() < AppSingleton::get().getTimerTick())
  405. {
  406. HighRezTimer::sleep(AppSingleton::get().getTimerTick()
  407. - timer.getElapsedTime());
  408. }
  409. #else
  410. if(MainRendererSingleton::get().getFramesCount() == 1000)
  411. {
  412. break;
  413. }
  414. #endif
  415. Timestamp::increaseTimestamp();
  416. }
  417. #if 0
  418. MainRendererSingleton::get().takeScreenshot("screenshot.tga");
  419. #endif
  420. HighRezTimer::Scalar timePassed = mainLoopTimer.getElapsedTime();
  421. ANKI_LOGI("Exiting main loop (" << timePassed
  422. << " sec) FPS: " << 1000.0 / timePassed);
  423. MainRendererSingleton::get().printProfileInfo();
  424. SceneGraphSingleton::get().printProfileInfo();
  425. }
  426. //==============================================================================
  427. // initSubsystems =
  428. //==============================================================================
  429. void initSubsystems(int argc, char* argv[])
  430. {
  431. #if ANKI_GL == ANKI_GL_DESKTOP
  432. U32 glmajor = 3;
  433. U32 glminor = 3;
  434. #else
  435. U32 glmajor = 3;
  436. U32 glminor = 0;
  437. #endif
  438. // App
  439. AppSingleton::get().init(argc, argv);
  440. // Window
  441. NativeWindowInitializer nwinit;
  442. nwinit.width = 1920;
  443. nwinit.height = 1080;
  444. nwinit.majorVersion = glmajor;
  445. nwinit.minorVersion = glminor;
  446. nwinit.depthBits = 0;
  447. nwinit.stencilBits = 0;
  448. nwinit.fullscreenDesktopRez = true;
  449. win = new NativeWindow;
  450. win->create(nwinit);
  451. // GL stuff
  452. GlStateCommonSingleton::get().init(glmajor, glminor);
  453. // Input
  454. InputSingleton::get().init(win);
  455. InputSingleton::get().hideCursor(true);
  456. // Main renderer
  457. RendererInitializer initializer;
  458. initializer.ms.ez.enabled = true;
  459. initializer.dbg.enabled = false;
  460. initializer.is.sm.bilinearEnabled = true;
  461. initializer.is.groundLightEnabled = true;
  462. initializer.is.sm.enabled = true;
  463. initializer.is.sm.pcfEnabled = false;
  464. initializer.is.sm.resolution = 512;
  465. initializer.pps.hdr.enabled = true;
  466. initializer.pps.hdr.renderingQuality = 0.25;
  467. initializer.pps.hdr.blurringDist = 1.0;
  468. initializer.pps.hdr.blurringIterationsCount = 2;
  469. initializer.pps.hdr.exposure = 8.0;
  470. initializer.pps.ssao.blurringIterationsNum = 4;
  471. initializer.pps.ssao.enabled = true;
  472. initializer.pps.ssao.mainPassRenderingQuality = 0.5;
  473. initializer.pps.ssao.blurringRenderingQuality = 0.7;
  474. initializer.pps.enabled = true;
  475. initializer.pps.bl.enabled = true;
  476. initializer.pps.bl.blurringIterationsNum = 2;
  477. initializer.pps.bl.sideBlurFactor = 1.0;
  478. initializer.renderingQuality = 1.0;
  479. initializer.width = nwinit.width;
  480. initializer.height = nwinit.height;
  481. initializer.lodDistance = 20.0;
  482. MainRendererSingleton::get().init(initializer);
  483. // Stdin listener
  484. StdinListenerSingleton::get().start();
  485. // Parallel jobs
  486. ThreadPoolSingleton::get().init(getCpuCoresCount());
  487. }
  488. //==============================================================================
  489. int main(int argc, char* argv[])
  490. {
  491. int exitCode;
  492. try
  493. {
  494. initSubsystems(argc, argv);
  495. init();
  496. mainLoop();
  497. ANKI_LOGI("Exiting...");
  498. AppSingleton::get().quit(EXIT_SUCCESS);
  499. exitCode = 0;
  500. }
  501. catch(std::exception& e)
  502. {
  503. ANKI_LOGE("Aborting: " << e.what());
  504. exitCode = 1;
  505. }
  506. ANKI_LOGI("Bye!!");
  507. return exitCode;
  508. }