chunk.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #pragma once
  2. #include "blocks.h"
  3. constexpr int CHUNK_SIZE = 16;
  4. constexpr int META_CHUNK_SIZE = 32;
  5. constexpr int CHUNK_HEIGHT = 256;
  6. struct ChunkData
  7. {
  8. Block blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_HEIGHT];
  9. unsigned char cachedBiomes[CHUNK_SIZE][CHUNK_SIZE];
  10. int x, z;
  11. Block &unsafeGet(int x, int y, int z)
  12. {
  13. return blocks[x][z][y];
  14. }
  15. unsigned char &unsafeGetCachedBiome(int x, int z)
  16. {
  17. return cachedBiomes[x][z];
  18. }
  19. void clear()
  20. {
  21. memset(blocks, 0, sizeof(blocks));
  22. }
  23. Block *safeGet(glm::ivec3 b)
  24. {
  25. return safeGet(b.x, b.y, b.z);
  26. }
  27. Block *safeGet(int x, int y, int z)
  28. {
  29. if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || z >= CHUNK_SIZE || y >= CHUNK_HEIGHT)
  30. {
  31. return nullptr;
  32. }
  33. else
  34. {
  35. return &unsafeGet(x, y, z);
  36. }
  37. }
  38. void clearLightLevels();
  39. };
  40. struct Chunk
  41. {
  42. ChunkData data;
  43. //std::vector<int> opaqueGeometry;
  44. GLuint opaqueGeometryBuffer = 0;
  45. GLuint vao = 0;
  46. size_t elementCountSize = 0;
  47. GLuint transparentGeometryBuffer = 0;
  48. GLuint transparentVao = 0;
  49. size_t transparentElementCountSize = 0;
  50. char dirty = 1;
  51. char dirtyTransparency = 1;
  52. char neighbourToLeft = 0;
  53. char neighbourToRight = 0;
  54. char neighbourToFront = 0;
  55. char neighbourToBack = 0;
  56. char dontDrawYet = 0; //first time ever don't draw it yet so we have time to process the light
  57. void clear()
  58. {
  59. memset(data.blocks, 0, sizeof(data.blocks));
  60. }
  61. Block &unsafeGet(int x, int y, int z)
  62. {
  63. return data.blocks[x][z][y];
  64. }
  65. Block *safeGet(int x, int y, int z);
  66. //returns true if it changed anything, it will also return true if the newly baked
  67. //geometry is 0 because that means that it took very little time.
  68. bool bake(Chunk *left, Chunk *right, Chunk *front, Chunk *back,
  69. glm::ivec3 playerPosition);
  70. bool shouldBakeOnlyBecauseOfTransparency(Chunk *left, Chunk *right, Chunk *front, Chunk *back);
  71. void createGpuData();
  72. void clearGpuData();
  73. };