Wrapper.cpp 9.9 KB

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