Game.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/Utils.h"
  8. #include "gaming-core/wrapper/GL.h"
  9. Game::Game(Shader& shader, Shader& noiceShader, LayeredFramebuffer& buffer,
  10. Buttons& buttons, const Size& size)
  11. : shader(shader), noiceShader(noiceShader), noiceBuffer(buffer),
  12. buttons(buttons), size(size), frustum(60, 0.1f, 1000.0f, size),
  13. up(GLFW_KEY_SPACE, "Up"), down(GLFW_KEY_LEFT_SHIFT, "Down"),
  14. left(GLFW_KEY_A, "left"), right(GLFW_KEY_D, "right"),
  15. front(GLFW_KEY_W, "front"), back(GLFW_KEY_S, "back"),
  16. toggle(GLFW_KEY_T, "toggle"), oldHeight(0.0f), height(0.0f) {
  17. buttons.add(up);
  18. buttons.add(down);
  19. buttons.add(left);
  20. buttons.add(right);
  21. buttons.add(front);
  22. buttons.add(back);
  23. buttons.add(toggle);
  24. rectangleBuffer.setAttributes(Attributes().addFloat(2));
  25. float data[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.0},
  26. {1.0f, 1.0f}, {-1.0, 1.0}, {1.0, -1.0}};
  27. rectangleBuffer.setStaticData(sizeof(data), data);
  28. noiceBuffer.bindTextureTo(0);
  29. }
  30. void Game::render(float lag) {
  31. GL::setViewport(64, 64);
  32. noiceShader.use();
  33. noiceBuffer.bindAndClear();
  34. float step = 1.0f / 31.5f;
  35. noiceShader.setFloat("height", oldHeight * step);
  36. for(int i = 0; i < 64; i++) {
  37. noiceShader.setFloat("layer", i * step - 1.0f);
  38. noiceBuffer.bindLayer(i);
  39. rectangleBuffer.draw(6);
  40. }
  41. GL::setViewport(size.width, size.height);
  42. shader.use();
  43. GL::bindMainFramebuffer();
  44. GL::clear();
  45. shader.setMatrix("proj", frustum.updateProjection().getValues());
  46. Matrix m;
  47. m.translate(Utils::interpolate(oldPosition, position, lag));
  48. m.translateZ(-32.0f);
  49. m.translateY(-32.0f + (oldHeight - height) * lag);
  50. m.translateX(-32.0f);
  51. shader.setMatrix("view", m.getValues());
  52. if(toggle.isDown()) {
  53. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  54. }
  55. noiceBuffer.bindTextureTo(0);
  56. emptyBuffer.drawPoints(64 * 64 * 64);
  57. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  58. }
  59. void Game::tick() {
  60. oldHeight = height;
  61. oldPosition = position;
  62. if(up.isDown()) {
  63. height += 1.0f;
  64. }
  65. if(down.isDown()) {
  66. height -= 1.0f;
  67. }
  68. const float speed = 2.5f;
  69. if(left.isDown()) {
  70. position += Vector3(speed, 0.0f, 0.0f);
  71. }
  72. if(right.isDown()) {
  73. position -= Vector3(speed, 0.0f, 0.0f);
  74. }
  75. if(front.isDown()) {
  76. position += Vector3(0.0f, 0.0f, speed);
  77. }
  78. if(back.isDown()) {
  79. position -= Vector3(0.0f, 0.0f, speed);
  80. }
  81. }
  82. bool Game::isRunning() const {
  83. return true;
  84. }