GameClient.cpp 13 KB

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