Moonyl

code

Jul 3rd, 2025 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.45 KB | None | 0 0
  1. class Chunk {
  2.     public:
  3.         constexpr static inline uint8_t CHUNK_WIDTH = 32;
  4.         constexpr static inline uint8_t CHUNK_DEPTH = 32;
  5.         constexpr static inline uint8_t CHUNK_HEIGHT = 32;
  6.         constexpr static inline uint16_t CHUNK_LAYER = CHUNK_WIDTH * CHUNK_DEPTH;
  7.         constexpr static inline uint16_t CHUNK_VOXEL_COUNT = CHUNK_WIDTH * CHUNK_DEPTH * CHUNK_HEIGHT;
  8.         constexpr static inline uint8_t CHUNK_BITS_PER_VOXEL = 24;
  9.         constexpr static inline uint8_t CHUNK_BITS_PER_VERTEX = 64; // rounded up from 42 (24 for colours and 18 for pos)
  10.  
  11.         std::vector<uint32_t> data{};
  12.  
  13.         Chunk() : data(CHUNK_VOXEL_COUNT) {}
  14.  
  15.         bool isVoxelTransparent(const uint16_t& index) {
  16.             return data.at(index) == 0;
  17.         }
  18. };
  19.  
  20. static void addChunkVertex(const auto& chunk, auto& chunkVertices, const auto& voxelX, const auto& voxelY, const auto& voxelZ, const auto& index) {
  21.     chunkVertices.push_back((voxelX) | (voxelY << 6) | (voxelZ << 12) | (chunk.data[index] << 18));
  22. }
  23.  
  24. static void updateIndices(auto& chunkIndices, const auto& index) {
  25.     chunkIndices.push_back(index + 1);
  26.     chunkIndices.push_back(index + 0);
  27.     chunkIndices.push_back(index + 2);
  28.     chunkIndices.push_back(index + 2);
  29.     chunkIndices.push_back(index + 3);
  30.     chunkIndices.push_back(index + 1);
  31. }
  32.  
  33. void render() {
  34.     for (uint16_t i = 0; i < (Chunk::CHUNK_VOXEL_COUNT - 1); ++i) {
  35.         uint8_t voxelX = i % Chunk::CHUNK_WIDTH;
  36.         uint8_t voxelY = (i / Chunk::CHUNK_WIDTH) % Chunk::CHUNK_DEPTH;
  37.         uint8_t voxelZ = i / Chunk::CHUNK_LAYER;
  38.  
  39.         if (chunkVertices.size() + 64 * 6 > chunkVertices.capacity()) { //
  40.             chunkVertices.reserve(chunkVertices.capacity() * 2);
  41.         }
  42.        
  43.         if (chunkIndices.size() + 64 * 6 > chunkIndices.capacity()) { //
  44.             chunkIndices.reserve(chunkIndices.capacity() * 2);
  45.         }
  46.  
  47.         if (voxelX > 0) {
  48.            if ((voxelX == (Chunk::CHUNK_WIDTH - 1) || chunk.isVoxelTransparent(i + 1))) { // +X
  49.                 addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY, voxelZ, i);
  50.                 addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY, voxelZ + 1, i);
  51.                 addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY + 1, voxelZ, i);
  52.                 addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY + 1, voxelZ + 1, i);
  53.                 updateIndices(chunkIndices, quadIndex);
  54.  
  55.                 quadIndex += 4;
  56.             }
  57.         }
  58.         if (voxelX < (Chunk::CHUNK_WIDTH - 1)) {
  59.            if ((voxelX == 0) || chunk.isVoxelTransparent(i - 1)) { // -X
  60.                 addChunkVertex(chunk, chunkVertices, voxelX, voxelY, voxelZ, i);
  61.                 addChunkVertex(chunk, chunkVertices, voxelX, voxelY, voxelZ + 1, i);
  62.                 addChunkVertex(chunk, chunkVertices, voxelX, voxelY + 1, voxelZ, i);
  63.                 addChunkVertex(chunk, chunkVertices, voxelX, voxelY + 1, voxelZ + 1, i);
  64.                 updateIndices(chunkIndices, quadIndex);
  65.  
  66.                 quadIndex += 4;
  67.             }
  68.         }
  69.         if (voxelY > 0) {
  70.            if ((voxelY == (Chunk::CHUNK_DEPTH - 1) || chunk.isVoxelTransparent(i + Chunk::CHUNK_WIDTH))) { // +Y
  71.                 addChunkVertex(chunk, chunkVertices, voxelX, voxelY + 1, voxelZ, i);
  72.                 addChunkVertex(chunk, chunkVertices, voxelX, voxelY + 1, voxelZ + 1, i);
  73.                 addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY + 1, voxelZ, i);
  74.                 addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY + 1, voxelZ + 1, i);
  75.                 updateIndices(chunkIndices, quadIndex);
  76.  
  77.                 quadIndex += 4;
  78.             }
  79.         }
  80.         if (voxelY < (Chunk::CHUNK_DEPTH - 1)) {
  81.            if ((voxelY == 0) || chunk.isVoxelTransparent(i - Chunk::CHUNK_WIDTH)) { // -Y
  82.                 addChunkVertex(chunk, chunkVertices, voxelX, voxelY, voxelZ, i);
  83.                 addChunkVertex(chunk, chunkVertices, voxelX, voxelY, voxelZ + 1, i);
  84.                 addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY, voxelZ, i);
  85.                 addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY, voxelZ + 1, i);
  86.                 updateIndices(chunkIndices, quadIndex);
  87.  
  88.                 quadIndex += 4;
  89.             }
  90.         }
  91.         if (voxelZ > 0) {
  92.            if (voxelZ == (Chunk::CHUNK_HEIGHT - 1) || chunk.isVoxelTransparent(i + Chunk::CHUNK_LAYER)) { // +Z
  93.                 addChunkVertex(chunk, chunkVertices, voxelX, voxelY, voxelZ + 1, i);
  94.                 addChunkVertex(chunk, chunkVertices, voxelX, voxelY + 1, voxelZ + 1, i);
  95.                 addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY, voxelZ + 1, i);
  96.                 addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY + 1, voxelZ + 1, i);
  97.                 updateIndices(chunkIndices, quadIndex);
  98.  
  99.                 quadIndex += 4;
  100.             }
  101.         }
  102.         if (voxelZ < (Chunk::CHUNK_HEIGHT - 1)) {
  103.            if ((voxelZ == 0) || chunk.isVoxelTransparent(i - Chunk::CHUNK_LAYER)) { // -Z
  104.                 addChunkVertex(chunk, chunkVertices, voxelX, voxelY, voxelZ, i);
  105.                 addChunkVertex(chunk, chunkVertices, voxelX, voxelY + 1, voxelZ, i);
  106.                 addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY, voxelZ, i);
  107.                 addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY + 1, voxelZ, i);
  108.                 updateIndices(chunkIndices, quadIndex);
  109.  
  110.                 quadIndex += 4;
  111.             }
  112.         }
  113.     }
  114.     // ...
  115. }
Advertisement
Add Comment
Please, Sign In to add comment