glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal;
if (!Chunk::InBounds(next_pos)) {
mod_chunk = &world.Next(*chunk, normal);
- next_pos -= normal * Chunk::Extent();
+ next_pos -= normal * glm::vec3(Chunk::Extent());
}
mod_chunk->BlockAt(next_pos).type = world.BlockTypes()[place_id];
mod_chunk->Invalidate();
namespace blank {
Chunk::Chunk()
-: blocks(Size())
+: blocks()
, model()
+, position(0, 0, 0)
, dirty(false) {
}
}
+void Chunk::Allocate() {
+ blocks.resize(Size());
+}
+
+
void Chunk::Draw() {
if (dirty) {
Update();
return true;
}
-void Chunk::Position(const glm::vec3 &pos) {
+void Chunk::Position(const glm::tvec3<int> &pos) {
position = pos;
}
-glm::mat4 Chunk::Transform(const glm::vec3 &offset) const {
+glm::mat4 Chunk::Transform(const glm::tvec3<int> &offset) const {
return glm::translate((position - offset) * Extent());
}
static constexpr int Width() { return 16; }
static constexpr int Height() { return 16; }
static constexpr int Depth() { return 16; }
- static glm::vec3 Extent() { return glm::vec3(Width(), Height(), Depth()); }
+ static glm::tvec3<int> Extent() { return { Width(), Height(), Depth() }; }
static constexpr int Size() { return Width() * Height() * Depth(); }
- static AABB Bounds() { return AABB{ { 0, 0, 0 }, { Width(), Height(), Depth() } }; }
+ static AABB Bounds() { return AABB{ { 0, 0, 0 }, Extent() }; }
static constexpr bool InBounds(const glm::vec3 &pos) {
return
);
}
+ void Allocate();
void Invalidate() { dirty = true; }
Block &BlockAt(int index) { return blocks[index]; }
float *dist = nullptr,
glm::vec3 *normal = nullptr) const;
- void Position(const glm::vec3 &);
- const glm::vec3 &Position() const { return position; }
- glm::mat4 Transform(const glm::vec3 &offset) const;
+ void Position(const glm::tvec3<int> &);
+ const glm::tvec3<int> &Position() const { return position; }
+ glm::mat4 Transform(const glm::tvec3<int> &offset) const;
void Draw();
private:
std::vector<Block> blocks;
Model model;
- glm::vec3 position;
+ glm::tvec3<int> position;
bool dirty;
};
}
glm::mat4 Entity::Transform(const glm::tvec3<int> &chunk_offset) const {
- const glm::vec3 chunk_pos = glm::vec3(chunk - chunk_offset) * Chunk::Extent();
+ const glm::vec3 chunk_pos = (chunk - chunk_offset) * Chunk::Extent();
return glm::translate(position + chunk_pos) * rotation;
}
, player()
, player_chunk(0, 0, 0)
, loaded()
-, to_generate() {
+, to_generate()
+, to_free() {
blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &blockShape }); // white block
blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &stairShape }); // white stair
blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &slabShape }); // white slab
namespace {
bool ChunkLess(const Chunk &a, const Chunk &b) {
- return dot(a.Position(), a.Position()) < dot(b.Position(), b.Position());
+ return
+ a.Position().x * a.Position().x +
+ a.Position().y * a.Position().y +
+ a.Position().z * a.Position().z <
+ b.Position().x * b.Position().x +
+ b.Position().y * b.Position().y +
+ b.Position().z * b.Position().z;
}
}
}
void World::Generate(Chunk &chunk) {
+ chunk.Allocate();
glm::vec3 pos(chunk.Position());
if (pos.x == 0 && pos.y == 0 && pos.z == 0) {
for (size_t i = 1; i < blockType.Size(); ++i) {
for (int y = 0; y < Chunk::Height(); ++y) {
for (int x = 0; x < Chunk::Width(); ++x) {
glm::vec3 block_pos{float(x), float(y), float(z)};
- glm::vec3 gen_pos = (pos * Chunk::Extent() + block_pos) / 64.0f;
+ glm::vec3 gen_pos = (pos * glm::vec3(Chunk::Extent()) + block_pos) / 64.0f;
float val = blockNoise(gen_pos);
if (val > 0.8f) {
int col_val = int((colorNoise(gen_pos) + 1.0f) * 2.0f) % 4;
return ChunkQueued(pos);
}
-Chunk &World::Next(const Chunk &to, const glm::vec3 &dir) {
- const glm::vec3 tgt_pos = to.Position() + dir;
+Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
+ const glm::tvec3<int> tgt_pos = to.Position() + dir;
Chunk *chunk = ChunkLoaded(tgt_pos);
if (chunk) {
if (std::abs(player_chunk.x - iter->Position().x) > max_dist
|| std::abs(player_chunk.y - iter->Position().y) > max_dist
|| std::abs(player_chunk.z - iter->Position().z) > max_dist) {
- iter = loaded.erase(iter);
+ auto saved = iter;
+ ++iter;
+ to_free.splice(to_free.end(), loaded, saved);
} else {
++iter;
}
Generate(to_generate.front());
loaded.splice(loaded.end(), to_generate, to_generate.begin());
}
+
+ if (!to_free.empty()) {
+ to_free.pop_front();
+ }
}
Chunk *ChunkLoaded(const glm::tvec3<int> &);
Chunk *ChunkQueued(const glm::tvec3<int> &);
Chunk *ChunkAvailable(const glm::tvec3<int> &);
- Chunk &Next(const Chunk &, const glm::vec3 &dir);
+ Chunk &Next(const Chunk &, const glm::tvec3<int> &dir);
void Update(int dt);
void CheckChunkGeneration();
std::list<Chunk> loaded;
std::list<Chunk> to_generate;
+ std::list<Chunk> to_free;
};