Main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. using namespace anki;
  38. ModelNode* horse;
  39. PerspectiveCamera* cam;
  40. NativeWindow* win;
  41. //==============================================================================
  42. void initPhysics()
  43. {
  44. Scene& scene = SceneSingleton::get();
  45. scene.getPhysics().setDebugDrawer(
  46. new PhysicsDebugDrawer(
  47. &MainRendererSingleton::get().getDbg().getDebugDrawer()));
  48. btCollisionShape* groundShape = new btBoxShape(
  49. btVector3(btScalar(50.), btScalar(50.), btScalar(50.)));
  50. Transform groundTransform;
  51. groundTransform.setIdentity();
  52. groundTransform.setOrigin(Vec3(0, -50, 0));
  53. RigidBody::Initializer init;
  54. init.mass = 0.0;
  55. init.shape = groundShape;
  56. init.startTrf = groundTransform;
  57. init.group = PhysWorld::CG_MAP;
  58. init.mask = PhysWorld::CG_ALL;
  59. new RigidBody(&SceneSingleton::get().getPhysics(), init);
  60. btCollisionShape* colShape = new btBoxShape(
  61. btVector3(1, 1, 1));
  62. init.startTrf.setOrigin(Vec3(0.0, 15.0, 0.0));
  63. init.mass = 1;
  64. init.shape = colShape;
  65. init.group = PhysWorld::CG_PARTICLE;
  66. init.mask = PhysWorld::CG_MAP | PhysWorld::CG_PARTICLE;
  67. const I ARRAY_SIZE_X = 5;
  68. const I ARRAY_SIZE_Y = 5;
  69. const I ARRAY_SIZE_Z = 5;
  70. const I START_POS_X = -5;
  71. const I START_POS_Y = 35;
  72. const I START_POS_Z = -3;
  73. float start_x = START_POS_X - ARRAY_SIZE_X / 2;
  74. float start_y = START_POS_Y;
  75. float start_z = START_POS_Z - ARRAY_SIZE_Z / 2;
  76. for(I k = 0; k < ARRAY_SIZE_Y; k++)
  77. {
  78. for(I i = 0; i < ARRAY_SIZE_X; i++)
  79. {
  80. for(I j = 0; j < ARRAY_SIZE_Z; j++)
  81. {
  82. std::string name = std::string("crate0") + std::to_string(i)
  83. + std::to_string(j) + std::to_string(k);
  84. ModelNode* mnode = new ModelNode(
  85. "data/models/crate0/crate0.mdl",
  86. name.c_str(),
  87. &SceneSingleton::get(), Movable::MF_NONE, nullptr);
  88. init.movable = mnode;
  89. ANKI_ASSERT(init.movable);
  90. Transform trf(
  91. Vec3(2.0 * i + start_x, 2.0 * k + start_y,
  92. 2.0 * j + start_z), Mat3::getIdentity(), 1.0);
  93. init.startTrf = trf;
  94. new RigidBody(&SceneSingleton::get().getPhysics(), init);
  95. }
  96. }
  97. }
  98. }
  99. //==============================================================================
  100. void init()
  101. {
  102. ANKI_LOGI("Other init...");
  103. Scene& scene = SceneSingleton::get();
  104. #if 0
  105. painter = new UiPainter(Vec2(AppSingleton::get().getWindowWidth(),
  106. AppSingleton::get().getWindowHeight()));
  107. painter->setFont("engine-rsrc/ModernAntiqua.ttf", 25, 25);
  108. #endif
  109. // camera
  110. cam = new PerspectiveCamera("main-camera", &scene,
  111. Movable::MF_NONE, nullptr);
  112. const F32 ang = 45.0;
  113. cam->setAll(
  114. MainRendererSingleton::get().getAspectRatio() * toRad(ang),
  115. toRad(ang), 0.5, 500.0);
  116. cam->setLocalTransform(Transform(Vec3(82.0, 5.0, 8.0),
  117. Mat3(Euler(toRad(-10.0), toRad(90.0), toRad(0.0))),
  118. 1.0));
  119. scene.setActiveCamera(cam);
  120. // camera 2
  121. PerspectiveCamera* pcam = new PerspectiveCamera("camera1", &scene,
  122. Movable::MF_NONE, nullptr);
  123. pcam->setAll(
  124. MainRendererSingleton::get().getAspectRatio() * toRad(ang),
  125. toRad(ang), 0.5, 200.0);
  126. pcam->setLocalTransform(Transform(Vec3(100.0, 3.0, 8.0),
  127. Mat3(Axisang(toRad(90.0), Vec3(0, 1, 0))),
  128. 1.0));
  129. // lights
  130. #if 1
  131. Vec3 lpos(-90.0, 1.2, -32.0);
  132. for(int i = 0; i < 50; i++)
  133. {
  134. for(int j = 0; j < 10; j++)
  135. {
  136. std::string name = "plight" + std::to_string(i) + std::to_string(j);
  137. PointLight* point = new PointLight(name.c_str(), &scene,
  138. Movable::MF_NONE, nullptr);
  139. point->setRadius(2.0);
  140. point->setDiffuseColor(Vec4(randFloat(6.0) - 2.0,
  141. randFloat(6.0) - 2.0, randFloat(6.0) - 2.0, 0.0));
  142. point->setSpecularColor(Vec4(randFloat(6.0) - 3.0,
  143. randFloat(6.0) - 3.0, randFloat(6.0) - 3.0, 0.0));
  144. point->setLocalTranslation(lpos);
  145. lpos.z() += 7.0;
  146. }
  147. lpos.x() += 3.5;
  148. lpos.z() = -32;
  149. }
  150. #endif
  151. #if 1
  152. SpotLight* spot = new SpotLight("spot0", &scene, Movable::MF_NONE, nullptr);
  153. spot->setOuterAngle(toRad(45.0));
  154. spot->setInnerAngle(toRad(15.0));
  155. spot->setLocalTransform(Transform(Vec3(1.3, 4.3, 3.0),
  156. Mat3::getIdentity(), 1.0));
  157. spot->setDiffuseColor(Vec4(2.0));
  158. spot->setSpecularColor(Vec4(-1.0));
  159. spot->loadTexture("gfx/lights/flashlight.tga");
  160. spot->setDistance(30.0);
  161. spot->setShadowEnabled(true);
  162. spot = new SpotLight("spot1", &scene, Movable::MF_NONE, nullptr);
  163. spot->setOuterAngle(toRad(45.0));
  164. spot->setInnerAngle(toRad(15.0));
  165. spot->setLocalTransform(Transform(Vec3(5.3, 4.3, 3.0),
  166. Mat3::getIdentity(), 1.0));
  167. spot->setDiffuseColor(Vec4(3.0, 0.0, 0.0, 0.0));
  168. spot->setSpecularColor(Vec4(3.0, 3.0, 0.0, 0.0));
  169. spot->loadTexture("gfx/lights/flashlight.tga");
  170. spot->setDistance(30.0);
  171. spot->setShadowEnabled(true);
  172. #endif
  173. /*PointLight* point = new PointLight("point0", &scene, Movable::MF_NONE,
  174. nullptr);
  175. point->setRadius(3.0);
  176. point->setDiffuseColor(Vec4(1.0, 0.0, 0.0, 0.0));
  177. point->setSpecularColor(Vec4(0.0, 0.0, 1.0, 0.0));
  178. PointLight* point1 = new PointLight("point1", &scene, Movable::MF_NONE,
  179. nullptr);
  180. point1->setRadius(3.0);
  181. point1->setDiffuseColor(Vec4(2.0, 2.0, 2.0, 0.0));
  182. point1->setSpecularColor(Vec4(3.0, 3.0, 0.0, 0.0));
  183. point1->setLocalTranslation(Vec3(-3.0, 2.0, 0.0));*/
  184. // horse
  185. horse = new ModelNode("data/models/horse/horse.mdl", "horse", &scene,
  186. Movable::MF_NONE, nullptr);
  187. horse->setLocalTransform(Transform(Vec3(-2, 0, 0), Mat3::getIdentity(),
  188. 1.0));
  189. #if 0
  190. // Sponza
  191. ModelNode* sponzaModel = new ModelNode(
  192. "maps/sponza-crytek/sponza_crytek.mdl",
  193. "sponza", &scene, Movable::MF_NONE, nullptr);
  194. sponzaModel->setLocalScale(0.1);
  195. // Sectors
  196. Aabb sectorAabb;
  197. sponzaModel->getModel().getVisibilityShape().toAabb(sectorAabb);
  198. scene.sectors.push_back(new Sector(sectorAabb));
  199. #endif
  200. #if 1
  201. ModelNode* sponzaModel = new ModelNode(
  202. "data/maps/sponza/sponza.mdl",
  203. "sponza", &scene, Movable::MF_NONE, nullptr);
  204. (void)sponzaModel;
  205. #endif
  206. initPhysics();
  207. }
  208. //==============================================================================
  209. /// The func pools the stdinListener for string in the console, if
  210. /// there are any it executes them with scriptingEngine
  211. void execStdinScpripts()
  212. {
  213. while(1)
  214. {
  215. std::string cmd = StdinListenerSingleton::get().getLine();
  216. if(cmd.length() < 1)
  217. {
  218. break;
  219. }
  220. try
  221. {
  222. ScriptManagerSingleton::get().evalString(cmd.c_str());
  223. }
  224. catch(Exception& e)
  225. {
  226. ANKI_LOGE(e.what());
  227. }
  228. }
  229. }
  230. //==============================================================================
  231. void mainLoopExtra()
  232. {
  233. F32 dist = 0.2;
  234. F32 ang = toRad(3.0);
  235. F32 scale = 0.01;
  236. F32 mouseSensivity = 9.0;
  237. // move the camera
  238. static Movable* mover = SceneSingleton::get().getActiveCamera().getMovable();
  239. Input& in = InputSingleton::get();
  240. if(in.getKey(KC_1))
  241. {
  242. mover = &SceneSingleton::get().getActiveCamera();
  243. }
  244. if(in.getKey(KC_2))
  245. {
  246. mover = SceneSingleton::get().findSceneNode("horse").getMovable();
  247. }
  248. if(in.getKey(KC_3))
  249. {
  250. mover = SceneSingleton::get().findSceneNode("spot0").getMovable();
  251. }
  252. if(in.getKey(KC_4))
  253. {
  254. mover = SceneSingleton::get().findSceneNode("spot1").getMovable();
  255. }
  256. /*if(in.getKey(KC_5))
  257. {
  258. mover = SceneSingleton::get().findSceneNode("point1")->getMovable();
  259. }*/
  260. if(in.getKey(KC_6))
  261. {
  262. mover = SceneSingleton::get().findSceneNode("camera1").getMovable();
  263. mover->setLocalTransform(cam->getLocalTransform());
  264. }
  265. if(in.getKey(KC_L) == 1)
  266. {
  267. Light* l = SceneSingleton::get().findSceneNode("point1").getLight();
  268. static_cast<PointLight*>(l)->setRadius(10.0);
  269. }
  270. if(in.getKey(KC_P) == 1)
  271. {
  272. //MainRendererSingleton::get().getPps().getHdr().setExposure(20);
  273. //in.hideCursor(true);
  274. MainRendererSingleton::get().getDbg().setDepthTestEnabled(
  275. !MainRendererSingleton::get().getDbg().getDepthTestEnabled());
  276. }
  277. if(in.getKey(KC_UP)) mover->rotateLocalX(ang);
  278. if(in.getKey(KC_DOWN)) mover->rotateLocalX(-ang);
  279. if(in.getKey(KC_LEFT)) mover->rotateLocalY(ang);
  280. if(in.getKey(KC_RIGHT)) mover->rotateLocalY(-ang);
  281. if(in.getKey(KC_A)) mover->moveLocalX(-dist);
  282. if(in.getKey(KC_D)) mover->moveLocalX(dist);
  283. if(in.getKey(KC_Z)) mover->moveLocalY(dist);
  284. if(in.getKey(KC_SPACE)) mover->moveLocalY(-dist);
  285. if(in.getKey(KC_W)) mover->moveLocalZ(-dist);
  286. if(in.getKey(KC_S)) mover->moveLocalZ(dist);
  287. if(in.getKey(KC_Q)) mover->rotateLocalZ(ang);
  288. if(in.getKey(KC_E)) mover->rotateLocalZ(-ang);
  289. if(in.getKey(KC_PAGEUP))
  290. {
  291. mover->scale(scale);
  292. }
  293. if(in.getKey(KC_PAGEDOWN))
  294. {
  295. mover->scale(-scale);
  296. }
  297. mover->rotateLocalY(-ang * in.getMousePosition().x() * mouseSensivity *
  298. MainRendererSingleton::get().getAspectRatio());
  299. mover->rotateLocalX(ang * in.getMousePosition().y() * mouseSensivity);
  300. execStdinScpripts();
  301. }
  302. //==============================================================================
  303. void mainLoop()
  304. {
  305. ANKI_LOGI("Entering main loop");
  306. HighRezTimer mainLoopTimer;
  307. mainLoopTimer.start();
  308. HighRezTimer::Scalar prevUpdateTime = HighRezTimer::getCurrentTime();
  309. HighRezTimer::Scalar crntTime = prevUpdateTime;
  310. while(1)
  311. {
  312. HighRezTimer timer;
  313. timer.start();
  314. prevUpdateTime = crntTime;
  315. crntTime = HighRezTimer::getCurrentTime();
  316. // Update
  317. //
  318. InputSingleton::get().handleEvents();
  319. InputSingleton::get().moveMouse(Vec2(0.0));
  320. mainLoopExtra();
  321. SceneSingleton::get().update(
  322. prevUpdateTime, crntTime, MainRendererSingleton::get());
  323. EventManagerSingleton::get().updateAllEvents(prevUpdateTime, crntTime);
  324. MainRendererSingleton::get().render(SceneSingleton::get());
  325. if(InputSingleton::get().getKey(KC_ESCAPE))
  326. {
  327. break;
  328. }
  329. win->swapBuffers();
  330. // Sleep
  331. //
  332. #if 1
  333. timer.stop();
  334. if(timer.getElapsedTime() < AppSingleton::get().getTimerTick())
  335. {
  336. HighRezTimer::sleep(AppSingleton::get().getTimerTick()
  337. - timer.getElapsedTime());
  338. }
  339. #else
  340. if(MainRendererSingleton::get().getFramesCount() == 1000)
  341. {
  342. break;
  343. }
  344. #endif
  345. Timestamp::increaseTimestamp();
  346. }
  347. #if 1
  348. MainRendererSingleton::get().takeScreenshot("screenshot.tga");
  349. #endif
  350. ANKI_LOGI("Exiting main loop (" << mainLoopTimer.getElapsedTime()
  351. << " sec)");
  352. }
  353. //==============================================================================
  354. // initSubsystems =
  355. //==============================================================================
  356. void initSubsystems(int argc, char* argv[])
  357. {
  358. #if ANKI_GL == ANKI_GL_DESKTOP
  359. U32 glmajor = 3;
  360. U32 glminor = 3;
  361. #else
  362. U32 glmajor = 3;
  363. U32 glminor = 0;
  364. #endif
  365. // App
  366. AppSingleton::get().init(argc, argv);
  367. // Window
  368. NativeWindowInitializer nwinit;
  369. nwinit.width = 1280;
  370. nwinit.height = 720;
  371. nwinit.majorVersion = glmajor;
  372. nwinit.minorVersion = glminor;
  373. nwinit.depthBits = 0;
  374. nwinit.stencilBits = 0;
  375. win = new NativeWindow;
  376. win->create(nwinit);
  377. // GL stuff
  378. GlStateCommonSingleton::get().init(glmajor, glminor);
  379. // Input
  380. InputSingleton::get().init(win);
  381. InputSingleton::get().hideCursor(true);
  382. // Main renderer
  383. RendererInitializer initializer;
  384. initializer.ms.ez.enabled = true;
  385. initializer.dbg.enabled = true;
  386. initializer.is.sm.bilinearEnabled = true;
  387. initializer.is.groundLightEnabled = false;
  388. initializer.is.sm.enabled = true;
  389. initializer.is.sm.pcfEnabled = false;
  390. initializer.is.sm.resolution = 512;
  391. initializer.pps.hdr.enabled = true;
  392. initializer.pps.hdr.renderingQuality = 0.25;
  393. initializer.pps.hdr.blurringDist = 1.0;
  394. initializer.pps.hdr.blurringIterationsCount = 2;
  395. initializer.pps.hdr.exposure = 8.0;
  396. initializer.pps.ssao.blurringIterationsNum = 4;
  397. initializer.pps.ssao.enabled = true;
  398. initializer.pps.ssao.renderingQuality = 0.3;
  399. initializer.pps.enabled = true;
  400. initializer.pps.bl.enabled = true;
  401. initializer.pps.bl.blurringIterationsNum = 2;
  402. initializer.pps.bl.sideBlurFactor = 1.0;
  403. initializer.renderingQuality = 1.0;
  404. initializer.width = nwinit.width;
  405. initializer.height = nwinit.height;
  406. MainRendererSingleton::get().init(initializer);
  407. // Stdin listener
  408. StdinListenerSingleton::get().start();
  409. // Parallel jobs
  410. ThreadPoolSingleton::get().init(8);
  411. }
  412. //==============================================================================
  413. int main(int argc, char* argv[])
  414. {
  415. int exitCode;
  416. try
  417. {
  418. initSubsystems(argc, argv);
  419. init();
  420. mainLoop();
  421. ANKI_LOGI("Exiting...");
  422. AppSingleton::get().quit(EXIT_SUCCESS);
  423. exitCode = 0;
  424. }
  425. catch(std::exception& e)
  426. {
  427. std::cerr << "Aborting: " << e.what() << std::endl;
  428. exitCode = 1;
  429. }
  430. ANKI_LOGI("Bye!!");
  431. return exitCode;
  432. }