Game.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #include "Game.h"
  2. #include "LayeredFramebuffer.h"
  3. #include "Texture3D.h"
  4. #include "math/Frustum.h"
  5. #include "rendering/FileTexture.h"
  6. #include "rendering/Framebuffer.h"
  7. #include "rendering/Shader.h"
  8. #include "rendering/VertexBuffer.h"
  9. #include "rendering/Window.h"
  10. #include "utils/Array.h"
  11. #include "utils/List.h"
  12. #include "utils/Random.h"
  13. #include "utils/Utils.h"
  14. #include "wrapper/GL.h"
  15. static constexpr float STEP = 1.0f / 31.5f;
  16. static Window window;
  17. static Shader cubeShader;
  18. static Shader noiseShader;
  19. static Shader particleShader;
  20. static Shader backgroundShader;
  21. static Shader blurShader;
  22. static Framebuffer<1> shadowBuffer;
  23. static Framebuffer<1> blurShadowBuffer;
  24. static LayeredFramebuffer noiceBuffer;
  25. static FileTexture bricks;
  26. static FileTexture bricksBump;
  27. static FileTexture bricksNormal;
  28. static VertexBuffer rectangleBuffer;
  29. static VertexBuffer emptyBuffer;
  30. static Frustum frustum{60.0f, 0.1f, 1000.0f, window.getSize()};
  31. static Matrix projection;
  32. static Matrix view;
  33. static Matrix shadowView;
  34. static Matrix shadowProjectionView;
  35. static Matrix model;
  36. static Button up{GLFW_KEY_SPACE, "Up"};
  37. static Button down{GLFW_KEY_LEFT_SHIFT, "Down"};
  38. static Button left{GLFW_KEY_A, "left"};
  39. static Button right{GLFW_KEY_D, "right"};
  40. static Button front{GLFW_KEY_W, "front"};
  41. static Button back{GLFW_KEY_S, "back"};
  42. static Button toggle{GLFW_KEY_T, "toggle"};
  43. static Button scaleUp{GLFW_KEY_G, "scale up"};
  44. static Button scaleDown{GLFW_KEY_H, "scale down"};
  45. static Button stepsUp{GLFW_KEY_Y, "steps up"};
  46. static Button stepsDown{GLFW_KEY_U, "steps down"};
  47. static Button fineStepsUp{GLFW_KEY_I, "fine steps up"};
  48. static Button fineStepsDown{GLFW_KEY_O, "fine steps down"};
  49. static Button modeToggle{GLFW_KEY_C, "mode toggle"};
  50. static Button primaryMouse{GLFW_MOUSE_BUTTON_1, "primary click"};
  51. static Button timeUp{GLFW_KEY_N, "time up"};
  52. static Button timeDown{GLFW_KEY_M, "time down"};
  53. static Button tesselationUp{GLFW_KEY_4, "time up"};
  54. static Button tesselationDown{GLFW_KEY_5, "time down"};
  55. static Vector3 oldPosition;
  56. static Vector3 position{-32.0f, 0.0f, -120.0f};
  57. static float oldHeight = 0.0f;
  58. static float height = 0.0f;
  59. static float heightScale = 0.01f;
  60. static int steps = 1;
  61. static int fineSteps = 1;
  62. static bool mode = false;
  63. static float timeTicks = 0.0f;
  64. static int tesselation = 1;
  65. static Vector3 emitterPos;
  66. static float emitterAge = 999999.0f;
  67. static float timeFactor = 1.0f;
  68. static void tickTimeFactors() {
  69. if(timeUp.isDown()) {
  70. timeFactor *= 1.05f;
  71. }
  72. if(timeDown.isDown()) {
  73. timeFactor /= 1.05f;
  74. }
  75. timeTicks++;
  76. }
  77. static void tickMovement() {
  78. oldHeight = height;
  79. oldPosition = position;
  80. if(up.isDown()) {
  81. height += 1.0f;
  82. }
  83. if(down.isDown()) {
  84. height -= 1.0f;
  85. }
  86. constexpr float speed = 3.5f;
  87. if(left.isDown()) {
  88. position += Vector3(speed, 0.0f, 0.0f);
  89. }
  90. if(right.isDown()) {
  91. position -= Vector3(speed, 0.0f, 0.0f);
  92. }
  93. if(front.isDown()) {
  94. position += Vector3(0.0f, 0.0f, speed);
  95. }
  96. if(back.isDown()) {
  97. position -= Vector3(0.0f, 0.0f, speed);
  98. }
  99. }
  100. static void tickParallaxSettings() {
  101. if(scaleUp.isDown()) {
  102. heightScale += 0.005f;
  103. }
  104. if(scaleDown.isDown()) {
  105. heightScale -= 0.005f;
  106. if(heightScale < 0.0f) {
  107. heightScale = 0.0f;
  108. }
  109. }
  110. if(stepsUp.wasReleased()) {
  111. steps++;
  112. }
  113. if(stepsDown.wasReleased() && steps > 1) {
  114. steps--;
  115. }
  116. if(fineStepsUp.wasReleased()) {
  117. fineSteps++;
  118. }
  119. if(fineStepsDown.wasReleased() && fineSteps > 1) {
  120. fineSteps--;
  121. }
  122. if(modeToggle.wasReleased()) {
  123. mode = !mode;
  124. }
  125. }
  126. static void tickTesselation() {
  127. if(tesselationUp.isDown()) {
  128. tesselation++;
  129. }
  130. if(tesselationDown.isDown() && tesselation > 1) {
  131. tesselation--;
  132. }
  133. }
  134. static void tickGame() {
  135. tickTimeFactors();
  136. tickMovement();
  137. tickParallaxSettings();
  138. tickTesselation();
  139. printf("\rFPS: %.2f", window.getFrameClock().getUpdatesPerSecond());
  140. fflush(stdout);
  141. }
  142. static void prepareMatrices(float lag) {
  143. projection = frustum.updateProjection();
  144. view.translateTo(Vector3(0.0f, 0.0f, 0.0f));
  145. view.translate(Utils::interpolate(oldPosition, position, lag));
  146. float h = -32.0f + (oldHeight - height) * lag;
  147. view.translateY(h);
  148. shadowView.translateTo(Vector3(-32.0f - 180.0f, h, -340.0f));
  149. shadowProjectionView.translateTo(Vector3(0.0f, 0.0f, 0.0f));
  150. shadowProjectionView.scale(0.5f);
  151. shadowProjectionView.translate(Vector3(0.5f, 0.5f, 0.5f));
  152. shadowProjectionView *= projection;
  153. shadowProjectionView *= shadowView;
  154. }
  155. static void renderNoise() {
  156. GL::setViewport(64, 64);
  157. noiseShader.use();
  158. noiceBuffer.bindAndClear();
  159. noiseShader.setFloat("height", oldHeight * STEP);
  160. for(int i = 0; i < 64; i++) {
  161. noiseShader.setFloat("layer", i * STEP - 1.0f);
  162. noiceBuffer.bindLayer(i);
  163. rectangleBuffer.draw(6);
  164. }
  165. }
  166. static void renderCubes(Matrix& view) {
  167. GL::setViewport(window.getSize().width, window.getSize().height);
  168. cubeShader.use();
  169. cubeShader.setMatrix("proj", projection.getValues());
  170. cubeShader.setMatrix("view", view.getValues());
  171. cubeShader.setMatrix("shadow", shadowProjectionView.getValues());
  172. cubeShader.setFloat("height", oldHeight * STEP * 0.5f);
  173. cubeShader.setVector("viewPos", -position + Vector3(0.0f, 32.0f, 0.0f));
  174. cubeShader.setVector("lightPos", Vector3());
  175. cubeShader.setFloat("heightScale", heightScale);
  176. cubeShader.setInt("steps", steps);
  177. cubeShader.setInt("fineSteps", fineSteps);
  178. cubeShader.setInt("kajetan", mode);
  179. noiceBuffer.bindTextureTo(0);
  180. bricks.bindTo(1);
  181. bricksBump.bindTo(2);
  182. bricksNormal.bindTo(3);
  183. blurShadowBuffer.bindTextureTo(0, 4);
  184. shadowBuffer.bindTextureTo(0, 5);
  185. for(int i = 0; i < 3; i++) {
  186. model.translateTo(Vector3(80.0f * i, 0.0f, 0.0f));
  187. cubeShader.setMatrix("model", model.getValues());
  188. emptyBuffer.drawPoints(64 * 64 * 64);
  189. }
  190. }
  191. static void pickParticleCenter() {
  192. if(!primaryMouse.wasReleased()) {
  193. return;
  194. }
  195. noiceBuffer.bindTextureTo(0);
  196. static float buffer[64][64][64];
  197. glGetTexImage(GL_TEXTURE_3D, 0, GL_RED, GL_FLOAT, buffer);
  198. float hWidth = window.getSize().width * 0.5f;
  199. float hHeight = window.getSize().height * 0.5f;
  200. float x = (window.buttons.getMouseX() - hWidth) / hWidth;
  201. float y = -(window.buttons.getMouseY() - hHeight) / hHeight;
  202. float aspect = hWidth / hHeight;
  203. float tan = tanf((0.5f * frustum.fieldOfView) * M_PI / 180.0f);
  204. float q = 1.0f / tan;
  205. Vector3 direction(x / (q / aspect), y / q, 1.0f);
  206. direction[2] = -direction[2];
  207. direction.normalize();
  208. Vector3 pos = -position;
  209. pos[1] = 32.0f;
  210. pos[1] = 32.0f;
  211. for(int i = 0; i < 150; i++) {
  212. int x = pos[0] + 0.5f;
  213. int y = pos[1] + 0.5f;
  214. int z = pos[2] + 0.5f;
  215. if(x >= 0 && x < 64 && y >= 0 && y < 64 && z >= 0 && z < 64 &&
  216. buffer[x][y][z] > 0.5f) {
  217. emitterPos = pos + Vector3(0.0f, height, 0.0f);
  218. emitterAge = timeTicks;
  219. break;
  220. }
  221. pos += direction;
  222. }
  223. }
  224. static void renderParticles(float lag) {
  225. glPointSize(5.0f);
  226. particleShader.use();
  227. particleShader.setMatrix("proj", projection.getValues());
  228. Matrix m;
  229. m.translateTo(Utils::interpolate(oldPosition, position, lag));
  230. m.translateY(-32.0f - (oldHeight + (height - oldHeight) * lag));
  231. particleShader.setMatrix("view", m.getValues());
  232. particleShader.setFloat("time", timeTicks + lag);
  233. GL::enableBlending();
  234. glDepthMask(false);
  235. pickParticleCenter();
  236. particleShader.setFloat("timeFactor", timeFactor);
  237. particleShader.setVector("position", emitterPos);
  238. particleShader.setFloat("age", emitterAge);
  239. particleShader.setVector("color", Vector4(1.0f, 0.0f, 0.0f, 1.0f));
  240. particleShader.setInt("seedBase", 0);
  241. emptyBuffer.drawPoints(200000);
  242. particleShader.setVector("position", emitterPos);
  243. particleShader.setFloat("age", emitterAge + 5);
  244. particleShader.setVector("color", Vector4(0.0f, 1.0f, 0.0f, 1.0f));
  245. particleShader.setInt("seedBase", 1);
  246. emptyBuffer.drawPoints(10000);
  247. particleShader.setVector("position", emitterPos);
  248. particleShader.setFloat("age", emitterAge + 10);
  249. particleShader.setVector("color", Vector4(0.0f, 0.0f, 1.0f, 1.0f));
  250. particleShader.setInt("seedBase", 2);
  251. emptyBuffer.drawPoints(5000);
  252. particleShader.setVector("position", emitterPos);
  253. particleShader.setFloat("age", emitterAge + 20);
  254. particleShader.setVector("color", Vector4(0.0f, 1.0f, 1.0f, 1.0f));
  255. particleShader.setInt("seedBase", 2);
  256. emptyBuffer.drawPoints(5000);
  257. glDepthMask(true);
  258. GL::disableBlending();
  259. }
  260. static void renderBackground(Matrix& view) {
  261. backgroundShader.use();
  262. blurShadowBuffer.bindTextureTo(0, 0);
  263. backgroundShader.setMatrix("proj", frustum.updateProjection().getValues());
  264. backgroundShader.setMatrix("view", view.getValues());
  265. model.translateTo(Vector3(0.0f, 0.0f, 0.0f));
  266. model.scale(Vector3(520.0f, 180.0f, 1.0f));
  267. model.translate(Vector3(-260.0f, -40.0f, -200.0f));
  268. model.rotateX(-5.0f);
  269. backgroundShader.setMatrix("model", model.getValues());
  270. backgroundShader.setMatrix("shadow", shadowProjectionView.getValues());
  271. backgroundShader.setInt("splits", tesselation);
  272. glPatchParameteri(GL_PATCH_VERTICES, 1);
  273. glDrawArrays(GL_PATCHES, 0, 1);
  274. }
  275. static void renderGame(float lag) {
  276. if(toggle.isDown()) {
  277. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  278. } else {
  279. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  280. }
  281. GL::enableDepthTesting();
  282. prepareMatrices(lag);
  283. renderNoise();
  284. shadowBuffer.bindAndClear();
  285. renderCubes(shadowView);
  286. renderBackground(shadowView);
  287. blurShader.use();
  288. shadowBuffer.bindTextureTo(0, 0);
  289. blurShadowBuffer.bindAndClear();
  290. rectangleBuffer.draw(6);
  291. GL::bindMainFramebuffer();
  292. GL::clear();
  293. renderCubes(view);
  294. renderBackground(view);
  295. renderParticles(lag);
  296. }
  297. void Game::init() {
  298. WindowOptions options(4, 0, {1024, 620}, false, "test");
  299. Error error = window.open(options);
  300. if(error.has()) {
  301. error.message.printLine();
  302. return;
  303. }
  304. error = cubeShader.compile("resources/cubes.vs", "resources/cubes.gs",
  305. "resources/cubes.fs");
  306. if(error.has()) {
  307. error.message.printLine();
  308. return;
  309. }
  310. error = noiseShader.compile("resources/noise.vs", "resources/noise.fs");
  311. if(error.has()) {
  312. error.message.printLine();
  313. return;
  314. }
  315. error = particleShader.compile("resources/particles.vs",
  316. "resources/particles.gs",
  317. "resources/particles.fs");
  318. if(error.has()) {
  319. error.message.printLine();
  320. return;
  321. }
  322. error = backgroundShader.compile(
  323. "resources/background.vs", "resources/background.fs",
  324. "resources/background.tcs", "resources/background.tes");
  325. if(error.has()) {
  326. error.message.printLine();
  327. return;
  328. }
  329. error = blurShader.compile("resources/blur.vs", "resources/blur.fs");
  330. if(error.has()) {
  331. error.message.printLine();
  332. return;
  333. }
  334. error = shadowBuffer.init(window.getSize(), TextureFormat::depth32(true));
  335. if(error.has()) {
  336. error.message.printLine();
  337. return;
  338. }
  339. error = blurShadowBuffer.init(window.getSize(),
  340. TextureFormat::float32(2, true));
  341. if(error.has()) {
  342. error.message.printLine();
  343. return;
  344. }
  345. error = bricks.load("resources/bricks.png", 0);
  346. if(error.has()) {
  347. error.message.printLine();
  348. return;
  349. }
  350. error = bricksBump.load("resources/bricks_bump.png", 0);
  351. if(error.has()) {
  352. error.message.printLine();
  353. return;
  354. }
  355. error = bricksNormal.load("resources/bricks_normal.png", 0);
  356. if(error.has()) {
  357. error.message.printLine();
  358. return;
  359. }
  360. noiceBuffer.init(64, 64, 64);
  361. window.buttons.add(up);
  362. window.buttons.add(down);
  363. window.buttons.add(left);
  364. window.buttons.add(right);
  365. window.buttons.add(front);
  366. window.buttons.add(back);
  367. window.buttons.add(toggle);
  368. window.buttons.add(scaleUp);
  369. window.buttons.add(scaleDown);
  370. window.buttons.add(stepsUp);
  371. window.buttons.add(stepsDown);
  372. window.buttons.add(fineStepsUp);
  373. window.buttons.add(fineStepsDown);
  374. window.buttons.add(modeToggle);
  375. window.buttons.add(timeUp);
  376. window.buttons.add(timeDown);
  377. window.buttons.add(tesselationUp);
  378. window.buttons.add(tesselationDown);
  379. window.buttons.addMouse(primaryMouse);
  380. bricks.setLinearFilter();
  381. bricksBump.setLinearFilter();
  382. bricksNormal.setLinearFilter();
  383. rectangleBuffer.init(Attributes().addFloat(2));
  384. float recData[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.0},
  385. {1.0f, 1.0f}, {-1.0, 1.0}, {1.0, -1.0}};
  386. rectangleBuffer.setStaticData(sizeof(recData), recData);
  387. emptyBuffer.init(Attributes());
  388. struct Intern {
  389. void tick() {
  390. tickGame();
  391. }
  392. void render(float lag) {
  393. renderGame(lag);
  394. }
  395. bool isRunning() const {
  396. return true;
  397. }
  398. };
  399. Intern intern;
  400. window.run(intern, 50'000'000);
  401. }