#pragma once #include "DeerCore/Tools/Memory.h" #include "DeerCore/Tools/TypeDefs.h" #include #include #include #define CHUNK_VOXEL_SIZE 16 #ifdef DEER_RENDER #include "DeerRender/Resource.h" namespace Deer { class VoxelBuilder; class GPUMesh; } // namespace Deer #endif namespace Deer { struct VoxelType { std::string id; }; struct Chunk { Chunk() { Voxel voxel = {.voxelTypeId = 0}; voxels_list.push_back(voxel); voxels_reference_map[voxel] = 0; } struct Voxel { uint32_t voxelTypeId; bool operator==(const Voxel& other) const noexcept { return voxelTypeId == other.voxelTypeId; } }; struct VoxelHasher { std::size_t operator()(const Voxel& v) const noexcept { return std::hash{}(v.voxelTypeId); } }; std::vector voxels_list; std::unordered_map voxels_reference_map; uint16_t reference_matrix[CHUNK_VOXEL_SIZE][CHUNK_VOXEL_SIZE][CHUNK_VOXEL_SIZE]; uint16_t getOrCreateVoxelReference(const Voxel& voxel); Voxel getVoxel(uint16_t voxelReference); }; class VoxelEnvironment { public: VoxelEnvironment(); void modifyVoxel(uint32_t voxelId, int x, int y, int z); uint32_t getVoxel(int x, int y, int z); #ifdef DEER_RENDER public: Resource buildChunk(int x, int y, int z); Scope voxelBuilder; #endif private: struct ChunkID { int x, y, z; bool operator==(const ChunkID& other) const noexcept { return x == other.x && y == other.y && z == other.z; } }; struct ChunkIDHasher { std::size_t operator()(const ChunkID& c) const noexcept { std::size_t h = 0; h ^= std::hash{}(c.x) + 0x9e3779b9 + (h << 6) + (h >> 2); h ^= std::hash{}(c.y) + 0x9e3779b9 + (h << 6) + (h >> 2); h ^= std::hash{}(c.z) + 0x9e3779b9 + (h << 6) + (h >> 2); return h; } }; Chunk* getOrCreateChunk(int x, int y, int z); Chunk* tryGetChunk(int x, int y, int z); std::vector> chunk_list; std::unordered_map chunk_map; }; } // namespace Deer