Engine.cpp 7.4 KB

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