From: Daniel Karbach Date: Mon, 9 Mar 2015 07:22:47 +0000 (+0100) Subject: use player-relative coordinates for rendering X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=b35ce3a6423c554b34b37362c5550bd705e63a1d;p=blank.git use player-relative coordinates for rendering --- diff --git a/src/app.cpp b/src/app.cpp index 202f98c..0d7e7db 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -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; diff --git a/src/chunk.cpp b/src/chunk.cpp index e928a21..a485bfb 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -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()); } diff --git a/src/chunk.hpp b/src/chunk.hpp index 9d2f319..185b3ea 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -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 blocks; Model model; glm::vec3 position; - glm::mat4 transform; bool dirty; }; diff --git a/src/controller.hpp b/src/controller.hpp index 63c102f..aaa51f5 100644 --- a/src/controller.hpp +++ b/src/controller.hpp @@ -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; } diff --git a/src/entity.cpp b/src/entity.cpp index ef9275f..cd2491f 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -1,5 +1,8 @@ #include "entity.hpp" +#include "chunk.hpp" + +#include #include @@ -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 &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 &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); diff --git a/src/entity.hpp b/src/entity.hpp index 59a3388..b739ae4 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -20,23 +20,23 @@ public: void Position(const glm::vec3 &); void Move(const glm::vec3 &delta); + const glm::tvec3 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 &chunk_offset) const; + Ray Aim(const glm::tvec3 &chunk_offset) const; void Update(int dt); private: glm::vec3 velocity; glm::vec3 position; + glm::tvec3 chunk; glm::mat4 rotation; - mutable glm::mat4 transform; - mutable bool dirty; - }; } diff --git a/src/world.cpp b/src/world.cpp index 0121a98..216f3a2 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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(); } }