Main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. // Vase point lights
  177. Array<Vec3, 4> vaseLightPos = {{Vec3(32.4, 9, -13.2), Vec3(32.4, 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("todo",
  189. ("pe" + std::to_string(i)).c_str(), &scene,
  190. Movable::MF_NONE, nullptr);
  191. pe->setLocalTranslation(vaseLightPos[i]);
  192. }
  193. /*PointLight* point1 = new PointLight("point1", &scene, Movable::MF_NONE,
  194. nullptr);
  195. point1->setRadius(3.0);
  196. point1->setDiffuseColor(Vec4(2.0, 2.0, 2.0, 0.0));
  197. point1->setSpecularColor(Vec4(3.0, 3.0, 0.0, 0.0));
  198. point1->setLocalTranslation(Vec3(-3.0, 2.0, 0.0));*/
  199. // horse
  200. horse = new ModelNode("data/models/horse/horse.mdl", "horse", &scene,
  201. Movable::MF_NONE, nullptr);
  202. horse->setLocalTransform(Transform(Vec3(-2, 0, 0), Mat3::getIdentity(),
  203. 1.0));
  204. #if 0
  205. // Sponza
  206. ModelNode* sponzaModel = new ModelNode(
  207. "maps/sponza-crytek/sponza_crytek.mdl",
  208. "sponza", &scene, Movable::MF_NONE, nullptr);
  209. sponzaModel->setLocalScale(0.1);
  210. // Sectors
  211. Aabb sectorAabb;
  212. sponzaModel->getModel().getVisibilityShape().toAabb(sectorAabb);
  213. scene.sectors.push_back(new Sector(sectorAabb));
  214. #endif
  215. #if 1
  216. ModelNode* sponzaModel = new ModelNode(
  217. "data/maps/sponza/sponza.mdl",
  218. "sponza", &scene, Movable::MF_NONE, nullptr);
  219. (void)sponzaModel;
  220. #endif
  221. initPhysics();
  222. /*ParticleEmitter* pe = new ParticleEmitter("todo", "pe", &scene,
  223. Movable::MF_NONE, nullptr);
  224. (void)pe;*/
  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_L) == 1)
  283. {
  284. Light* l = SceneSingleton::get().findSceneNode("point1").getLight();
  285. static_cast<PointLight*>(l)->setRadius(10.0);
  286. }
  287. if(in.getKey(KC_P) == 1)
  288. {
  289. //MainRendererSingleton::get().getPps().getHdr().setExposure(20);
  290. //in.hideCursor(true);
  291. MainRendererSingleton::get().getDbg().setDepthTestEnabled(
  292. !MainRendererSingleton::get().getDbg().getDepthTestEnabled());
  293. }
  294. if(in.getKey(KC_UP)) mover->rotateLocalX(ang);
  295. if(in.getKey(KC_DOWN)) mover->rotateLocalX(-ang);
  296. if(in.getKey(KC_LEFT)) mover->rotateLocalY(ang);
  297. if(in.getKey(KC_RIGHT)) mover->rotateLocalY(-ang);
  298. if(in.getKey(KC_A)) mover->moveLocalX(-dist);
  299. if(in.getKey(KC_D)) mover->moveLocalX(dist);
  300. if(in.getKey(KC_Z)) mover->moveLocalY(dist);
  301. if(in.getKey(KC_SPACE)) mover->moveLocalY(-dist);
  302. if(in.getKey(KC_W)) mover->moveLocalZ(-dist);
  303. if(in.getKey(KC_S)) mover->moveLocalZ(dist);
  304. if(in.getKey(KC_Q)) mover->rotateLocalZ(ang);
  305. if(in.getKey(KC_E)) mover->rotateLocalZ(-ang);
  306. if(in.getKey(KC_PAGEUP))
  307. {
  308. mover->scale(scale);
  309. }
  310. if(in.getKey(KC_PAGEDOWN))
  311. {
  312. mover->scale(-scale);
  313. }
  314. if(in.getKey(KC_P))
  315. {
  316. ANKI_LOGI("pos: " << mover->getWorldTransform().getOrigin());
  317. }
  318. mover->rotateLocalY(-ang * in.getMousePosition().x() * mouseSensivity *
  319. MainRendererSingleton::get().getAspectRatio());
  320. mover->rotateLocalX(ang * in.getMousePosition().y() * mouseSensivity);
  321. execStdinScpripts();
  322. }
  323. //==============================================================================
  324. void mainLoop()
  325. {
  326. ANKI_LOGI("Entering main loop");
  327. HighRezTimer mainLoopTimer;
  328. mainLoopTimer.start();
  329. HighRezTimer::Scalar prevUpdateTime = HighRezTimer::getCurrentTime();
  330. HighRezTimer::Scalar crntTime = prevUpdateTime;
  331. while(1)
  332. {
  333. HighRezTimer timer;
  334. timer.start();
  335. prevUpdateTime = crntTime;
  336. crntTime = HighRezTimer::getCurrentTime();
  337. // Update
  338. //
  339. InputSingleton::get().handleEvents();
  340. InputSingleton::get().moveMouse(Vec2(0.0));
  341. mainLoopExtra();
  342. SceneSingleton::get().update(
  343. prevUpdateTime, crntTime, MainRendererSingleton::get());
  344. EventManagerSingleton::get().updateAllEvents(prevUpdateTime, crntTime);
  345. MainRendererSingleton::get().render(SceneSingleton::get());
  346. if(InputSingleton::get().getKey(KC_ESCAPE))
  347. {
  348. break;
  349. }
  350. win->swapBuffers();
  351. // Sleep
  352. //
  353. #if 1
  354. timer.stop();
  355. if(timer.getElapsedTime() < AppSingleton::get().getTimerTick())
  356. {
  357. HighRezTimer::sleep(AppSingleton::get().getTimerTick()
  358. - timer.getElapsedTime());
  359. }
  360. #else
  361. if(MainRendererSingleton::get().getFramesCount() == 1000)
  362. {
  363. break;
  364. }
  365. #endif
  366. Timestamp::increaseTimestamp();
  367. }
  368. #if 1
  369. MainRendererSingleton::get().takeScreenshot("screenshot.tga");
  370. #endif
  371. ANKI_LOGI("Exiting main loop (" << mainLoopTimer.getElapsedTime()
  372. << " sec)");
  373. }
  374. //==============================================================================
  375. // initSubsystems =
  376. //==============================================================================
  377. void initSubsystems(int argc, char* argv[])
  378. {
  379. #if ANKI_GL == ANKI_GL_DESKTOP
  380. U32 glmajor = 3;
  381. U32 glminor = 3;
  382. #else
  383. U32 glmajor = 3;
  384. U32 glminor = 0;
  385. #endif
  386. // App
  387. AppSingleton::get().init(argc, argv);
  388. // Window
  389. NativeWindowInitializer nwinit;
  390. nwinit.width = 1280;
  391. nwinit.height = 720;
  392. nwinit.majorVersion = glmajor;
  393. nwinit.minorVersion = glminor;
  394. nwinit.depthBits = 0;
  395. nwinit.stencilBits = 0;
  396. win = new NativeWindow;
  397. win->create(nwinit);
  398. // GL stuff
  399. GlStateCommonSingleton::get().init(glmajor, glminor);
  400. // Input
  401. InputSingleton::get().init(win);
  402. InputSingleton::get().hideCursor(true);
  403. // Main renderer
  404. RendererInitializer initializer;
  405. initializer.ms.ez.enabled = true;
  406. initializer.dbg.enabled = false;
  407. initializer.is.sm.bilinearEnabled = true;
  408. initializer.is.groundLightEnabled = false;
  409. initializer.is.sm.enabled = true;
  410. initializer.is.sm.pcfEnabled = false;
  411. initializer.is.sm.resolution = 512;
  412. initializer.pps.hdr.enabled = true;
  413. initializer.pps.hdr.renderingQuality = 0.25;
  414. initializer.pps.hdr.blurringDist = 1.0;
  415. initializer.pps.hdr.blurringIterationsCount = 2;
  416. initializer.pps.hdr.exposure = 8.0;
  417. initializer.pps.ssao.blurringIterationsNum = 4;
  418. initializer.pps.ssao.enabled = true;
  419. initializer.pps.ssao.mainPassRenderingQuality = 0.3;
  420. initializer.pps.ssao.blurringRenderingQuality = 0.5;
  421. initializer.pps.enabled = true;
  422. initializer.pps.bl.enabled = true;
  423. initializer.pps.bl.blurringIterationsNum = 2;
  424. initializer.pps.bl.sideBlurFactor = 1.0;
  425. initializer.renderingQuality = 1.0;
  426. initializer.width = nwinit.width;
  427. initializer.height = nwinit.height;
  428. MainRendererSingleton::get().init(initializer);
  429. // Stdin listener
  430. StdinListenerSingleton::get().start();
  431. // Parallel jobs
  432. ThreadPoolSingleton::get().init(8);
  433. }
  434. //==============================================================================
  435. int main(int argc, char* argv[])
  436. {
  437. int exitCode;
  438. try
  439. {
  440. initSubsystems(argc, argv);
  441. init();
  442. mainLoop();
  443. ANKI_LOGI("Exiting...");
  444. AppSingleton::get().quit(EXIT_SUCCESS);
  445. exitCode = 0;
  446. }
  447. catch(std::exception& e)
  448. {
  449. std::cerr << "Aborting: " << e.what() << std::endl;
  450. exitCode = 1;
  451. }
  452. ANKI_LOGI("Bye!!");
  453. return exitCode;
  454. }