Engine.cpp 9.4 KB

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