Game.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. buttons(buttons), size(size), frustum(60, 0.1f, 1000.0f, size),
  14. up(GLFW_KEY_SPACE, "Up"), down(GLFW_KEY_LEFT_SHIFT, "Down"),
  15. left(GLFW_KEY_A, "left"), right(GLFW_KEY_D, "right"),
  16. front(GLFW_KEY_W, "front"), back(GLFW_KEY_S, "back"),
  17. toggle(GLFW_KEY_T, "toggle"), oldHeight(0.0f), height(0.0f) {
  18. buttons.add(up);
  19. buttons.add(down);
  20. buttons.add(left);
  21. buttons.add(right);
  22. buttons.add(front);
  23. buttons.add(back);
  24. buttons.add(toggle);
  25. rectangleBuffer.setAttributes(Attributes().addFloat(2));
  26. float recData[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.0},
  27. {1.0f, 1.0f}, {-1.0, 1.0}, {1.0, -1.0}};
  28. rectangleBuffer.setStaticData(sizeof(recData), recData);
  29. noiceBuffer.bindTextureTo(0);
  30. Random r;
  31. const int textureSize = 64;
  32. float tData[textureSize][textureSize][textureSize][3];
  33. for(int x = 0; x < textureSize; x++) {
  34. for(int y = 0; y < textureSize; y++) {
  35. for(int z = 0; z < textureSize; z++) {
  36. tData[x][y][z][0] = r.nextFloat();
  37. }
  38. }
  39. }
  40. for(int k = 1; k < 3; k++) {
  41. int factor = (k * 2 + 1) * (k * 2 + 1) * (k * 2 + 1);
  42. for(int x = 0; x < textureSize; x++) {
  43. for(int y = 0; y < textureSize; y++) {
  44. for(int z = 0; z < textureSize; z++) {
  45. float sum = 0.0f;
  46. for(int mx = -k; mx <= k; mx++) {
  47. for(int my = -k; my <= k; my++) {
  48. for(int mz = -k; mz <= k; mz++) {
  49. int rx = std::abs(x + mx);
  50. int ry = std::abs(y + my);
  51. int rz = std::abs(z + mz);
  52. rx = rx >= 64 ? 127 - rx : rx;
  53. ry = ry >= 64 ? 127 - ry : ry;
  54. rz = rz >= 64 ? 127 - rz : rz;
  55. sum += tData[rx][ry][rz][0];
  56. }
  57. }
  58. }
  59. tData[x][y][z][0] = sum / factor;
  60. }
  61. }
  62. }
  63. }
  64. for(int x = 0; x < textureSize; x++) {
  65. for(int y = 0; y < textureSize; y++) {
  66. for(int z = 0; z < textureSize; z++) {
  67. float v = tData[x][y][z][0];
  68. float vvv = v * v * v;
  69. v = 6.0f * vvv * v * v - 15.0f * vvv * v + 10.0f * vvv;
  70. tData[x][y][z][0] = v;
  71. tData[x][y][z][1] = v;
  72. tData[x][y][z][2] = v;
  73. }
  74. }
  75. }
  76. texture.setData(textureSize, textureSize, textureSize, tData);
  77. }
  78. void Game::render(float lag) {
  79. GL::setViewport(64, 64);
  80. noiceShader.use();
  81. noiceBuffer.bindAndClear();
  82. float step = 1.0f / 31.5f;
  83. noiceShader.setFloat("height", oldHeight * step);
  84. for(int i = 0; i < 64; i++) {
  85. noiceShader.setFloat("layer", i * step - 1.0f);
  86. noiceBuffer.bindLayer(i);
  87. rectangleBuffer.draw(6);
  88. }
  89. GL::setViewport(size.width, size.height);
  90. shader.use();
  91. GL::bindMainFramebuffer();
  92. GL::clear();
  93. shader.setMatrix("proj", frustum.updateProjection().getValues());
  94. Matrix m;
  95. m.translate(Utils::interpolate(oldPosition, position, lag));
  96. m.translateZ(-32.0f);
  97. m.translateY(-32.0f + (oldHeight - height) * lag);
  98. m.translateX(-32.0f);
  99. shader.setMatrix("view", m.getValues());
  100. shader.setFloat("height", oldHeight * step * 0.5f);
  101. if(toggle.isDown()) {
  102. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  103. }
  104. noiceBuffer.bindTextureTo(0);
  105. texture.bindTo(1);
  106. emptyBuffer.drawPoints(64 * 64 * 64);
  107. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  108. }
  109. void Game::tick() {
  110. oldHeight = height;
  111. oldPosition = position;
  112. if(up.isDown()) {
  113. height += 1.0f;
  114. }
  115. if(down.isDown()) {
  116. height -= 1.0f;
  117. }
  118. const float speed = 2.5f;
  119. if(left.isDown()) {
  120. position += Vector3(speed, 0.0f, 0.0f);
  121. }
  122. if(right.isDown()) {
  123. position -= Vector3(speed, 0.0f, 0.0f);
  124. }
  125. if(front.isDown()) {
  126. position += Vector3(0.0f, 0.0f, speed);
  127. }
  128. if(back.isDown()) {
  129. position -= Vector3(0.0f, 0.0f, speed);
  130. }
  131. }
  132. bool Game::isRunning() const {
  133. return true;
  134. }