Engine.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <cmath>
  2. #include <cfloat>
  3. #include "client/rendering/Engine.h"
  4. #include "client/rendering/wrapper/GLFWWrapper.h"
  5. #include "client/rendering/wrapper/GLWrapper.h"
  6. #include "client/rendering/Renderer.h"
  7. #include "client/math/Plane.h"
  8. Engine::Engine(Shaders& shaders, Framebuffers& fb, const WindowSize& size, RenderSettings& renderSettings) :
  9. shaders(shaders), fb(fb), size(size), renderSettings(renderSettings), frustum(60.0f, 0.1f, 80.0f) {
  10. rectangle.add( {-1, -1, 0, 0, 0, 0, 0, 0});
  11. rectangle.add( {1, 1, 0, 1, 1, 0, 0, 0});
  12. rectangle.add( {-1, 1, 0, 0, 1, 0, 0, 0});
  13. rectangle.add( {-1, -1, 0, 0, 0, 0, 0, 0});
  14. rectangle.add( {1, -1, 0, 1, 0, 0, 0, 0});
  15. rectangle.add( {1, 1, 0, 1, 1, 0, 0, 0});
  16. rectangle.build();
  17. }
  18. void Engine::renderTick(float lag, const Game& game) {
  19. updateWorldProjection();
  20. updateWorldView();
  21. if(renderSettings.shadows) {
  22. renderShadow(lag, game);
  23. }
  24. renderWorld(lag, game);
  25. if(renderSettings.ssao) {
  26. renderSSAO();
  27. }
  28. renderPostWorld();
  29. renderTextOverlay(lag, game);
  30. }
  31. void Engine::renderShadow(float lag, const Game& game) {
  32. fb.shadow.bind();
  33. GLWrapper::enableDepthTesting();
  34. shaders.shadow.use();
  35. worldShadowProjView.set(worldShadowProj).mul(worldShadowView);
  36. shaders.shadow.setMatrix("projView", worldShadowProjView.getValues());
  37. model.clear();
  38. shaders.shadow.setMatrix("model", model.get().getValues());
  39. Renderer renderer(shaders.shadow, model, worldView);
  40. game.renderWorld(lag, renderer);
  41. }
  42. void Engine::renderWorld(float lag, const Game& game) {
  43. fb.world.bind();
  44. GLWrapper::enableDepthTesting();
  45. shaders.world.use();
  46. Matrix rWorldShadowProjView;
  47. rWorldShadowProjView.translate(0.5f, 0.5f, 0.5f).scale(0.5f).mul(worldShadowProjView);
  48. shaders.world.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
  49. shaders.world.setMatrix("proj", worldProj.getValues());
  50. shaders.world.setMatrix("view", worldView.setToIdentity().getValues());
  51. model.clear();
  52. shaders.world.setMatrix("model", model.get().getValues());
  53. fb.shadow.bindDepthTexture(1);
  54. shaders.world.setInt("shadows", renderSettings.shadows);
  55. shaders.world.setFloat("radius", renderSettings.testRadius);
  56. shaders.world.setFloat("zbias", renderSettings.testBias);
  57. Renderer renderer(shaders.world, model, worldView);
  58. game.renderWorld(lag, renderer);
  59. }
  60. void Engine::renderSSAO() {
  61. shaders.ssao.use();
  62. Matrix rProj;
  63. rProj.translate(0.5f, 0.5f, 0.5f).scale(0.5f).mul(worldProj);
  64. shaders.ssao.setMatrix("proj", rProj.getValues());
  65. shaders.ssao.setInt("width", size.width);
  66. shaders.ssao.setInt("height", size.height);
  67. fb.world.bindPositionTexture(0);
  68. fb.world.bindNormalTexture(1);
  69. fb.world.bindColorTexture(2);
  70. fb.world.bindDepthTexture(3);
  71. ssaoNoise.bind(4);
  72. fb.ssao.bind();
  73. rectangle.draw();
  74. shaders.ssaoBlur.use();
  75. fb.ssao.bindRedTexture(0);
  76. fb.ssaoBlur.bind();
  77. rectangle.draw();
  78. }
  79. void Engine::renderPostWorld() {
  80. GLWrapper::prepareMainFramebuffer();
  81. shaders.postWorld.use();
  82. fb.world.bindColorTexture(0);
  83. fb.ssaoBlur.bindRedTexture(1);
  84. fb.world.bindRedTexture(2);
  85. fb.world.bindNormalTexture(3);
  86. shaders.postWorld.setInt("ssao", renderSettings.ssao);
  87. shaders.postWorld.setInt("shadows", renderSettings.shadows);
  88. rectangle.draw();
  89. }
  90. void Engine::renderTextOverlay(float lag, const Game& game) {
  91. GLWrapper::disableDepthTesting();
  92. shaders.text.use();
  93. Matrix m;
  94. shaders.text.setMatrix("proj", m.getValues());
  95. m.translate(-1.0f, 1.0f, 0.0f).scale(2.0f / size.width, -2.0f / size.height, 1.0f);
  96. shaders.text.setMatrix("view", m.getValues());
  97. model.clear();
  98. shaders.text.setMatrix("model", model.get().getValues());
  99. GLWrapper::enableBlending();
  100. Renderer renderer(shaders.text, model, m);
  101. game.renderTextOverlay(lag, renderer, fontRenderer);
  102. GLWrapper::disableBlending();
  103. }
  104. void Engine::updateWorldProjection() {
  105. frustum.setProjection(worldProj, size.width, size.height);
  106. if(!renderSettings.shadows) {
  107. return;
  108. }
  109. worldShadowProj.setToIdentity();
  110. worldShadowProj.set(0, 2.0f / 40.0f);
  111. worldShadowProj.set(5, 2.0f / 30.0f);
  112. worldShadowProj.set(10, -2.0f / frustum.getClipDifference());
  113. }
  114. void Engine::updateWorldView() {
  115. if(!renderSettings.shadows) {
  116. return;
  117. }
  118. Vector right(0.939693f, 0.0f, -0.34202f);
  119. Vector back(0.280166f, 0.573576f, 0.769751f);
  120. Vector up(-0.196175f, 0.819152f, -0.538986f);
  121. Vector center(16.0f, 24.0f, 24.0f);
  122. worldShadowView.set(0, right.getX());
  123. worldShadowView.set(1, up.getX());
  124. worldShadowView.set(2, back.getX());
  125. worldShadowView.set(4, right.getY());
  126. worldShadowView.set(5, up.getY());
  127. worldShadowView.set(6, back.getY());
  128. worldShadowView.set(8, right.getZ());
  129. worldShadowView.set(9, up.getZ());
  130. worldShadowView.set(10, back.getZ());
  131. worldShadowView.set(12, right.dotInverse(center));
  132. worldShadowView.set(13, up.dotInverse(center));
  133. worldShadowView.set(14, back.dotInverse(center));
  134. }