Game.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include <cmath>
  2. #include <iostream>
  3. #include "Game.h"
  4. #include "MarchingCubes.h"
  5. #include "gaming-core/utils/Array.h"
  6. #include "gaming-core/utils/List.h"
  7. #include "gaming-core/utils/Random.h"
  8. #include "gaming-core/utils/Utils.h"
  9. #include "gaming-core/wrapper/GL.h"
  10. Game::Game(Shader& shader, Shader& noiceShader, Shader& particleShader,
  11. LayeredFramebuffer& buffer, Buttons& buttons, const Size& size)
  12. : shader(shader), noiceShader(noiceShader), particleShader(particleShader),
  13. noiceBuffer(buffer), bricks("resources/bricks.png"),
  14. bricksBump("resources/bricks_bump.png"),
  15. bricksNormal("resources/bricks_normal.png"), buttons(buttons), size(size),
  16. frustum(60.0f, 0.1f, 1000.0f, size), up(GLFW_KEY_SPACE, "Up"),
  17. down(GLFW_KEY_LEFT_SHIFT, "Down"), left(GLFW_KEY_A, "left"),
  18. right(GLFW_KEY_D, "right"), front(GLFW_KEY_W, "front"),
  19. back(GLFW_KEY_S, "back"), toggle(GLFW_KEY_T, "toggle"),
  20. scaleUp(GLFW_KEY_G, "scale up"), scaleDown(GLFW_KEY_H, "scale down"),
  21. stepsUp(GLFW_KEY_Y, "steps up"), stepsDown(GLFW_KEY_U, "steps down"),
  22. fineStepsUp(GLFW_KEY_I, "fine steps up"),
  23. fineStepsDown(GLFW_KEY_O, "fine steps down"),
  24. modeToggle(GLFW_KEY_C, "mode toggle"),
  25. primaryMouse(GLFW_MOUSE_BUTTON_1, "primary click"),
  26. timeUp(GLFW_KEY_N, "time up"), timeDown(GLFW_KEY_M, "time down"),
  27. oldHeight(0.0f), height(0.0f), heightScale(0.01f), steps(1), fineSteps(1),
  28. mode(false), time(0.0f), emitterAge(999999.0f), timeFactor(1.0f) {
  29. buttons.add(up);
  30. buttons.add(down);
  31. buttons.add(left);
  32. buttons.add(right);
  33. buttons.add(front);
  34. buttons.add(back);
  35. buttons.add(toggle);
  36. buttons.add(scaleUp);
  37. buttons.add(scaleDown);
  38. buttons.add(stepsUp);
  39. buttons.add(stepsDown);
  40. buttons.add(fineStepsUp);
  41. buttons.add(fineStepsDown);
  42. buttons.add(modeToggle);
  43. buttons.add(timeUp);
  44. buttons.add(timeDown);
  45. buttons.addMouse(primaryMouse);
  46. bricks.setLinearFilter();
  47. bricksBump.setLinearFilter();
  48. bricksNormal.setLinearFilter();
  49. position = Vector3(-32.0f, 0.0f, -120.0f);
  50. rectangleBuffer.setAttributes(Attributes().addFloat(2));
  51. float recData[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.0},
  52. {1.0f, 1.0f}, {-1.0, 1.0}, {1.0, -1.0}};
  53. rectangleBuffer.setStaticData(sizeof(recData), recData);
  54. noiceBuffer.bindTextureTo(0);
  55. }
  56. void Game::render(float lag) {
  57. GL::setViewport(64, 64);
  58. noiceShader.use();
  59. noiceBuffer.bindAndClear();
  60. float step = 1.0f / 31.5f;
  61. noiceShader.setFloat("height", oldHeight * step);
  62. for(int i = 0; i < 64; i++) {
  63. noiceShader.setFloat("layer", i * step - 1.0f);
  64. noiceBuffer.bindLayer(i);
  65. rectangleBuffer.draw(6);
  66. }
  67. GL::setViewport(size.width, size.height);
  68. shader.use();
  69. GL::bindMainFramebuffer();
  70. GL::clear();
  71. Matrix& proj = frustum.updateProjection();
  72. shader.setMatrix("proj", proj.getValues());
  73. Matrix m;
  74. m.translate(Utils::interpolate(oldPosition, position, lag));
  75. m.translateY(-32.0f + (oldHeight - height) * lag);
  76. shader.setMatrix("view", m.getValues());
  77. shader.setFloat("height", oldHeight * step * 0.5f);
  78. shader.setVector("viewPos", -position + Vector3(0.0f, 32.0f, 0.0f));
  79. shader.setVector("lightPos", Vector3());
  80. shader.setFloat("heightScale", heightScale);
  81. shader.setInt("steps", steps);
  82. shader.setInt("fineSteps", fineSteps);
  83. shader.setInt("kajetan", mode);
  84. if(toggle.isDown()) {
  85. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  86. }
  87. noiceBuffer.bindTextureTo(0);
  88. bricks.bindTo(1);
  89. bricksBump.bindTo(2);
  90. bricksNormal.bindTo(3);
  91. emptyBuffer.drawPoints(64 * 64 * 64);
  92. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  93. glPointSize(5.0f);
  94. particleShader.use();
  95. particleShader.setMatrix("proj", proj.getValues());
  96. m.translateTo(Utils::interpolate(oldPosition, position, lag));
  97. m.translateY(-32.0f - (oldHeight + (height - oldHeight) * lag));
  98. particleShader.setMatrix("view", m.getValues());
  99. particleShader.setFloat("time", time + lag);
  100. GL::enableBlending();
  101. glDepthMask(false);
  102. if(primaryMouse.wasReleased()) {
  103. noiceBuffer.bindTextureTo(0);
  104. static float buffer[64][64][64];
  105. glGetTexImage(GL_TEXTURE_3D, 0, GL_RED, GL_FLOAT, buffer);
  106. float hWidth = size.width * 0.5f;
  107. float hHeight = size.height * 0.5f;
  108. float x = (buttons.getMouseX() - hWidth) / hWidth;
  109. float y = -(buttons.getMouseY() - hHeight) / hHeight;
  110. float aspect = hWidth / hHeight;
  111. float tan = tanf((0.5f * frustum.fieldOfView) * M_PI / 180.0f);
  112. float q = 1.0f / tan;
  113. Vector3 direction(x / (q / aspect), y / q, 1.0f);
  114. direction[2] = -direction[2];
  115. direction.normalize();
  116. Vector3 pos = -position;
  117. pos[1] = 32.0f;
  118. pos[1] = 32.0f;
  119. for(int i = 0; i < 150; i++) {
  120. int x = pos[0] + 0.5f;
  121. int y = pos[1] + 0.5f;
  122. int z = pos[2] + 0.5f;
  123. if(x >= 0 && x < 64 && y >= 0 && y < 64 && z >= 0 && z < 64 &&
  124. buffer[x][y][z] > 0.5f) {
  125. emitterPos = pos + Vector3(0.0f, height, 0.0f);
  126. emitterAge = time;
  127. break;
  128. }
  129. pos += direction;
  130. }
  131. }
  132. particleShader.setFloat("timeFactor", timeFactor);
  133. particleShader.setVector("position", emitterPos);
  134. particleShader.setFloat("age", emitterAge);
  135. particleShader.setVector("color", Vector4(1.0f, 0.0f, 0.0f, 1.0f));
  136. particleShader.setInt("seedBase", 0);
  137. emptyBuffer.drawPoints(2000000);
  138. particleShader.setVector("position", emitterPos);
  139. particleShader.setFloat("age", emitterAge + 5);
  140. particleShader.setVector("color", Vector4(0.0f, 1.0f, 0.0f, 1.0f));
  141. particleShader.setInt("seedBase", 1);
  142. emptyBuffer.drawPoints(10000);
  143. particleShader.setVector("position", emitterPos);
  144. particleShader.setFloat("age", emitterAge + 10);
  145. particleShader.setVector("color", Vector4(0.0f, 0.0f, 1.0f, 1.0f));
  146. particleShader.setInt("seedBase", 2);
  147. emptyBuffer.drawPoints(5000);
  148. particleShader.setVector("position", emitterPos);
  149. particleShader.setFloat("age", emitterAge + 20);
  150. particleShader.setVector("color", Vector4(0.0f, 1.0f, 1.0f, 1.0f));
  151. particleShader.setInt("seedBase", 2);
  152. emptyBuffer.drawPoints(5000);
  153. glDepthMask(true);
  154. GL::disableBlending();
  155. }
  156. void Game::tick() {
  157. if(timeUp.isDown()) {
  158. timeFactor *= 1.05f;
  159. std::cout << timeFactor << "\n";
  160. }
  161. if(timeDown.isDown()) {
  162. timeFactor /= 1.05f;
  163. std::cout << timeFactor << "\n";
  164. }
  165. time++;
  166. oldHeight = height;
  167. oldPosition = position;
  168. if(up.isDown()) {
  169. height += 1.0f;
  170. }
  171. if(down.isDown()) {
  172. height -= 1.0f;
  173. }
  174. const float speed = 1.15f;
  175. if(left.isDown()) {
  176. position += Vector3(speed, 0.0f, 0.0f);
  177. }
  178. if(right.isDown()) {
  179. position -= Vector3(speed, 0.0f, 0.0f);
  180. }
  181. if(front.isDown()) {
  182. position += Vector3(0.0f, 0.0f, speed);
  183. }
  184. if(back.isDown()) {
  185. position -= Vector3(0.0f, 0.0f, speed);
  186. }
  187. if(scaleUp.isDown()) {
  188. heightScale += 0.005f;
  189. }
  190. if(scaleDown.isDown()) {
  191. heightScale -= 0.005f;
  192. if(heightScale < 0.0f) {
  193. heightScale = 0.0f;
  194. }
  195. }
  196. if(stepsUp.wasReleased()) {
  197. steps++;
  198. }
  199. if(stepsDown.wasReleased() && steps > 1) {
  200. steps--;
  201. }
  202. if(fineStepsUp.wasReleased()) {
  203. fineSteps++;
  204. }
  205. if(fineStepsDown.wasReleased() && fineSteps > 1) {
  206. fineSteps--;
  207. }
  208. if(modeToggle.wasReleased()) {
  209. mode = !mode;
  210. }
  211. }
  212. bool Game::isRunning() const {
  213. return true;
  214. }