Wrapper.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. void Engine::sleep(uint64_t nanos)
  32. {
  33. uint64_t end = glfwGetTimerValue() + nanos - 10000;
  34. // non busy wait until a close range
  35. while(end > glfwGetTimerValue() + 1000000)
  36. {
  37. this_thread::sleep_for(chrono::nanoseconds(1000000));
  38. }
  39. while(end > glfwGetTimerValue() + 100000)
  40. {
  41. this_thread::sleep_for(chrono::nanoseconds(100000));
  42. }
  43. // busy wait for higher accuracy at the end
  44. while(end > glfwGetTimerValue());
  45. }
  46. void Engine::stop()
  47. {
  48. glfwSetWindowShouldClose(window, 1);
  49. }
  50. void Engine::onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mods)
  51. {
  52. client->onKeyEvent(key, scancode, action, mods);
  53. }
  54. void Engine::onMouseMove(GLFWwindow* w, double x, double y)
  55. {
  56. client->onMouseMove(x, y);
  57. }
  58. void Engine::onMouseClick(GLFWwindow* w, int button, int action, int mods)
  59. {
  60. client->onMouseClick(button, action, mods);
  61. }
  62. void Engine::onWindowResize(GLFWwindow* w, int width, int height)
  63. {
  64. Engine::width = width;
  65. Engine::height = height;
  66. resizeTicks = 10;
  67. }
  68. void Engine::updateScale()
  69. {
  70. scale = 1;
  71. while(width / (scale + 1) >= 400 && height / (scale + 1) >= 300)
  72. {
  73. scale++;
  74. }
  75. }
  76. int Engine::getScale()
  77. {
  78. return scale;
  79. }
  80. int Engine::getWidth()
  81. {
  82. return width;
  83. }
  84. int Engine::getHeight()
  85. {
  86. return height;
  87. }
  88. float Engine::getScaledWidth()
  89. {
  90. return (float) width / scale;
  91. }
  92. float Engine::getScaledHeight()
  93. {
  94. return (float) height / scale;
  95. }
  96. float Engine::getFieldOfView()
  97. {
  98. return fovY;
  99. }
  100. float Engine::getNearClip()
  101. {
  102. return nearClip;
  103. }
  104. float Engine::getFarClip()
  105. {
  106. return farClip;
  107. }
  108. void Engine::printError()
  109. {
  110. GLenum error = glGetError();
  111. switch(error)
  112. {
  113. case GL_NO_ERROR:
  114. //cout << "> No error has been recorded." << endl;
  115. break;
  116. case GL_INVALID_ENUM:
  117. cout << "> An unacceptable value is specified for an enumerated argument." << endl;
  118. break;
  119. case GL_INVALID_VALUE:
  120. cout << "> A numeric argument is out of range." << endl;
  121. break;
  122. case GL_INVALID_OPERATION:
  123. cout << "> The specified operation is not allowed in the current state." << endl;
  124. break;
  125. case GL_INVALID_FRAMEBUFFER_OPERATION:
  126. cout << "> The framebuffer object is not complete." << endl;
  127. break;
  128. case GL_OUT_OF_MEMORY:
  129. cout << "> There is not enough memory left to execute the command." << endl;
  130. break;
  131. case GL_STACK_UNDERFLOW:
  132. cout << "> An attempt has been made to perform an operation that would cause an internal stack to underflow." << endl;
  133. break;
  134. case GL_STACK_OVERFLOW:
  135. cout << "> An attempt has been made to perform an operation that would cause an internal stack to overflow." << endl;
  136. break;
  137. default:
  138. cout << "> Unknown OpenGL error: " << error << endl;
  139. }
  140. }
  141. void Engine::onRenderTick(float lag)
  142. {
  143. // update projection matrix
  144. float tan = tanf((0.5f * fovY) * M_PI / 180.0f);
  145. float q = 1.0f / tan;
  146. float aspect = (float) width / height;
  147. projMatrix.set(0, 0, q / aspect);
  148. projMatrix.set(1, 1, q);
  149. projMatrix.set(2, 2, (nearClip + farClip) / (nearClip - farClip));
  150. projMatrix.set(3, 2, -1.0f);
  151. projMatrix.set(2, 3, (2.0f * nearClip * farClip) / (nearClip - farClip));
  152. projMatrix.set(3, 3, 0);
  153. // -------------------------------------------------------------------------
  154. // shader stage 1 - world
  155. // -------------------------------------------------------------------------
  156. stage = 1;
  157. worldShader.preRender(projMatrix.getValues());
  158. // call render tick for further drawing
  159. client->render3DTick(lag);
  160. // -------------------------------------------------------------------------
  161. // shader stage 2 - world ssao
  162. // -------------------------------------------------------------------------
  163. stage = 2;
  164. ssaoShader.preRender(projMatrix.getValues());
  165. // bind previously generated texture data buffers
  166. worldShader.bindPositionTexture(1);
  167. worldShader.bindNormalTexture(2);
  168. worldShader.bindColorTexture(3);
  169. worldShader.bindDepthTexture(4);
  170. ssaoShader.bindNoiseTexture(5);
  171. rectangle.draw();
  172. // -------------------------------------------------------------------------
  173. // shader stage 3 - world ssao blur
  174. // -------------------------------------------------------------------------
  175. stage = 3;
  176. ssaoBlurShader.preRender();
  177. ssaoShader.bindTexture(6);
  178. rectangle.draw();
  179. // -------------------------------------------------------------------------
  180. // shader stage 4 - world post
  181. // -------------------------------------------------------------------------
  182. stage = 4;
  183. worldPostShader.preRender();
  184. ssaoBlurShader.bindTexture(7);
  185. rectangle.draw();
  186. // -------------------------------------------------------------------------
  187. // shader stage 5 - 2D overlay
  188. // -------------------------------------------------------------------------
  189. stage = 5;
  190. overlayShader.preRender();
  191. worldPostShader.bindTexture(0);
  192. rectangle.draw();
  193. overlayShader.setViewMatrix();
  194. setMixMode();
  195. glEnable(GL_BLEND);
  196. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  197. glBlendEquation(GL_FUNC_ADD);
  198. client->render2DTick(lag);
  199. glDisable(GL_BLEND);
  200. }
  201. void Engine::setViewMatrix(const float* data)
  202. {
  203. if(stage == 1)
  204. {
  205. worldShader.setViewMatrix(data);
  206. }
  207. }
  208. void Engine::setModelMatrix(const float* data)
  209. {
  210. if(stage == 1)
  211. {
  212. worldShader.setModelMatrix(data);
  213. }
  214. else if(stage == 5)
  215. {
  216. overlayShader.setModelMatrix(data);
  217. }
  218. }
  219. void Engine::setMouseTrapped(bool mode)
  220. {
  221. if(mode)
  222. {
  223. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  224. }
  225. else
  226. {
  227. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  228. }
  229. }
  230. void Engine::setMixMode()
  231. {
  232. if(stage == 5)
  233. {
  234. overlayShader.setUseColor(true);
  235. overlayShader.setUseTexture(true);
  236. }
  237. }
  238. void Engine::setColorMode()
  239. {
  240. if(stage == 5)
  241. {
  242. overlayShader.setUseColor(true);
  243. overlayShader.setUseTexture(false);
  244. }
  245. }
  246. void Engine::setTextureMode()
  247. {
  248. if(stage == 5)
  249. {
  250. overlayShader.setUseColor(false);
  251. overlayShader.setUseTexture(true);
  252. }
  253. }