Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Chunk {
- public:
- constexpr static inline uint8_t CHUNK_WIDTH = 32;
- constexpr static inline uint8_t CHUNK_DEPTH = 32;
- constexpr static inline uint8_t CHUNK_HEIGHT = 32;
- constexpr static inline uint16_t CHUNK_LAYER = CHUNK_WIDTH * CHUNK_DEPTH;
- constexpr static inline uint16_t CHUNK_VOXEL_COUNT = CHUNK_WIDTH * CHUNK_DEPTH * CHUNK_HEIGHT;
- constexpr static inline uint8_t CHUNK_BITS_PER_VOXEL = 24;
- constexpr static inline uint8_t CHUNK_BITS_PER_VERTEX = 64; // rounded up from 42 (24 for colours and 18 for pos)
- std::vector<uint32_t> data{};
- Chunk() : data(CHUNK_VOXEL_COUNT) {}
- bool isVoxelTransparent(const uint16_t& index) {
- return data.at(index) == 0;
- }
- };
- static void addChunkVertex(const auto& chunk, auto& chunkVertices, const auto& voxelX, const auto& voxelY, const auto& voxelZ, const auto& index) {
- chunkVertices.push_back((voxelX) | (voxelY << 6) | (voxelZ << 12) | (chunk.data[index] << 18));
- }
- static void updateIndices(auto& chunkIndices, const auto& index) {
- chunkIndices.push_back(index + 1);
- chunkIndices.push_back(index + 0);
- chunkIndices.push_back(index + 2);
- chunkIndices.push_back(index + 2);
- chunkIndices.push_back(index + 3);
- chunkIndices.push_back(index + 1);
- }
- void render() {
- for (uint16_t i = 0; i < (Chunk::CHUNK_VOXEL_COUNT - 1); ++i) {
- uint8_t voxelX = i % Chunk::CHUNK_WIDTH;
- uint8_t voxelY = (i / Chunk::CHUNK_WIDTH) % Chunk::CHUNK_DEPTH;
- uint8_t voxelZ = i / Chunk::CHUNK_LAYER;
- if (chunkVertices.size() + 64 * 6 > chunkVertices.capacity()) { //
- chunkVertices.reserve(chunkVertices.capacity() * 2);
- }
- if (chunkIndices.size() + 64 * 6 > chunkIndices.capacity()) { //
- chunkIndices.reserve(chunkIndices.capacity() * 2);
- }
- if (voxelX > 0) {
- if ((voxelX == (Chunk::CHUNK_WIDTH - 1) || chunk.isVoxelTransparent(i + 1))) { // +X
- addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY, voxelZ, i);
- addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY, voxelZ + 1, i);
- addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY + 1, voxelZ, i);
- addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY + 1, voxelZ + 1, i);
- updateIndices(chunkIndices, quadIndex);
- quadIndex += 4;
- }
- }
- if (voxelX < (Chunk::CHUNK_WIDTH - 1)) {
- if ((voxelX == 0) || chunk.isVoxelTransparent(i - 1)) { // -X
- addChunkVertex(chunk, chunkVertices, voxelX, voxelY, voxelZ, i);
- addChunkVertex(chunk, chunkVertices, voxelX, voxelY, voxelZ + 1, i);
- addChunkVertex(chunk, chunkVertices, voxelX, voxelY + 1, voxelZ, i);
- addChunkVertex(chunk, chunkVertices, voxelX, voxelY + 1, voxelZ + 1, i);
- updateIndices(chunkIndices, quadIndex);
- quadIndex += 4;
- }
- }
- if (voxelY > 0) {
- if ((voxelY == (Chunk::CHUNK_DEPTH - 1) || chunk.isVoxelTransparent(i + Chunk::CHUNK_WIDTH))) { // +Y
- addChunkVertex(chunk, chunkVertices, voxelX, voxelY + 1, voxelZ, i);
- addChunkVertex(chunk, chunkVertices, voxelX, voxelY + 1, voxelZ + 1, i);
- addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY + 1, voxelZ, i);
- addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY + 1, voxelZ + 1, i);
- updateIndices(chunkIndices, quadIndex);
- quadIndex += 4;
- }
- }
- if (voxelY < (Chunk::CHUNK_DEPTH - 1)) {
- if ((voxelY == 0) || chunk.isVoxelTransparent(i - Chunk::CHUNK_WIDTH)) { // -Y
- addChunkVertex(chunk, chunkVertices, voxelX, voxelY, voxelZ, i);
- addChunkVertex(chunk, chunkVertices, voxelX, voxelY, voxelZ + 1, i);
- addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY, voxelZ, i);
- addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY, voxelZ + 1, i);
- updateIndices(chunkIndices, quadIndex);
- quadIndex += 4;
- }
- }
- if (voxelZ > 0) {
- if (voxelZ == (Chunk::CHUNK_HEIGHT - 1) || chunk.isVoxelTransparent(i + Chunk::CHUNK_LAYER)) { // +Z
- addChunkVertex(chunk, chunkVertices, voxelX, voxelY, voxelZ + 1, i);
- addChunkVertex(chunk, chunkVertices, voxelX, voxelY + 1, voxelZ + 1, i);
- addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY, voxelZ + 1, i);
- addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY + 1, voxelZ + 1, i);
- updateIndices(chunkIndices, quadIndex);
- quadIndex += 4;
- }
- }
- if (voxelZ < (Chunk::CHUNK_HEIGHT - 1)) {
- if ((voxelZ == 0) || chunk.isVoxelTransparent(i - Chunk::CHUNK_LAYER)) { // -Z
- addChunkVertex(chunk, chunkVertices, voxelX, voxelY, voxelZ, i);
- addChunkVertex(chunk, chunkVertices, voxelX, voxelY + 1, voxelZ, i);
- addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY, voxelZ, i);
- addChunkVertex(chunk, chunkVertices, voxelX + 1, voxelY + 1, voxelZ, i);
- updateIndices(chunkIndices, quadIndex);
- quadIndex += 4;
- }
- }
- }
- // ...
- }
Advertisement
Add Comment
Please, Sign In to add comment