Main.cpp 14 KB

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