]> git.localhorst.tv Git - blank.git/commitdiff
use player-relative coordinates for rendering
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 9 Mar 2015 07:22:47 +0000 (08:22 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 9 Mar 2015 07:22:47 +0000 (08:22 +0100)
src/app.cpp
src/chunk.cpp
src/chunk.hpp
src/controller.hpp
src/entity.cpp
src/entity.hpp
src/world.cpp

index 202f98c5b4fe46af2ae85a8824d69908fc354a31..0d7e7dbb8fb511c40dbff366394699c99b1753e7 100644 (file)
@@ -119,7 +119,7 @@ void Application::Update(int dt) {
                outline_visible = true;
                outline.Clear();
                chunk->BlockAt(blkid).type->FillOutlineModel(outline);
-               outline_transform = glm::translate(chunk->Transform(), pos);
+               outline_transform = glm::translate(chunk->Transform(world.Player().ChunkCoords()), pos);
                outline_transform = glm::scale(outline_transform, glm::vec3(1.0001f));
        } else {
                outline_visible = false;
index e928a2199b80b2c007ef27d89446e9b41347fb85..a485bfbb7d37c61eab4f80c091f1b78659c29b01 100644 (file)
@@ -9,7 +9,6 @@ namespace blank {
 Chunk::Chunk()
 : blocks(Size())
 , model()
-, transform(1.0f)
 , dirty(false) {
 
 }
@@ -17,7 +16,6 @@ Chunk::Chunk()
 Chunk::Chunk(Chunk &&other)
 : blocks(std::move(other.blocks))
 , model(std::move(other.model))
-, transform(other.transform)
 , dirty(other.dirty) {
 
 }
@@ -25,7 +23,6 @@ Chunk::Chunk(Chunk &&other)
 Chunk &Chunk::operator =(Chunk &&other) {
        blocks = std::move(other.blocks);
        model = std::move(other.model);
-       transform = other.transform;
        dirty = other.dirty;
        return *this;
 }
@@ -99,7 +96,10 @@ bool Chunk::Intersection(
 
 void Chunk::Position(const glm::vec3 &pos) {
        position = pos;
-       transform = glm::translate(pos * Extent());
+}
+
+glm::mat4 Chunk::Transform(const glm::vec3 &offset) const {
+       return glm::translate((position - offset) * Extent());
 }
 
 
index 9d2f319c08a2a7a4913a1c7b63be0032ecb1f997..185b3eabefd5deb1076b77f36dd8a45950257900 100644 (file)
@@ -62,7 +62,7 @@ public:
 
        void Position(const glm::vec3 &);
        const glm::vec3 &Position() const { return position; }
-       const glm::mat4 &Transform() const { return transform; }
+       glm::mat4 Transform(const glm::vec3 &offset) const;
 
        void Draw();
 
@@ -74,7 +74,6 @@ private:
        std::vector<Block> blocks;
        Model model;
        glm::vec3 position;
-       glm::mat4 transform;
        bool dirty;
 
 };
index 63c102f489445a26bf241309ba0153b15de4c7d8..aaa51f5da094069dbdb2551813fd7b9c3a26820d 100644 (file)
@@ -15,7 +15,7 @@ class FPSController {
 public:
        explicit FPSController(Entity &);
 
-       Ray Aim() const { return entity.Aim(); }
+       Ray Aim() const { return entity.Aim(entity.ChunkCoords()); }
 
        // all angles in radians (full circle = 2π)
        float Pitch() const { return pitch; }
index ef9275f41f5f3a05ec975f88d6bdbf9afb7ca46b..cd2491f1bdf441282ea666a6f15218075b9ac6b6 100644 (file)
@@ -1,5 +1,8 @@
 #include "entity.hpp"
 
+#include "chunk.hpp"
+
+#include <cmath>
 #include <glm/gtx/transform.hpp>
 
 
@@ -8,9 +11,7 @@ namespace blank {
 Entity::Entity()
 : velocity()
 , position()
-, rotation(1.0f)
-, transform(1.0f)
-, dirty(false) {
+, rotation(1.0f) {
 
 }
 
@@ -21,28 +22,47 @@ void Entity::Velocity(const glm::vec3 &vel) {
 
 void Entity::Position(const glm::vec3 &pos) {
        position = pos;
-       dirty = true;
+       while (position.x >= Chunk::Width()) {
+               position.x -= Chunk::Width();
+               ++chunk.x;
+       }
+       while (position.x < 0) {
+               position.x += Chunk::Width();
+               --chunk.x;
+       }
+       while (position.y >= Chunk::Height()) {
+               position.y -= Chunk::Height();
+               ++chunk.y;
+       }
+       while (position.y < 0) {
+               position.y += Chunk::Height();
+               --chunk.y;
+       }
+       while (position.z >= Chunk::Depth()) {
+               position.z -= Chunk::Depth();
+               ++chunk.z;
+       }
+       while (position.z < 0) {
+               position.z += Chunk::Depth();
+               --chunk.z;
+       }
 }
 
 void Entity::Move(const glm::vec3 &delta) {
-       position += delta;
-       dirty = true;
+       Position(position + delta);
 }
 
 void Entity::Rotation(const glm::mat4 &rot) {
        rotation = rot;
 }
 
-const glm::mat4 &Entity::Transform() const {
-       if (dirty) {
-               transform = glm::translate(position) * rotation;
-               dirty = false;
-       }
-       return transform;
+glm::mat4 Entity::Transform(const glm::tvec3<int> &chunk_offset) const {
+       const glm::vec3 chunk_pos = glm::vec3(chunk - chunk_offset) * Chunk::Extent();
+       return glm::translate(position + chunk_pos) * rotation;
 }
 
-Ray Entity::Aim() const {
-       Transform();
+Ray Entity::Aim(const glm::tvec3<int> &chunk_offset) const {
+       glm::mat4 transform = Transform(chunk_offset);
        glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
        from /= from.w;
        glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
index 59a3388757e0fc25132259faaddcb5931de17dd7..b739ae439bc753e56e0bafb0cbd4865539141271 100644 (file)
@@ -20,23 +20,23 @@ public:
        void Position(const glm::vec3 &);
        void Move(const glm::vec3 &delta);
 
+       const glm::tvec3<int> ChunkCoords() const { return chunk; }
+
        const glm::mat4 &Rotation() const { return rotation; }
        void Rotation(const glm::mat4 &);
 
-       const glm::mat4 &Transform() const;
-       Ray Aim() const;
+       glm::mat4 Transform(const glm::tvec3<int> &chunk_offset) const;
+       Ray Aim(const glm::tvec3<int> &chunk_offset) const;
 
        void Update(int dt);
 
 private:
        glm::vec3 velocity;
        glm::vec3 position;
+       glm::tvec3<int> chunk;
 
        glm::mat4 rotation;
 
-       mutable glm::mat4 transform;
-       mutable bool dirty;
-
 };
 
 }
index 0121a98a83e36ed13a87b48b93fe06d688665ebb..216f3a2d007916ae902747d8eee5b4b8b2470108 100644 (file)
@@ -102,7 +102,7 @@ bool World::Intersection(
                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_chunk.Intersection(ray, M * cur_chunk.Transform(player.ChunkCoords()), &cur_blkid, &cur_dist, &cur_normal)) {
                        if (cur_dist < closest_dist) {
                                closest_chunk = &cur_chunk;
                                closest_blkid = cur_blkid;
@@ -160,10 +160,10 @@ void World::Update(int dt) {
 
 void World::Render(DirectionalLighting &program) {
        program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
-       program.SetView(glm::inverse(player.Transform()));
+       program.SetView(glm::inverse(player.Transform(player.ChunkCoords())));
 
        for (Chunk &chunk : LoadedChunks()) {
-               program.SetM(chunk.Transform());
+               program.SetM(chunk.Transform(player.ChunkCoords()));
                chunk.Draw();
        }
 }