Engine.cpp 5.2 KB

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