Wrapper.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "Wrapper.h"
  2. #include <cmath>
  3. DummyClient DummyClient::dummy;
  4. IClient* Engine::client = &DummyClient::dummy;
  5. // window data
  6. GLFWwindow* Engine::window = nullptr;
  7. int Engine::scale = 1;
  8. int Engine::width = 0;
  9. int Engine::height = 0;
  10. // projection data
  11. float Engine::fovY = 60;
  12. float Engine::nearClip = 0.1f;
  13. float Engine::farClip = 1000.0f;
  14. Matrix3D Engine::projMatrix;
  15. // rectangle for framebuffer drawing
  16. FramebufferRectangle Engine::rectangle;
  17. // shader stage 1 - world
  18. WorldShader Engine::worldShader;
  19. // shader stage 2 - world ssao
  20. SSAOShader Engine::ssaoShader;
  21. // shader stage 3 - world ssao blur
  22. SSAOBlurShader Engine::ssaoBlurShader;
  23. // shader stage 4 - world post
  24. WorldPostShader Engine::worldPostShader;
  25. // shader stage 5 - 2D overlay
  26. OverlayShader Engine::overlayShader;
  27. bool Engine::init(int width, int height, const char* name)
  28. {
  29. Engine::width = width;
  30. Engine::height = height;
  31. updateScale();
  32. if(!glfwInit())
  33. {
  34. cout << "could not initialize GLFW" << endl;
  35. return false;
  36. }
  37. glfwDefaultWindowHints();
  38. glfwWindowHint(GLFW_VISIBLE, 0);
  39. glfwWindowHint(GLFW_RESIZABLE, 1);
  40. window = glfwCreateWindow(width, height, name, nullptr, nullptr);
  41. if(!window)
  42. {
  43. cout << "could not create window" << endl;
  44. glfwTerminate();
  45. return false;
  46. }
  47. glfwMakeContextCurrent(window);
  48. glfwSwapInterval(1);
  49. glfwShowWindow(window);
  50. GLenum err = glewInit();
  51. if(GLEW_OK != err)
  52. {
  53. cout << "could not initialize GLEW: " << glewGetErrorString(err) << endl;
  54. return false;
  55. }
  56. cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << endl;
  57. if(!worldShader.init() || !ssaoShader.init() || !ssaoBlurShader.init() || !worldPostShader.init() || !overlayShader.init() || !rectangle.init())
  58. {
  59. glfwDestroyWindow(window);
  60. glfwTerminate();
  61. return false;
  62. }
  63. glfwSetKeyCallback(window, onKeyEvent);
  64. glfwSetMouseButtonCallback(window, onMouseClick);
  65. glfwSetFramebufferSizeCallback(window, onWindowResize);
  66. //glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  67. //glfwSetCursorPosCallback(window, onMouseMove);
  68. return true;
  69. }
  70. void Engine::start(IClient* client)
  71. {
  72. if(client != nullptr)
  73. {
  74. Engine::client = client;
  75. }
  76. glEnable(GL_CULL_FACE);
  77. glDepthFunc(GL_LEQUAL);
  78. uint64_t newTime = glfwGetTimerValue();
  79. uint64_t oldTime = newTime;
  80. uint64_t lag = 0;
  81. while(!glfwWindowShouldClose(window))
  82. {
  83. oldTime = newTime;
  84. newTime = glfwGetTimerValue();
  85. lag += newTime - oldTime;
  86. int ticksPerFrame = 0;
  87. while(lag >= NANOS_PER_TICK)
  88. {
  89. lag -= NANOS_PER_TICK;
  90. Engine::client->tick();
  91. ticksPerFrame++;
  92. if(ticksPerFrame >= MAX_TICKS_PER_FRAME)
  93. {
  94. long skip = lag / NANOS_PER_TICK;
  95. lag -= skip * NANOS_PER_TICK;
  96. if(skip > 0)
  97. {
  98. cout << "skipped " << skip << " game ticks " << lag << endl;
  99. }
  100. break;
  101. }
  102. }
  103. onRenderTick((float) lag / NANOS_PER_TICK);
  104. glfwSwapBuffers(window);
  105. glfwPollEvents();
  106. }
  107. glfwDestroyWindow(window);
  108. glfwTerminate();
  109. }
  110. void Engine::stop()
  111. {
  112. glfwSetWindowShouldClose(window, 1);
  113. }
  114. void Engine::onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mods)
  115. {
  116. client->onKeyEvent(key, scancode, action, mods);
  117. }
  118. void Engine::onMouseClick(GLFWwindow* w, int button, int action, int mods)
  119. {
  120. client->onMouseClick(button, action, mods);
  121. }
  122. void Engine::onWindowResize(GLFWwindow* w, int width, int height)
  123. {
  124. glViewport(0, 0, width, height);
  125. Engine::width = width;
  126. Engine::height = height;
  127. updateScale();
  128. worldShader.resize();
  129. ssaoShader.resize();
  130. ssaoBlurShader.resize();
  131. worldPostShader.resize();
  132. }
  133. void Engine::updateScale()
  134. {
  135. scale = 1;
  136. while(width / (scale + 1) >= 400 && height / (scale + 1) >= 300)
  137. {
  138. scale++;
  139. }
  140. }
  141. int Engine::getScale()
  142. {
  143. return scale;
  144. }
  145. int Engine::getWidth()
  146. {
  147. return width;
  148. }
  149. int Engine::getHeight()
  150. {
  151. return height;
  152. }
  153. float Engine::getFieldOfView()
  154. {
  155. return fovY;
  156. }
  157. float Engine::getNearClip()
  158. {
  159. return nearClip;
  160. }
  161. float Engine::getFarClip()
  162. {
  163. return farClip;
  164. }
  165. void Engine::printError()
  166. {
  167. GLenum error = glGetError();
  168. switch(error)
  169. {
  170. case GL_NO_ERROR:
  171. cout << "> No error has been recorded." << endl;
  172. break;
  173. case GL_INVALID_ENUM:
  174. cout << "> An unacceptable value is specified for an enumerated argument." << endl;
  175. break;
  176. case GL_INVALID_VALUE:
  177. cout << "> A numeric argument is out of range." << endl;
  178. break;
  179. case GL_INVALID_OPERATION:
  180. cout << "> The specified operation is not allowed in the current state." << endl;
  181. break;
  182. case GL_INVALID_FRAMEBUFFER_OPERATION:
  183. cout << "> The framebuffer object is not complete." << endl;
  184. break;
  185. case GL_OUT_OF_MEMORY:
  186. cout << "> There is not enough memory left to execute the command." << endl;
  187. break;
  188. case GL_STACK_UNDERFLOW:
  189. cout << "> An attempt has been made to perform an operation that would cause an internal stack to underflow." << endl;
  190. break;
  191. case GL_STACK_OVERFLOW:
  192. cout << "> An attempt has been made to perform an operation that would cause an internal stack to overflow." << endl;
  193. break;
  194. default:
  195. cout << "> Unknown OpenGL error: " << error << endl;
  196. }
  197. }
  198. void Engine::onRenderTick(float lag)
  199. {
  200. // update projection matrix
  201. float tan = tanf((0.5f * fovY) * M_PI / 180.0f);
  202. float q = 1.0f / tan;
  203. float aspect = (float) width / height;
  204. projMatrix.set(0, 0, q / aspect);
  205. projMatrix.set(1, 1, q);
  206. projMatrix.set(2, 2, (nearClip + farClip) / (nearClip - farClip));
  207. projMatrix.set(3, 2, -1.0f);
  208. projMatrix.set(2, 3, (2.0f * nearClip * farClip) / (nearClip - farClip));
  209. projMatrix.set(3, 3, 0);
  210. // -------------------------------------------------------------------------
  211. // shader stage 1 - world
  212. // -------------------------------------------------------------------------
  213. worldShader.preRender(projMatrix.getValues());
  214. // call render tick for further drawing
  215. client->render3DTick(lag);
  216. // -------------------------------------------------------------------------
  217. // shader stage 2 - world ssao
  218. // -------------------------------------------------------------------------
  219. ssaoShader.preRender(projMatrix.getValues());
  220. // bind previously generated texture data buffers
  221. worldShader.bindPositionTexture(1);
  222. worldShader.bindNormalTexture(2);
  223. worldShader.bindColorTexture(3);
  224. worldShader.bindDepthTexture(4);
  225. ssaoShader.bindNoiseTexture(5);
  226. rectangle.draw();
  227. // -------------------------------------------------------------------------
  228. // shader stage 3 - world ssao blur
  229. // -------------------------------------------------------------------------
  230. ssaoBlurShader.preRender();
  231. ssaoShader.bindTexture(6);
  232. rectangle.draw();
  233. // -------------------------------------------------------------------------
  234. // shader stage 4 - world post
  235. // -------------------------------------------------------------------------
  236. worldPostShader.preRender();
  237. ssaoBlurShader.bindTexture(7);
  238. rectangle.draw();
  239. // -------------------------------------------------------------------------
  240. // shader stage 5 - 2D overlay
  241. // -------------------------------------------------------------------------
  242. overlayShader.preRender();
  243. worldPostShader.bindTexture(0);
  244. rectangle.draw();
  245. overlayShader.setViewMatrix();
  246. overlayShader.setUseColor(true);
  247. overlayShader.setUseTexture(true);
  248. glEnable(GL_BLEND);
  249. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  250. glBlendEquation(GL_FUNC_ADD);
  251. client->render2DTick(lag);
  252. glDisable(GL_BLEND);
  253. }
  254. void Engine::setWorldViewMatrix(const float* data)
  255. {
  256. worldShader.setViewMatrix(data);
  257. }
  258. void Engine::setWorldModelMatrix(const float* data)
  259. {
  260. worldShader.setModelMatrix(data);
  261. }
  262. void Engine::setOverlayModelMatrix(const float* data)
  263. {
  264. overlayShader.setModelMatrix(data);
  265. }
  266. // 562