Engine.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. Renderer renderer(shaders.shadow, model, worldView);
  38. game.renderWorld(lag, renderer);
  39. }
  40. void Engine::renderWorld(float lag, const Game& game) {
  41. fb.world.bind();
  42. GLWrapper::enableDepthTesting();
  43. shaders.world.use();
  44. Matrix rWorldShadowProjView;
  45. rWorldShadowProjView.translate(0.5f, 0.5f, 0.5f).scale(0.5f).mul(worldShadowProjView);
  46. shaders.world.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
  47. shaders.world.setMatrix("proj", worldProj.getValues());
  48. shaders.world.setMatrix("view", worldView.setToIdentity().getValues());
  49. model.clear();
  50. shaders.world.setMatrix("model", model.get().getValues());
  51. fb.shadow.bindDepthTexture(1);
  52. shaders.world.setInt("shadows", renderSettings.shadows);
  53. shaders.world.setFloat("radius", renderSettings.testRadius);
  54. shaders.world.setFloat("bias", renderSettings.testBias);
  55. Renderer renderer(shaders.world, model, worldView);
  56. game.renderWorld(lag, renderer);
  57. }
  58. void Engine::renderSSAO() {
  59. shaders.ssao.use();
  60. Matrix rProj;
  61. rProj.translate(0.5f, 0.5f, 0.5f).scale(0.5f).mul(worldProj);
  62. shaders.ssao.setMatrix("proj", rProj.getValues());
  63. shaders.ssao.setInt("width", size.width);
  64. shaders.ssao.setInt("height", size.height);
  65. fb.world.bindPositionTexture(0);
  66. fb.world.bindNormalTexture(1);
  67. fb.world.bindColorTexture(2);
  68. fb.world.bindDepthTexture(3);
  69. ssaoNoise.bind(4);
  70. fb.ssao.bind();
  71. rectangle.draw();
  72. shaders.ssaoBlur.use();
  73. fb.ssao.bindRedTexture(0);
  74. fb.ssaoBlur.bind();
  75. rectangle.draw();
  76. }
  77. void Engine::renderPostWorld() {
  78. GLWrapper::prepareMainFramebuffer();
  79. shaders.postWorld.use();
  80. fb.world.bindColorTexture(0);
  81. fb.ssaoBlur.bindRedTexture(1);
  82. fb.world.bindRedTexture(2);
  83. fb.world.bindNormalTexture(3);
  84. shaders.postWorld.setInt("ssao", renderSettings.ssao);
  85. shaders.postWorld.setInt("shadows", renderSettings.shadows);
  86. rectangle.draw();
  87. }
  88. void Engine::renderTextOverlay(float lag, const Game& game) {
  89. GLWrapper::disableDepthTesting();
  90. shaders.text.use();
  91. Matrix m;
  92. shaders.text.setMatrix("proj", m.getValues());
  93. m.translate(-1.0f, 1.0f, 0.0f).scale(2.0f / size.width, -2.0f / size.height, 1.0f);
  94. shaders.text.setMatrix("view", m.getValues());
  95. model.clear();
  96. shaders.text.setMatrix("model", model.get().getValues());
  97. GLWrapper::enableBlending();
  98. Renderer renderer(shaders.text, model, m);
  99. game.renderTextOverlay(lag, renderer, fontRenderer);
  100. GLWrapper::disableBlending();
  101. }
  102. void Engine::updateWorldProjection() {
  103. frustum.setProjection(worldProj, size.width, size.height);
  104. if(!renderSettings.shadows) {
  105. return;
  106. }
  107. worldShadowProj.setToIdentity();
  108. worldShadowProj.set(0, 2.0f / 40.0f);
  109. worldShadowProj.set(5, 2.0f / 30.0f);
  110. worldShadowProj.set(10, -2.0f / frustum.getClipDifference());
  111. }
  112. void Engine::updateWorldView() {
  113. if(!renderSettings.shadows) {
  114. return;
  115. }
  116. Vector right(0.939693f, 0.0f, -0.34202f);
  117. Vector back(0.280166f, 0.573576f, 0.769751f);
  118. Vector up(-0.196175f, 0.819152f, -0.538986f);
  119. Vector center(16.0f, 24.0f, 24.0f);
  120. worldShadowView.set(0, right.getX());
  121. worldShadowView.set(1, up.getX());
  122. worldShadowView.set(2, back.getX());
  123. worldShadowView.set(4, right.getY());
  124. worldShadowView.set(5, up.getY());
  125. worldShadowView.set(6, back.getY());
  126. worldShadowView.set(8, right.getZ());
  127. worldShadowView.set(9, up.getZ());
  128. worldShadowView.set(10, back.getZ());
  129. worldShadowView.set(12, right.dotInverse(center));
  130. worldShadowView.set(13, up.dotInverse(center));
  131. worldShadowView.set(14, back.dotInverse(center));
  132. }