Engine.cpp 9.1 KB

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