Main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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/core/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 1
  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. resources.fixResourceFilename("maps/sponza/scene.lua").toCString(),
  187. File::OpenFlag::READ);
  188. file.readAllText(str);
  189. app->getScriptManager().evalString(str.toCString());
  190. }
  191. #endif
  192. /*AnimationResourcePointer anim;
  193. anim.load("maps/sponza/unnamed_0.ankianim");
  194. AnimationEvent* event;
  195. scene.getEventManager().newEvent(event, anim, cam);*/
  196. // Sectors
  197. #if 0
  198. SectorGroup& sgroup = scene.getSectorGroup();
  199. Sector* sectorA = sgroup.createNewSector(
  200. Aabb(Vec3(-38, -3, -20), Vec3(38, 27, 20)));
  201. Sector* sectorB = sgroup.createNewSector(Aabb(Vec3(-5), Vec3(5)));
  202. sgroup.createNewPortal(sectorA, sectorB, Obb(Vec3(0.0, 3.0, 0.0),
  203. Mat3::getIdentity(), Vec3(1.0, 2.0, 2.0)));
  204. Sector* sectorC = sgroup.createNewSector(
  205. Aabb(Vec3(-30, -10, -35), Vec3(30, 10, -25)));
  206. sgroup.createNewPortal(sectorA, sectorC, Obb(Vec3(-1.1, 2.0, -11.0),
  207. Mat3::getIdentity(), Vec3(1.3, 1.8, 0.5)));
  208. #endif
  209. // Path
  210. /*Path* path = new Path("todo", "path", &scene, MoveComponent::MF_NONE, nullptr);
  211. (void)path;
  212. const F32 distPerSec = 2.0;
  213. scene.getEventManager().newFollowPathEvent(-1.0,
  214. path->getDistance() / distPerSec,
  215. cam, path, distPerSec);*/
  216. #if 0
  217. horse = scene.newSceneNode<ModelNode>("shape0",
  218. "models/collision_test/Cube_Material-material.ankimdl");
  219. horse->setLocalTransform(Transform(Vec4(0.0, 0, 0, 0),
  220. Mat3x4::getIdentity(), 0.01));
  221. horse = scene.newSceneNode<ModelNode>("shape1",
  222. "models/collision_test/Cube.001_Material_001-material.ankimdl");
  223. horse->setLocalTransform(Transform(Vec4(3.1, 2, 0.2, 0),
  224. Mat3x4(Euler(toRad(-13.0), toRad(90.0), toRad(2.0))),
  225. 0.01));
  226. horse->setLocalRotation(Mat3x4(0.135899, -0.534728, 0.834033, 0.000000,
  227. 0.091205, 0.845038, 0.526900, 0.000000 ,
  228. -0.986537, 0.004463, 0.163603, 0.000000));
  229. #endif
  230. }
  231. //==============================================================================
  232. #if 0
  233. /// The func pools the stdinListener for string in the console, if
  234. /// there are any it executes them with scriptingEngine
  235. void execStdinScpripts()
  236. {
  237. while(1)
  238. {
  239. std::string cmd = StdinListenerSingleton::get().getLine();
  240. if(cmd.length() < 1)
  241. {
  242. break;
  243. }
  244. try
  245. {
  246. ScriptManagerSingleton::get().evalString(cmd.c_str());
  247. }
  248. catch(Exception& e)
  249. {
  250. ANKI_LOGE(e.what());
  251. }
  252. }
  253. }
  254. #endif
  255. //==============================================================================
  256. I32 mainLoopExtra(App& app, void*)
  257. {
  258. F32 dist = 0.1;
  259. F32 ang = toRad(2.5);
  260. F32 scale = 0.01;
  261. F32 mouseSensivity = 9.0;
  262. SceneGraph& scene = app.getSceneGraph();
  263. Input& in = app.getInput();
  264. MainRenderer& renderer = app.getMainRenderer();
  265. if(in.getKey(KeyCode::ESCAPE))
  266. {
  267. return 1;
  268. }
  269. // move the camera
  270. static MoveComponent* mover =
  271. &scene.getActiveCamera().getComponent<MoveComponent>();
  272. if(in.getKey(KeyCode::_1))
  273. {
  274. mover = &scene.getActiveCamera();
  275. }
  276. if(in.getKey(KeyCode::_2))
  277. {
  278. mover = &scene.findSceneNode("horse").getComponent<MoveComponent>();
  279. }
  280. if(in.getKey(KeyCode::_3))
  281. {
  282. mover = &scene.findSceneNode("spot0").getComponent<MoveComponent>();
  283. }
  284. if(in.getKey(KeyCode::_4))
  285. {
  286. mover = &scene.findSceneNode("spot1").getComponent<MoveComponent>();
  287. }
  288. if(in.getKey(KeyCode::_5))
  289. {
  290. mover = &scene.findSceneNode("pe").getComponent<MoveComponent>();
  291. }
  292. if(in.getKey(KeyCode::_6))
  293. {
  294. mover = &scene.findSceneNode("shape0").getComponent<MoveComponent>();
  295. }
  296. if(in.getKey(KeyCode::_7))
  297. {
  298. mover = &scene.findSceneNode("shape1").getComponent<MoveComponent>();
  299. }
  300. /*if(in.getKey(KeyCode::L) == 1)
  301. {
  302. SceneNode& l =
  303. SceneGraphSingleton::get().findSceneNode("crate");
  304. Transform trf;
  305. trf.setIdentity();
  306. trf.getOrigin().y() = 20.0;
  307. l.getComponent<MoveComponent>().setLocalTransform(trf);
  308. }*/
  309. if(in.getKey(KeyCode::F1) == 1)
  310. {
  311. renderer.getDbg().setEnabled(!renderer.getDbg().getEnabled());
  312. }
  313. if(in.getKey(KeyCode::F2) == 1)
  314. {
  315. renderer.getDbg().switchBits(Dbg::Flag::SPATIAL);
  316. }
  317. if(in.getKey(KeyCode::F3) == 1)
  318. {
  319. renderer.getDbg().switchBits(Dbg::Flag::PHYSICS);
  320. }
  321. if(in.getKey(KeyCode::F4) == 1)
  322. {
  323. renderer.getDbg().switchBits(Dbg::Flag::SECTOR);
  324. }
  325. if(in.getKey(KeyCode::F5) == 1)
  326. {
  327. renderer.getDbg().switchBits(Dbg::Flag::OCTREE);
  328. }
  329. if(in.getKey(KeyCode::F6) == 1)
  330. {
  331. renderer.getDbg().switchDepthTestEnabled();
  332. }
  333. if(in.getKey(KeyCode::F12) == 1)
  334. {
  335. renderer.takeScreenshot("screenshot.tga");
  336. }
  337. if(in.getKey(KeyCode::UP)) mover->rotateLocalX(ang);
  338. if(in.getKey(KeyCode::DOWN)) mover->rotateLocalX(-ang);
  339. if(in.getKey(KeyCode::LEFT)) mover->rotateLocalY(ang);
  340. if(in.getKey(KeyCode::RIGHT)) mover->rotateLocalY(-ang);
  341. if(in.getKey(KeyCode::A))
  342. {
  343. mover->moveLocalX(-dist);
  344. }
  345. if(in.getKey(KeyCode::D)) mover->moveLocalX(dist);
  346. if(in.getKey(KeyCode::Z)) mover->moveLocalY(dist);
  347. if(in.getKey(KeyCode::SPACE)) mover->moveLocalY(-dist);
  348. if(in.getKey(KeyCode::W)) mover->moveLocalZ(-dist);
  349. if(in.getKey(KeyCode::S)) mover->moveLocalZ(dist);
  350. if(in.getKey(KeyCode::Q)) mover->rotateLocalZ(ang);
  351. if(in.getKey(KeyCode::E)) mover->rotateLocalZ(-ang);
  352. if(in.getKey(KeyCode::PAGEUP))
  353. {
  354. mover->scale(scale);
  355. }
  356. if(in.getKey(KeyCode::PAGEDOWN))
  357. {
  358. mover->scale(-scale);
  359. }
  360. #if 0
  361. if(in.getKey(KeyCode::P) == 1)
  362. {
  363. std::cout << "{Vec3("
  364. << mover->getWorldTransform().getOrigin().toString()
  365. << "), Quat("
  366. << Quat(mover->getWorldTransform().getRotation()).toString()
  367. << ")}," << std::endl;
  368. }
  369. #endif
  370. #if 0
  371. if(in.getKey(KeyCode::L) == 1)
  372. {
  373. try
  374. {
  375. ScriptManagerSingleton::get().evalString(
  376. R"(scene = SceneGraphSingleton.get()
  377. node = scene:tryFindSceneNode("horse")
  378. if Anki.userDataValid(node) == 1 then
  379. print("valid")
  380. else
  381. print("invalid")
  382. end)");
  383. }
  384. catch(Exception& e)
  385. {
  386. ANKI_LOGE(e.what());
  387. }
  388. }
  389. #endif
  390. if(in.getMousePosition() != Vec2(0.0))
  391. {
  392. F32 angY = -ang * in.getMousePosition().x() * mouseSensivity *
  393. renderer.getAspectRatio();
  394. mover->rotateLocalY(angY);
  395. mover->rotateLocalX(ang * in.getMousePosition().y() * mouseSensivity);
  396. }
  397. //execStdinScpripts();
  398. if(getenv("PROFILE") && getGlobTimestamp() == 2000)
  399. {
  400. return 1;
  401. }
  402. return 0;
  403. }
  404. //==============================================================================
  405. void initSubsystems(int argc, char* argv[])
  406. {
  407. // Config
  408. Config config;
  409. config.set("ms.ez.enabled", false);
  410. config.set("ms.ez.maxObjectsToDraw", 100);
  411. config.set("dbg.enabled", false);
  412. config.set("is.sm.bilinearEnabled", true);
  413. config.set("is.groundLightEnabled", true);
  414. config.set("is.sm.enabled", true);
  415. config.set("is.sm.poissonEnabled", true);
  416. config.set("is.sm.resolution", 1024);
  417. config.set("pps.enabled", true);
  418. config.set("pps.hdr.enabled", true);
  419. config.set("pps.hdr.renderingQuality", 0.5);
  420. config.set("pps.hdr.blurringDist", 1.0);
  421. config.set("pps.hdr.blurringIterationsCount", 2);
  422. config.set("pps.hdr.exposure", 10.0);
  423. config.set("pps.hdr.samples", 17);
  424. config.set("pps.sslr.enabled", true);
  425. config.set("pps.sslr.renderingQuality", 0.5);
  426. config.set("pps.sslr.blurringIterationsCount", 1);
  427. config.set("pps.ssao.blurringIterationsCount", 2);
  428. config.set("pps.ssao.enabled", true);
  429. config.set("pps.ssao.renderingQuality", 0.35);
  430. config.set("pps.bl.enabled", true);
  431. config.set("pps.bl.blurringIterationsCount", 2);
  432. config.set("pps.bl.sideBlurFactor", 1.0);
  433. config.set("pps.lf.enabled", true);
  434. config.set("pps.sharpen", true);
  435. config.set("renderingQuality", 1.0);
  436. config.set("width", 1280);
  437. config.set("height", 720);
  438. config.set("lodDistance", 20.0);
  439. config.set("samples", 1);
  440. config.set("tessellation", false);
  441. config.set("tilesXCount", 16);
  442. config.set("tilesYCount", 16);
  443. config.set("fullscreenDesktopResolution", false);
  444. config.set("debugContext", false);
  445. app = new App(config, allocAligned, nullptr);
  446. // Input
  447. app->getInput().lockCursor(true);
  448. app->getInput().hideCursor(true);
  449. app->getInput().moveCursor(Vec2(0.0));
  450. }
  451. //==============================================================================
  452. int main(int argc, char* argv[])
  453. {
  454. int exitCode;
  455. try
  456. {
  457. initSubsystems(argc, argv);
  458. init();
  459. app->mainLoop(mainLoopExtra, nullptr);
  460. ANKI_LOGI("Exiting...");
  461. exitCode = 0;
  462. }
  463. catch(std::exception& e)
  464. {
  465. ANKI_LOGE("Aborting: %s", e.what());
  466. exitCode = 1;
  467. }
  468. ANKI_LOGI("Bye!!");
  469. return exitCode;
  470. }