Main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include "anki/input/Input.h"
  5. #include "anki/Math.h"
  6. #include "anki/renderer/Renderer.h"
  7. #include "anki/core/App.h"
  8. #include "anki/resource/Mesh.h"
  9. #include "anki/resource/Material.h"
  10. #include "anki/resource/SkelAnim.h"
  11. #include "anki/renderer/Renderer.h"
  12. #include "anki/renderer/MainRenderer.h"
  13. #include "anki/script/ScriptManager.h"
  14. #include "anki/core/StdinListener.h"
  15. #include "anki/resource/Model.h"
  16. #include "anki/util/Logger.h"
  17. #include "anki/Util.h"
  18. #include "anki/resource/Skin.h"
  19. #include "anki/event/EventManager.h"
  20. #include "anki/event/MainRendererPpsHdrEvent.h"
  21. #include "anki/resource/Material.h"
  22. #include "anki/core/Timestamp.h"
  23. #include "anki/core/NativeWindow.h"
  24. #include "anki/Scene.h"
  25. #include "anki/event/LightEvent.h"
  26. #include "anki/event/AnimationEvent.h"
  27. #include "anki/event/MoveEvent.h"
  28. #include "anki/core/Counters.h"
  29. #include "anki/core/Config.h"
  30. using namespace anki;
  31. App* app;
  32. ModelNode* horse;
  33. PerspectiveCamera* cam;
  34. //==============================================================================
  35. void init()
  36. {
  37. ANKI_LOGI("Other init...");
  38. SceneGraph& scene = app->getSceneGraph();
  39. MainRenderer& renderer = app->getMainRenderer();
  40. ResourceManager& resources = app->getResourceManager();
  41. scene.setAmbientColor(Vec4(0.1, 0.05, 0.05, 0.0) * 3);
  42. if(getenv("PROFILE"))
  43. {
  44. app->setTimerTick(0.0);
  45. }
  46. #if 0
  47. painter = new UiPainter(Vec2(AppSingleton::get().getWindowWidth(),
  48. AppSingleton::get().getWindowHeight()));
  49. painter->setFont("engine-rsrc/ModernAntiqua.ttf", 25, 25);
  50. #endif
  51. // camera
  52. cam = scene.newSceneNode<PerspectiveCamera>("main-camera");
  53. const F32 ang = 45.0;
  54. cam->setAll(
  55. renderer.getAspectRatio() * toRad(ang),
  56. toRad(ang), 0.5, 500.0);
  57. cam->setLocalTransform(Transform(Vec4(17.0, 5.2, 0.0, 0),
  58. Mat3x4(Euler(toRad(-10.0), toRad(90.0), toRad(0.0))),
  59. 1.0));
  60. scene.setActiveCamera(cam);
  61. // lights
  62. #if 1
  63. Vec3 lpos(-24.0, 0.1, -10.0);
  64. for(int i = 0; i < 50; i++)
  65. {
  66. for(int j = 0; j < 10; j++)
  67. {
  68. std::string name = "plight" + std::to_string(i) + std::to_string(j);
  69. PointLight* point = scene.newSceneNode<PointLight>(name.c_str());
  70. point->setRadius(0.5);
  71. point->setDiffuseColor(Vec4(randFloat(6.0) - 2.0,
  72. randFloat(6.0) - 2.0, randFloat(6.0) - 2.0, 0.0));
  73. point->setSpecularColor(Vec4(randFloat(6.0) - 3.0,
  74. randFloat(6.0) - 3.0, randFloat(6.0) - 3.0, 0.0));
  75. point->setLocalOrigin(lpos.xyz0());
  76. lpos.z() += 2.0;
  77. }
  78. lpos.x() += 0.93;
  79. lpos.z() = -10;
  80. }
  81. #endif
  82. #if 1
  83. SpotLight* spot = scene.newSceneNode<SpotLight>("spot0");
  84. spot->setOuterAngle(toRad(45.0));
  85. spot->setInnerAngle(toRad(15.0));
  86. spot->setLocalTransform(Transform(Vec4(8.27936, 5.86285, 1.85526, 0.0),
  87. Mat3x4(Quat(-0.125117, 0.620465, 0.154831, 0.758544)), 1.0));
  88. spot->setDiffuseColor(Vec4(1.0));
  89. spot->setSpecularColor(Vec4(-1.0));
  90. spot->setDistance(30.0);
  91. spot->setShadowEnabled(true);
  92. #endif
  93. #if 1
  94. spot = scene.newSceneNode<SpotLight>("spot1");
  95. spot->setOuterAngle(toRad(45.0));
  96. spot->setInnerAngle(toRad(15.0));
  97. spot->setLocalTransform(Transform(Vec4(5.3, 4.3, 3.0, 0.0),
  98. Mat3x4::getIdentity(), 1.0));
  99. spot->setDiffuseColor(Vec4(3.0, 0.0, 0.0, 0.0));
  100. spot->setSpecularColor(Vec4(3.0, 3.0, 0.0, 0.0));
  101. spot->setDistance(30.0);
  102. spot->setShadowEnabled(true);
  103. #endif
  104. #if 0
  105. // Vase point lights
  106. F32 x = 8.5;
  107. F32 y = 2.25;
  108. F32 z = 2.49;
  109. Array<Vec3, 4> vaseLightPos = {{Vec3(x, y, -z - 1.4), Vec3(x, y, z),
  110. Vec3(-x - 2.3, y, z), Vec3(-x - 2.3, y, -z - 1.4)}};
  111. for(U i = 0; i < vaseLightPos.getSize(); i++)
  112. {
  113. Vec4 lightPos = vaseLightPos[i].xyz0();
  114. PointLight* point = scene.newSceneNode<PointLight>(
  115. ("vase_plight" + std::to_string(i)).c_str());
  116. point->loadLensFlare("textures/lens_flare/flares0.ankitex");
  117. point->setRadius(2.0);
  118. point->setLocalOrigin(lightPos);
  119. point->setDiffuseColor(Vec4(3.0, 0.2, 0.0, 0.0));
  120. point->setSpecularColor(Vec4(1.0, 1.0, 0.0, 0.0));
  121. point->setLensFlaresStretchMultiplier(Vec2(10.0, 1.0));
  122. point->setLensFlaresAlpha(1.0);
  123. LightEventData eventData;
  124. eventData.radiusMultiplier = 0.2;
  125. eventData.intensityMultiplier = Vec4(-1.2, 0.0, 0.0, 0.0);
  126. eventData.specularIntensityMultiplier = Vec4(0.1, 0.1, 0.0, 0.0);
  127. LightEvent* event;
  128. scene.getEventManager().newEvent(event, 0.0, 0.8, point, eventData);
  129. event->enableBits(Event::EF_REANIMATE);
  130. MoveEventData moveData;
  131. moveData.m_posMin = Vec4(-0.5, 0.0, -0.5, 0);
  132. moveData.m_posMax = Vec4(0.5, 0.0, 0.5, 0);
  133. MoveEvent* mevent;
  134. scene.getEventManager().newEvent(mevent, 0.0, 2.0, point, moveData);
  135. mevent->enableBits(Event::EF_REANIMATE);
  136. ParticleEmitter* pe;
  137. /**/
  138. if(i == 0)
  139. {
  140. pe = scene.newSceneNode<ParticleEmitter>(
  141. "pefire", "particles/fire.ankipart");
  142. pe->setLocalOrigin(lightPos);
  143. pe = scene.newSceneNode<ParticleEmitter>(
  144. "pesmoke", "particles/smoke.ankipart");
  145. pe->setLocalOrigin(lightPos);
  146. }
  147. else
  148. {
  149. InstanceNode* instance;
  150. instance = scene.newSceneNode<InstanceNode>(
  151. ("pefire_inst" + std::to_string(i)).c_str());
  152. instance->setLocalOrigin(lightPos);
  153. SceneNode& sn = scene.findSceneNode("pefire");
  154. sn.addChild(instance);
  155. instance = scene.newSceneNode<InstanceNode>(
  156. ("pesmoke_inst" + std::to_string(i)).c_str());
  157. instance->setLocalOrigin(lightPos);
  158. scene.findSceneNode("pesmoke").addChild(instance);
  159. }
  160. /*{
  161. scene.newSceneNode(pe, ("pesparks" + std::to_string(i)).c_str(),
  162. "particles/sparks.ankipart");
  163. pe->setLocalOrigin(lightPos);
  164. }*/
  165. }
  166. #endif
  167. #if 1
  168. // horse
  169. horse = scene.newSceneNode<ModelNode>("horse", "models/horse/horse.ankimdl");
  170. horse->setLocalTransform(
  171. Transform(Vec4(-2, 0, 0, 0.0), Mat3x4::getIdentity(), 0.7));
  172. //horse = scene.newSceneNode<ModelNode>("crate", "models/crate0/crate0.ankimdl");
  173. //horse->setLocalTransform(Transform(Vec3(2, 10.0, 0), Mat3::getIdentity(),
  174. // 1.0));
  175. // barrel
  176. /*ModelNode* redBarrel = new ModelNode(
  177. "red_barrel", &scene, nullptr, MoveComponent::MF_NONE,
  178. "models/red_barrel/red_barrel.mdl");
  179. redBarrel->setLocalTransform(Transform(Vec3(+2, 0, 0), Mat3::getIdentity(),
  180. 0.7));*/
  181. #endif
  182. #if 1
  183. {
  184. String str(app->getAllocator());
  185. File file;
  186. file.open(
  187. resources.fixResourceFilename("maps/sponza/scene.lua").toCString(),
  188. File::OpenFlag::READ);
  189. file.readAllText(str);
  190. app->getScriptManager().evalString(str.toCString());
  191. }
  192. #endif
  193. /*AnimationResourcePointer anim;
  194. anim.load("maps/sponza/unnamed_0.ankianim");
  195. AnimationEvent* event;
  196. scene.getEventManager().newEvent(event, anim, cam);*/
  197. // Sectors
  198. #if 0
  199. SectorGroup& sgroup = scene.getSectorGroup();
  200. Sector* sectorA = sgroup.createNewSector(
  201. Aabb(Vec3(-38, -3, -20), Vec3(38, 27, 20)));
  202. Sector* sectorB = sgroup.createNewSector(Aabb(Vec3(-5), Vec3(5)));
  203. sgroup.createNewPortal(sectorA, sectorB, Obb(Vec3(0.0, 3.0, 0.0),
  204. Mat3::getIdentity(), Vec3(1.0, 2.0, 2.0)));
  205. Sector* sectorC = sgroup.createNewSector(
  206. Aabb(Vec3(-30, -10, -35), Vec3(30, 10, -25)));
  207. sgroup.createNewPortal(sectorA, sectorC, Obb(Vec3(-1.1, 2.0, -11.0),
  208. Mat3::getIdentity(), Vec3(1.3, 1.8, 0.5)));
  209. #endif
  210. // Path
  211. /*Path* path = new Path("todo", "path", &scene, MoveComponent::MF_NONE, nullptr);
  212. (void)path;
  213. const F32 distPerSec = 2.0;
  214. scene.getEventManager().newFollowPathEvent(-1.0,
  215. path->getDistance() / distPerSec,
  216. cam, path, distPerSec);*/
  217. #if 0
  218. horse = scene.newSceneNode<ModelNode>("shape0",
  219. "models/collision_test/Cube_Material-material.ankimdl");
  220. horse->setLocalTransform(Transform(Vec4(0.0, 0, 0, 0),
  221. Mat3x4::getIdentity(), 0.01));
  222. horse = scene.newSceneNode<ModelNode>("shape1",
  223. "models/collision_test/Cube.001_Material_001-material.ankimdl");
  224. horse->setLocalTransform(Transform(Vec4(3.1, 2, 0.2, 0),
  225. Mat3x4(Euler(toRad(-13.0), toRad(90.0), toRad(2.0))),
  226. 0.01));
  227. horse->setLocalRotation(Mat3x4(0.135899, -0.534728, 0.834033, 0.000000,
  228. 0.091205, 0.845038, 0.526900, 0.000000 ,
  229. -0.986537, 0.004463, 0.163603, 0.000000));
  230. #endif
  231. }
  232. //==============================================================================
  233. #if 0
  234. /// The func pools the stdinListener for string in the console, if
  235. /// there are any it executes them with scriptingEngine
  236. void execStdinScpripts()
  237. {
  238. while(1)
  239. {
  240. std::string cmd = StdinListenerSingleton::get().getLine();
  241. if(cmd.length() < 1)
  242. {
  243. break;
  244. }
  245. try
  246. {
  247. ScriptManagerSingleton::get().evalString(cmd.c_str());
  248. }
  249. catch(Exception& e)
  250. {
  251. ANKI_LOGE(e.what());
  252. }
  253. }
  254. }
  255. #endif
  256. //==============================================================================
  257. I32 mainLoopExtra(App& app, void*)
  258. {
  259. F32 dist = 0.1;
  260. F32 ang = toRad(2.5);
  261. F32 scale = 0.01;
  262. F32 mouseSensivity = 9.0;
  263. SceneGraph& scene = app.getSceneGraph();
  264. Input& in = app.getInput();
  265. MainRenderer& renderer = app.getMainRenderer();
  266. if(in.getKey(KeyCode::ESCAPE))
  267. {
  268. return 1;
  269. }
  270. // move the camera
  271. static MoveComponent* mover =
  272. &scene.getActiveCamera().getComponent<MoveComponent>();
  273. if(in.getKey(KeyCode::_1))
  274. {
  275. mover = &scene.getActiveCamera();
  276. }
  277. if(in.getKey(KeyCode::_2))
  278. {
  279. mover = &scene.findSceneNode("horse").getComponent<MoveComponent>();
  280. }
  281. if(in.getKey(KeyCode::_3))
  282. {
  283. mover = &scene.findSceneNode("spot0").getComponent<MoveComponent>();
  284. }
  285. if(in.getKey(KeyCode::_4))
  286. {
  287. mover = &scene.findSceneNode("spot1").getComponent<MoveComponent>();
  288. }
  289. if(in.getKey(KeyCode::_5))
  290. {
  291. mover = &scene.findSceneNode("pe").getComponent<MoveComponent>();
  292. }
  293. if(in.getKey(KeyCode::_6))
  294. {
  295. mover = &scene.findSceneNode("shape0").getComponent<MoveComponent>();
  296. }
  297. if(in.getKey(KeyCode::_7))
  298. {
  299. mover = &scene.findSceneNode("shape1").getComponent<MoveComponent>();
  300. }
  301. /*if(in.getKey(KeyCode::L) == 1)
  302. {
  303. SceneNode& l =
  304. SceneGraphSingleton::get().findSceneNode("crate");
  305. Transform trf;
  306. trf.setIdentity();
  307. trf.getOrigin().y() = 20.0;
  308. l.getComponent<MoveComponent>().setLocalTransform(trf);
  309. }*/
  310. if(in.getKey(KeyCode::F1) == 1)
  311. {
  312. renderer.getDbg().setEnabled(!renderer.getDbg().getEnabled());
  313. }
  314. if(in.getKey(KeyCode::F2) == 1)
  315. {
  316. renderer.getDbg().switchBits(Dbg::Flag::SPATIAL);
  317. }
  318. if(in.getKey(KeyCode::F3) == 1)
  319. {
  320. renderer.getDbg().switchBits(Dbg::Flag::PHYSICS);
  321. }
  322. if(in.getKey(KeyCode::F4) == 1)
  323. {
  324. renderer.getDbg().switchBits(Dbg::Flag::SECTOR);
  325. }
  326. if(in.getKey(KeyCode::F5) == 1)
  327. {
  328. renderer.getDbg().switchBits(Dbg::Flag::OCTREE);
  329. }
  330. if(in.getKey(KeyCode::F6) == 1)
  331. {
  332. renderer.getDbg().switchDepthTestEnabled();
  333. }
  334. if(in.getKey(KeyCode::F12) == 1)
  335. {
  336. renderer.takeScreenshot("screenshot.tga");
  337. }
  338. if(in.getKey(KeyCode::UP)) mover->rotateLocalX(ang);
  339. if(in.getKey(KeyCode::DOWN)) mover->rotateLocalX(-ang);
  340. if(in.getKey(KeyCode::LEFT)) mover->rotateLocalY(ang);
  341. if(in.getKey(KeyCode::RIGHT)) mover->rotateLocalY(-ang);
  342. if(in.getKey(KeyCode::A))
  343. {
  344. mover->moveLocalX(-dist);
  345. }
  346. if(in.getKey(KeyCode::D)) mover->moveLocalX(dist);
  347. if(in.getKey(KeyCode::Z)) mover->moveLocalY(dist);
  348. if(in.getKey(KeyCode::SPACE)) mover->moveLocalY(-dist);
  349. if(in.getKey(KeyCode::W)) mover->moveLocalZ(-dist);
  350. if(in.getKey(KeyCode::S)) mover->moveLocalZ(dist);
  351. if(in.getKey(KeyCode::Q)) mover->rotateLocalZ(ang);
  352. if(in.getKey(KeyCode::E)) mover->rotateLocalZ(-ang);
  353. if(in.getKey(KeyCode::PAGEUP))
  354. {
  355. mover->scale(scale);
  356. }
  357. if(in.getKey(KeyCode::PAGEDOWN))
  358. {
  359. mover->scale(-scale);
  360. }
  361. #if 0
  362. if(in.getKey(KeyCode::P) == 1)
  363. {
  364. std::cout << "{Vec3("
  365. << mover->getWorldTransform().getOrigin().toString()
  366. << "), Quat("
  367. << Quat(mover->getWorldTransform().getRotation()).toString()
  368. << ")}," << std::endl;
  369. }
  370. #endif
  371. #if 0
  372. if(in.getKey(KeyCode::L) == 1)
  373. {
  374. try
  375. {
  376. ScriptManagerSingleton::get().evalString(
  377. R"(scene = SceneGraphSingleton.get()
  378. node = scene:tryFindSceneNode("horse")
  379. if Anki.userDataValid(node) == 1 then
  380. print("valid")
  381. else
  382. print("invalid")
  383. end)");
  384. }
  385. catch(Exception& e)
  386. {
  387. ANKI_LOGE(e.what());
  388. }
  389. }
  390. #endif
  391. if(in.getMousePosition() != Vec2(0.0))
  392. {
  393. F32 angY = -ang * in.getMousePosition().x() * mouseSensivity *
  394. renderer.getAspectRatio();
  395. mover->rotateLocalY(angY);
  396. mover->rotateLocalX(ang * in.getMousePosition().y() * mouseSensivity);
  397. }
  398. //execStdinScpripts();
  399. if(getenv("PROFILE") && getGlobTimestamp() == 2000)
  400. {
  401. return 1;
  402. }
  403. return 0;
  404. }
  405. //==============================================================================
  406. void initSubsystems(int argc, char* argv[])
  407. {
  408. // Config
  409. Config config;
  410. config.set("ms.ez.enabled", false);
  411. config.set("ms.ez.maxObjectsToDraw", 100);
  412. config.set("dbg.enabled", false);
  413. config.set("is.sm.bilinearEnabled", true);
  414. config.set("is.groundLightEnabled", true);
  415. config.set("is.sm.enabled", true);
  416. config.set("is.sm.poissonEnabled", true);
  417. config.set("is.sm.resolution", 1024);
  418. config.set("pps.enabled", true);
  419. config.set("pps.hdr.enabled", true);
  420. config.set("pps.hdr.renderingQuality", 0.5);
  421. config.set("pps.hdr.blurringDist", 1.0);
  422. config.set("pps.hdr.blurringIterationsCount", 2);
  423. config.set("pps.hdr.exposure", 10.0);
  424. config.set("pps.hdr.samples", 17);
  425. config.set("pps.sslr.enabled", true);
  426. config.set("pps.sslr.renderingQuality", 0.5);
  427. config.set("pps.sslr.blurringIterationsCount", 1);
  428. config.set("pps.ssao.blurringIterationsCount", 2);
  429. config.set("pps.ssao.enabled", true);
  430. config.set("pps.ssao.renderingQuality", 0.35);
  431. config.set("pps.bl.enabled", true);
  432. config.set("pps.bl.blurringIterationsCount", 2);
  433. config.set("pps.bl.sideBlurFactor", 1.0);
  434. config.set("pps.lf.enabled", true);
  435. config.set("pps.sharpen", true);
  436. config.set("renderingQuality", 1.0);
  437. config.set("width", 1280);
  438. config.set("height", 720);
  439. config.set("lodDistance", 20.0);
  440. config.set("samples", 1);
  441. config.set("tessellation", true);
  442. config.set("tilesXCount", 16);
  443. config.set("tilesYCount", 16);
  444. config.set("fullscreenDesktopResolution", true);
  445. config.set("debugContext", false);
  446. app = new App(config, allocAligned, nullptr);
  447. // Input
  448. app->getInput().lockCursor(true);
  449. app->getInput().hideCursor(true);
  450. app->getInput().moveCursor(Vec2(0.0));
  451. }
  452. //==============================================================================
  453. int main(int argc, char* argv[])
  454. {
  455. int exitCode;
  456. try
  457. {
  458. initSubsystems(argc, argv);
  459. init();
  460. app->mainLoop(mainLoopExtra, nullptr);
  461. ANKI_LOGI("Exiting...");
  462. exitCode = 0;
  463. }
  464. catch(std::exception& e)
  465. {
  466. ANKI_LOGE("Aborting: %s", e.what());
  467. exitCode = 1;
  468. }
  469. ANKI_LOGI("Bye!!");
  470. return exitCode;
  471. }