Main.cpp 15 KB

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