Engine.cpp 9.2 KB

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