Main.cpp 11 KB

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