Engine.cpp 5.1 KB

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