Wrapper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include <cmath>
  2. #include <chrono>
  3. #include <thread>
  4. #include "client/engine/Wrapper.h"
  5. DummyClient DummyClient::dummy;
  6. IClient* Engine::client = &DummyClient::dummy;
  7. // window data
  8. GLFWwindow* Engine::window = nullptr;
  9. int Engine::scale = 1;
  10. int Engine::width = 0;
  11. int Engine::height = 0;
  12. int Engine::resizeTicks = -1;
  13. // projection data
  14. float Engine::fovY = 60;
  15. float Engine::nearClip = 0.1f;
  16. float Engine::farClip = 1000.0f;
  17. Matrix3D Engine::projMatrix;
  18. // rectangle for framebuffer drawing
  19. FramebufferRectangle Engine::rectangle;
  20. int Engine::stage = 0;
  21. // shader stage 1 - world
  22. WorldShader Engine::worldShader;
  23. // shader stage 2 - world ssao
  24. SSAOShader Engine::ssaoShader;
  25. // shader stage 3 - world ssao blur
  26. SSAOBlurShader Engine::ssaoBlurShader;
  27. // shader stage 4 - world post
  28. WorldPostShader Engine::worldPostShader;
  29. // shader stage 5 - 2D overlay
  30. OverlayShader Engine::overlayShader;
  31. bool Engine::init(int width, int height, const char* name)
  32. {
  33. Engine::width = width;
  34. Engine::height = height;
  35. updateScale();
  36. if(!glfwInit())
  37. {
  38. cout << "could not initialize GLFW" << endl;
  39. return false;
  40. }
  41. glfwDefaultWindowHints();
  42. glfwWindowHint(GLFW_VISIBLE, 0);
  43. glfwWindowHint(GLFW_RESIZABLE, 1);
  44. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  45. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  46. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  47. window = glfwCreateWindow(width, height, name, nullptr, nullptr);
  48. if(!window)
  49. {
  50. cout << "could not create window" << endl;
  51. glfwTerminate();
  52. return false;
  53. }
  54. glfwMakeContextCurrent(window);
  55. glfwSwapInterval(1);
  56. glfwShowWindow(window);
  57. GLenum err = glewInit();
  58. if(GLEW_OK != err)
  59. {
  60. cout << "could not initialize GLEW: " << glewGetErrorString(err) << endl;
  61. return false;
  62. }
  63. cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << endl;
  64. if(!worldShader.init() || !ssaoShader.init() || !ssaoBlurShader.init() ||
  65. !worldPostShader.init() || !overlayShader.init() || !rectangle.init())
  66. {
  67. glfwDestroyWindow(window);
  68. glfwTerminate();
  69. return false;
  70. }
  71. glfwSetKeyCallback(window, onKeyEvent);
  72. glfwSetMouseButtonCallback(window, onMouseClick);
  73. glfwSetFramebufferSizeCallback(window, onWindowResize);
  74. glfwSetCursorPosCallback(window, onMouseMove);
  75. return true;
  76. }
  77. void Engine::sleep(uint64_t nanos)
  78. {
  79. uint64_t end = glfwGetTimerValue() + nanos - 10000;
  80. // non busy wait until a close range
  81. while(end > glfwGetTimerValue() + 1000000)
  82. {
  83. this_thread::sleep_for(chrono::nanoseconds(1000000));
  84. }
  85. while(end > glfwGetTimerValue() + 100000)
  86. {
  87. this_thread::sleep_for(chrono::nanoseconds(100000));
  88. }
  89. // busy wait for higher accuracy at the end
  90. while(end > glfwGetTimerValue());
  91. }
  92. void Engine::start(IClient* client)
  93. {
  94. if(client != nullptr)
  95. {
  96. Engine::client = client;
  97. }
  98. glEnable(GL_CULL_FACE);
  99. glDepthFunc(GL_LEQUAL);
  100. uint64_t newTime = glfwGetTimerValue();
  101. uint64_t oldTime = newTime;
  102. uint64_t lag = 0;
  103. uint64_t frameLag = 0;
  104. uint64_t lastFrame = 0;
  105. while(!glfwWindowShouldClose(window))
  106. {
  107. oldTime = newTime;
  108. newTime = glfwGetTimerValue();
  109. lag += newTime - oldTime;
  110. frameLag += newTime - oldTime;
  111. if(lag >= NANOS_PER_TICK || frameLag >= NANOS_PER_FRAME)
  112. {
  113. int ticksPerFrame = 0;
  114. while(lag >= NANOS_PER_TICK)
  115. {
  116. lag -= NANOS_PER_TICK;
  117. Engine::client->tick();
  118. ticksPerFrame++;
  119. resizeTicks -= (resizeTicks >= 0);
  120. if(resizeTicks == 0)
  121. {
  122. glViewport(0, 0, width, height);
  123. updateScale();
  124. worldShader.resize();
  125. ssaoShader.resize();
  126. ssaoBlurShader.resize();
  127. worldPostShader.resize();
  128. }
  129. if(ticksPerFrame >= MAX_TICKS_PER_FRAME)
  130. {
  131. long skip = lag / NANOS_PER_TICK;
  132. lag -= skip * NANOS_PER_TICK;
  133. if(skip > 0)
  134. {
  135. cout << "skipped " << skip << " game ticks " << lag << endl;
  136. }
  137. break;
  138. }
  139. }
  140. if(frameLag >= NANOS_PER_FRAME)
  141. {
  142. frameLag -= NANOS_PER_FRAME;
  143. // make sure no frames are rendered immediately after each other
  144. // this happens if the game tick takes too long
  145. if(lastFrame + NANOS_PER_FRAME - 1000000 < glfwGetTimerValue())
  146. {
  147. lastFrame = glfwGetTimerValue();
  148. if(resizeTicks == -1)
  149. {
  150. onRenderTick((float) lag / NANOS_PER_TICK);
  151. glfwSwapBuffers(window);
  152. }
  153. }
  154. }
  155. glfwPollEvents();
  156. }
  157. else
  158. {
  159. // wait until next frame
  160. long waitingTime = min(NANOS_PER_FRAME - frameLag, NANOS_PER_TICK - lag);
  161. sleep(waitingTime);
  162. }
  163. }
  164. glfwDestroyWindow(window);
  165. glfwTerminate();
  166. }
  167. void Engine::stop()
  168. {
  169. glfwSetWindowShouldClose(window, 1);
  170. }
  171. void Engine::onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mods)
  172. {
  173. client->onKeyEvent(key, scancode, action, mods);
  174. }
  175. void Engine::onMouseMove(GLFWwindow* w, double x, double y)
  176. {
  177. client->onMouseMove(x, y);
  178. }
  179. void Engine::onMouseClick(GLFWwindow* w, int button, int action, int mods)
  180. {
  181. client->onMouseClick(button, action, mods);
  182. }
  183. void Engine::onWindowResize(GLFWwindow* w, int width, int height)
  184. {
  185. Engine::width = width;
  186. Engine::height = height;
  187. resizeTicks = 10;
  188. }
  189. void Engine::updateScale()
  190. {
  191. scale = 1;
  192. while(width / (scale + 1) >= 400 && height / (scale + 1) >= 300)
  193. {
  194. scale++;
  195. }
  196. }
  197. int Engine::getScale()
  198. {
  199. return scale;
  200. }
  201. int Engine::getWidth()
  202. {
  203. return width;
  204. }
  205. int Engine::getHeight()
  206. {
  207. return height;
  208. }
  209. float Engine::getScaledWidth()
  210. {
  211. return (float) width / scale;
  212. }
  213. float Engine::getScaledHeight()
  214. {
  215. return (float) height / scale;
  216. }
  217. float Engine::getFieldOfView()
  218. {
  219. return fovY;
  220. }
  221. float Engine::getNearClip()
  222. {
  223. return nearClip;
  224. }
  225. float Engine::getFarClip()
  226. {
  227. return farClip;
  228. }
  229. void Engine::printError()
  230. {
  231. GLenum error = glGetError();
  232. switch(error)
  233. {
  234. case GL_NO_ERROR:
  235. //cout << "> No error has been recorded." << endl;
  236. break;
  237. case GL_INVALID_ENUM:
  238. cout << "> An unacceptable value is specified for an enumerated argument." << endl;
  239. break;
  240. case GL_INVALID_VALUE:
  241. cout << "> A numeric argument is out of range." << endl;
  242. break;
  243. case GL_INVALID_OPERATION:
  244. cout << "> The specified operation is not allowed in the current state." << endl;
  245. break;
  246. case GL_INVALID_FRAMEBUFFER_OPERATION:
  247. cout << "> The framebuffer object is not complete." << endl;
  248. break;
  249. case GL_OUT_OF_MEMORY:
  250. cout << "> There is not enough memory left to execute the command." << endl;
  251. break;
  252. case GL_STACK_UNDERFLOW:
  253. cout << "> An attempt has been made to perform an operation that would cause an internal stack to underflow." << endl;
  254. break;
  255. case GL_STACK_OVERFLOW:
  256. cout << "> An attempt has been made to perform an operation that would cause an internal stack to overflow." << endl;
  257. break;
  258. default:
  259. cout << "> Unknown OpenGL error: " << error << endl;
  260. }
  261. }
  262. void Engine::onRenderTick(float lag)
  263. {
  264. // update projection matrix
  265. float tan = tanf((0.5f * fovY) * M_PI / 180.0f);
  266. float q = 1.0f / tan;
  267. float aspect = (float) width / height;
  268. projMatrix.set(0, 0, q / aspect);
  269. projMatrix.set(1, 1, q);
  270. projMatrix.set(2, 2, (nearClip + farClip) / (nearClip - farClip));
  271. projMatrix.set(3, 2, -1.0f);
  272. projMatrix.set(2, 3, (2.0f * nearClip * farClip) / (nearClip - farClip));
  273. projMatrix.set(3, 3, 0);
  274. // -------------------------------------------------------------------------
  275. // shader stage 1 - world
  276. // -------------------------------------------------------------------------
  277. stage = 1;
  278. worldShader.preRender(projMatrix.getValues());
  279. // call render tick for further drawing
  280. client->render3DTick(lag);
  281. // -------------------------------------------------------------------------
  282. // shader stage 2 - world ssao
  283. // -------------------------------------------------------------------------
  284. stage = 2;
  285. ssaoShader.preRender(projMatrix.getValues());
  286. // bind previously generated texture data buffers
  287. worldShader.bindPositionTexture(1);
  288. worldShader.bindNormalTexture(2);
  289. worldShader.bindColorTexture(3);
  290. worldShader.bindDepthTexture(4);
  291. ssaoShader.bindNoiseTexture(5);
  292. rectangle.draw();
  293. // -------------------------------------------------------------------------
  294. // shader stage 3 - world ssao blur
  295. // -------------------------------------------------------------------------
  296. stage = 3;
  297. ssaoBlurShader.preRender();
  298. ssaoShader.bindTexture(6);
  299. rectangle.draw();
  300. // -------------------------------------------------------------------------
  301. // shader stage 4 - world post
  302. // -------------------------------------------------------------------------
  303. stage = 4;
  304. worldPostShader.preRender();
  305. ssaoBlurShader.bindTexture(7);
  306. rectangle.draw();
  307. // -------------------------------------------------------------------------
  308. // shader stage 5 - 2D overlay
  309. // -------------------------------------------------------------------------
  310. stage = 5;
  311. overlayShader.preRender();
  312. worldPostShader.bindTexture(0);
  313. rectangle.draw();
  314. overlayShader.setViewMatrix();
  315. setMixMode();
  316. glEnable(GL_BLEND);
  317. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  318. glBlendEquation(GL_FUNC_ADD);
  319. client->render2DTick(lag);
  320. glDisable(GL_BLEND);
  321. }
  322. void Engine::setViewMatrix(const float* data)
  323. {
  324. if(stage == 1)
  325. {
  326. worldShader.setViewMatrix(data);
  327. }
  328. }
  329. void Engine::setModelMatrix(const float* data)
  330. {
  331. if(stage == 1)
  332. {
  333. worldShader.setModelMatrix(data);
  334. }
  335. else if(stage == 5)
  336. {
  337. overlayShader.setModelMatrix(data);
  338. }
  339. }
  340. void Engine::setMouseTrapped(bool mode)
  341. {
  342. if(mode)
  343. {
  344. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  345. }
  346. else
  347. {
  348. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  349. }
  350. }
  351. void Engine::setMixMode()
  352. {
  353. if(stage == 5)
  354. {
  355. overlayShader.setUseColor(true);
  356. overlayShader.setUseTexture(true);
  357. }
  358. }
  359. void Engine::setColorMode()
  360. {
  361. if(stage == 5)
  362. {
  363. overlayShader.setUseColor(true);
  364. overlayShader.setUseTexture(false);
  365. }
  366. }
  367. void Engine::setTextureMode()
  368. {
  369. if(stage == 5)
  370. {
  371. overlayShader.setUseColor(false);
  372. overlayShader.setUseTexture(true);
  373. }
  374. }