Main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 0
  62. btCollisionShape* colShape = new btBoxShape(
  63. btVector3(1, 1, 1));
  64. init.startTrf.setOrigin(Vec3(0.0, 15.0, 0.0));
  65. init.mass = 1;
  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. /*PointLight* point = new PointLight("point0", &scene, Movable::MF_NONE,
  177. nullptr);
  178. point->setRadius(3.0);
  179. point->setDiffuseColor(Vec4(1.0, 0.0, 0.0, 0.0));
  180. point->setSpecularColor(Vec4(0.0, 0.0, 1.0, 0.0));
  181. PointLight* point1 = new PointLight("point1", &scene, Movable::MF_NONE,
  182. nullptr);
  183. point1->setRadius(3.0);
  184. point1->setDiffuseColor(Vec4(2.0, 2.0, 2.0, 0.0));
  185. point1->setSpecularColor(Vec4(3.0, 3.0, 0.0, 0.0));
  186. point1->setLocalTranslation(Vec3(-3.0, 2.0, 0.0));*/
  187. // horse
  188. horse = new ModelNode("data/models/horse/horse.mdl", "horse", &scene,
  189. Movable::MF_NONE, nullptr);
  190. horse->setLocalTransform(Transform(Vec3(-2, 0, 0), Mat3::getIdentity(),
  191. 1.0));
  192. #if 0
  193. // Sponza
  194. ModelNode* sponzaModel = new ModelNode(
  195. "maps/sponza-crytek/sponza_crytek.mdl",
  196. "sponza", &scene, Movable::MF_NONE, nullptr);
  197. sponzaModel->setLocalScale(0.1);
  198. // Sectors
  199. Aabb sectorAabb;
  200. sponzaModel->getModel().getVisibilityShape().toAabb(sectorAabb);
  201. scene.sectors.push_back(new Sector(sectorAabb));
  202. #endif
  203. #if 1
  204. ModelNode* sponzaModel = new ModelNode(
  205. "data/maps/sponza/sponza.mdl",
  206. "sponza", &scene, Movable::MF_NONE, nullptr);
  207. (void)sponzaModel;
  208. #endif
  209. initPhysics();
  210. ParticleEmitter* pe = new ParticleEmitter("todo", "pe", &scene,
  211. Movable::MF_NONE, nullptr);
  212. (void)pe;
  213. }
  214. //==============================================================================
  215. /// The func pools the stdinListener for string in the console, if
  216. /// there are any it executes them with scriptingEngine
  217. void execStdinScpripts()
  218. {
  219. while(1)
  220. {
  221. std::string cmd = StdinListenerSingleton::get().getLine();
  222. if(cmd.length() < 1)
  223. {
  224. break;
  225. }
  226. try
  227. {
  228. ScriptManagerSingleton::get().evalString(cmd.c_str());
  229. }
  230. catch(Exception& e)
  231. {
  232. ANKI_LOGE(e.what());
  233. }
  234. }
  235. }
  236. //==============================================================================
  237. void mainLoopExtra()
  238. {
  239. F32 dist = 0.2;
  240. F32 ang = toRad(3.0);
  241. F32 scale = 0.01;
  242. F32 mouseSensivity = 9.0;
  243. // move the camera
  244. static Movable* mover = SceneSingleton::get().getActiveCamera().getMovable();
  245. Input& in = InputSingleton::get();
  246. if(in.getKey(KC_1))
  247. {
  248. mover = &SceneSingleton::get().getActiveCamera();
  249. }
  250. if(in.getKey(KC_2))
  251. {
  252. mover = SceneSingleton::get().findSceneNode("horse").getMovable();
  253. }
  254. if(in.getKey(KC_3))
  255. {
  256. mover = SceneSingleton::get().findSceneNode("spot0").getMovable();
  257. }
  258. if(in.getKey(KC_4))
  259. {
  260. mover = SceneSingleton::get().findSceneNode("spot1").getMovable();
  261. }
  262. if(in.getKey(KC_5))
  263. {
  264. mover = SceneSingleton::get().findSceneNode("pe").getMovable();
  265. }
  266. if(in.getKey(KC_6))
  267. {
  268. mover = SceneSingleton::get().findSceneNode("camera1").getMovable();
  269. mover->setLocalTransform(cam->getLocalTransform());
  270. }
  271. if(in.getKey(KC_L) == 1)
  272. {
  273. Light* l = SceneSingleton::get().findSceneNode("point1").getLight();
  274. static_cast<PointLight*>(l)->setRadius(10.0);
  275. }
  276. if(in.getKey(KC_P) == 1)
  277. {
  278. //MainRendererSingleton::get().getPps().getHdr().setExposure(20);
  279. //in.hideCursor(true);
  280. MainRendererSingleton::get().getDbg().setDepthTestEnabled(
  281. !MainRendererSingleton::get().getDbg().getDepthTestEnabled());
  282. }
  283. if(in.getKey(KC_UP)) mover->rotateLocalX(ang);
  284. if(in.getKey(KC_DOWN)) mover->rotateLocalX(-ang);
  285. if(in.getKey(KC_LEFT)) mover->rotateLocalY(ang);
  286. if(in.getKey(KC_RIGHT)) mover->rotateLocalY(-ang);
  287. if(in.getKey(KC_A)) mover->moveLocalX(-dist);
  288. if(in.getKey(KC_D)) mover->moveLocalX(dist);
  289. if(in.getKey(KC_Z)) mover->moveLocalY(dist);
  290. if(in.getKey(KC_SPACE)) mover->moveLocalY(-dist);
  291. if(in.getKey(KC_W)) mover->moveLocalZ(-dist);
  292. if(in.getKey(KC_S)) mover->moveLocalZ(dist);
  293. if(in.getKey(KC_Q)) mover->rotateLocalZ(ang);
  294. if(in.getKey(KC_E)) mover->rotateLocalZ(-ang);
  295. if(in.getKey(KC_PAGEUP))
  296. {
  297. mover->scale(scale);
  298. }
  299. if(in.getKey(KC_PAGEDOWN))
  300. {
  301. mover->scale(-scale);
  302. }
  303. mover->rotateLocalY(-ang * in.getMousePosition().x() * mouseSensivity *
  304. MainRendererSingleton::get().getAspectRatio());
  305. mover->rotateLocalX(ang * in.getMousePosition().y() * mouseSensivity);
  306. execStdinScpripts();
  307. }
  308. //==============================================================================
  309. void mainLoop()
  310. {
  311. ANKI_LOGI("Entering main loop");
  312. HighRezTimer mainLoopTimer;
  313. mainLoopTimer.start();
  314. HighRezTimer::Scalar prevUpdateTime = HighRezTimer::getCurrentTime();
  315. HighRezTimer::Scalar crntTime = prevUpdateTime;
  316. while(1)
  317. {
  318. HighRezTimer timer;
  319. timer.start();
  320. prevUpdateTime = crntTime;
  321. crntTime = HighRezTimer::getCurrentTime();
  322. // Update
  323. //
  324. InputSingleton::get().handleEvents();
  325. InputSingleton::get().moveMouse(Vec2(0.0));
  326. mainLoopExtra();
  327. SceneSingleton::get().update(
  328. prevUpdateTime, crntTime, MainRendererSingleton::get());
  329. EventManagerSingleton::get().updateAllEvents(prevUpdateTime, crntTime);
  330. MainRendererSingleton::get().render(SceneSingleton::get());
  331. if(InputSingleton::get().getKey(KC_ESCAPE))
  332. {
  333. break;
  334. }
  335. win->swapBuffers();
  336. // Sleep
  337. //
  338. #if 1
  339. timer.stop();
  340. if(timer.getElapsedTime() < AppSingleton::get().getTimerTick())
  341. {
  342. HighRezTimer::sleep(AppSingleton::get().getTimerTick()
  343. - timer.getElapsedTime());
  344. }
  345. #else
  346. if(MainRendererSingleton::get().getFramesCount() == 1000)
  347. {
  348. break;
  349. }
  350. #endif
  351. Timestamp::increaseTimestamp();
  352. }
  353. #if 1
  354. MainRendererSingleton::get().takeScreenshot("screenshot.tga");
  355. #endif
  356. ANKI_LOGI("Exiting main loop (" << mainLoopTimer.getElapsedTime()
  357. << " sec)");
  358. }
  359. //==============================================================================
  360. // initSubsystems =
  361. //==============================================================================
  362. void initSubsystems(int argc, char* argv[])
  363. {
  364. #if ANKI_GL == ANKI_GL_DESKTOP
  365. U32 glmajor = 3;
  366. U32 glminor = 3;
  367. #else
  368. U32 glmajor = 3;
  369. U32 glminor = 0;
  370. #endif
  371. // App
  372. AppSingleton::get().init(argc, argv);
  373. // Window
  374. NativeWindowInitializer nwinit;
  375. nwinit.width = 1280;
  376. nwinit.height = 720;
  377. nwinit.majorVersion = glmajor;
  378. nwinit.minorVersion = glminor;
  379. nwinit.depthBits = 0;
  380. nwinit.stencilBits = 0;
  381. win = new NativeWindow;
  382. win->create(nwinit);
  383. // GL stuff
  384. GlStateCommonSingleton::get().init(glmajor, glminor);
  385. // Input
  386. InputSingleton::get().init(win);
  387. InputSingleton::get().hideCursor(true);
  388. // Main renderer
  389. RendererInitializer initializer;
  390. initializer.ms.ez.enabled = true;
  391. initializer.dbg.enabled = false;
  392. initializer.is.sm.bilinearEnabled = true;
  393. initializer.is.groundLightEnabled = false;
  394. initializer.is.sm.enabled = true;
  395. initializer.is.sm.pcfEnabled = false;
  396. initializer.is.sm.resolution = 512;
  397. initializer.pps.hdr.enabled = true;
  398. initializer.pps.hdr.renderingQuality = 0.25;
  399. initializer.pps.hdr.blurringDist = 1.0;
  400. initializer.pps.hdr.blurringIterationsCount = 2;
  401. initializer.pps.hdr.exposure = 8.0;
  402. initializer.pps.ssao.blurringIterationsNum = 4;
  403. initializer.pps.ssao.enabled = true;
  404. initializer.pps.ssao.mainPassRenderingQuality = 0.3;
  405. initializer.pps.ssao.blurringRenderingQuality = 0.6;
  406. initializer.pps.enabled = true;
  407. initializer.pps.bl.enabled = true;
  408. initializer.pps.bl.blurringIterationsNum = 2;
  409. initializer.pps.bl.sideBlurFactor = 1.0;
  410. initializer.renderingQuality = 1.0;
  411. initializer.width = nwinit.width;
  412. initializer.height = nwinit.height;
  413. MainRendererSingleton::get().init(initializer);
  414. // Stdin listener
  415. StdinListenerSingleton::get().start();
  416. // Parallel jobs
  417. ThreadPoolSingleton::get().init(8);
  418. }
  419. //==============================================================================
  420. int main(int argc, char* argv[])
  421. {
  422. int exitCode;
  423. try
  424. {
  425. initSubsystems(argc, argv);
  426. init();
  427. mainLoop();
  428. ANKI_LOGI("Exiting...");
  429. AppSingleton::get().quit(EXIT_SUCCESS);
  430. exitCode = 0;
  431. }
  432. catch(std::exception& e)
  433. {
  434. std::cerr << "Aborting: " << e.what() << std::endl;
  435. exitCode = 1;
  436. }
  437. ANKI_LOGI("Bye!!");
  438. return exitCode;
  439. }