From: Daniel Karbach Date: Tue, 2 Jun 2015 13:30:17 +0000 (+0200) Subject: minor optimizations in chunk X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=e53a0e2e711a7d8bd9b0ddacd1360aa14370643f;p=blank.git minor optimizations in chunk --- diff --git a/src/chunk.cpp b/src/chunk.cpp index a0415fd..3824a47 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -9,6 +9,12 @@ namespace blank { +constexpr int Chunk::width; +constexpr int Chunk::height; +constexpr int Chunk::depth; +constexpr int Chunk::size; + + Chunk::Chunk(const BlockTypeRegistry &types) noexcept : types(&types) , neighbor{0} @@ -180,10 +186,10 @@ void Chunk::SetNeighbor(Chunk &other) noexcept { if (neighbor[Block::FACE_LEFT] != &other) { neighbor[Block::FACE_LEFT] = &other; other.neighbor[Block::FACE_RIGHT] = this; - for (int z = 0; z < Depth(); ++z) { - for (int y = 0; y < Height(); ++y) { + for (int z = 0; z < depth; ++z) { + for (int y = 0; y < height; ++y) { Pos my_pos(0, y, z); - Pos other_pos(Width() - 1, y, z); + Pos other_pos(width - 1, y, z); if (GetLight(my_pos) > 0) { light_queue.emplace(this, my_pos); } @@ -198,9 +204,9 @@ void Chunk::SetNeighbor(Chunk &other) noexcept { if (neighbor[Block::FACE_RIGHT] != &other) { neighbor[Block::FACE_RIGHT] = &other; other.neighbor[Block::FACE_LEFT] = this; - for (int z = 0; z < Depth(); ++z) { - for (int y = 0; y < Height(); ++y) { - Pos my_pos(Width() - 1, y, z); + for (int z = 0; z < depth; ++z) { + for (int y = 0; y < height; ++y) { + Pos my_pos(width - 1, y, z); Pos other_pos(0, y, z); if (GetLight(my_pos) > 0) { light_queue.emplace(this, my_pos); @@ -216,10 +222,10 @@ void Chunk::SetNeighbor(Chunk &other) noexcept { if (neighbor[Block::FACE_DOWN] != &other) { neighbor[Block::FACE_DOWN] = &other; other.neighbor[Block::FACE_UP] = this; - for (int z = 0; z < Depth(); ++z) { - for (int x = 0; x < Width(); ++x) { + for (int z = 0; z < depth; ++z) { + for (int x = 0; x < width; ++x) { Pos my_pos(x, 0, z); - Pos other_pos(x, Height() - 1, z); + Pos other_pos(x, height - 1, z); if (GetLight(my_pos) > 0) { light_queue.emplace(this, my_pos); } @@ -234,9 +240,9 @@ void Chunk::SetNeighbor(Chunk &other) noexcept { if (neighbor[Block::FACE_UP] != &other) { neighbor[Block::FACE_UP] = &other; other.neighbor[Block::FACE_DOWN] = this; - for (int z = 0; z < Depth(); ++z) { - for (int x = 0; x < Width(); ++x) { - Pos my_pos(x, Height() - 1, z); + for (int z = 0; z < depth; ++z) { + for (int x = 0; x < width; ++x) { + Pos my_pos(x, height - 1, z); Pos other_pos(x, 0, z); if (GetLight(my_pos) > 0) { light_queue.emplace(this, my_pos); @@ -252,10 +258,10 @@ void Chunk::SetNeighbor(Chunk &other) noexcept { if (neighbor[Block::FACE_BACK] != &other) { neighbor[Block::FACE_BACK] = &other; other.neighbor[Block::FACE_FRONT] = this; - for (int y = 0; y < Height(); ++y) { - for (int x = 0; x < Width(); ++x) { + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { Pos my_pos(x, y, 0); - Pos other_pos(x, y, Depth() - 1); + Pos other_pos(x, y, depth - 1); if (GetLight(my_pos) > 0) { light_queue.emplace(this, my_pos); } @@ -270,9 +276,9 @@ void Chunk::SetNeighbor(Chunk &other) noexcept { if (neighbor[Block::FACE_FRONT] != &other) { neighbor[Block::FACE_FRONT] = &other; other.neighbor[Block::FACE_BACK] = this; - for (int y = 0; y < Height(); ++y) { - for (int x = 0; x < Width(); ++x) { - Pos my_pos(x, y, Depth() - 1); + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + Pos my_pos(x, y, depth - 1); Pos other_pos(x, y, 0); if (GetLight(my_pos) > 0) { light_queue.emplace(this, my_pos); @@ -321,9 +327,9 @@ int Chunk::GetLight(int index) const noexcept { return light[index]; } -float Chunk::GetVertexLight(int index, const BlockModel::Position &vtx, const Model::Normal &norm) const noexcept { +float Chunk::GetVertexLight(const Pos &pos, const BlockModel::Position &vtx, const Model::Normal &norm) const noexcept { + int index = ToIndex(pos); float light = GetLight(index); - Chunk::Pos pos(ToPos(index)); Block::Face direct_face(Block::NormalFace(norm)); // tis okay @@ -442,20 +448,21 @@ bool Chunk::Intersection( glm::vec3 &normal ) const noexcept { // TODO: should be possible to heavily optimize this - int id = 0; + int idx = 0; blkid = -1; dist = std::numeric_limits::infinity(); - for (int z = 0; z < Depth(); ++z) { - for (int y = 0; y < Height(); ++y) { - for (int x = 0; x < Width(); ++x, ++id) { - if (!Type(blocks[id]).visible) { + for (int z = 0; z < depth; ++z) { + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x, ++idx) { + const BlockType &type = Type(idx); + if (!type.visible) { continue; } float cur_dist; glm::vec3 cur_norm; - if (Type(blocks[id]).shape->Intersects(ray, M * ToTransform(id), cur_dist, cur_norm)) { + if (type.shape->Intersects(ray, M * ToTransform(Pos(x, y, z), idx), cur_dist, cur_norm)) { if (cur_dist < dist) { - blkid = id; + blkid = idx; dist = cur_dist; normal = cur_norm; } @@ -495,22 +502,28 @@ void Chunk::Update() noexcept { buf.Clear(); buf.Reserve(vtx_count, idx_count); + int idx = 0; BlockModel::Index vtx_counter = 0; - for (size_t i = 0; i < Size(); ++i) { - const BlockType &type = Type(blocks[i]); - - if (!type.visible || Obstructed(i).All()) continue; - - type.FillBlockModel(buf, ToTransform(i), vtx_counter); - size_t vtx_begin = vtx_counter; - vtx_counter += type.shape->VertexCount(); - - for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) { - buf.lights.emplace_back(GetVertexLight( - i, - buf.vertices[vtx], - type.shape->VertexNormal(vtx - vtx_begin, blocks[i].Transform()) - )); + for (size_t z = 0; z < depth; ++z) { + for (size_t y = 0; y < height; ++y) { + for (size_t x = 0; x < width; ++x, ++idx) { + const BlockType &type = Type(BlockAt(idx)); + const Pos pos(x, y, z); + + if (!type.visible || Obstructed(pos).All()) continue; + + type.FillBlockModel(buf, ToTransform(pos, idx), vtx_counter); + size_t vtx_begin = vtx_counter; + vtx_counter += type.shape->VertexCount(); + + for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) { + buf.lights.emplace_back(GetVertexLight( + pos, + buf.vertices[vtx], + type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform()) + )); + } + } } } @@ -518,8 +531,7 @@ void Chunk::Update() noexcept { dirty = false; } -Block::FaceSet Chunk::Obstructed(int idx) const noexcept { - Chunk::Pos pos(ToPos(idx)); +Block::FaceSet Chunk::Obstructed(const Pos &pos) const noexcept { Block::FaceSet result; for (int f = 0; f < Block::FACE_COUNT; ++f) { @@ -533,17 +545,17 @@ Block::FaceSet Chunk::Obstructed(int idx) const noexcept { return result; } -glm::mat4 Chunk::ToTransform(int idx) const noexcept { - return glm::translate(ToCoords(idx)) * blocks[idx].Transform(); +glm::mat4 Chunk::ToTransform(const Pos &pos, int idx) const noexcept { + return glm::translate(ToCoords(pos)) * BlockAt(idx).Transform(); } BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept : chunk(c), pos(p) { - while (pos.x >= Chunk::Width()) { + while (pos.x >= Chunk::width) { if (chunk->HasNeighbor(Block::FACE_RIGHT)) { chunk = &chunk->GetNeighbor(Block::FACE_RIGHT); - pos.x -= Chunk::Width(); + pos.x -= Chunk::width; } else { chunk = nullptr; return; @@ -552,16 +564,16 @@ BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept while (pos.x < 0) { if (chunk->HasNeighbor(Block::FACE_LEFT)) { chunk = &chunk->GetNeighbor(Block::FACE_LEFT); - pos.x += Chunk::Width(); + pos.x += Chunk::width; } else { chunk = nullptr; return; } } - while (pos.y >= Chunk::Height()) { + while (pos.y >= Chunk::height) { if (chunk->HasNeighbor(Block::FACE_UP)) { chunk = &chunk->GetNeighbor(Block::FACE_UP); - pos.y -= Chunk::Height(); + pos.y -= Chunk::height; } else { chunk = nullptr; return; @@ -570,16 +582,16 @@ BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept while (pos.y < 0) { if (chunk->HasNeighbor(Block::FACE_DOWN)) { chunk = &chunk->GetNeighbor(Block::FACE_DOWN); - pos.y += Chunk::Height(); + pos.y += Chunk::height; } else { chunk = nullptr; return; } } - while (pos.z >= Chunk::Depth()) { + while (pos.z >= Chunk::depth) { if (chunk->HasNeighbor(Block::FACE_FRONT)) { chunk = &chunk->GetNeighbor(Block::FACE_FRONT); - pos.z -= Chunk::Depth(); + pos.z -= Chunk::depth; } else { chunk = nullptr; return; @@ -588,7 +600,7 @@ BlockLookup::BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept while (pos.z < 0) { if (chunk->HasNeighbor(Block::FACE_BACK)) { chunk = &chunk->GetNeighbor(Block::FACE_BACK); - pos.z += Chunk::Depth(); + pos.z += Chunk::depth; } else { chunk = nullptr; return; diff --git a/src/chunk.hpp b/src/chunk.hpp index 040a817..d86f417 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -25,56 +25,59 @@ public: Chunk(Chunk &&) noexcept; Chunk &operator =(Chunk &&) noexcept; - static constexpr int Width() { return 16; } - static constexpr int Height() { return 16; } - static constexpr int Depth() { return 16; } - static Pos Extent() noexcept { return { Width(), Height(), Depth() }; } - static constexpr int Size() { return Width() * Height() * Depth(); } + 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 AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; } static constexpr bool InBounds(const Block::Pos &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 < width && + pos.y >= 0 && pos.y < height && + pos.z >= 0 && pos.z < depth; } static constexpr bool InBounds(const Pos &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 < width && + pos.y >= 0 && pos.y < height && + pos.z >= 0 && pos.z < depth; } static constexpr int ToIndex(const Pos &pos) noexcept { - return pos.x + pos.y * Width() + pos.z * Width() * Height(); + return pos.x + pos.y * width + pos.z * width * height; } static constexpr bool InBounds(int idx) noexcept { - return idx >= 0 && idx < Size(); + 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())) + 0.5f + (idx % width), + 0.5f + ((idx / width) % height), + 0.5f + (idx / (width * height)) ); } + static Block::Pos ToCoords(const Pos &pos) noexcept { + return Block::Pos(pos) + 0.5f; + } static Pos ToPos(int idx) noexcept { return Pos( - (idx % Width()), - ((idx / Width()) % Height()), - (idx / (Width() * Height())) + (idx % width), + ((idx / width) % height), + (idx / (width * height)) ); } - glm::mat4 ToTransform(int idx) const noexcept; + glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept; 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 < 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 } bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); } @@ -90,7 +93,7 @@ public: void Relink() noexcept; // check which faces of a block at given index are obstructed (and therefore invisible) - Block::FaceSet Obstructed(int idx) const noexcept; + Block::FaceSet Obstructed(const Pos &) const noexcept; void Invalidate() noexcept { dirty = true; } @@ -103,6 +106,7 @@ public: const Block &BlockAt(const Pos &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); } @@ -112,7 +116,7 @@ public: int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); } int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); } - float GetVertexLight(int index, const BlockModel::Position &, const Model::Normal &) const noexcept; + float GetVertexLight(const Pos &, const BlockModel::Position &, const Model::Normal &) const noexcept; bool Intersection( const Ray &ray, diff --git a/src/entity.cpp b/src/entity.cpp index 054af2a..275c846 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -45,28 +45,28 @@ void Entity::Velocity(const glm::vec3 &vel) noexcept { void Entity::Position(const Block::Pos &pos) noexcept { position = pos; - while (position.x >= Chunk::Width()) { - position.x -= Chunk::Width(); + while (position.x >= Chunk::width) { + position.x -= Chunk::width; ++chunk.x; } while (position.x < 0) { - position.x += Chunk::Width(); + position.x += Chunk::width; --chunk.x; } - while (position.y >= Chunk::Height()) { - position.y -= Chunk::Height(); + while (position.y >= Chunk::height) { + position.y -= Chunk::height; ++chunk.y; } while (position.y < 0) { - position.y += Chunk::Height(); + position.y += Chunk::height; --chunk.y; } - while (position.z >= Chunk::Depth()) { - position.z -= Chunk::Depth(); + while (position.z >= Chunk::depth) { + position.z -= Chunk::depth; ++chunk.z; } while (position.z < 0) { - position.z += Chunk::Depth(); + position.z += Chunk::depth; --chunk.z; } } diff --git a/src/generator.cpp b/src/generator.cpp index 3714065..3d61b75 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -20,9 +20,9 @@ Generator::Generator(const Config &config) noexcept void Generator::operator ()(Chunk &chunk) const noexcept { Chunk::Pos pos(chunk.Position()); glm::vec3 coords(pos * Chunk::Extent()); - for (int z = 0; z < Chunk::Depth(); ++z) { - for (int y = 0; y < Chunk::Height(); ++y) { - for (int x = 0; x < Chunk::Width(); ++x) { + for (int z = 0; z < Chunk::depth; ++z) { + for (int y = 0; y < Chunk::height; ++y) { + for (int x = 0; x < Chunk::width; ++x) { Block::Pos block_pos(x, y, z); glm::vec3 gen_pos = (coords + block_pos) * stretch; float val = OctaveNoise(solidNoise, coords + block_pos, 3, 0.5f, stretch, 2.0f); @@ -36,7 +36,7 @@ void Generator::operator ()(Chunk &chunk) const noexcept { } } unsigned int random = 263167 * pos.x + 2097593 * pos.y + 426389 * pos.z; - for (int index = 0; index < Chunk::Size(); ++index) { + for (int index = 0; index < Chunk::size; ++index) { if (chunk.IsSurface(index)) { random = random * 666649 + 7778777; if ((random % 32) == 0) { diff --git a/src/interface.cpp b/src/interface.cpp index f846cc8..a5dcc0e 100644 --- a/src/interface.cpp +++ b/src/interface.cpp @@ -294,7 +294,7 @@ void Interface::CheckAim() { aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline); outline_transform = glm::scale(glm::vec3(1.0002f)); outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords()); - outline_transform *= aim_chunk->ToTransform(aim_block); + outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block); } else { aim_chunk = nullptr; }