Game.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, LayeredFramebuffer& buffer,
  11. Buttons& buttons, const Size& size)
  12. : shader(shader), noiceShader(noiceShader), noiceBuffer(buffer),
  13. bricks("resources/bricks.png"), bricksBump("resources/bricks_bump.png"),
  14. bricksNormal("resources/bricks_normal.png"), buttons(buttons), size(size),
  15. frustum(60, 0.1f, 1000.0f, size), up(GLFW_KEY_SPACE, "Up"),
  16. down(GLFW_KEY_LEFT_SHIFT, "Down"), left(GLFW_KEY_A, "left"),
  17. right(GLFW_KEY_D, "right"), front(GLFW_KEY_W, "front"),
  18. back(GLFW_KEY_S, "back"), toggle(GLFW_KEY_T, "toggle"),
  19. scaleUp(GLFW_KEY_G, "scale up"), scaleDown(GLFW_KEY_H, "scale down"),
  20. oldHeight(0.0f), height(0.0f), heightScale(0.0f) {
  21. buttons.add(up);
  22. buttons.add(down);
  23. buttons.add(left);
  24. buttons.add(right);
  25. buttons.add(front);
  26. buttons.add(back);
  27. buttons.add(toggle);
  28. buttons.add(scaleUp);
  29. buttons.add(scaleDown);
  30. position = Vector3(0.0f, 0.0f, -40.0f);
  31. rectangleBuffer.setAttributes(Attributes().addFloat(2));
  32. float recData[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.0},
  33. {1.0f, 1.0f}, {-1.0, 1.0}, {1.0, -1.0}};
  34. rectangleBuffer.setStaticData(sizeof(recData), recData);
  35. noiceBuffer.bindTextureTo(0);
  36. }
  37. void Game::render(float lag) {
  38. GL::setViewport(64, 64);
  39. noiceShader.use();
  40. noiceBuffer.bindAndClear();
  41. float step = 1.0f / 31.5f;
  42. noiceShader.setFloat("height", oldHeight * step);
  43. for(int i = 0; i < 64; i++) {
  44. noiceShader.setFloat("layer", i * step - 1.0f);
  45. noiceBuffer.bindLayer(i);
  46. rectangleBuffer.draw(6);
  47. }
  48. GL::setViewport(size.width, size.height);
  49. shader.use();
  50. GL::bindMainFramebuffer();
  51. GL::clear();
  52. shader.setMatrix("proj", frustum.updateProjection().getValues());
  53. Matrix m;
  54. m.translate(Utils::interpolate(oldPosition, position, lag));
  55. m.translateY(-32.0f + (oldHeight - height) * lag);
  56. shader.setMatrix("view", m.getValues());
  57. shader.setFloat("height", oldHeight * step * 0.5f);
  58. shader.setVector("viewPos", -position + Vector3(0.0f, 32.0f, 0.0f));
  59. shader.setVector("lightPos", Vector3());
  60. shader.setFloat("heightScale", heightScale);
  61. if(toggle.isDown()) {
  62. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  63. }
  64. noiceBuffer.bindTextureTo(0);
  65. bricks.bindTo(1);
  66. bricksBump.bindTo(2);
  67. bricksNormal.bindTo(3);
  68. emptyBuffer.drawPoints(64 * 64 * 64);
  69. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  70. }
  71. void Game::tick() {
  72. oldHeight = height;
  73. oldPosition = position;
  74. if(up.isDown()) {
  75. height += 1.0f;
  76. }
  77. if(down.isDown()) {
  78. height -= 1.0f;
  79. }
  80. const float speed = 1.15f;
  81. if(left.isDown()) {
  82. position += Vector3(speed, 0.0f, 0.0f);
  83. }
  84. if(right.isDown()) {
  85. position -= Vector3(speed, 0.0f, 0.0f);
  86. }
  87. if(front.isDown()) {
  88. position += Vector3(0.0f, 0.0f, speed);
  89. }
  90. if(back.isDown()) {
  91. position -= Vector3(0.0f, 0.0f, speed);
  92. }
  93. if(scaleUp.isDown()) {
  94. heightScale += 0.005f;
  95. }
  96. if(scaleDown.isDown()) {
  97. heightScale -= 0.005f;
  98. if(heightScale < 0.0f) {
  99. heightScale = 0.0f;
  100. }
  101. }
  102. }
  103. bool Game::isRunning() const {
  104. return true;
  105. }