]> git.localhorst.tv Git - blank.git/blobdiff - src/chunk.cpp
minor optimization of noise generator
[blank.git] / src / chunk.cpp
index 514431cbdd636209368d86127cf812d5facfcad7..fbfc8578ac3a93da8eab354ad1e37fa56ee07c5f 100644 (file)
@@ -6,21 +6,25 @@
 
 namespace blank {
 
-Chunk::Chunk()
-: blocks(Size())
+Chunk::Chunk(const BlockTypeRegistry &types)
+: types(&types)
+, blocks()
 , model()
+, position(0, 0, 0)
 , dirty(false) {
 
 }
 
 Chunk::Chunk(Chunk &&other)
-: blocks(std::move(other.blocks))
+: types(other.types)
+, blocks(std::move(other.blocks))
 , model(std::move(other.model))
 , dirty(other.dirty) {
 
 }
 
 Chunk &Chunk::operator =(Chunk &&other) {
+       types = other.types;
        blocks = std::move(other.blocks);
        model = std::move(other.model);
        dirty = other.dirty;
@@ -28,6 +32,11 @@ Chunk &Chunk::operator =(Chunk &&other) {
 }
 
 
+void Chunk::Allocate() {
+       blocks.resize(Size());
+}
+
+
 void Chunk::Draw() {
        if (dirty) {
                Update();
@@ -60,13 +69,13 @@ bool Chunk::Intersection(
        for (int z = 0; z < Depth(); ++z) {
                for (int y = 0; y < Height(); ++y) {
                        for (int x = 0; x < Width(); ++x, ++id) {
-                               if (!blocks[id].type->visible) {
+                               if (!Type(blocks[id]).visible) {
                                        continue;
                                }
                                float cur_dist;
                                glm::vec3 cur_norm;
-                               glm::vec3 pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
-                               if (blocks[id].type->shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
+                               Block::Pos pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
+                               if (Type(blocks[id]).shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
                                        if (cur_dist < closest_dist) {
                                                closest_id = id;
                                                closest_dist = cur_dist;
@@ -93,29 +102,37 @@ bool Chunk::Intersection(
        return true;
 }
 
-void Chunk::Position(const glm::vec3 &pos) {
+void Chunk::Position(const Pos &pos) {
        position = pos;
 }
 
-glm::mat4 Chunk::Transform(const glm::vec3 &offset) const {
+glm::mat4 Chunk::Transform(const Pos &offset) const {
        return glm::translate((position - offset) * Extent());
 }
 
 
-int Chunk::VertexCount() const {
-       int count = 0;
-       for (const auto &block : blocks) {
-               count += block.type->shape->VertexCount();
+void Chunk::CheckUpdate() {
+       if (dirty) {
+               Update();
        }
-       return count;
+       model.CheckUpdate();
 }
 
 void Chunk::Update() {
+       int vtx_count = 0, idx_count = 0;
+       for (const auto &block : blocks) {
+               const Shape *shape = Type(block).shape;
+               vtx_count += shape->VertexCount();
+               idx_count += shape->VertexIndexCount();
+       }
        model.Clear();
-       model.Reserve(VertexCount());
+       model.Reserve(vtx_count, idx_count);
 
+       Model::Index vtx_counter = 0;
        for (size_t i = 0; i < Size(); ++i) {
-               blocks[i].type->FillModel(ToCoords(i), model);
+               const BlockType &type = Type(blocks[i]);
+               type.FillModel(model, ToCoords(i), vtx_counter);
+               vtx_counter += type.shape->VertexCount();
        }
 
        model.Invalidate();