Game.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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(buttons.add(GLFW_KEY_SPACE, "Up")),
  14. down(buttons.add(GLFW_KEY_LEFT_SHIFT, "Down")),
  15. left(buttons.add(GLFW_KEY_A, "left")),
  16. right(buttons.add(GLFW_KEY_D, "right")),
  17. front(buttons.add(GLFW_KEY_W, "front")),
  18. back(buttons.add(GLFW_KEY_S, "back")) {
  19. rectangleBuffer.setAttributes(Attributes().addFloat(2));
  20. float data[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.0},
  21. {1.0f, 1.0f}, {-1.0, 1.0}, {1.0, -1.0}};
  22. rectangleBuffer.setStaticData(sizeof(data), data);
  23. noiceBuffer.bindTextureTo(0);
  24. }
  25. void Game::render(float lag) {
  26. GL::setViewport(64, 64);
  27. noiceShader.use();
  28. noiceBuffer.bindAndClear();
  29. for(int i = 0; i < 64; i++) {
  30. noiceShader.setFloat("layer", i * (1.0f / 63.0f));
  31. noiceBuffer.bindLayer(i);
  32. rectangleBuffer.draw(6);
  33. }
  34. GL::setViewport(size.width, size.height);
  35. shader.use();
  36. GL::bindMainFramebuffer();
  37. GL::clearFramebuffer();
  38. Vector3 interPos = Utils::interpolate(oldPosition, position, lag);
  39. shader.setMatrix("proj", frustum.updateProjection().getValues());
  40. Matrix m;
  41. m.translate(interPos);
  42. m.translateZ(-30.0f);
  43. m.translateX(-8.0f);
  44. shader.setMatrix("view", m.getValues());
  45. noiceBuffer.bindTextureTo(0);
  46. emptyBuffer.drawPoints(0);
  47. glDrawArrays(GL_POINTS, 0, 64 * 64 * 64);
  48. }
  49. void Game::tick() {
  50. oldPosition = position;
  51. const float speed = 2.5f;
  52. if(up.isDown()) {
  53. position -= Vector3(0.0f, speed, 0.0f);
  54. }
  55. if(down.isDown()) {
  56. position += Vector3(0.0f, speed, 0.0f);
  57. }
  58. if(left.isDown()) {
  59. position += Vector3(speed, 0.0f, 0.0f);
  60. }
  61. if(right.isDown()) {
  62. position -= Vector3(speed, 0.0f, 0.0f);
  63. }
  64. if(front.isDown()) {
  65. position += Vector3(0.0f, 0.0f, speed);
  66. }
  67. if(back.isDown()) {
  68. position -= Vector3(0.0f, 0.0f, speed);
  69. }
  70. }