From: Daniel Karbach Date: Mon, 9 Mar 2015 20:30:32 +0000 (+0100) Subject: some cleanup X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=eca1fdcc8e34a4918418b2de122c6200aeb7ceaf;p=blank.git some cleanup --- diff --git a/src/block.hpp b/src/block.hpp index 1fa9c84..a757740 100644 --- a/src/block.hpp +++ b/src/block.hpp @@ -81,6 +81,8 @@ private: /// single 1x1x1 cube struct Block { + using Pos = glm::vec3; + const BlockType *type; constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT) diff --git a/src/chunk.cpp b/src/chunk.cpp index 483a4e5..c30e2cd 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -71,7 +71,7 @@ bool Chunk::Intersection( } float cur_dist; glm::vec3 cur_norm; - glm::vec3 pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f); + Block::Pos 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)) { if (cur_dist < closest_dist) { closest_id = id; @@ -99,11 +99,11 @@ bool Chunk::Intersection( return true; } -void Chunk::Position(const glm::tvec3 &pos) { +void Chunk::Position(const Pos &pos) { position = pos; } -glm::mat4 Chunk::Transform(const glm::tvec3 &offset) const { +glm::mat4 Chunk::Transform(const Pos &offset) const { return glm::translate((position - offset) * Extent()); } diff --git a/src/chunk.hpp b/src/chunk.hpp index d89fefc..3c30180 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -14,6 +14,9 @@ namespace blank { /// cube of size 16 (256 tiles, 4096 blocks) class Chunk { +public: + using Pos = glm::tvec3; + public: Chunk(); @@ -23,7 +26,7 @@ public: static constexpr int Width() { return 16; } static constexpr int Height() { return 16; } static constexpr int Depth() { return 16; } - static glm::tvec3 Extent() { return { Width(), Height(), Depth() }; } + static Pos Extent() { return { Width(), Height(), Depth() }; } static constexpr int Size() { return Width() * Height() * Depth(); } static AABB Bounds() { return AABB{ { 0, 0, 0 }, Extent() }; } @@ -40,11 +43,11 @@ public: static constexpr bool InBounds(int idx) { return idx >= 0 && idx < Size(); } - static glm::vec3 ToCoords(int idx) { - return glm::vec3( - 0.5f + idx % Width(), - 0.5f + (idx / Width()) % Height(), - 0.5f + idx / (Width() * Height()) + static Block::Pos ToCoords(int idx) { + return Block::Pos( + 0.5f + (idx % Width()), + 0.5f + ((idx / Width()) % Height()), + 0.5f + (idx / (Width() * Height())) ); } @@ -53,8 +56,8 @@ public: Block &BlockAt(int index) { return blocks[index]; } const Block &BlockAt(int index) const { return blocks[index]; } - Block &BlockAt(const glm::vec3 &pos) { return BlockAt(ToIndex(pos)); } - const Block &BlockAt(const glm::vec3 &pos) const { return BlockAt(ToIndex(pos)); } + Block &BlockAt(const Block::Pos &pos) { return BlockAt(ToIndex(pos)); } + const Block &BlockAt(const Block::Pos &pos) const { return BlockAt(ToIndex(pos)); } bool Intersection( const Ray &, @@ -63,9 +66,9 @@ public: float *dist = nullptr, glm::vec3 *normal = nullptr) const; - void Position(const glm::tvec3 &); - const glm::tvec3 &Position() const { return position; } - glm::mat4 Transform(const glm::tvec3 &offset) const; + void Position(const Pos &); + const Pos &Position() const { return position; } + glm::mat4 Transform(const Pos &offset) const; void Draw(); @@ -76,7 +79,7 @@ private: private: std::vector blocks; Model model; - glm::tvec3 position; + Pos position; bool dirty; }; diff --git a/src/controller.cpp b/src/controller.cpp index ef09ace..a1d3a98 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -10,7 +10,7 @@ FPSController::FPSController(Entity &entity) : entity(entity) , pitch(0) , yaw(0) -, move_velocity(0.003f) +, move_velocity(0.005f) , pitch_sensitivity(-0.0025f) , yaw_sensitivity(-0.001f) , front(false) diff --git a/src/entity.cpp b/src/entity.cpp index c572429..d72bd1c 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -20,7 +20,7 @@ void Entity::Velocity(const glm::vec3 &vel) { velocity = vel; } -void Entity::Position(const glm::vec3 &pos) { +void Entity::Position(const Block::Pos &pos) { position = pos; while (position.x >= Chunk::Width()) { position.x -= Chunk::Width(); @@ -56,12 +56,12 @@ void Entity::Rotation(const glm::mat4 &rot) { rotation = rot; } -glm::mat4 Entity::Transform(const glm::tvec3 &chunk_offset) const { +glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const { const glm::vec3 chunk_pos = (chunk - chunk_offset) * Chunk::Extent(); return glm::translate(position + chunk_pos) * rotation; } -Ray Entity::Aim(const glm::tvec3 &chunk_offset) const { +Ray Entity::Aim(const Chunk::Pos &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; diff --git a/src/entity.hpp b/src/entity.hpp index b739ae4..77c7eb5 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -1,6 +1,8 @@ #ifndef BLANK_ENTITY_HPP_ #define BLANK_ENTITY_HPP_ +#include "block.hpp" +#include "chunk.hpp" #include "geometry.hpp" #include @@ -16,24 +18,24 @@ public: const glm::vec3 &Velocity() const { return velocity; } void Velocity(const glm::vec3 &); - const glm::vec3 &Position() const { return position; } - void Position(const glm::vec3 &); + const Block::Pos &Position() const { return position; } + void Position(const Block::Pos &); void Move(const glm::vec3 &delta); - const glm::tvec3 ChunkCoords() const { return chunk; } + const Chunk::Pos ChunkCoords() const { return chunk; } const glm::mat4 &Rotation() const { return rotation; } void Rotation(const glm::mat4 &); - glm::mat4 Transform(const glm::tvec3 &chunk_offset) const; - Ray Aim(const glm::tvec3 &chunk_offset) const; + glm::mat4 Transform(const Chunk::Pos &chunk_offset) const; + Ray Aim(const Chunk::Pos &chunk_offset) const; void Update(int dt); private: glm::vec3 velocity; - glm::vec3 position; - glm::tvec3 chunk; + Block::Pos position; + Chunk::Pos chunk; glm::mat4 rotation; diff --git a/src/model.cpp b/src/model.cpp index f6a4476..08b2924 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -200,6 +200,7 @@ void OutlineModel::Draw() { nullptr // offset ); + glEnable(GL_LINE_SMOOTH); glLineWidth(2.0f); glDrawArrays( diff --git a/src/world.cpp b/src/world.cpp index 4d14dfd..b67bef3 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -49,11 +49,11 @@ bool ChunkLess(const Chunk &a, const Chunk &b) { } -void World::Generate(const glm::tvec3 &from, const glm::tvec3 &to) { +void World::Generate(const Chunk::Pos &from, const Chunk::Pos &to) { for (int z = from.z; z < to.z; ++z) { for (int y = from.y; y < to.y; ++y) { for (int x = from.x; x < to.x; ++x) { - glm::vec3 pos{float(x), float(y), float(z)}; + Block::Pos pos{float(x), float(y), float(z)}; if (ChunkAvailable(pos)) { continue; } else if (x == 0 && y == 0 && z == 0) { @@ -72,7 +72,8 @@ void World::Generate(const glm::tvec3 &from, const glm::tvec3 &to) { void World::Generate(Chunk &chunk) { chunk.Allocate(); - glm::vec3 pos(chunk.Position()); + 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(blockType[i]); @@ -83,8 +84,8 @@ void World::Generate(Chunk &chunk) { for (int z = 0; z < Chunk::Depth(); ++z) { for (int y = 0; y < Chunk::Height(); ++y) { for (int x = 0; x < Chunk::Width(); ++x) { - glm::vec3 block_pos{float(x), float(y), float(z)}; - glm::vec3 gen_pos = (pos * glm::vec3(Chunk::Extent()) + block_pos) / 64.0f; + 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; @@ -139,25 +140,25 @@ bool World::Intersection( } -Chunk *World::ChunkLoaded(const glm::tvec3 &pos) { +Chunk *World::ChunkLoaded(const Chunk::Pos &pos) { for (Chunk &chunk : loaded) { - if (glm::tvec3(chunk.Position()) == pos) { + if (chunk.Position() == pos) { return &chunk; } } return nullptr; } -Chunk *World::ChunkQueued(const glm::tvec3 &pos) { +Chunk *World::ChunkQueued(const Chunk::Pos &pos) { for (Chunk &chunk : to_generate) { - if (glm::tvec3(chunk.Position()) == pos) { + if (chunk.Position() == pos) { return &chunk; } } return nullptr; } -Chunk *World::ChunkAvailable(const glm::tvec3 &pos) { +Chunk *World::ChunkAvailable(const Chunk::Pos &pos) { Chunk *chunk = ChunkLoaded(pos); if (chunk) return chunk; @@ -165,7 +166,7 @@ Chunk *World::ChunkAvailable(const glm::tvec3 &pos) { } Chunk &World::Next(const Chunk &to, const glm::tvec3 &dir) { - const glm::tvec3 tgt_pos = to.Position() + dir; + const Chunk::Pos tgt_pos = to.Position() + dir; Chunk *chunk = ChunkLoaded(tgt_pos); if (chunk) { @@ -219,7 +220,7 @@ void World::CheckChunkGeneration() { } } // add missing new chunks - glm::tvec3 offset(max_dist, max_dist, max_dist); + const Chunk::Pos offset(max_dist, max_dist, max_dist); Generate(player_chunk - offset, player_chunk + offset); } diff --git a/src/world.hpp b/src/world.hpp index 4c7a59f..461da56 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -19,7 +19,7 @@ class World { public: World(); - void Generate(const glm::tvec3 &from, const glm::tvec3 &to); + void Generate(const Chunk::Pos &from, const Chunk::Pos &to); bool Intersection( const Ray &, @@ -34,9 +34,9 @@ public: Entity &Player() { return player; } - Chunk *ChunkLoaded(const glm::tvec3 &); - Chunk *ChunkQueued(const glm::tvec3 &); - Chunk *ChunkAvailable(const glm::tvec3 &); + Chunk *ChunkLoaded(const Chunk::Pos &); + Chunk *ChunkQueued(const Chunk::Pos &); + Chunk *ChunkAvailable(const Chunk::Pos &); Chunk &Next(const Chunk &, const glm::tvec3 &dir); void Update(int dt); @@ -57,7 +57,7 @@ private: SimplexNoise colorNoise; Entity player; - glm::tvec3 player_chunk; + Chunk::Pos player_chunk; std::list loaded; std::list to_generate;