Main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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(82.0, 5.0, 8.0),
  120. Mat3(Euler(toRad(-10.0), toRad(90.0), toRad(0.0))),
  121. 1.0));
  122. scene.setActiveCamera(cam);
  123. // camera 2
  124. PerspectiveCamera* pcam = new PerspectiveCamera("camera1", &scene,
  125. Movable::MF_NONE, nullptr);
  126. pcam->setAll(
  127. MainRendererSingleton::get().getAspectRatio() * toRad(ang),
  128. toRad(ang), 0.5, 200.0);
  129. pcam->setLocalTransform(Transform(Vec3(100.0, 3.0, 8.0),
  130. Mat3(Axisang(toRad(90.0), Vec3(0, 1, 0))),
  131. 1.0));
  132. // lights
  133. #if 1
  134. Vec3 lpos(-90.0, 1.2, -32.0);
  135. for(int i = 0; i < 50; i++)
  136. {
  137. for(int j = 0; j < 10; j++)
  138. {
  139. std::string name = "plight" + std::to_string(i) + std::to_string(j);
  140. PointLight* point = new PointLight(name.c_str(), &scene,
  141. Movable::MF_NONE, nullptr);
  142. point->setRadius(2.0);
  143. point->setDiffuseColor(Vec4(randFloat(6.0) - 2.0,
  144. randFloat(6.0) - 2.0, randFloat(6.0) - 2.0, 0.0));
  145. point->setSpecularColor(Vec4(randFloat(6.0) - 3.0,
  146. randFloat(6.0) - 3.0, randFloat(6.0) - 3.0, 0.0));
  147. point->setLocalTranslation(lpos);
  148. lpos.z() += 7.0;
  149. }
  150. lpos.x() += 3.5;
  151. lpos.z() = -32;
  152. }
  153. #endif
  154. #if 1
  155. SpotLight* spot = new SpotLight("spot0", &scene, Movable::MF_NONE, nullptr);
  156. spot->setOuterAngle(toRad(45.0));
  157. spot->setInnerAngle(toRad(15.0));
  158. spot->setLocalTransform(Transform(Vec3(1.3, 4.3, 3.0),
  159. Mat3::getIdentity(), 1.0));
  160. spot->setDiffuseColor(Vec4(2.0));
  161. spot->setSpecularColor(Vec4(-1.0));
  162. spot->loadTexture("gfx/lights/flashlight.tga");
  163. spot->setDistance(30.0);
  164. spot->setShadowEnabled(true);
  165. spot = new SpotLight("spot1", &scene, Movable::MF_NONE, nullptr);
  166. spot->setOuterAngle(toRad(45.0));
  167. spot->setInnerAngle(toRad(15.0));
  168. spot->setLocalTransform(Transform(Vec3(5.3, 4.3, 3.0),
  169. Mat3::getIdentity(), 1.0));
  170. spot->setDiffuseColor(Vec4(3.0, 0.0, 0.0, 0.0));
  171. spot->setSpecularColor(Vec4(3.0, 3.0, 0.0, 0.0));
  172. spot->loadTexture("gfx/lights/flashlight.tga");
  173. spot->setDistance(30.0);
  174. spot->setShadowEnabled(true);
  175. #endif
  176. // Vase point lights
  177. Array<Vec3, 4> vaseLightPos = {{Vec3(32.6, 9, -13.2), Vec3(32.6, 9, 10),
  178. Vec3(-37.6001, 9, 10), Vec3(-37.6001, 9, -13.2)}};
  179. for(U i = 0; i < vaseLightPos.getSize(); i++)
  180. {
  181. PointLight* point =
  182. new PointLight(("vase_plight" + std::to_string(i)).c_str(),
  183. &scene, Movable::MF_NONE, nullptr);
  184. point->setRadius(3.5);
  185. point->setLocalTranslation(vaseLightPos[i]);
  186. point->setDiffuseColor(Vec4(3.0, 0.0, 0.0, 0.0));
  187. point->setSpecularColor(Vec4(1.0, 1.0, 0.0, 0.0));
  188. ParticleEmitter* pe = new ParticleEmitter(
  189. "data/particles/smoke.particles",
  190. ("pe" + std::to_string(i)).c_str(), &scene,
  191. Movable::MF_NONE, nullptr);
  192. pe->setLocalTranslation(vaseLightPos[i]);
  193. pe = new ParticleEmitter(
  194. "data/particles/fire.particles",
  195. ("pef" + std::to_string(i)).c_str(), &scene,
  196. Movable::MF_NONE, nullptr);
  197. pe->setLocalTranslation(vaseLightPos[i]);
  198. }
  199. /*PointLight* point1 = new PointLight("point1", &scene, Movable::MF_NONE,
  200. nullptr);
  201. point1->setRadius(3.0);
  202. point1->setDiffuseColor(Vec4(2.0, 2.0, 2.0, 0.0));
  203. point1->setSpecularColor(Vec4(3.0, 3.0, 0.0, 0.0));
  204. point1->setLocalTranslation(Vec3(-3.0, 2.0, 0.0));*/
  205. // horse
  206. horse = new ModelNode("data/models/horse/horse.mdl", "horse", &scene,
  207. Movable::MF_NONE, nullptr);
  208. horse->setLocalTransform(Transform(Vec3(-2, 0, 0), Mat3::getIdentity(),
  209. 1.0));
  210. #if 0
  211. // Sponza
  212. ModelNode* sponzaModel = new ModelNode(
  213. "maps/sponza-crytek/sponza_crytek.mdl",
  214. "sponza", &scene, Movable::MF_NONE, nullptr);
  215. sponzaModel->setLocalScale(0.1);
  216. // Sectors
  217. Aabb sectorAabb;
  218. sponzaModel->getModel().getVisibilityShape().toAabb(sectorAabb);
  219. scene.sectors.push_back(new Sector(sectorAabb));
  220. #endif
  221. #if 1
  222. ModelNode* sponzaModel = new ModelNode(
  223. "data/maps/sponza/sponza.mdl",
  224. "sponza", &scene, Movable::MF_NONE, nullptr);
  225. (void)sponzaModel;
  226. #endif
  227. initPhysics();
  228. /*ParticleEmitter* pe = new ParticleEmitter("todo", "pe", &scene,
  229. Movable::MF_NONE, nullptr);
  230. (void)pe;*/
  231. }
  232. //==============================================================================
  233. /// The func pools the stdinListener for string in the console, if
  234. /// there are any it executes them with scriptingEngine
  235. void execStdinScpripts()
  236. {
  237. while(1)
  238. {
  239. std::string cmd = StdinListenerSingleton::get().getLine();
  240. if(cmd.length() < 1)
  241. {
  242. break;
  243. }
  244. try
  245. {
  246. ScriptManagerSingleton::get().evalString(cmd.c_str());
  247. }
  248. catch(Exception& e)
  249. {
  250. ANKI_LOGE(e.what());
  251. }
  252. }
  253. }
  254. //==============================================================================
  255. void mainLoopExtra()
  256. {
  257. F32 dist = 0.2;
  258. F32 ang = toRad(3.0);
  259. F32 scale = 0.01;
  260. F32 mouseSensivity = 9.0;
  261. // move the camera
  262. static Movable* mover = SceneSingleton::get().getActiveCamera().getMovable();
  263. Input& in = InputSingleton::get();
  264. if(in.getKey(KC_1))
  265. {
  266. mover = &SceneSingleton::get().getActiveCamera();
  267. }
  268. if(in.getKey(KC_2))
  269. {
  270. mover = SceneSingleton::get().findSceneNode("horse").getMovable();
  271. }
  272. if(in.getKey(KC_3))
  273. {
  274. mover = SceneSingleton::get().findSceneNode("spot0").getMovable();
  275. }
  276. if(in.getKey(KC_4))
  277. {
  278. mover = SceneSingleton::get().findSceneNode("spot1").getMovable();
  279. }
  280. if(in.getKey(KC_5))
  281. {
  282. mover = SceneSingleton::get().findSceneNode("pe").getMovable();
  283. }
  284. if(in.getKey(KC_6))
  285. {
  286. mover = SceneSingleton::get().findSceneNode("vase_plight0").getMovable();
  287. }
  288. if(in.getKey(KC_L) == 1)
  289. {
  290. Light* l = SceneSingleton::get().findSceneNode("point1").getLight();
  291. static_cast<PointLight*>(l)->setRadius(10.0);
  292. }
  293. if(in.getKey(KC_P) == 1)
  294. {
  295. //MainRendererSingleton::get().getPps().getHdr().setExposure(20);
  296. //in.hideCursor(true);
  297. MainRendererSingleton::get().getDbg().setDepthTestEnabled(
  298. !MainRendererSingleton::get().getDbg().getDepthTestEnabled());
  299. }
  300. if(in.getKey(KC_UP)) mover->rotateLocalX(ang);
  301. if(in.getKey(KC_DOWN)) mover->rotateLocalX(-ang);
  302. if(in.getKey(KC_LEFT)) mover->rotateLocalY(ang);
  303. if(in.getKey(KC_RIGHT)) mover->rotateLocalY(-ang);
  304. if(in.getKey(KC_A)) mover->moveLocalX(-dist);
  305. if(in.getKey(KC_D)) mover->moveLocalX(dist);
  306. if(in.getKey(KC_Z)) mover->moveLocalY(dist);
  307. if(in.getKey(KC_SPACE)) mover->moveLocalY(-dist);
  308. if(in.getKey(KC_W)) mover->moveLocalZ(-dist);
  309. if(in.getKey(KC_S)) mover->moveLocalZ(dist);
  310. if(in.getKey(KC_Q)) mover->rotateLocalZ(ang);
  311. if(in.getKey(KC_E)) mover->rotateLocalZ(-ang);
  312. if(in.getKey(KC_PAGEUP))
  313. {
  314. mover->scale(scale);
  315. }
  316. if(in.getKey(KC_PAGEDOWN))
  317. {
  318. mover->scale(-scale);
  319. }
  320. if(in.getKey(KC_P))
  321. {
  322. ANKI_LOGI("pos: " << mover->getWorldTransform().getOrigin());
  323. }
  324. mover->rotateLocalY(-ang * in.getMousePosition().x() * mouseSensivity *
  325. MainRendererSingleton::get().getAspectRatio());
  326. mover->rotateLocalX(ang * in.getMousePosition().y() * mouseSensivity);
  327. execStdinScpripts();
  328. }
  329. //==============================================================================
  330. void mainLoop()
  331. {
  332. ANKI_LOGI("Entering main loop");
  333. HighRezTimer mainLoopTimer;
  334. mainLoopTimer.start();
  335. HighRezTimer::Scalar prevUpdateTime = HighRezTimer::getCurrentTime();
  336. HighRezTimer::Scalar crntTime = prevUpdateTime;
  337. while(1)
  338. {
  339. HighRezTimer timer;
  340. timer.start();
  341. prevUpdateTime = crntTime;
  342. crntTime = HighRezTimer::getCurrentTime();
  343. // Update
  344. //
  345. InputSingleton::get().handleEvents();
  346. InputSingleton::get().moveMouse(Vec2(0.0));
  347. mainLoopExtra();
  348. SceneSingleton::get().update(
  349. prevUpdateTime, crntTime, MainRendererSingleton::get());
  350. EventManagerSingleton::get().updateAllEvents(prevUpdateTime, crntTime);
  351. MainRendererSingleton::get().render(SceneSingleton::get());
  352. if(InputSingleton::get().getKey(KC_ESCAPE))
  353. {
  354. break;
  355. }
  356. win->swapBuffers();
  357. // Sleep
  358. //
  359. #if 1
  360. timer.stop();
  361. if(timer.getElapsedTime() < AppSingleton::get().getTimerTick())
  362. {
  363. HighRezTimer::sleep(AppSingleton::get().getTimerTick()
  364. - timer.getElapsedTime());
  365. }
  366. #else
  367. if(MainRendererSingleton::get().getFramesCount() == 1000)
  368. {
  369. break;
  370. }
  371. #endif
  372. Timestamp::increaseTimestamp();
  373. }
  374. #if 1
  375. MainRendererSingleton::get().takeScreenshot("screenshot.tga");
  376. #endif
  377. ANKI_LOGI("Exiting main loop (" << mainLoopTimer.getElapsedTime()
  378. << " sec)");
  379. }
  380. //==============================================================================
  381. // initSubsystems =
  382. //==============================================================================
  383. void initSubsystems(int argc, char* argv[])
  384. {
  385. #if ANKI_GL == ANKI_GL_DESKTOP
  386. U32 glmajor = 3;
  387. U32 glminor = 3;
  388. #else
  389. U32 glmajor = 3;
  390. U32 glminor = 0;
  391. #endif
  392. // App
  393. AppSingleton::get().init(argc, argv);
  394. // Window
  395. NativeWindowInitializer nwinit;
  396. nwinit.width = 1280;
  397. nwinit.height = 720;
  398. nwinit.majorVersion = glmajor;
  399. nwinit.minorVersion = glminor;
  400. nwinit.depthBits = 0;
  401. nwinit.stencilBits = 0;
  402. win = new NativeWindow;
  403. win->create(nwinit);
  404. // GL stuff
  405. GlStateCommonSingleton::get().init(glmajor, glminor);
  406. // Input
  407. InputSingleton::get().init(win);
  408. InputSingleton::get().hideCursor(true);
  409. // Main renderer
  410. RendererInitializer initializer;
  411. initializer.ms.ez.enabled = true;
  412. initializer.dbg.enabled = false;
  413. initializer.is.sm.bilinearEnabled = true;
  414. initializer.is.groundLightEnabled = false;
  415. initializer.is.sm.enabled = true;
  416. initializer.is.sm.pcfEnabled = false;
  417. initializer.is.sm.resolution = 512;
  418. initializer.pps.hdr.enabled = true;
  419. initializer.pps.hdr.renderingQuality = 0.25;
  420. initializer.pps.hdr.blurringDist = 1.0;
  421. initializer.pps.hdr.blurringIterationsCount = 2;
  422. initializer.pps.hdr.exposure = 8.0;
  423. initializer.pps.ssao.blurringIterationsNum = 4;
  424. initializer.pps.ssao.enabled = true;
  425. initializer.pps.ssao.mainPassRenderingQuality = 0.3;
  426. initializer.pps.ssao.blurringRenderingQuality = 0.5;
  427. initializer.pps.enabled = true;
  428. initializer.pps.bl.enabled = true;
  429. initializer.pps.bl.blurringIterationsNum = 2;
  430. initializer.pps.bl.sideBlurFactor = 1.0;
  431. initializer.renderingQuality = 1.0;
  432. initializer.width = nwinit.width;
  433. initializer.height = nwinit.height;
  434. MainRendererSingleton::get().init(initializer);
  435. // Stdin listener
  436. StdinListenerSingleton::get().start();
  437. // Parallel jobs
  438. ThreadPoolSingleton::get().init(8);
  439. }
  440. //==============================================================================
  441. int main(int argc, char* argv[])
  442. {
  443. int exitCode;
  444. try
  445. {
  446. initSubsystems(argc, argv);
  447. init();
  448. mainLoop();
  449. ANKI_LOGI("Exiting...");
  450. AppSingleton::get().quit(EXIT_SUCCESS);
  451. exitCode = 0;
  452. }
  453. catch(std::exception& e)
  454. {
  455. std::cerr << "Aborting: " << e.what() << std::endl;
  456. exitCode = 1;
  457. }
  458. ANKI_LOGI("Bye!!");
  459. return exitCode;
  460. }