]> git.localhorst.tv Git - blank.git/commitdiff
world class for multiple chunks
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 3 Mar 2015 17:42:16 +0000 (18:42 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 3 Mar 2015 17:42:16 +0000 (18:42 +0100)
src/app.cpp
src/app.hpp
src/model.cpp
src/model.hpp
src/world.cpp
src/world.hpp

index 9ec9384f9ce749198af886d8fb16cff93c5d5ac3..56b12dda8c58bf5b29a5decefd301f69c0c71c5a 100644 (file)
@@ -19,9 +19,8 @@ Application::Application()
 , move_velocity(0.003f)
 , pitch_sensitivity(-0.0025f)
 , yaw_sensitivity(-0.001f)
-, blockType()
 , cam()
-, chunk()
+, world()
 , outline()
 , outline_visible(false)
 , outline_transform(1.0f)
@@ -58,6 +57,7 @@ Application::Application()
                "layout(location = 2) in vec3 vtx_normal;\n"
                "uniform mat4 M;\n"
                "uniform mat4 V;\n"
+               "uniform mat4 MV;\n"
                "uniform mat4 MVP;\n"
                "uniform vec3 light_position;\n"
                "out vec3 frag_color;\n"
@@ -69,11 +69,11 @@ Application::Application()
                        "vec4 v = vec4(vtx_position, 1);\n"
                        "gl_Position = MVP * v;\n"
                        "vtx_world = (M * v).xyz;\n"
-                       "vec3 vtx_camera = (V * M * v).xyz;\n"
+                       "vec3 vtx_camera = (MV * v).xyz;\n"
                        "eye = vec3(0, 0, 0) - vtx_camera;\n"
                        "vec3 light_camera = (V * v).xyz;\n"
                        "light_direction = light_position + eye;\n"
-                       "normal = (V * M * vec4(vtx_normal, 0)).xyz;\n"
+                       "normal = (MV * vec4(vtx_normal, 0)).xyz;\n"
                        "frag_color = vtx_color;\n"
                "}\n"
        );
@@ -117,28 +117,7 @@ Application::Application()
 
        cam.Position(glm::vec3(0, 4, 4));
 
-       blockType.Add(BlockType(true, glm::vec3(1, 1, 1)));
-       blockType.Add(BlockType(true, glm::vec3(1, 0, 0)));
-       blockType.Add(BlockType(true, glm::vec3(0, 1, 0)));
-       blockType.Add(BlockType(true, glm::vec3(0, 0, 1)));
-
-       chunk.BlockAt(glm::vec3(0, 0, 0)) = Block(blockType[4]);
-       chunk.BlockAt(glm::vec3(0, 0, 1)) = Block(blockType[1]);
-       chunk.BlockAt(glm::vec3(1, 0, 0)) = Block(blockType[2]);
-       chunk.BlockAt(glm::vec3(1, 0, 1)) = Block(blockType[3]);
-       chunk.BlockAt(glm::vec3(2, 0, 0)) = Block(blockType[4]);
-       chunk.BlockAt(glm::vec3(2, 0, 1)) = Block(blockType[1]);
-       chunk.BlockAt(glm::vec3(3, 0, 0)) = Block(blockType[2]);
-       chunk.BlockAt(glm::vec3(3, 0, 1)) = Block(blockType[3]);
-       chunk.BlockAt(glm::vec3(2, 0, 2)) = Block(blockType[4]);
-       chunk.BlockAt(glm::vec3(2, 0, 3)) = Block(blockType[1]);
-       chunk.BlockAt(glm::vec3(3, 0, 2)) = Block(blockType[2]);
-       chunk.BlockAt(glm::vec3(3, 0, 3)) = Block(blockType[3]);
-       chunk.BlockAt(glm::vec3(1, 1, 0)) = Block(blockType[1]);
-       chunk.BlockAt(glm::vec3(1, 1, 1)) = Block(blockType[4]);
-       chunk.BlockAt(glm::vec3(2, 1, 1)) = Block(blockType[3]);
-       chunk.BlockAt(glm::vec3(2, 2, 1)) = Block(blockType[2]);
-       chunk.Invalidate();
+       world.Generate();
 
        outline.vertices = std::vector<glm::vec3>({
                { 0.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f },
@@ -271,35 +250,41 @@ void Application::Update(int dt) {
        cam.Update(dt);
 
        Ray aim = cam.Aim();
+       Chunk *chunk;
        int blkid;
        float dist;
        glm::vec3 normal;
-       if (chunk.Intersection(aim, glm::mat4(1.0f), &blkid, &dist, &normal)) {
+       if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) {
                glm::vec3 pos = Chunk::ToCoords(blkid);
                outline_visible = true;
-               outline_transform = glm::translate(glm::mat4(1.0f), pos);
+               outline_transform = glm::translate(chunk->Transform(), pos);
        } else {
                outline_visible = false;
        }
 
        if (pick) {
-               if (outline_visible) {
-                       place_id = chunk.BlockAt(blkid).type->id;
+               if (chunk) {
+                       place_id = chunk->BlockAt(blkid).type->id;
                }
                pick = false;
        }
        if (remove) {
-               chunk.BlockAt(blkid).type = blockType[remove_id];
-               chunk.Invalidate();
+               if (chunk) {
+                       chunk->BlockAt(blkid).type = world.BlockTypes()[remove_id];
+                       chunk->Invalidate();
+               }
                remove = false;
        }
        if (place) {
-               if (outline_visible) {
-                       int next_blkid = Chunk::ToIndex(Chunk::ToCoords(blkid) + normal);
-                       if (next_blkid >= 0 && next_blkid < Chunk::Size()) {
-                               chunk.BlockAt(next_blkid).type = blockType[place_id];
-                               chunk.Invalidate();
+               if (chunk) {
+                       Chunk *mod_chunk = chunk;
+                       glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal;
+                       if (!Chunk::InBounds(next_pos)) {
+                               mod_chunk = &world.Next(*chunk, normal);
+                               next_pos -= normal * Chunk::Extent();
                        }
+                       mod_chunk->BlockAt(next_pos).type = world.BlockTypes()[place_id];
+                       mod_chunk->Invalidate();
                }
                place = false;
        }
@@ -310,21 +295,25 @@ void Application::Render() {
 
        program.Use();
 
-       glm::mat4 m(1.0f);
-       glm::mat4 mv(cam.View() * m);
-       glm::mat4 mvp(cam.MakeMVP(m));
-       glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]);
        glUniformMatrix4fv(v_handle, 1, GL_FALSE, &cam.View()[0][0]);
-       glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]);
-       glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
        glUniform3f(light_position_handle, light_position.x, light_position.y, light_position.z);
        glUniform3f(light_color_handle, light_color.x, light_color.y, light_color.z);
        glUniform1f(light_power_handle, light_power);
 
-       chunk.Draw();
+       for (Chunk &chunk : world.LoadedChunks()) {
+               glm::mat4 m(chunk.Transform());
+               glm::mat4 mv(cam.View() * m);
+               glm::mat4 mvp(cam.MakeMVP(m));
+               glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]);
+               glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]);
+               glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
+               chunk.Draw();
+       }
+
        if (outline_visible) {
-               mv = cam.View() * outline_transform;
-               mvp = cam.MakeMVP(outline_transform);
+               glm::mat4 m(outline_transform);
+               glm::mat4 mv(cam.View() * outline_transform);
+               glm::mat4 mvp(cam.MakeMVP(outline_transform));
                glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]);
                glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]);
                glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
index 28c7536e440ec781926eb7af2b8a8099e4bfe6ed..1c3c69da31833dd67df43086667369d4922846ed 100644 (file)
@@ -42,10 +42,8 @@ private:
        float pitch_sensitivity;
        float yaw_sensitivity;
 
-       BlockTypeRegistry blockType;
-
        Camera cam;
-       Chunk chunk;
+       World world;
        OutlineModel outline;
 
        bool outline_visible;
index b50ccf0b168888ce0dff21e7d7423cc643c4cc01..f6a4476ecba2a98c6be7be87f0a70861ab7a1810 100644 (file)
@@ -18,6 +18,28 @@ Model::~Model() {
        glDeleteBuffers(ATTRIB_COUNT, handle);
 }
 
+Model::Model(Model &&other)
+: vertices(std::move(other.vertices))
+, colors(std::move(other.colors))
+, normals(std::move(other.normals))
+, dirty(other.dirty) {
+       for (int i = 0; i < ATTRIB_COUNT; ++i) {
+               handle[i] = other.handle[i];
+               other.handle[i] = 0;
+       }
+}
+
+Model &Model::operator =(Model &&other) {
+       vertices = std::move(other.vertices);
+       colors = std::move(other.colors);
+       normals = std::move(other.normals);
+       for (int i = 0; i < ATTRIB_COUNT; ++i) {
+               std::swap(handle[i], other.handle[i]);
+       }
+       dirty = other.dirty;
+       return *this;
+}
+
 
 void Model::Clear() {
        vertices.clear();
index 4fd4515563022215e0ca8e2f847813b4610b501f..4423cd7115338f671847d2461def60ffd11454cd 100644 (file)
@@ -22,6 +22,9 @@ public:
        Model(const Model &) = delete;
        Model &operator =(const Model &) = delete;
 
+       Model(Model &&);
+       Model &operator =(Model &&);
+
        void Invalidate() { dirty = true; }
 
        void Clear();
index e3c10b166d7af213a407ea076658155bfb1b5ce9..c24450cdb154e93d460cb4405146f8d3d8d08096 100644 (file)
@@ -1,6 +1,7 @@
 #include "world.hpp"
 
 #include <limits>
+#include <glm/gtx/transform.hpp>
 
 
 namespace blank {
@@ -76,10 +77,27 @@ int BlockTypeRegistry::Add(const BlockType &t) {
 Chunk::Chunk()
 : blocks(Size())
 , model()
+, transform(1.0f)
 , dirty(false) {
 
 }
 
+Chunk::Chunk(Chunk &&other)
+: blocks(std::move(other.blocks))
+, model(std::move(other.model))
+, transform(other.transform)
+, dirty(other.dirty) {
+
+}
+
+Chunk &Chunk::operator =(Chunk &&other) {
+       blocks = std::move(other.blocks);
+       model = std::move(other.model);
+       transform = other.transform;
+       dirty = other.dirty;
+       return *this;
+}
+
 
 void Chunk::Draw() {
        if (dirty) {
@@ -147,6 +165,11 @@ bool Chunk::Intersection(
        return true;
 }
 
+void Chunk::Position(const glm::vec3 &pos) {
+       position = pos;
+       transform = glm::translate(pos * Extent());
+}
+
 
 int Chunk::VertexCount() const {
        // TODO: query blocks as soon as type shapes are implemented
@@ -167,4 +190,101 @@ void Chunk::Update() {
        dirty = false;
 }
 
+
+World::World()
+: blockType()
+, chunks() {
+       blockType.Add(BlockType(true, glm::vec3(1, 1, 1)));
+       blockType.Add(BlockType(true, glm::vec3(1, 0, 0)));
+       blockType.Add(BlockType(true, glm::vec3(0, 1, 0)));
+       blockType.Add(BlockType(true, glm::vec3(0, 0, 1)));
+}
+
+
+void World::Generate() {
+       for (int z = -1; z < 2; ++z) {
+               for (int y = -1; y < 2; ++y) {
+                       for (int x = -1; x < 2; ++x) {
+                               Generate(glm::vec3(x, y, z));
+                       }
+               }
+       }
+}
+
+Chunk &World::Generate(const glm::vec3 &pos) {
+       chunks.emplace_back();
+       Chunk &chunk = chunks.back();
+       chunk.Position(pos);
+       chunk.BlockAt(glm::vec3(0, 0, 0)) = Block(blockType[4]);
+       chunk.BlockAt(glm::vec3(0, 0, 1)) = Block(blockType[1]);
+       chunk.BlockAt(glm::vec3(1, 0, 0)) = Block(blockType[2]);
+       chunk.BlockAt(glm::vec3(1, 0, 1)) = Block(blockType[3]);
+       chunk.BlockAt(glm::vec3(2, 0, 0)) = Block(blockType[4]);
+       chunk.BlockAt(glm::vec3(2, 0, 1)) = Block(blockType[1]);
+       chunk.BlockAt(glm::vec3(3, 0, 0)) = Block(blockType[2]);
+       chunk.BlockAt(glm::vec3(3, 0, 1)) = Block(blockType[3]);
+       chunk.BlockAt(glm::vec3(2, 0, 2)) = Block(blockType[4]);
+       chunk.BlockAt(glm::vec3(2, 0, 3)) = Block(blockType[1]);
+       chunk.BlockAt(glm::vec3(3, 0, 2)) = Block(blockType[2]);
+       chunk.BlockAt(glm::vec3(3, 0, 3)) = Block(blockType[3]);
+       chunk.BlockAt(glm::vec3(1, 1, 0)) = Block(blockType[1]);
+       chunk.BlockAt(glm::vec3(1, 1, 1)) = Block(blockType[4]);
+       chunk.BlockAt(glm::vec3(2, 1, 1)) = Block(blockType[3]);
+       chunk.BlockAt(glm::vec3(2, 2, 1)) = Block(blockType[2]);
+       chunk.Invalidate();
+       return chunk;
+}
+
+bool World::Intersection(
+               const Ray &ray,
+               const glm::mat4 &M,
+               Chunk **chunk,
+               int *blkid,
+               float *dist,
+               glm::vec3 *normal) {
+       Chunk *closest_chunk = nullptr;
+       int closest_blkid = -1;
+       float closest_dist = std::numeric_limits<float>::infinity();
+       glm::vec3 closest_normal;
+
+       for (Chunk &cur_chunk : chunks) {
+               int cur_blkid;
+               float cur_dist;
+               glm::vec3 cur_normal;
+               if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(), &cur_blkid, &cur_dist, &cur_normal)) {
+                       if (cur_dist < closest_dist) {
+                               closest_chunk = &cur_chunk;
+                               closest_blkid = cur_blkid;
+                               closest_dist = cur_dist;
+                               closest_normal = cur_normal;
+                       }
+               }
+       }
+
+       if (chunk) {
+               *chunk = closest_chunk;
+       }
+       if (blkid) {
+               *blkid = closest_blkid;
+       }
+       if (dist) {
+               *dist = closest_dist;
+       }
+       if (normal) {
+               *normal = closest_normal;
+       }
+       return closest_chunk;
+}
+
+
+Chunk &World::Next(const Chunk &to, const glm::vec3 &dir) {
+       const glm::vec3 tgt_pos = to.Position() + dir;
+       for (Chunk &chunk : chunks) {
+               if (chunk.Position() == tgt_pos) {
+                       return chunk;
+               }
+       }
+       return Generate(tgt_pos);
+}
+
 }
index 67ee355ccbc386d5d8fb08f75ec5c0b7d7fb27df..9f29ab4ee4ff09b579689765b96872a2eb5f8766 100644 (file)
@@ -4,6 +4,7 @@
 #include "model.hpp"
 #include "geometry.hpp"
 
+#include <list>
 #include <vector>
 #include <GL/glew.h>
 #include <glm/glm.hpp>
@@ -75,14 +76,27 @@ class Chunk {
 public:
        Chunk();
 
+       Chunk(Chunk &&);
+       Chunk &operator =(Chunk &&);
+
        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 constexpr int Size() { return Width() * Height() * Depth(); }
 
+       static constexpr bool InBounds(const glm::vec3 &pos) {
+               return
+                       pos.x >= 0 && pos.x < Width() &&
+                       pos.y >= 0 && pos.y < Height() &&
+                       pos.z >= 0 && pos.z < Depth();
+       }
        static constexpr int ToIndex(const glm::vec3 &pos) {
                return pos.x + pos.y * Width() + pos.z * Width() * Height();
        }
+       static constexpr bool InBounds(int idx) {
+               return idx >= 0 && idx < Size();
+       }
        static glm::vec3 ToCoords(int idx) {
                return glm::vec3(
                        idx % Width(),
@@ -105,6 +119,10 @@ public:
                float *dist = nullptr,
                glm::vec3 *normal = nullptr) const;
 
+       void Position(const glm::vec3 &);
+       const glm::vec3 &Position() const { return position; }
+       const glm::mat4 &Transform() const { return transform; }
+
        void Draw();
 
 private:
@@ -114,10 +132,42 @@ private:
 private:
        std::vector<Block> blocks;
        Model model;
+       glm::vec3 position;
+       glm::mat4 transform;
        bool dirty;
 
 };
 
+
+class World {
+
+public:
+       World();
+
+       void Generate();
+
+       bool Intersection(
+               const Ray &,
+               const glm::mat4 &M,
+               Chunk **chunk = nullptr,
+               int *blkid = nullptr,
+               float *dist = nullptr,
+               glm::vec3 *normal = nullptr);
+
+       BlockTypeRegistry &BlockTypes() { return blockType; }
+       std::list<Chunk> &LoadedChunks() { return chunks; }
+
+       Chunk &Next(const Chunk &, const glm::vec3 &dir);
+
+private:
+       Chunk &Generate(const glm::vec3 &);
+
+private:
+       BlockTypeRegistry blockType;
+       std::list<Chunk> chunks;
+
+};
+
 }
 
 #endif