Engine.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "client/Engine.h"
  2. #include "client/rendering/wrapper/GLFWWrapper.h"
  3. Engine::Engine(Shaders& shaders, Framebuffers& fb, Camera& camera, const WindowSize& size,
  4. RenderSettings& renderSettings) :
  5. shaders(shaders), fb(fb), camera(camera), size(size), renderSettings(renderSettings), frustum(60.0f, 0.1f, 80.0f),
  6. ssaoNoise(4, 4) {
  7. rectangle.add( {-1, -1, 0, 0, 0, 0, 0, 0});
  8. rectangle.add( {1, 1, 0, 1, 1, 0, 0, 0});
  9. rectangle.add( {-1, 1, 0, 0, 1, 0, 0, 0});
  10. rectangle.add( {-1, -1, 0, 0, 0, 0, 0, 0});
  11. rectangle.add( {1, -1, 0, 1, 0, 0, 0, 0});
  12. rectangle.add( {1, 1, 0, 1, 1, 0, 0, 0});
  13. rectangle.build();
  14. }
  15. void Engine::renderTick(float lag, const Game& game) {
  16. camera.update(lag);
  17. updateWorldProjection();
  18. updateWorldView();
  19. renderShadow(lag, game);
  20. renderWorld(lag, game);
  21. if(renderSettings.useSSAO) {
  22. renderSSAO();
  23. }
  24. renderPostWorld();
  25. renderTextOverlay(lag, game);
  26. }
  27. void Engine::renderShadow(float lag, const Game& game) {
  28. fb.shadow.bind();
  29. glEnable(GL_DEPTH_TEST);
  30. shaders.shadow.use();
  31. worldShadowProjView.set(worldShadowProj).mul(worldShadowView);
  32. shaders.shadow.setMatrix("projView", worldShadowProjView.getValues());
  33. game.renderWorld(lag, model, shaders.shadow);
  34. }
  35. void Engine::renderWorld(float lag, const Game& game) {
  36. fb.world.bind();
  37. glEnable(GL_DEPTH_TEST);
  38. shaders.world.use();
  39. Matrix rWorldShadowProjView;
  40. rWorldShadowProjView.translate(0.5f, 0.5f, 0.5f).scale(0.5f).mul(worldShadowProjView);
  41. shaders.world.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
  42. shaders.world.setMatrix("proj", worldProj.getValues());
  43. shaders.world.setMatrix("view", worldView.getValues());
  44. model.clear();
  45. shaders.world.setMatrix("model", model.get().getValues());
  46. fb.shadow.bindDepthTexture(1);
  47. shaders.world.setFloat("radius", renderSettings.testRadius);
  48. shaders.world.setFloat("bias", renderSettings.testBias);
  49. game.renderWorld(lag, model, shaders.world);
  50. }
  51. void Engine::renderSSAO() {
  52. shaders.ssao.use();
  53. Matrix rProj;
  54. rProj.translate(0.5f, 0.5f, 0.5f).scale(0.5f).mul(worldProj);
  55. shaders.ssao.setMatrix("proj", rProj.getValues());
  56. shaders.ssao.setInt("width", size.width);
  57. shaders.ssao.setInt("height", size.height);
  58. fb.world.bindPositionTexture(0);
  59. fb.world.bindNormalTexture(1);
  60. fb.world.bindColorTexture(2);
  61. fb.world.bindDepthTexture(3);
  62. ssaoNoise.bind(4);
  63. fb.ssao.bind();
  64. rectangle.draw();
  65. shaders.ssaoBlur.use();
  66. fb.ssao.bindRedTexture(0);
  67. fb.ssaoBlur.bind();
  68. rectangle.draw();
  69. }
  70. void Engine::renderPostWorld() {
  71. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  72. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  73. shaders.postWorld.use();
  74. fb.world.bindColorTexture(0);
  75. fb.ssaoBlur.bindRedTexture(1);
  76. fb.world.bindRedTexture(2);
  77. fb.world.bindNormalTexture(3);
  78. shaders.postWorld.setInt("useSSAO", renderSettings.useSSAO);
  79. rectangle.draw();
  80. }
  81. void Engine::renderTextOverlay(float lag, const Game& game) {
  82. glDisable(GL_DEPTH_TEST);
  83. shaders.text.use();
  84. Matrix m;
  85. shaders.text.setMatrix("proj", m.getValues());
  86. m.translate(-1.0f, 1.0f, 0.0f).scale(2.0f / size.width, -2.0f / size.height, 1.0f);
  87. shaders.text.setMatrix("view", m.getValues());
  88. model.clear();
  89. model.get().scale(2.0f, 2.0f, 2.0f);
  90. shaders.text.setMatrix("model", model.get().getValues());
  91. glEnable(GL_BLEND);
  92. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  93. glBlendEquation(GL_FUNC_ADD);
  94. game.renderTextOverlay(lag, model, shaders.text, fontRenderer);
  95. glDisable(GL_BLEND);
  96. }
  97. void Engine::updateWorldProjection() {
  98. frustum.setProjection(worldProj, size.width, size.height);
  99. // http://cgvr.informatik.uni-bremen.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html
  100. const float fovY = 60.0f;
  101. const float nearClip = 0.1f;
  102. const float farClip = 80.0f;
  103. float tan = tanf((0.5f * fovY) * M_PI / 180.0f);
  104. float aspect = (float) size.width / size.height;
  105. float closeFarClip = 4;
  106. float nearHigh = tan * nearClip;
  107. float nearWidth = nearHigh * aspect;
  108. float farHigh = tan * closeFarClip;
  109. float farWidth = farHigh * aspect;
  110. Vector farCenter = camera.getPosition();
  111. farCenter.addMul(camera.getBack(), -closeFarClip);
  112. Vector farTopLeft = farCenter;
  113. farTopLeft.addMul(camera.getRight(), -farWidth).addMul(camera.getUp(), farHigh);
  114. Vector farBottomLeft = farCenter;
  115. farBottomLeft.addMul(camera.getRight(), -farWidth).addMul(camera.getUp(), -farHigh);
  116. Vector farTopRight = farCenter;
  117. farTopRight.addMul(camera.getRight(), farWidth).addMul(camera.getUp(), farHigh);
  118. Vector farBottomRight = farCenter;
  119. farBottomRight.addMul(camera.getRight(), farWidth).addMul(camera.getUp(), -farHigh);
  120. Vector nearCenter = camera.getPosition();
  121. nearCenter.addMul(camera.getBack(), -nearClip);
  122. Vector nearTopLeft = nearCenter;
  123. nearTopLeft.addMul(camera.getRight(), -nearWidth).addMul(camera.getUp(), nearHigh);
  124. Vector nearBottomLeft = nearCenter;
  125. nearBottomLeft.addMul(camera.getRight(), -nearWidth).addMul(camera.getUp(), -nearHigh);
  126. Vector nearTopRight = nearCenter;
  127. nearTopRight.addMul(camera.getRight(), nearWidth).addMul(camera.getUp(), nearHigh);
  128. Vector nearBottomRight = nearCenter;
  129. nearBottomRight.addMul(camera.getRight(), nearWidth).addMul(camera.getUp(), -nearHigh);
  130. Vector light(-0.280166, -0.573576, -0.769751);
  131. Vector lightLeft = light;
  132. lightLeft.cross(0.0f, 1.0f, 0.0f);
  133. Vector lightUp = lightLeft;
  134. lightUp.cross(light);
  135. Plane plane;
  136. plane.set(Vector(), light, lightUp);
  137. float f[8];
  138. f[0] = plane.getSignedDistance(farTopLeft);
  139. f[1] = plane.getSignedDistance(farBottomLeft);
  140. f[2] = plane.getSignedDistance(farTopRight);
  141. f[3] = plane.getSignedDistance(farBottomRight);
  142. f[4] = plane.getSignedDistance(nearTopLeft);
  143. f[5] = plane.getSignedDistance(nearBottomLeft);
  144. f[6] = plane.getSignedDistance(nearTopRight);
  145. f[7] = plane.getSignedDistance(nearBottomRight);
  146. float min = FLT_MAX;
  147. float max = -FLT_MAX;
  148. for(uint i = 0; i < 8; i++) {
  149. if(f[i] < min) {
  150. min = f[i];
  151. }
  152. if(f[i] > max) {
  153. max = f[i];
  154. }
  155. }
  156. float lightWidth = max - min;
  157. plane.set(Vector(), light, lightLeft);
  158. f[0] = plane.getSignedDistance(farTopLeft);
  159. f[1] = plane.getSignedDistance(farBottomLeft);
  160. f[2] = plane.getSignedDistance(farTopRight);
  161. f[3] = plane.getSignedDistance(farBottomRight);
  162. f[4] = plane.getSignedDistance(nearTopLeft);
  163. f[5] = plane.getSignedDistance(nearBottomLeft);
  164. f[6] = plane.getSignedDistance(nearTopRight);
  165. f[7] = plane.getSignedDistance(nearBottomRight);
  166. min = FLT_MAX;
  167. max = -FLT_MAX;
  168. for(uint i = 0; i < 8; i++) {
  169. if(f[i] < min) {
  170. min = f[i];
  171. }
  172. if(f[i] > max) {
  173. max = f[i];
  174. }
  175. }
  176. float lightHeight = max - min;
  177. // not the real center, but good guess
  178. renderSettings.testOrthoCenter = nearCenter;
  179. renderSettings.testOrthoCenter.addMul(camera.getBack(), -closeFarClip * 0.5f);
  180. if(renderSettings.ortho) {
  181. worldProj.setToIdentity();
  182. worldProj.set(0, 2.0f / lightWidth);
  183. worldProj.set(5, 2.0f / lightHeight);
  184. worldProj.set(10, -2.0f / (farClip - nearClip));
  185. }
  186. worldShadowProj.setToIdentity();
  187. worldShadowProj.set(0, 2.0f / lightWidth);
  188. worldShadowProj.set(5, 2.0f / lightHeight);
  189. worldShadowProj.set(10, -2.0f / (farClip - nearClip));
  190. }
  191. void Engine::updateWorldView() {
  192. Vector right = camera.getRight();
  193. Vector up = camera.getUp();
  194. Vector back = camera.getBack();
  195. Vector pos = camera.getPosition();
  196. if(renderSettings.ortho) {
  197. right.set(0.939693f, 0.0f, -0.34202f);
  198. back.set(0.280166f, 0.573576f, 0.769751f);
  199. up.set(-0.196175f, 0.819152f, -0.538986f);
  200. pos = renderSettings.testOrthoCenter;
  201. }
  202. worldView.set(0, right.getX());
  203. worldView.set(1, up.getX());
  204. worldView.set(2, back.getX());
  205. worldView.set(4, right.getY());
  206. worldView.set(5, up.getY());
  207. worldView.set(6, back.getY());
  208. worldView.set(8, right.getZ());
  209. worldView.set(9, up.getZ());
  210. worldView.set(10, back.getZ());
  211. worldView.set(12, right.dotInverse(pos));
  212. worldView.set(13, up.dotInverse(pos));
  213. worldView.set(14, back.dotInverse(pos));
  214. right.set(0.939693f, 0.0f, -0.34202f);
  215. back.set(0.280166f, 0.573576f, 0.769751f);
  216. up.set(-0.196175f, 0.819152f, -0.538986f);
  217. pos = renderSettings.testOrthoCenter;
  218. worldShadowView.set(0, right.getX());
  219. worldShadowView.set(1, up.getX());
  220. worldShadowView.set(2, back.getX());
  221. worldShadowView.set(4, right.getY());
  222. worldShadowView.set(5, up.getY());
  223. worldShadowView.set(6, back.getY());
  224. worldShadowView.set(8, right.getZ());
  225. worldShadowView.set(9, up.getZ());
  226. worldShadowView.set(10, back.getZ());
  227. worldShadowView.set(12, right.dotInverse(pos));
  228. worldShadowView.set(13, up.dotInverse(pos));
  229. worldShadowView.set(14, back.dotInverse(pos));
  230. }