Game.cpp 13 KB

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