Main.cpp 16 KB

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