Game.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 float tData[16][16][16];
  12. Game::Game(Shader& shader, Buttons& buttons, const Size& size)
  13. : shader(shader), buttons(buttons), size(size),
  14. frustum(60, 0.1f, 1000.0f, size), up(buttons.add(GLFW_KEY_SPACE, "Up")),
  15. down(buttons.add(GLFW_KEY_LEFT_SHIFT, "Down")),
  16. left(buttons.add(GLFW_KEY_A, "left")),
  17. right(buttons.add(GLFW_KEY_D, "right")),
  18. front(buttons.add(GLFW_KEY_W, "front")),
  19. back(buttons.add(GLFW_KEY_S, "back")) {
  20. shader.use();
  21. Random r(0);
  22. for(int x = 0; x < 16; x++) {
  23. for(int y = 0; y < 16; y++) {
  24. for(int z = 0; z < 16; z++) {
  25. float sinX = sinf(M_PI * x / 8.0f);
  26. float cosY = cosf(M_PI * y / 8.0f);
  27. float cosZ = cosf(M_PI * z / 8.0f);
  28. tData[z][x][y] =
  29. (sinX * sinX + cosY * cosY + cosZ * cosZ) * (1.0f / 3.0f);
  30. }
  31. }
  32. }
  33. glGenTextures(1, &texture3d);
  34. glBindTexture(GL_TEXTURE_3D, texture3d);
  35. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  36. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  37. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  38. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
  39. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  40. glTexImage3D(GL_TEXTURE_3D, 0, GL_R32F, 16, 16, 16, 0, GL_RED, GL_FLOAT,
  41. tData);
  42. glActiveTexture(GL_TEXTURE0);
  43. }
  44. void Game::render(float lag) {
  45. Vector3 interPos = Utils::interpolate(oldPosition, position, lag);
  46. shader.setMatrix("proj", frustum.updateProjection().getValues());
  47. Matrix m;
  48. m.translate(interPos);
  49. m.translateZ(-30.0f);
  50. m.translateX(-8.0f);
  51. shader.setMatrix("view", m.getValues());
  52. vertexBuffer.drawPoints(0);
  53. glDrawArrays(GL_POINTS, 0, 16 * 16 * 16);
  54. }
  55. void Game::tick() {
  56. oldPosition = position;
  57. if(up.isDown()) {
  58. position -= Vector3(0.0f, 0.5f, 0.0f);
  59. }
  60. if(down.isDown()) {
  61. position += Vector3(0.0f, 0.5f, 0.0f);
  62. }
  63. if(left.isDown()) {
  64. position += Vector3(0.5f, 0.0f, 0.0f);
  65. }
  66. if(right.isDown()) {
  67. position -= Vector3(0.5f, 0.0f, 0.0f);
  68. }
  69. if(front.isDown()) {
  70. position += Vector3(0.0f, 0.0f, 0.5f);
  71. }
  72. if(back.isDown()) {
  73. position -= Vector3(0.0f, 0.0f, 0.5f);
  74. }
  75. }