GameClient.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #include <iostream>
  2. #include <GL/glew.h>
  3. #include <GLFW/glfw3.h>
  4. #include <unordered_map>
  5. #include <cmath>
  6. #include <cstring>
  7. #include "common/utils/Types.h"
  8. #include "client/GameClient.h"
  9. #include "client/input/Keys.h"
  10. #include "client/input/MouseButtons.h"
  11. #include "client/rendering/Shader.h"
  12. #include "client/rendering/FontRenderer.h"
  13. #include "client/rendering/Mesh.h"
  14. #include "client/rendering/Framebuffer.h"
  15. #include "client/Utils.h"
  16. #include "client/math/Camera.h"
  17. #include "client/math/Matrix.h"
  18. #include "client/math/MatrixStack.h"
  19. #include "client/Game.h"
  20. #include "rendering/NoiseTexture.h"
  21. struct InternGameClient
  22. {
  23. ~InternGameClient()
  24. {
  25. if(window != nullptr)
  26. {
  27. glfwDestroyWindow(window);
  28. }
  29. if(glfwInitDone)
  30. {
  31. glfwTerminate();
  32. }
  33. }
  34. bool glfwInitDone = false;
  35. GLFWwindow* window = nullptr;
  36. };
  37. static const u64 NANOS_PER_TICK = 50000000;
  38. static const float lagFactor = 1.0f / NANOS_PER_TICK;
  39. static u64 timeFactor = 1;
  40. static int width = 0;
  41. static int height = 0;
  42. static bool resize = false;
  43. static float fovY = 60.0f;
  44. static float nearClip = 0.1f;
  45. static float farClip = 1000.0f;
  46. static InternGameClient client;
  47. static Keys keys;
  48. static MouseButtons mButtons;
  49. struct Shaders
  50. {
  51. Shaders() :
  52. world("resources/shader/worldVertex.vs", "resources/shader/worldFragment.fs"),
  53. ssao("resources/shader/ssaoVertex.vs", "resources/shader/ssaoFragment.fs"),
  54. ssaoBlur("resources/shader/ssaoBlurVertex.vs", "resources/shader/ssaoBlurFragment.fs"),
  55. postWorld("resources/shader/worldPostVertex.vs", "resources/shader/worldPostFragment.fs"),
  56. text("resources/shader/textVertex.vs", "resources/shader/textFragment.fs")
  57. {
  58. }
  59. Shader world;
  60. Shader ssao;
  61. Shader ssaoBlur;
  62. Shader postWorld;
  63. Shader text;
  64. float worldProj[16] =
  65. {
  66. 1.0f, 0.0f, 0.0f, 0.0f,
  67. 0.0f, 1.0f, 0.0f, 0.0f,
  68. 0.0f, 0.0f, 1.0f, -1.0f,
  69. 0.0f, 0.0f, 1.0, 0.0f
  70. };
  71. float worldView[16]=
  72. {
  73. 1.0f, 0.0f, 0.0f, 0.0f,
  74. 0.0f, 1.0f, 0.0f, 0.0f,
  75. 0.0f, 0.0f, 1.0f, 0.0f,
  76. 0.0f, 0.0f, 0.0f, 1.0f
  77. };
  78. bool isValid() const
  79. {
  80. return world.isValid() && ssao.isValid() && ssaoBlur.isValid() && postWorld.isValid() && text.isValid();
  81. }
  82. void updateWorldProjection()
  83. {
  84. float tan = tanf((0.5f * fovY) * M_PI / 180.0f);
  85. float q = 1.0f / tan;
  86. float aspect = (float) width / height;
  87. worldProj[0] = q / aspect;
  88. worldProj[5] = q;
  89. worldProj[10] = (nearClip + farClip) / (nearClip - farClip);
  90. worldProj[14] = (2.0f * nearClip * farClip) / (nearClip - farClip);
  91. }
  92. void updateWorldView(float lag, Camera& cam)
  93. {
  94. cam.update(lag);
  95. const Vector right = cam.getRight();
  96. const Vector up = cam.getUp();
  97. const Vector back = cam.getBack();
  98. const Vector pos = cam.getPosition();
  99. worldView[0] = right.getX();
  100. worldView[1] = up.getX();
  101. worldView[2] = back.getX();
  102. worldView[4] = right.getY();
  103. worldView[5] = up.getY();
  104. worldView[6] = back.getY();
  105. worldView[8] = right.getZ();
  106. worldView[9] = up.getZ();
  107. worldView[10] = back.getZ();
  108. worldView[12] = right.dotInverse(pos);
  109. worldView[13] = up.dotInverse(pos);
  110. worldView[14] = back.dotInverse(pos);
  111. }
  112. };
  113. struct Framebuffers
  114. {
  115. Framebuffers(u32 w, u32 h) : worldBuffer(w, h, Framebuffer::POSITION |
  116. Framebuffer::NORMAL | Framebuffer::COLOR | Framebuffer::DEPTH24_STENCIL8),
  117. ssaoBuffer(w, h, Framebuffer::RED), ssaoBlurBuffer(w, h, Framebuffer::RED)
  118. {
  119. }
  120. void resize(u32 w, u32 h) const
  121. {
  122. worldBuffer.resize(w, h);
  123. ssaoBuffer.resize(w, h);
  124. ssaoBlurBuffer.resize(w, h);
  125. }
  126. bool isValid() const
  127. {
  128. return worldBuffer.isValid() && ssaoBuffer.isValid() && ssaoBlurBuffer.isValid();
  129. }
  130. Framebuffer worldBuffer;
  131. Framebuffer ssaoBuffer;
  132. Framebuffer ssaoBlurBuffer;
  133. };
  134. struct InternGame
  135. {
  136. InternGame() : ssaoNoise(4, 4)
  137. {
  138. rectangle.add({-1, -1, 0, 0, 0, 0, 0, 0});
  139. rectangle.add({ 1, 1, 0, 1, 1, 0, 0, 0});
  140. rectangle.add({-1, 1, 0, 0, 1, 0, 0, 0});
  141. rectangle.add({-1, -1, 0, 0, 0, 0, 0, 0});
  142. rectangle.add({ 1, -1, 0, 1, 0, 0, 0, 0});
  143. rectangle.add({ 1, 1, 0, 1, 1, 0, 0, 0});
  144. rectangle.build();
  145. }
  146. Game game;
  147. Camera cam;
  148. MatrixStack model;
  149. FontRenderer fontRenderer;
  150. NoiseTexture ssaoNoise;
  151. Mesh rectangle;
  152. };
  153. static u64 getTimeNanos()
  154. {
  155. return glfwGetTimerValue() * timeFactor;
  156. }
  157. static bool initGLFW()
  158. {
  159. client.glfwInitDone = glfwInit();
  160. if(!client.glfwInitDone)
  161. {
  162. std::cout << "could not initialize GLFW\n";
  163. return true;
  164. }
  165. timeFactor = 1000000000 / glfwGetTimerFrequency();
  166. return false;
  167. }
  168. static bool initWindow(int w, int h, const char* windowName)
  169. {
  170. width = w;
  171. height = h;
  172. glfwDefaultWindowHints();
  173. glfwWindowHint(GLFW_VISIBLE, 0);
  174. glfwWindowHint(GLFW_RESIZABLE, 1);
  175. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  176. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  177. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  178. client.window = glfwCreateWindow(width, height, windowName, nullptr, nullptr);
  179. if(client.window == nullptr)
  180. {
  181. std::cout << "could not create window\n";
  182. return true;
  183. }
  184. glfwMakeContextCurrent(client.window);
  185. glfwSwapInterval(1);
  186. return false;
  187. }
  188. static bool initGLEW()
  189. {
  190. GLenum err = glewInit();
  191. if(err != GLEW_OK)
  192. {
  193. std::cout << "could not initialize GLEW: " << glewGetErrorString(err) << "\n";
  194. return true;
  195. }
  196. std::cout << "using GLEW " << glewGetString(GLEW_VERSION) << "\n";
  197. return false;
  198. }
  199. static void initCallbacks()
  200. {
  201. // GLFWwindow* w, int key, int scancode, int action, int mod
  202. glfwSetKeyCallback(client.window, [](GLFWwindow*, int key, int, int action, int)
  203. {
  204. if(action == GLFW_PRESS)
  205. {
  206. keys.press(key);
  207. }
  208. else if(action == GLFW_RELEASE)
  209. {
  210. keys.release(key);
  211. }
  212. });
  213. // GLFWwindow* w, int button, int action, int mods
  214. glfwSetMouseButtonCallback(client.window, [](GLFWwindow*, int button, int action, int)
  215. {
  216. if(action == GLFW_PRESS)
  217. {
  218. mButtons.press(button);
  219. }
  220. else if(action == GLFW_RELEASE)
  221. {
  222. mButtons.release(button);
  223. }
  224. });
  225. // GLFWwindow* w, double xpos, double ypos
  226. glfwSetCursorPosCallback(client.window, [](GLFWwindow*, double x, double y)
  227. {
  228. mButtons.move(x, y);
  229. });
  230. // GLFWwindow* w, int width, int height
  231. glfwSetFramebufferSizeCallback(client.window, [](GLFWwindow*, int w, int h)
  232. {
  233. glViewport(0, 0, w, h);
  234. width = w;
  235. height = h;
  236. resize = true;
  237. });
  238. }
  239. static void tick(InternGame& game)
  240. {
  241. keys.tick();
  242. mButtons.tick();
  243. game.game.tick(keys, mButtons, game.cam);
  244. mButtons.postTick();
  245. }
  246. static void renderWorld(float lag, Shaders& shaders, InternGame& game, Framebuffers& fb)
  247. {
  248. fb.worldBuffer.bind();
  249. glEnable(GL_DEPTH_TEST);
  250. shaders.world.use();
  251. shaders.world.setMatrix("proj", shaders.worldProj);
  252. shaders.world.setMatrix("view", shaders.worldView);
  253. game.model.clear();
  254. shaders.world.setMatrix("model", game.model.get().getValues());
  255. game.game.renderWorld(lag, game.model, shaders.world);
  256. }
  257. static void renderSSAO(Shaders& shaders, InternGame& game, Framebuffers& fb)
  258. {
  259. // ssao
  260. shaders.ssao.use();
  261. shaders.ssao.setMatrix("view", shaders.worldView);
  262. shaders.ssao.setMatrix("proj", shaders.worldProj);
  263. shaders.ssao.setInt("width", width);
  264. shaders.ssao.setInt("height", height);
  265. fb.worldBuffer.bindPositionTexture(0);
  266. fb.worldBuffer.bindNormalTexture(1);
  267. fb.worldBuffer.bindColorTexture(2);
  268. fb.worldBuffer.bindDepthTexture(3);
  269. game.ssaoNoise.bind(4);
  270. fb.ssaoBuffer.bind();
  271. game.rectangle.draw();
  272. // ssao blur
  273. shaders.ssaoBlur.use();
  274. fb.ssaoBuffer.bindRedTexture(0);
  275. fb.worldBuffer.bindColorTexture(1);
  276. fb.ssaoBlurBuffer.bind();
  277. game.rectangle.draw();
  278. }
  279. static void renderPostWorld(Shaders& shaders, InternGame& game, Framebuffers& fb)
  280. {
  281. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  282. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  283. fb.worldBuffer.bindColorTexture(0);
  284. fb.ssaoBlurBuffer.bindRedTexture(1);
  285. shaders.postWorld.use();
  286. //glDisable(GL_CULL_FACE);
  287. glDisable(GL_DEPTH_TEST);
  288. glEnable(GL_BLEND);
  289. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  290. glBlendEquation(GL_FUNC_ADD);
  291. game.rectangle.draw();
  292. glDisable(GL_BLEND);
  293. }
  294. /*static void renderTextOverlay(float lag, Shaders& shaders, InternGame& game)
  295. {
  296. glDisable(GL_DEPTH_TEST);
  297. shaders.text.use();
  298. Matrix m;
  299. shaders.text.setMatrix("proj", m.getValues());
  300. m.translate(-1.0f, 1.0f, 0.0f);
  301. m.scale(2.0f / width, -2.0f / height, 1.0f);
  302. shaders.text.setMatrix("view", m.getValues());
  303. game.model.clear();
  304. game.model.get().scale(2.0f, 2.0f, 2.0f);
  305. shaders.text.setMatrix("model", game.model.get().getValues());
  306. glEnable(GL_BLEND);
  307. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  308. glBlendEquation(GL_FUNC_ADD);
  309. game.game.renderTextOverlay(lag, game.model, shaders.text, game.fontRenderer);
  310. glDisable(GL_BLEND);
  311. }*/
  312. static void renderTick(float lag, Shaders& shaders, InternGame& game, Framebuffers& fb)
  313. {
  314. if(resize)
  315. {
  316. fb.resize(width, height);
  317. resize = false;
  318. }
  319. shaders.updateWorldProjection();
  320. shaders.updateWorldView(lag, game.cam);
  321. renderWorld(lag, shaders, game, fb);
  322. renderSSAO(shaders, game, fb);
  323. renderPostWorld(shaders, game, fb);
  324. }
  325. static void loop()
  326. {
  327. Shaders shaders;
  328. if(!shaders.isValid())
  329. {
  330. return;
  331. }
  332. Framebuffers fb(width, height);
  333. if(!fb.isValid())
  334. {
  335. return;
  336. }
  337. InternGame game;
  338. u64 lastTime = getTimeNanos();
  339. u64 lag = 0;
  340. while(!glfwWindowShouldClose(client.window))
  341. {
  342. renderTick(lag * lagFactor, shaders, game, fb);
  343. glfwSwapBuffers(client.window);
  344. u64 newTime = getTimeNanos();
  345. lag += newTime - lastTime;
  346. lastTime = newTime;
  347. while(lag >= NANOS_PER_TICK)
  348. {
  349. lag -= NANOS_PER_TICK;
  350. tick(game);
  351. }
  352. glfwPollEvents();
  353. }
  354. }
  355. void GameClient::start(int w, int h, const char* windowName)
  356. {
  357. if(initGLFW() || initWindow(w, h, windowName) || initGLEW())
  358. {
  359. return;
  360. }
  361. initCallbacks();
  362. glfwShowWindow(client.window);
  363. loop();
  364. }