Engine.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "client/rendering/Engine.h"
  2. #include "client/Game.h"
  3. #include "client/rendering/Framebuffers.h"
  4. #include "client/rendering/Mesh.h"
  5. #include "client/rendering/NoiseTexture.h"
  6. #include "client/rendering/Shaders.h"
  7. #include "math/Frustum.h"
  8. #include "rendering/Window.h"
  9. #include "utils/Logger.h"
  10. #include "wrapper/GL.h"
  11. static Window window;
  12. static Shaders shaders;
  13. static Framebuffers framebuffers;
  14. static Size lastSize{0, 0};
  15. static Frustum frustum{60.0f, 0.1f, 1000.0f, window.getSize()};
  16. static MatrixStack<16> model;
  17. Renderer Engine::renderer;
  18. ShaderMatrix Engine::matrix{nullptr, model, nullptr};
  19. float Engine::lag = 0.0f;
  20. static NoiseTexture ssaoNoise;
  21. static Mesh rectangle;
  22. static Matrix worldProj;
  23. static Matrix worldView;
  24. static Matrix worldShadowProj;
  25. static Matrix worldShadowView;
  26. static Matrix worldShadowProjView;
  27. static bool useSsao = true;
  28. static bool useShadows = false;
  29. static float shadowRadius = 0.01f;
  30. static float shadowBias = 0.0002f;
  31. static bool running = true;
  32. static bool initRectangle() {
  33. if(rectangle.init()) {
  34. return true;
  35. }
  36. TypedBuffer<Triangle> buffer(2);
  37. buffer.add(
  38. Triangle(Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
  39. Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f)),
  40. Vertex(Vector3(-1.0f, 1.0f, 0.0f), Vector2(0.0f, 1.0f))));
  41. buffer.add(
  42. Triangle(Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
  43. Vertex(Vector3(1.0f, -1.0f, 0.0f), Vector2(1.0f, 0.0f)),
  44. Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f))));
  45. rectangle.build(buffer);
  46. return false;
  47. }
  48. bool Engine::init() {
  49. WindowOptions options(4, 0, {1024, 620}, false, "test");
  50. Error error = window.open(options);
  51. if(error.has()) {
  52. LOG_ERROR(error.message);
  53. return true;
  54. }
  55. lastSize = window.getSize();
  56. error = shaders.init();
  57. if(error.has()) {
  58. LOG_ERROR(error.message);
  59. return true;
  60. }
  61. error = framebuffers.init(window.getSize());
  62. if(error.has()) {
  63. LOG_ERROR(error.message);
  64. return true;
  65. }
  66. if(renderer.init() || ssaoNoise.init() || initRectangle()) {
  67. return true;
  68. }
  69. return false;
  70. }
  71. static void renderShadow() {
  72. framebuffers.shadow.bindAndClear();
  73. GL::enableDepthTesting();
  74. shaders.shadow.use();
  75. worldShadowProjView = worldShadowProj;
  76. worldShadowProjView *= worldShadowView;
  77. shaders.shadow.setMatrix("projView", worldShadowProjView.getValues());
  78. model.clear();
  79. shaders.shadow.setMatrix("model", model.peek().getValues());
  80. Engine::matrix = {&shaders.shadow, model, &worldView};
  81. Game::renderWorld();
  82. }
  83. static void renderWorld() {
  84. framebuffers.world.bindAndClear();
  85. GL::enableDepthTesting();
  86. shaders.world.use();
  87. Matrix rWorldShadowProjView;
  88. rWorldShadowProjView.scale(0.5f).translate(Vector3(0.5f, 0.5f, 0.5f));
  89. rWorldShadowProjView *= worldShadowProjView;
  90. shaders.world.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
  91. shaders.world.setMatrix("proj", worldProj.getValues());
  92. worldView = Matrix();
  93. shaders.world.setMatrix("view", worldView.getValues());
  94. model.clear();
  95. shaders.world.setMatrix("model", model.peek().getValues());
  96. framebuffers.shadow.bindTextureTo(0, 1);
  97. shaders.world.setInt("shadows", useShadows);
  98. shaders.world.setFloat("radius", shadowRadius);
  99. shaders.world.setFloat("zbias", shadowBias);
  100. Engine::matrix = {&shaders.world, model, &worldView};
  101. Game::renderWorld();
  102. }
  103. static void renderSSAO() {
  104. shaders.ssao.use();
  105. Matrix rProj;
  106. rProj.scale(0.5f).translate(Vector3(0.5f, 0.5f, 0.5f));
  107. rProj *= worldProj;
  108. shaders.ssao.setMatrix("proj", rProj.getValues());
  109. const Size& size = window.getSize();
  110. shaders.ssao.setInt("width", size.width);
  111. shaders.ssao.setInt("height", size.height);
  112. framebuffers.world.bindTextureTo(0, 0);
  113. framebuffers.world.bindTextureTo(4, 1);
  114. ssaoNoise.bindTo(2);
  115. framebuffers.ssao.bindAndClear();
  116. rectangle.draw();
  117. shaders.ssaoBlur.use();
  118. framebuffers.ssao.bindTextureTo(0, 0);
  119. framebuffers.ssaoBlur.bindAndClear();
  120. rectangle.draw();
  121. }
  122. static void renderPostWorld() {
  123. GL::bindMainFramebuffer();
  124. GL::clear();
  125. shaders.postWorld.use();
  126. framebuffers.world.bindTextureTo(2, 0);
  127. framebuffers.ssaoBlur.bindTextureTo(0, 1);
  128. framebuffers.world.bindTextureTo(3, 2);
  129. framebuffers.world.bindTextureTo(1, 3);
  130. shaders.postWorld.setInt("ssao", useSsao);
  131. shaders.postWorld.setInt("shadows", useShadows);
  132. rectangle.draw();
  133. }
  134. static void renderOverlay() {
  135. GL::disableDepthTesting();
  136. shaders.overlay.use();
  137. const Size& size = window.getSize();
  138. Matrix m;
  139. m.scale(Vector3(2.0f / size.width, -2.0f / size.height, 1.0f))
  140. .translate(Vector3(-1.0f, 1.0f, 0.0f));
  141. shaders.overlay.setMatrix("view", m.getValues());
  142. model.clear();
  143. shaders.overlay.setMatrix("model", model.peek().getValues());
  144. GL::enableBlending();
  145. Engine::matrix = {&shaders.overlay, model, &m};
  146. Game::renderOverlay();
  147. GL::disableBlending();
  148. }
  149. static void updateWorldProjection() {
  150. worldProj = frustum.updateProjection();
  151. if(!useShadows) {
  152. return;
  153. }
  154. worldShadowProj.set(0, Vector4(2.0f / 40.0f, 0.0f, 0.0f, 0.0f));
  155. worldShadowProj.set(1, Vector4(0.0f, 2.0f / 30.0f, 0.0f, 0.0f));
  156. worldShadowProj.set(2, Vector4(0.0f, 0.0f, -2.0f / (1000.0f - 0.1f), 0.0f));
  157. worldShadowProj.set(3, Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  158. }
  159. static void updateWorldView() {
  160. if(!useShadows) {
  161. return;
  162. }
  163. Vector3 right(0.939693f, 0.0f, -0.34202f);
  164. Vector3 back(0.280166f, 0.573576f, 0.769751f);
  165. Vector3 up(-0.196175f, 0.819152f, -0.538986f);
  166. Vector3 center(16.0f, 24.0f, 24.0f);
  167. worldShadowView.set(
  168. 0, Vector4(right[0], right[1], right[2], right.dot(-center)));
  169. worldShadowView.set(1, Vector4(up[0], up[1], up[2], up.dot(-center)));
  170. worldShadowView.set(2,
  171. Vector4(back[0], back[1], back[2], back.dot(-center)));
  172. worldShadowView.set(3, Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  173. }
  174. static void startRender() {
  175. const Size& size = window.getSize();
  176. if(size.width != lastSize.width || size.height != lastSize.height) {
  177. GL::setViewport(size.width, size.height);
  178. framebuffers.resize(size);
  179. lastSize = size;
  180. }
  181. GL::printError("loop error");
  182. updateWorldProjection();
  183. updateWorldView();
  184. if(useShadows) {
  185. renderShadow();
  186. }
  187. renderWorld();
  188. if(useSsao) {
  189. renderSSAO();
  190. }
  191. renderPostWorld();
  192. renderOverlay();
  193. }
  194. struct Loop final {
  195. void render(float lag) {
  196. Engine::lag = lag;
  197. startRender();
  198. }
  199. void tick() {
  200. Game::tick();
  201. }
  202. bool isRunning() const {
  203. return running;
  204. }
  205. };
  206. void Engine::run() {
  207. Loop loop;
  208. window.run(loop, 50'000'000);
  209. }
  210. void Engine::stop() {
  211. running = false;
  212. }
  213. void Engine::setTextInput(TextInput* input) {
  214. window.textInput = input;
  215. if(input != nullptr) {
  216. input->setActive(true);
  217. }
  218. }
  219. bool Engine::isActiveTextInput(TextInput* input) {
  220. return window.textInput == input;
  221. }
  222. Buttons& Engine::getButtons() {
  223. return window.buttons;
  224. }
  225. const Size& Engine::getSize() {
  226. return window.getSize();
  227. }
  228. const Clock& Engine::getFrameClock() {
  229. return window.getFrameClock();
  230. }
  231. const Clock& Engine::getTickClock() {
  232. return window.getTickClock();
  233. }