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}
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);
}
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);
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);
}
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);
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);
}
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);
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
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<float>::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;
}
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())
+ ));
+ }
+ }
}
}
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) {
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;
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;
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;
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;
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)); }
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; }
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); }
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,