]> git.localhorst.tv Git - blank.git/commitdiff
separate file for world generation
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 10 Mar 2015 17:40:06 +0000 (18:40 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 10 Mar 2015 17:40:06 +0000 (18:40 +0100)
src/generator.cpp [new file with mode: 0644]
src/generator.hpp [new file with mode: 0644]
src/world.cpp
src/world.hpp

diff --git a/src/generator.cpp b/src/generator.cpp
new file mode 100644 (file)
index 0000000..10cb668
--- /dev/null
@@ -0,0 +1,38 @@
+#include "generator.hpp"
+
+#include <glm/glm.hpp>
+
+
+namespace blank {
+
+Generator::Generator(unsigned int seed)
+: solidNoise(seed)
+, typeNoise(seed + 1)
+, stretch(64.0f)
+, solid_threshold(0.8f)
+, solids() {
+
+}
+
+
+void Generator::operator ()(Chunk &chunk) const {
+       chunk.Allocate();
+       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) {
+                               Block::Pos block_pos(x, y, z);
+                               glm::vec3 gen_pos = (coords + block_pos) / stretch;
+                               float val = solidNoise(gen_pos);
+                               if (val > solid_threshold) {
+                                       int type_val = int((typeNoise(gen_pos) + 1.0f) * solids.size()) % solids.size();
+                                       chunk.BlockAt(block_pos) = Block(solids[type_val]);
+                               }
+                       }
+               }
+       }
+       chunk.Invalidate();
+}
+
+}
diff --git a/src/generator.hpp b/src/generator.hpp
new file mode 100644 (file)
index 0000000..a40184b
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef BLANK_GENERATOR_HPP_
+#define BLANK_GENERATOR_HPP_
+
+#include "block.hpp"
+#include "chunk.hpp"
+#include "noise.hpp"
+
+#include <vector>
+
+
+namespace blank {
+
+class Generator {
+
+public:
+       explicit Generator(unsigned int seed);
+
+       void operator ()(Chunk &) const;
+
+       void Solids(const std::vector<Block::Type> &s) { solids = s; }
+
+private:
+       SimplexNoise solidNoise;
+       SimplexNoise typeNoise;
+
+       float stretch;
+       float solid_threshold;
+
+       std::vector<Block::Type> solids;
+
+};
+
+}
+
+#endif
index e0411159ffec2e7a5f3cdc08702554d19bd55b67..03e0ebb0b474ef3bb7d47a490e4bcf8cce7ef67d 100644 (file)
@@ -11,8 +11,7 @@ World::World()
 , blockShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }})
 , stairShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}, { 0.0f, 0.0f })
 , slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }})
-, blockNoise(0)
-, colorNoise(1)
+, generate(0)
 , player()
 , player_chunk(0, 0, 0)
 , loaded()
@@ -31,6 +30,8 @@ World::World()
        blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &stairShape }); // blue stair
        blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &slabShape }); // blue slab
 
+       generate.Solids({ 1, 4, 7, 10 });
+
        player.Position({ 4.0f, 4.0f, 4.0f });
 }
 
@@ -59,7 +60,7 @@ void World::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
                                } else if (x == 0 && y == 0 && z == 0) {
                                        loaded.emplace_back(blockType);
                                        loaded.back().Position(pos);
-                                       Generate(loaded.back());
+                                       generate(loaded.back());
                                } else {
                                        to_generate.emplace_back(blockType);
                                        to_generate.back().Position(pos);
@@ -70,34 +71,6 @@ void World::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
        to_generate.sort(ChunkLess);
 }
 
-void World::Generate(Chunk &chunk) {
-       chunk.Allocate();
-       Chunk::Pos pos(chunk.Position());
-       glm::vec3 coords(pos * Chunk::Extent());
-       if (pos.x == 0 && pos.y == 0 && pos.z == 0) {
-               for (size_t i = 1; i < blockType.Size(); ++i) {
-                       chunk.BlockAt(i) = Block(i);
-                       chunk.BlockAt(i + 257) = Block(i);
-                       chunk.BlockAt(i + 514) = Block(i);
-               }
-       } else {
-               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{float(x), float(y), float(z)};
-                                       glm::vec3 gen_pos = (coords + 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;
-                                               chunk.BlockAt(block_pos) = Block(col_val * 3 + 1);
-                                       }
-                               }
-                       }
-               }
-       }
-       chunk.Invalidate();
-}
-
 bool World::Intersection(
                const Ray &ray,
                const glm::mat4 &M,
@@ -175,13 +148,13 @@ Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
 
        chunk = ChunkQueued(tgt_pos);
        if (chunk) {
-               Generate(*chunk);
+               generate(*chunk);
                return *chunk;
        }
 
        loaded.emplace_back(blockType);
        loaded.back().Position(tgt_pos);
-       Generate(loaded.back());
+       generate(loaded.back());
        return loaded.back();
 }
 
@@ -225,7 +198,7 @@ void World::CheckChunkGeneration() {
        }
 
        if (!to_generate.empty()) {
-               Generate(to_generate.front());
+               generate(to_generate.front());
                loaded.splice(loaded.end(), to_generate, to_generate.begin());
        }
 
index 461da5623f5874c7fae25e9923359eaa29e4ead5..4823c47b0c8c0ec804f62f10c5d83f4e5c2dbd04 100644 (file)
@@ -4,7 +4,7 @@
 #include "block.hpp"
 #include "chunk.hpp"
 #include "entity.hpp"
-#include "noise.hpp"
+#include "generator.hpp"
 #include "shader.hpp"
 #include "shape.hpp"
 
@@ -44,17 +44,13 @@ public:
 
        void Render(DirectionalLighting &);
 
-private:
-       void Generate(Chunk &);
-
 private:
        BlockTypeRegistry blockType;
        CuboidShape blockShape;
        StairShape stairShape;
        CuboidShape slabShape;
 
-       SimplexNoise blockNoise;
-       SimplexNoise colorNoise;
+       Generator generate;
 
        Entity player;
        Chunk::Pos player_chunk;