Main.cpp 13 KB

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