X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld%2FChunk.hpp;fp=src%2Fworld%2FChunk.hpp;h=6e87a0907d15059cea8b15e5255022b333b4c12b;hb=4727825186798902f68df5b99a6a32f0ef618454;hp=b6298b3c7d2239e77c86394b1a7bf7d95bf339dc;hpb=4fbf5a3c1b0e530706023f5fc4be2f68d30ea645;p=blank.git diff --git a/src/world/Chunk.hpp b/src/world/Chunk.hpp index b6298b3..6e87a09 100644 --- a/src/world/Chunk.hpp +++ b/src/world/Chunk.hpp @@ -3,6 +3,7 @@ #include "Block.hpp" #include "BlockTypeRegistry.hpp" +#include "../geometry/Location.hpp" #include "../geometry/primitive.hpp" #include @@ -18,86 +19,81 @@ class WorldCollision; /// cube of size 16 (256 tiles, 4096 blocks) class Chunk { -public: - using Pos = glm::ivec3; - public: explicit Chunk(const BlockTypeRegistry &) noexcept; Chunk(Chunk &&) noexcept; Chunk &operator =(Chunk &&) noexcept; - static constexpr int width = 16; - static constexpr int height = 16; - static constexpr int depth = 16; - static Pos Extent() noexcept { return { width, height, depth }; } - static constexpr int size = width * height * depth; + static constexpr int side = ExactLocation::scale; + static constexpr float fside = ExactLocation::fscale; + static constexpr int size = side * side * side; - static AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; } + static AABB Bounds() noexcept { return AABB{ { 0.0f, 0.0f, 0.0f }, ExactLocation::FExtent() }; } - static constexpr bool InBounds(const Block::Pos &pos) noexcept { + static constexpr bool InBounds(const ExactLocation::Fine &pos) noexcept { return - pos.x >= 0 && pos.x < width && - pos.y >= 0 && pos.y < height && - pos.z >= 0 && pos.z < depth; + pos.x >= 0.0f && pos.x < fside && + pos.y >= 0.0f && pos.y < fside && + pos.z >= 0.0f && pos.z < fside; } - static constexpr bool InBounds(const Pos &pos) noexcept { + static constexpr bool InBounds(const RoughLocation::Fine &pos) noexcept { return - pos.x >= 0 && pos.x < width && - pos.y >= 0 && pos.y < height && - pos.z >= 0 && pos.z < depth; + pos.x >= 0 && pos.x < side && + pos.y >= 0 && pos.y < side && + pos.z >= 0 && pos.z < side; } - static constexpr int ToIndex(const Pos &pos) noexcept { - return pos.x + pos.y * width + pos.z * width * height; + static constexpr int ToIndex(const RoughLocation::Fine &pos) noexcept { + return pos.x + pos.y * side + pos.z * side * side; } static constexpr bool InBounds(int idx) noexcept { return idx >= 0 && idx < size; } - static Block::Pos ToCoords(int idx) noexcept { - return Block::Pos( - 0.5f + (idx % width), - 0.5f + ((idx / width) % height), - 0.5f + (idx / (width * height)) + static ExactLocation::Fine ToCoords(int idx) noexcept { + return ExactLocation::Fine( + 0.5f + (idx % side), + 0.5f + ((idx / side) % side), + 0.5f + (idx / (side * side)) ); } - static Block::Pos ToCoords(const Pos &pos) noexcept { - return Block::Pos(pos) + 0.5f; + static ExactLocation::Fine ToCoords(const RoughLocation::Fine &pos) noexcept { + return ExactLocation::Fine(pos) + 0.5f; } - static Pos ToPos(int idx) noexcept { - return Pos( - (idx % width), - ((idx / width) % height), - (idx / (width * height)) + static RoughLocation::Fine ToPos(int idx) noexcept { + return RoughLocation::Fine( + (idx % side), + ((idx / side) % side), + (idx / (side * side)) ); } - glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept; + glm::mat4 ToTransform(const RoughLocation::Fine &pos, int idx) const noexcept; - Block::Pos ToSceneCoords(const Pos &base, const Block::Pos &pos) const noexcept { - return Block::Pos((position - base) * Extent()) + pos; + ExactLocation::Fine ToSceneCoords(const ExactLocation::Coarse &base, const ExactLocation::Fine &pos) const noexcept { + return ExactLocation::Fine((position - base) * ExactLocation::Extent()) + pos; } - static bool IsBorder(const Pos &pos) noexcept { + static bool IsBorder(const RoughLocation::Fine &pos) noexcept { return pos.x == 0 || - pos.x == width - 1 || + pos.x == side - 1 || pos.y == 0 || - pos.y == height - 1 || + pos.y == side - 1 || pos.z == 0 || - pos.z == depth - 1; + pos.z == side - 1; } static constexpr bool IsBorder(int idx) noexcept { return - idx < width * height || // low Z plane - idx % width == 0 || // low X plane - (idx / (width * height)) == depth - 1 || // high Z plane - idx % width == width - 1 || // high X plane - (idx / width) % height == 0 || // low Y plane - (idx / width) % height == height - 1; // high Y plane + idx < side * side || // low Z plane + idx % side == 0 || // low X plane + (idx / (side * side)) == side - 1 || // high Z plane + idx % side == side - 1 || // high X plane + (idx / side) % side == 0 || // low Y plane + (idx / side) % side == side - 1; // high Y plane } bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); } - bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); } - bool IsSurface(const Pos &pos) const noexcept; + bool IsSurface(const ExactLocation::Fine &pos) const noexcept { return IsSurface(RoughLocation::Fine(pos)); } + bool IsSurface(const RoughLocation::Fine &pos) const noexcept; void SetNeighbor(Block::Face, Chunk &) noexcept; bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; } @@ -106,28 +102,28 @@ public: void Unlink() noexcept; // check which faces of a block at given index are obstructed (and therefore invisible) - Block::FaceSet Obstructed(const Pos &) const noexcept; + Block::FaceSet Obstructed(const RoughLocation::Fine &) const noexcept; void SetBlock(int index, const Block &) noexcept; - void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); } - void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); } + void SetBlock(const ExactLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); } + void SetBlock(const RoughLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); } const Block &BlockAt(int index) const noexcept { return blocks[index]; } - const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); } - const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); } + const Block &BlockAt(const ExactLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); } + const Block &BlockAt(const RoughLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); } const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); } const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); } void SetLight(int index, int level) noexcept; - void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); } - void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); } + void SetLight(const ExactLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); } + void SetLight(const RoughLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); } int GetLight(int index) const noexcept; - int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); } - int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); } + int GetLight(const ExactLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); } + int GetLight(const RoughLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); } - float GetVertexLight(const Pos &, const BlockMesh::Position &, const EntityMesh::Normal &) const noexcept; + float GetVertexLight(const RoughLocation::Fine &, const BlockMesh::Position &, const EntityMesh::Normal &) const noexcept; bool Intersection( const Ray &ray, @@ -148,10 +144,10 @@ public: const glm::mat4 &Mchunk, std::vector &) noexcept; - void Position(const Pos &pos) noexcept { position = pos; } - const Pos &Position() const noexcept { return position; } - glm::mat4 Transform(const Pos &offset) const noexcept { - return glm::translate((position - offset) * Extent()); + void Position(const ExactLocation::Coarse &pos) noexcept { position = pos; } + const ExactLocation::Coarse &Position() const noexcept { return position; } + glm::mat4 Transform(const ExactLocation::Coarse &offset) const noexcept { + return glm::translate((position - offset) * ExactLocation::Extent()); } void *BlockData() noexcept { return &blocks[0]; } @@ -185,7 +181,7 @@ private: bool generated; bool lighted; - Pos position; + ExactLocation::Coarse position; int ref_count; bool dirty_mesh; bool dirty_save;