/// single 1x1x1 cube
struct Block {
+ using Pos = glm::vec3;
+
const BlockType *type;
constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT)
}
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;
return true;
}
-void Chunk::Position(const glm::tvec3<int> &pos) {
+void Chunk::Position(const Pos &pos) {
position = pos;
}
-glm::mat4 Chunk::Transform(const glm::tvec3<int> &offset) const {
+glm::mat4 Chunk::Transform(const Pos &offset) const {
return glm::translate((position - offset) * Extent());
}
/// cube of size 16 (256 tiles, 4096 blocks)
class Chunk {
+public:
+ using Pos = glm::tvec3<int>;
+
public:
Chunk();
static constexpr int Width() { return 16; }
static constexpr int Height() { return 16; }
static constexpr int Depth() { return 16; }
- static glm::tvec3<int> 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() }; }
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()))
);
}
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 &,
float *dist = nullptr,
glm::vec3 *normal = nullptr) const;
- void Position(const glm::tvec3<int> &);
- const glm::tvec3<int> &Position() const { return position; }
- glm::mat4 Transform(const glm::tvec3<int> &offset) const;
+ void Position(const Pos &);
+ const Pos &Position() const { return position; }
+ glm::mat4 Transform(const Pos &offset) const;
void Draw();
private:
std::vector<Block> blocks;
Model model;
- glm::tvec3<int> position;
+ Pos position;
bool dirty;
};
: entity(entity)
, pitch(0)
, yaw(0)
-, move_velocity(0.003f)
+, move_velocity(0.005f)
, pitch_sensitivity(-0.0025f)
, yaw_sensitivity(-0.001f)
, front(false)
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();
rotation = rot;
}
-glm::mat4 Entity::Transform(const glm::tvec3<int> &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<int> &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;
#ifndef BLANK_ENTITY_HPP_
#define BLANK_ENTITY_HPP_
+#include "block.hpp"
+#include "chunk.hpp"
#include "geometry.hpp"
#include <glm/glm.hpp>
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<int> 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<int> &chunk_offset) const;
- Ray Aim(const glm::tvec3<int> &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<int> chunk;
+ Block::Pos position;
+ Chunk::Pos chunk;
glm::mat4 rotation;
nullptr // offset
);
+ glEnable(GL_LINE_SMOOTH);
glLineWidth(2.0f);
glDrawArrays(
}
-void World::Generate(const glm::tvec3<int> &from, const glm::tvec3<int> &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) {
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]);
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;
}
-Chunk *World::ChunkLoaded(const glm::tvec3<int> &pos) {
+Chunk *World::ChunkLoaded(const Chunk::Pos &pos) {
for (Chunk &chunk : loaded) {
- if (glm::tvec3<int>(chunk.Position()) == pos) {
+ if (chunk.Position() == pos) {
return &chunk;
}
}
return nullptr;
}
-Chunk *World::ChunkQueued(const glm::tvec3<int> &pos) {
+Chunk *World::ChunkQueued(const Chunk::Pos &pos) {
for (Chunk &chunk : to_generate) {
- if (glm::tvec3<int>(chunk.Position()) == pos) {
+ if (chunk.Position() == pos) {
return &chunk;
}
}
return nullptr;
}
-Chunk *World::ChunkAvailable(const glm::tvec3<int> &pos) {
+Chunk *World::ChunkAvailable(const Chunk::Pos &pos) {
Chunk *chunk = ChunkLoaded(pos);
if (chunk) return chunk;
}
Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
- const glm::tvec3<int> tgt_pos = to.Position() + dir;
+ const Chunk::Pos tgt_pos = to.Position() + dir;
Chunk *chunk = ChunkLoaded(tgt_pos);
if (chunk) {
}
}
// add missing new chunks
- glm::tvec3<int> offset(max_dist, max_dist, max_dist);
+ const Chunk::Pos offset(max_dist, max_dist, max_dist);
Generate(player_chunk - offset, player_chunk + offset);
}
public:
World();
- void Generate(const glm::tvec3<int> &from, const glm::tvec3<int> &to);
+ void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
bool Intersection(
const Ray &,
Entity &Player() { return player; }
- Chunk *ChunkLoaded(const glm::tvec3<int> &);
- Chunk *ChunkQueued(const glm::tvec3<int> &);
- Chunk *ChunkAvailable(const glm::tvec3<int> &);
+ Chunk *ChunkLoaded(const Chunk::Pos &);
+ Chunk *ChunkQueued(const Chunk::Pos &);
+ Chunk *ChunkAvailable(const Chunk::Pos &);
Chunk &Next(const Chunk &, const glm::tvec3<int> &dir);
void Update(int dt);
SimplexNoise colorNoise;
Entity player;
- glm::tvec3<int> player_chunk;
+ Chunk::Pos player_chunk;
std::list<Chunk> loaded;
std::list<Chunk> to_generate;