GameClient.cpp 11 KB

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