Wrapper.cpp 11 KB

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