Engine.cpp 9.6 KB

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