Engine.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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, Camera& camera, const WindowSize& size,
  9. RenderSettings& renderSettings) :
  10. shaders(shaders), fb(fb), camera(camera), size(size), renderSettings(renderSettings), frustum(60.0f, 0.1f, 80.0f),
  11. ssaoNoise(4, 4) {
  12. rectangle.add( {-1, -1, 0, 0, 0, 0, 0, 0});
  13. rectangle.add( {1, 1, 0, 1, 1, 0, 0, 0});
  14. rectangle.add( {-1, 1, 0, 0, 1, 0, 0, 0});
  15. rectangle.add( {-1, -1, 0, 0, 0, 0, 0, 0});
  16. rectangle.add( {1, -1, 0, 1, 0, 0, 0, 0});
  17. rectangle.add( {1, 1, 0, 1, 1, 0, 0, 0});
  18. rectangle.build();
  19. }
  20. void Engine::renderTick(float lag, const Game& game) {
  21. camera.update(lag);
  22. updateWorldProjection();
  23. updateWorldView();
  24. if(renderSettings.shadows) {
  25. renderShadow(lag, game);
  26. }
  27. renderWorld(lag, game);
  28. if(renderSettings.ssao) {
  29. renderSSAO();
  30. }
  31. renderPostWorld();
  32. renderTextOverlay(lag, game);
  33. }
  34. void Engine::renderShadow(float lag, const Game& game) {
  35. fb.shadow.bind();
  36. GLWrapper::enableDepthTesting();
  37. shaders.shadow.use();
  38. worldShadowProjView.set(worldShadowProj).mul(worldShadowView);
  39. shaders.shadow.setMatrix("projView", worldShadowProjView.getValues());
  40. Renderer renderer(shaders.shadow, model);
  41. game.renderWorld(lag, renderer);
  42. }
  43. void Engine::renderWorld(float lag, const Game& game) {
  44. fb.world.bind();
  45. GLWrapper::enableDepthTesting();
  46. shaders.world.use();
  47. Matrix rWorldShadowProjView;
  48. rWorldShadowProjView.translate(0.5f, 0.5f, 0.5f).scale(0.5f).mul(worldShadowProjView);
  49. shaders.world.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
  50. shaders.world.setMatrix("proj", worldProj.getValues());
  51. shaders.world.setMatrix("view", worldView.getValues());
  52. model.clear();
  53. shaders.world.setMatrix("model", model.get().getValues());
  54. fb.shadow.bindDepthTexture(1);
  55. shaders.world.setInt("shadows", renderSettings.shadows);
  56. shaders.world.setFloat("radius", renderSettings.testRadius);
  57. shaders.world.setFloat("bias", renderSettings.testBias);
  58. Renderer renderer(shaders.world, model);
  59. game.renderWorld(lag, renderer);
  60. }
  61. void Engine::renderSSAO() {
  62. shaders.ssao.use();
  63. Matrix rProj;
  64. rProj.translate(0.5f, 0.5f, 0.5f).scale(0.5f).mul(worldProj);
  65. shaders.ssao.setMatrix("proj", rProj.getValues());
  66. shaders.ssao.setInt("width", size.width);
  67. shaders.ssao.setInt("height", size.height);
  68. fb.world.bindPositionTexture(0);
  69. fb.world.bindNormalTexture(1);
  70. fb.world.bindColorTexture(2);
  71. fb.world.bindDepthTexture(3);
  72. ssaoNoise.bind(4);
  73. fb.ssao.bind();
  74. rectangle.draw();
  75. shaders.ssaoBlur.use();
  76. fb.ssao.bindRedTexture(0);
  77. fb.ssaoBlur.bind();
  78. rectangle.draw();
  79. }
  80. void Engine::renderPostWorld() {
  81. GLWrapper::prepareMainFramebuffer();
  82. shaders.postWorld.use();
  83. fb.world.bindColorTexture(0);
  84. fb.ssaoBlur.bindRedTexture(1);
  85. fb.world.bindRedTexture(2);
  86. fb.world.bindNormalTexture(3);
  87. shaders.postWorld.setInt("ssao", renderSettings.ssao);
  88. shaders.postWorld.setInt("shadows", renderSettings.shadows);
  89. rectangle.draw();
  90. }
  91. void Engine::renderTextOverlay(float lag, const Game& game) {
  92. GLWrapper::disableDepthTesting();
  93. shaders.text.use();
  94. Matrix m;
  95. shaders.text.setMatrix("proj", m.getValues());
  96. m.translate(-1.0f, 1.0f, 0.0f).scale(2.0f / size.width, -2.0f / size.height, 1.0f);
  97. shaders.text.setMatrix("view", m.getValues());
  98. model.clear();
  99. model.get().scale(2.0f, 2.0f, 2.0f);
  100. shaders.text.setMatrix("model", model.get().getValues());
  101. GLWrapper::enableBlending();
  102. Renderer renderer(shaders.text, model);
  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. // http://cgvr.informatik.uni-bremen.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html
  112. const float fovY = 60.0f;
  113. const float nearClip = 0.1f;
  114. const float farClip = 80.0f;
  115. float tan = tanf((0.5f * fovY) * M_PI / 180.0f);
  116. float aspect = (float) size.width / size.height;
  117. float closeFarClip = 16;
  118. float nearHigh = tan * nearClip;
  119. float nearWidth = nearHigh * aspect;
  120. float farHigh = tan * closeFarClip;
  121. float farWidth = farHigh * aspect;
  122. Vector farCenter = camera.getPosition();
  123. farCenter.addMul(camera.getBack(), -closeFarClip);
  124. Vector farTopLeft = farCenter;
  125. farTopLeft.addMul(camera.getRight(), -farWidth).addMul(camera.getUp(), farHigh);
  126. Vector farBottomLeft = farCenter;
  127. farBottomLeft.addMul(camera.getRight(), -farWidth).addMul(camera.getUp(), -farHigh);
  128. Vector farTopRight = farCenter;
  129. farTopRight.addMul(camera.getRight(), farWidth).addMul(camera.getUp(), farHigh);
  130. Vector farBottomRight = farCenter;
  131. farBottomRight.addMul(camera.getRight(), farWidth).addMul(camera.getUp(), -farHigh);
  132. Vector nearCenter = camera.getPosition();
  133. nearCenter.addMul(camera.getBack(), -nearClip);
  134. Vector nearTopLeft = nearCenter;
  135. nearTopLeft.addMul(camera.getRight(), -nearWidth).addMul(camera.getUp(), nearHigh);
  136. Vector nearBottomLeft = nearCenter;
  137. nearBottomLeft.addMul(camera.getRight(), -nearWidth).addMul(camera.getUp(), -nearHigh);
  138. Vector nearTopRight = nearCenter;
  139. nearTopRight.addMul(camera.getRight(), nearWidth).addMul(camera.getUp(), nearHigh);
  140. Vector nearBottomRight = nearCenter;
  141. nearBottomRight.addMul(camera.getRight(), nearWidth).addMul(camera.getUp(), -nearHigh);
  142. Vector light(-0.280166, -0.573576, -0.769751);
  143. Vector lightLeft = light;
  144. lightLeft.cross(0.0f, 1.0f, 0.0f);
  145. Vector lightUp = lightLeft;
  146. lightUp.cross(light);
  147. Plane plane;
  148. plane.set(Vector(), light, lightUp);
  149. float f[8];
  150. f[0] = plane.getSignedDistance(farTopLeft);
  151. f[1] = plane.getSignedDistance(farBottomLeft);
  152. f[2] = plane.getSignedDistance(farTopRight);
  153. f[3] = plane.getSignedDistance(farBottomRight);
  154. f[4] = plane.getSignedDistance(nearTopLeft);
  155. f[5] = plane.getSignedDistance(nearBottomLeft);
  156. f[6] = plane.getSignedDistance(nearTopRight);
  157. f[7] = plane.getSignedDistance(nearBottomRight);
  158. float min = FLT_MAX;
  159. float max = -FLT_MAX;
  160. for(uint i = 0; i < 8; i++) {
  161. if(f[i] < min) {
  162. min = f[i];
  163. }
  164. if(f[i] > max) {
  165. max = f[i];
  166. }
  167. }
  168. float lightWidth = max - min;
  169. plane.set(Vector(), light, lightLeft);
  170. f[0] = plane.getSignedDistance(farTopLeft);
  171. f[1] = plane.getSignedDistance(farBottomLeft);
  172. f[2] = plane.getSignedDistance(farTopRight);
  173. f[3] = plane.getSignedDistance(farBottomRight);
  174. f[4] = plane.getSignedDistance(nearTopLeft);
  175. f[5] = plane.getSignedDistance(nearBottomLeft);
  176. f[6] = plane.getSignedDistance(nearTopRight);
  177. f[7] = plane.getSignedDistance(nearBottomRight);
  178. min = FLT_MAX;
  179. max = -FLT_MAX;
  180. for(uint i = 0; i < 8; i++) {
  181. if(f[i] < min) {
  182. min = f[i];
  183. }
  184. if(f[i] > max) {
  185. max = f[i];
  186. }
  187. }
  188. float lightHeight = max - min;
  189. // not the real center, but good guess
  190. renderSettings.testOrthoCenter = nearCenter;
  191. renderSettings.testOrthoCenter.addMul(camera.getBack(), -closeFarClip * 0.5f);
  192. if(renderSettings.ortho) {
  193. worldProj.setToIdentity();
  194. worldProj.set(0, 2.0f / lightWidth);
  195. worldProj.set(5, 2.0f / lightHeight);
  196. worldProj.set(10, -2.0f / (farClip - nearClip));
  197. }
  198. worldShadowProj.setToIdentity();
  199. worldShadowProj.set(0, 2.0f / lightWidth);
  200. worldShadowProj.set(5, 2.0f / lightHeight);
  201. worldShadowProj.set(10, -2.0f / (farClip - nearClip));
  202. }
  203. void Engine::updateWorldView() {
  204. Vector right = camera.getRight();
  205. Vector up = camera.getUp();
  206. Vector back = camera.getBack();
  207. Vector pos = camera.getPosition();
  208. if(renderSettings.ortho) {
  209. right.set(0.939693f, 0.0f, -0.34202f);
  210. back.set(0.280166f, 0.573576f, 0.769751f);
  211. up.set(-0.196175f, 0.819152f, -0.538986f);
  212. pos = renderSettings.testOrthoCenter;
  213. }
  214. worldView.set(0, right.getX());
  215. worldView.set(1, up.getX());
  216. worldView.set(2, back.getX());
  217. worldView.set(4, right.getY());
  218. worldView.set(5, up.getY());
  219. worldView.set(6, back.getY());
  220. worldView.set(8, right.getZ());
  221. worldView.set(9, up.getZ());
  222. worldView.set(10, back.getZ());
  223. worldView.set(12, right.dotInverse(pos));
  224. worldView.set(13, up.dotInverse(pos));
  225. worldView.set(14, back.dotInverse(pos));
  226. if(!renderSettings.shadows) {
  227. return;
  228. }
  229. right.set(0.939693f, 0.0f, -0.34202f);
  230. back.set(0.280166f, 0.573576f, 0.769751f);
  231. up.set(-0.196175f, 0.819152f, -0.538986f);
  232. pos = renderSettings.testOrthoCenter;
  233. worldShadowView.set(0, right.getX());
  234. worldShadowView.set(1, up.getX());
  235. worldShadowView.set(2, back.getX());
  236. worldShadowView.set(4, right.getY());
  237. worldShadowView.set(5, up.getY());
  238. worldShadowView.set(6, back.getY());
  239. worldShadowView.set(8, right.getZ());
  240. worldShadowView.set(9, up.getZ());
  241. worldShadowView.set(10, back.getZ());
  242. worldShadowView.set(12, right.dotInverse(pos));
  243. worldShadowView.set(13, up.dotInverse(pos));
  244. worldShadowView.set(14, back.dotInverse(pos));
  245. }