Game.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. static GLuint texture3d;
  11. static List<Vector3, 122880> list;
  12. static float tData[16][16][16];
  13. static void addCube(int x, int y, int z) {
  14. bool b1 = tData[x][y][z] < 0.5f;
  15. bool b2 = tData[x + 1][y][z] < 0.5f;
  16. bool b3 = tData[x + 1][y][z + 1] < 0.5f;
  17. bool b4 = tData[x][y][z + 1] < 0.5f;
  18. bool b5 = tData[x][y + 1][z] < 0.5f;
  19. bool b6 = tData[x + 1][y + 1][z] < 0.5f;
  20. bool b7 = tData[x + 1][y + 1][z + 1] < 0.5f;
  21. bool b8 = tData[x][y + 1][z + 1] < 0.5f;
  22. int index = (b1 << 7) | (b2 << 6) | (b3 << 5) | (b4 << 4) | (b5 << 3) |
  23. (b6 << 2) | (b7 << 1) | b8;
  24. Vector3 v(static_cast<float>(x), static_cast<float>(y),
  25. static_cast<float>(z));
  26. for(int i = 0; i < 12; i++) {
  27. list.add(MarchingCubes::getVector(index, i) + v);
  28. }
  29. }
  30. Game::Game(Shader& shader, Buttons& buttons, const Size& size)
  31. : shader(shader), buttons(buttons), size(size),
  32. frustum(60, 0.1f, 1000.0f, size), up(buttons.add(GLFW_KEY_SPACE, "Up")),
  33. down(buttons.add(GLFW_KEY_LEFT_SHIFT, "Down")),
  34. left(buttons.add(GLFW_KEY_A, "left")),
  35. right(buttons.add(GLFW_KEY_D, "right")),
  36. front(buttons.add(GLFW_KEY_W, "front")),
  37. back(buttons.add(GLFW_KEY_S, "back")) {
  38. shader.use();
  39. // vertexBuffer.setAttributes(Attributes().addFloat(3));
  40. Random r(0);
  41. for(int x = 0; x < 16; x++) {
  42. for(int y = 0; y < 16; y++) {
  43. for(int z = 0; z < 16; z++) {
  44. if(x == 0 || x == 15 || y == 0 || y == 15 || z == 0 ||
  45. z == 15) {
  46. tData[z][x][y] = 0.1f;
  47. } else {
  48. // tData[x][y][z] = 1.0f;
  49. float sinX = sinf(M_PI * x / 8.0f);
  50. float cosY = cosf(M_PI * y / 8.0f);
  51. float cosZ = cosf(M_PI * z / 8.0f);
  52. tData[z][x][y] = (sinX * sinX + cosY * cosY + cosZ * cosZ) *
  53. (1.0f / 3.0f);
  54. }
  55. }
  56. }
  57. }
  58. for(int x = 0; x < 15; x++) {
  59. for(int y = 0; y < 15; y++) {
  60. for(int z = 0; z < 15; z++) {
  61. addCube(x, y, z);
  62. }
  63. }
  64. }
  65. vertexBuffer.setStaticData(sizeof(Vector3) * list.getLength(),
  66. list.begin());
  67. glGenTextures(1, &texture3d);
  68. glBindTexture(GL_TEXTURE_3D, texture3d);
  69. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  70. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  71. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  72. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
  73. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  74. glTexImage3D(GL_TEXTURE_3D, 0, GL_R32F, 16, 16, 16, 0, GL_RED, GL_FLOAT,
  75. tData);
  76. glActiveTexture(GL_TEXTURE0);
  77. }
  78. void Game::render(float lag) {
  79. Vector3 interPos = Utils::interpolate(oldPosition, position, lag);
  80. shader.setMatrix("proj", frustum.updateProjection().getValues());
  81. Matrix m;
  82. m.translate(interPos);
  83. m.translateZ(-30.0f);
  84. m.translateX(-8.0f);
  85. shader.setMatrix("view", m.getValues());
  86. glPointSize(10.0f);
  87. // vertexBuffer.draw(list.getLength());
  88. GL::checkAndPrintError("test3");
  89. vertexBuffer.drawPoints(0);
  90. GL::checkAndPrintError("HERE");
  91. glDrawArrays(GL_POINTS, 0, 16 * 16 * 16);
  92. }
  93. void Game::tick() {
  94. oldPosition = position;
  95. if(up.isDown()) {
  96. position -= Vector3(0.0f, 0.5f, 0.0f);
  97. }
  98. if(down.isDown()) {
  99. position += Vector3(0.0f, 0.5f, 0.0f);
  100. }
  101. if(left.isDown()) {
  102. position += Vector3(0.5f, 0.0f, 0.0f);
  103. }
  104. if(right.isDown()) {
  105. position -= Vector3(0.5f, 0.0f, 0.0f);
  106. }
  107. if(front.isDown()) {
  108. position += Vector3(0.0f, 0.0f, 0.5f);
  109. }
  110. if(back.isDown()) {
  111. position -= Vector3(0.0f, 0.0f, 0.5f);
  112. }
  113. }