Main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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/physics/Character.h"
  12. #include "anki/renderer/Renderer.h"
  13. #include "anki/renderer/MainRenderer.h"
  14. #include "anki/physics/Character.h"
  15. #include "anki/physics/RigidBody.h"
  16. #include "anki/script/ScriptManager.h"
  17. #include "anki/core/StdinListener.h"
  18. #include "anki/resource/Model.h"
  19. #include "anki/core/Logger.h"
  20. #include "anki/Util.h"
  21. #include "anki/resource/Skin.h"
  22. #include "anki/resource/ShaderProgramPrePreprocessor.h"
  23. #include "anki/resource/Material.h"
  24. #include "anki/core/ThreadPool.h"
  25. #include "anki/core/NativeWindow.h"
  26. #include "anki/core/Counters.h"
  27. #include "anki/Scene.h"
  28. #include "anki/Event.h"
  29. using namespace anki;
  30. //==============================================================================
  31. struct LogFile
  32. {
  33. ANKI_HAS_SLOTS(LogFile)
  34. void handler(const Logger::Info& info)
  35. {
  36. const char* x = nullptr;
  37. switch(info.type)
  38. {
  39. case Logger::LMT_NORMAL:
  40. x = "Info";
  41. break;
  42. case Logger::LMT_ERROR:
  43. x = "Error";
  44. break;
  45. case Logger::LMT_WARNING:
  46. x = "Warn";
  47. break;
  48. }
  49. file.writeText("(%s:%d %s) %s: %s\n",
  50. info.file, info.line, info.func, x, info.msg);
  51. }
  52. ANKI_SLOT(handler, const Logger::Info&)
  53. File file;
  54. };
  55. static LogFile logfile;
  56. //==============================================================================
  57. void initSubsystems()
  58. {
  59. #if ANKI_OS == ANKI_OS_ANDROID
  60. // Log file
  61. logfile.file.open("/sdcard/anki.log", File::OF_WRITE);
  62. ANKI_CONNECT(&LoggerSingleton::get(), messageRecieved, &logfile, handler);
  63. #endif
  64. // App
  65. AppSingleton::get().init();
  66. // Window
  67. NativeWindowInitializer nwinit;
  68. nwinit.width = 1280;
  69. nwinit.height = 720;
  70. #if ANKI_GL == ANKI_GL_ES
  71. nwinit.majorVersion = 3;
  72. nwinit.minorVersion = 0;
  73. #else
  74. nwinit.majorVersion = 3;
  75. nwinit.minorVersion = 3;
  76. #endif
  77. nwinit.depthBits = 0;
  78. nwinit.stencilBits = 0;
  79. nwinit.fullscreenDesktopRez = true;
  80. nwinit.debugContext = false;
  81. NativeWindowSingleton::get().create(nwinit);
  82. // GL stuff
  83. GlStateCommonSingleton::get().init(
  84. nwinit.majorVersion, nwinit.minorVersion, nwinit.debugContext);
  85. // Input
  86. InputSingleton::get().init(&NativeWindowSingleton::get());
  87. InputSingleton::get().lockCursor(true);
  88. InputSingleton::get().hideCursor(true);
  89. InputSingleton::get().moveCursor(Vec2(0.0));
  90. // Main renderer
  91. RendererInitializer initializer;
  92. initializer.get("ms.ez.enabled") = false;
  93. initializer.get("ms.ez.maxObjectsToDraw") = 100;
  94. initializer.get("dbg.enabled") = false;
  95. initializer.get("is.sm.bilinearEnabled") = true;
  96. initializer.get("is.groundLightEnabled") = true;
  97. initializer.get("is.sm.enabled") = true;
  98. initializer.get("is.sm.pcfEnabled") = false;
  99. initializer.get("is.sm.resolution") = 512;
  100. initializer.get("pps.enabled") = true;
  101. initializer.get("pps.hdr.enabled") = true;
  102. initializer.get("pps.hdr.renderingQuality") = 0.5;
  103. initializer.get("pps.hdr.blurringDist") = 1.0;
  104. initializer.get("pps.hdr.blurringIterationsCount") = 1;
  105. initializer.get("pps.hdr.exposure") = 8.0;
  106. initializer.get("pps.ssao.enabled") = false;
  107. initializer.get("pps.ssao.blurringIterationsNum") = 1;
  108. initializer.get("pps.ssao.renderingQuality") = 0.25;
  109. initializer.get("pps.bl.enabled") = true;
  110. initializer.get("pps.bl.blurringIterationsNum") = 2;
  111. initializer.get("pps.bl.sideBlurFactor") = 1.0;
  112. initializer.get("pps.lf.enabled") = true;
  113. initializer.get("pps.sharpen") = true;
  114. initializer.get("renderingQuality") = 1.0;
  115. initializer.get("width") = NativeWindowSingleton::get().getWidth();
  116. initializer.get("height") = NativeWindowSingleton::get().getHeight();
  117. initializer.get("lodDistance") = 20.0;
  118. initializer.get("samples") = 16;
  119. //#if ANKI_GL == ANKI_GL_ES
  120. #if 1
  121. initializer.get("samples") = 1;
  122. initializer.get("is.groundLightEnabled") = false;
  123. initializer.get("is.maxPointLights") = 64;
  124. initializer.get("is.maxPointLightsPerTile") = 4;
  125. initializer.get("is.maxSpotLightsPerTile") = 4;
  126. initializer.get("is.maxSpotTexLightsPerTile") = 4;
  127. initializer.get("renderingQuality") = 0.5;
  128. initializer.get("pps.hdr.renderingQuality") = 0.3;
  129. initializer.get("maxTextureSize") = 1024;
  130. initializer.get("mrt") = false;
  131. initializer.get("pps.sharpen") = false;
  132. initializer.get("pps.gammaCorrection") = false;
  133. #endif
  134. MainRendererSingleton::get().init(initializer);
  135. // Stdin listener
  136. StdinListenerSingleton::get().start();
  137. // Parallel jobs
  138. ThreadPoolSingleton::get().init(getCpuCoresCount());
  139. }
  140. //==============================================================================
  141. void initScene()
  142. {
  143. SceneGraph& scene = SceneGraphSingleton::get();
  144. scene.setAmbientColor(Vec4(0.1, 0.05, 0.05, 0.0) * 2);
  145. PerspectiveCamera* cam = nullptr;
  146. scene.newSceneNode(
  147. cam, "main_camera");
  148. const F32 ang = 45.0;
  149. cam->setAll(
  150. MainRendererSingleton::get().getAspectRatio() * toRad(ang),
  151. toRad(ang), 0.5, 500.0);
  152. cam->setLocalTransform(Transform(Vec3(18.0, 5.2, 0.0),
  153. Mat3(Euler(toRad(-10.0), toRad(90.0), toRad(0.0))),
  154. 1.0));
  155. scene.setActiveCamera(cam);
  156. #if 1
  157. AnimationResourcePointer anim;
  158. anim.load("maps/sponza/animation_0.ankianim");
  159. AnimationEvent* event;
  160. scene.getEventManager().newEvent(event, anim, cam);
  161. #endif
  162. #if 1
  163. F32 x = 8.5;
  164. F32 y = 2.25;
  165. F32 z = 2.49;
  166. Array<Vec3, 4> vaseLightPos = {{Vec3(x, y, -z - 1.4), Vec3(x, y, z),
  167. Vec3(-x - 2.3, y, z), Vec3(-x - 2.3, y, -z - 1.4)}};
  168. for(U i = 0; i < vaseLightPos.getSize(); i++)
  169. {
  170. Vec3 lightPos = vaseLightPos[i];
  171. PointLight* point;
  172. scene.newSceneNode(point, ("vase_plight" + std::to_string(i)).c_str());
  173. point->loadLensFlare("textures/lens_flare/flares0.ankitex");
  174. point->setRadius(2.0);
  175. point->setLocalOrigin(lightPos);
  176. point->setDiffuseColor(Vec4(3.0, 0.2, 0.0, 0.0));
  177. point->setSpecularColor(Vec4(1.0, 1.0, 0.0, 0.0));
  178. point->setLensFlaresStretchMultiplier(Vec2(10.0, 1.0));
  179. point->setLensFlaresAlpha(1.0);
  180. LightEventData eventData;
  181. eventData.radiusMultiplier = 0.2;
  182. eventData.intensityMultiplier = Vec4(-1.2, 0.0, 0.0, 0.0);
  183. eventData.specularIntensityMultiplier = Vec4(0.1, 0.1, 0.0, 0.0);
  184. LightEvent* event;
  185. scene.getEventManager().newEvent(event, 0.0, 0.8, point, eventData);
  186. event->enableBits(Event::EF_REANIMATE);
  187. MoveEventData moveData;
  188. moveData.posMin = Vec3(-0.5, 0.0, -0.5);
  189. moveData.posMax = Vec3(0.5, 0.0, 0.5);
  190. MoveEvent* mevent;
  191. scene.getEventManager().newEvent(mevent, 0.0, 2.0, point, moveData);
  192. mevent->enableBits(Event::EF_REANIMATE);
  193. ParticleEmitter* pe;
  194. scene.newSceneNode(pe,
  195. ("pe" + std::to_string(i)).c_str(), "particles/smoke.ankipart");
  196. pe->setLocalOrigin(lightPos);
  197. scene.newSceneNode(pe,
  198. ("pef" + std::to_string(i)).c_str(), "particles/fire.ankipart");
  199. pe->setLocalOrigin(lightPos);
  200. }
  201. #endif
  202. // horse
  203. ModelNode* horse;
  204. scene.newSceneNode(horse, "horse", "models/horse/horse.ankimdl");
  205. horse->setLocalTransform(Transform(Vec3(-2, 0, 0), Mat3::getIdentity(),
  206. 0.7));
  207. // Light
  208. SpotLight* spot;
  209. scene.newSceneNode(spot, "spot0");
  210. spot->setOuterAngle(toRad(45.0));
  211. spot->setInnerAngle(toRad(15.0));
  212. spot->setLocalTransform(Transform(Vec3(8.27936, 5.86285, 1.85526),
  213. Mat3(Quat(-0.125117, 0.620465, 0.154831, 0.758544)), 1.0));
  214. spot->setDiffuseColor(Vec4(2.0));
  215. spot->setSpecularColor(Vec4(1.0, 0.0, 1.0, 1.0));
  216. spot->setDistance(30.0);
  217. spot->setShadowEnabled(true);
  218. // Scene
  219. scene.load("maps/sponza/master.ankiscene");
  220. PointLight* pl;
  221. scene.newSceneNode(pl, "pl0");
  222. pl->setRadius(12.5);
  223. pl->setDiffuseColor(Vec4(0.5, 0.3, 0.2, 1.0));
  224. pl->setSpecularColor(Vec4(0.1, 0.0, 0.0, 1.0));
  225. pl->setLocalOrigin(Vec3(10, 2.0, -0.8));
  226. scene.newSceneNode(pl, "pl1");
  227. pl->setRadius(12.5);
  228. pl->setDiffuseColor(Vec4(0.5, 0.3, 0.2, 1.0));
  229. pl->setSpecularColor(Vec4(0.1, 0.0, 0.0, 1.0));
  230. pl->setLocalOrigin(Vec3(0, 2.0, -0.8));
  231. scene.newSceneNode(pl, "pl2");
  232. pl->setRadius(12.5);
  233. pl->setDiffuseColor(Vec4(0.5, 0.3, 0.2, 1.0));
  234. pl->setSpecularColor(Vec4(0.1, 0.0, 0.0, 1.0));
  235. pl->setLocalOrigin(Vec3(-11, 2.0, -0.8));
  236. }
  237. //==============================================================================
  238. #if ANKI_OS == ANKI_OS_ANDROID
  239. static void handleEvents(android_app* app, int32_t cmd)
  240. {
  241. switch(cmd)
  242. {
  243. case APP_CMD_SAVE_STATE:
  244. ANKI_LOGI("APP_CMD_SAVE_STATE");
  245. break;
  246. case APP_CMD_INIT_WINDOW:
  247. ANKI_LOGI("APP_CMD_INIT_WINDOW");
  248. break;
  249. case APP_CMD_TERM_WINDOW:
  250. ANKI_LOGI("APP_CMD_TERM_WINDOW");
  251. break;
  252. case APP_CMD_GAINED_FOCUS:
  253. ANKI_LOGI("APP_CMD_GAINED_FOCUS");
  254. break;
  255. case APP_CMD_LOST_FOCUS:
  256. ANKI_LOGI("APP_CMD_LOST_FOCUS");
  257. break;
  258. }
  259. }
  260. #endif
  261. //==============================================================================
  262. static Bool mainLoopExtra()
  263. {
  264. const F32 dist = 0.2;
  265. const F32 ang = toRad(3.0);
  266. const F32 mouseSensivity = 9.0;
  267. Input& in = InputSingleton::get();
  268. MoveComponent* mover =
  269. SceneGraphSingleton::get().getActiveCamera().getMoveComponent();
  270. if(in.getKey(KC_UP)) mover->rotateLocalX(ang);
  271. if(in.getKey(KC_DOWN)) mover->rotateLocalX(-ang);
  272. if(in.getKey(KC_LEFT)) mover->rotateLocalY(ang);
  273. if(in.getKey(KC_RIGHT)) mover->rotateLocalY(-ang);
  274. if(in.getKey(KC_A)) mover->moveLocalX(-dist);
  275. if(in.getKey(KC_D)) mover->moveLocalX(dist);
  276. if(in.getKey(KC_Z)) mover->moveLocalY(dist);
  277. if(in.getKey(KC_SPACE)) mover->moveLocalY(-dist);
  278. if(in.getKey(KC_W)) mover->moveLocalZ(-dist);
  279. if(in.getKey(KC_S)) mover->moveLocalZ(dist);
  280. if(in.getKey(KC_Q)) mover->rotateLocalZ(ang);
  281. if(in.getKey(KC_E)) mover->rotateLocalZ(-ang);
  282. if(in.getMousePosition() != Vec2(0.0))
  283. {
  284. F32 angY = -ang * in.getMousePosition().x() * mouseSensivity *
  285. MainRendererSingleton::get().getAspectRatio();
  286. mover->rotateLocalY(angY);
  287. mover->rotateLocalX(ang * in.getMousePosition().y() * mouseSensivity);
  288. }
  289. if(InputSingleton::get().getKey(KC_ESCAPE))
  290. {
  291. return false;
  292. }
  293. return true;
  294. }
  295. //==============================================================================
  296. static void mainLoop()
  297. {
  298. ANKI_LOGI("Entering main loop");
  299. HighRezTimer::Scalar prevUpdateTime = HighRezTimer::getCurrentTime();
  300. HighRezTimer::Scalar crntTime = prevUpdateTime;
  301. SceneGraph& scene = SceneGraphSingleton::get();
  302. MainRenderer& renderer = MainRendererSingleton::get();
  303. Input& input = InputSingleton::get();
  304. NativeWindow& window = NativeWindowSingleton::get();
  305. ANKI_COUNTER_START_TIMER(C_FPS);
  306. while(true)
  307. {
  308. HighRezTimer timer;
  309. timer.start();
  310. prevUpdateTime = crntTime;
  311. crntTime = HighRezTimer::getCurrentTime();
  312. // Update
  313. input.handleEvents();
  314. if(input.getEvent(Input::WINDOW_CLOSED_EVENT) > 0)
  315. {
  316. break;
  317. }
  318. if(!mainLoopExtra())
  319. {
  320. break;
  321. }
  322. scene.update(prevUpdateTime, crntTime, renderer);
  323. renderer.render(scene);
  324. window.swapBuffers();
  325. ANKI_COUNTERS_RESOLVE_FRAME();
  326. // Sleep
  327. timer.stop();
  328. if(timer.getElapsedTime() < AppSingleton::get().getTimerTick())
  329. {
  330. HighRezTimer::sleep(
  331. AppSingleton::get().getTimerTick() - timer.getElapsedTime());
  332. }
  333. // Timestamp
  334. increaseGlobTimestamp();
  335. }
  336. // Counters end
  337. ANKI_COUNTER_STOP_TIMER_INC(C_FPS);
  338. ANKI_COUNTERS_FLUSH();
  339. }
  340. //==============================================================================
  341. #if ANKI_OS == ANKI_OS_ANDROID
  342. void android_main(android_app* app)
  343. {
  344. app_dummy();
  345. // First thing to do
  346. gAndroidApp = app;
  347. app->onAppCmd = handleEvents;
  348. #else
  349. int main(int, char**)
  350. {
  351. #endif
  352. try
  353. {
  354. initSubsystems();
  355. initScene();
  356. mainLoop();
  357. ANKI_LOGI("Exiting...");
  358. }
  359. catch(std::exception& e)
  360. {
  361. ANKI_LOGE("Aborting: %s", e.what());
  362. }
  363. ANKI_LOGI("Bye!!");
  364. #if ANKI_OS == ANKI_OS_ANDROID
  365. exit(0);
  366. #else
  367. return 0;
  368. #endif
  369. }