4 #include <glm/gtx/transform.hpp>
9 const BlockType BlockType::DEFAULT;
10 const NullShape BlockType::DEFAULT_SHAPE;
12 void BlockType::FillVBO(
14 std::vector<glm::vec3> &vertices,
15 std::vector<glm::vec3> &colors,
16 std::vector<glm::vec3> &normals
18 shape->Vertices(vertices, pos);
19 colors.insert(colors.end(), shape->VertexCount(), color);
20 shape->Normals(normals);
23 void BlockType::FillOutlineVBO(
24 std::vector<glm::vec3> &vertices,
25 std::vector<glm::vec3> &colors
27 shape->Outline(vertices);
28 colors.insert(colors.end(), shape->OutlineCount(), outline_color);
32 BlockTypeRegistry::BlockTypeRegistry() {
33 Add(BlockType::DEFAULT);
36 int BlockTypeRegistry::Add(const BlockType &t) {
37 int id = types.size();
52 Chunk::Chunk(Chunk &&other)
53 : blocks(std::move(other.blocks))
54 , model(std::move(other.model))
55 , transform(other.transform)
56 , dirty(other.dirty) {
60 Chunk &Chunk::operator =(Chunk &&other) {
61 blocks = std::move(other.blocks);
62 model = std::move(other.model);
63 transform = other.transform;
77 bool Chunk::Intersection(
82 glm::vec3 *normal) const {
84 const AABB bb{{0, 0, 0}, {Width(), Height(), Depth()}};
85 if (!blank::Intersection(ray, bb, M)) {
90 if (!blkid && !dist && !normal) {
94 // TODO: should be possible to heavily optimize this
97 float closest_dist = std::numeric_limits<float>::infinity();
98 glm::vec3 closest_normal(0, 1, 0);
99 for (int z = 0; z < Depth(); ++z) {
100 for (int y = 0; y < Height(); ++y) {
101 for (int x = 0; x < Width(); ++x, ++id) {
102 if (!blocks[id].type->visible) {
107 glm::vec3 pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
108 if (blocks[id].type->shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
109 if (cur_dist < closest_dist) {
111 closest_dist = cur_dist;
112 closest_normal = cur_norm;
119 if (closest_id < 0) {
127 *dist = closest_dist;
130 *normal = closest_normal;
135 void Chunk::Position(const glm::vec3 &pos) {
137 transform = glm::translate(pos * Extent());
141 int Chunk::VertexCount() const {
143 for (const auto &block : blocks) {
144 count += block.type->shape->VertexCount();
149 void Chunk::Update() {
151 model.Reserve(VertexCount());
153 for (size_t i = 0; i < Size(); ++i) {
154 blocks[i].type->FillModel(ToCoords(i), model);
164 , blockShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }})
165 , stairShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}, { 0.0f, 0.0f })
166 , slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }})
171 blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &blockShape }); // white block
172 blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &stairShape }); // white stair
173 blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &slabShape }); // white slab
174 blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &blockShape }); // red block
175 blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &stairShape }); // red stair
176 blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &slabShape }); // red slab
177 blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &blockShape }); // green block
178 blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &stairShape }); // green stair
179 blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &slabShape }); // green slab
180 blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &blockShape }); // blue block
181 blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &stairShape }); // blue stair
182 blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &slabShape }); // blue slab
184 player.Position({ 4.0f, 4.0f, 4.0f });
190 bool ChunkLess(const Chunk &a, const Chunk &b) {
191 return dot(a.Position(), a.Position()) < dot(b.Position(), b.Position());
196 void World::Generate(const glm::tvec3<int> &from, const glm::tvec3<int> &to) {
197 for (int z = from.z; z < to.z; ++z) {
198 for (int y = from.y; y < to.y; ++y) {
199 for (int x = from.x; x < to.x; ++x) {
200 glm::vec3 pos{float(x), float(y), float(z)};
201 if (x == 0 && y == 0 && z == 0) {
202 loaded.emplace_back();
203 loaded.back().Position(pos);
204 Generate(loaded.back());
206 to_generate.emplace_back();
207 to_generate.back().Position(pos);
212 to_generate.sort(ChunkLess);
215 void World::Generate(Chunk &chunk) {
216 glm::vec3 pos(chunk.Position());
217 if (pos.x == 0 && pos.y == 0 && pos.z == 0) {
218 for (size_t i = 1; i < blockType.Size(); ++i) {
219 chunk.BlockAt(i) = Block(blockType[i]);
220 chunk.BlockAt(i + 257) = Block(blockType[i]);
221 chunk.BlockAt(i + 514) = Block(blockType[i]);
224 for (int z = 0; z < Chunk::Depth(); ++z) {
225 for (int y = 0; y < Chunk::Height(); ++y) {
226 for (int x = 0; x < Chunk::Width(); ++x) {
227 glm::vec3 block_pos{float(x), float(y), float(z)};
228 glm::vec3 gen_pos = (pos * Chunk::Extent() + block_pos) / 64.0f;
229 float val = blockNoise(gen_pos);
231 int col_val = int((colorNoise(gen_pos) + 1.0f) * 2.0f) % 4;
232 chunk.BlockAt(block_pos) = Block(blockType[col_val * 3 + 1]);
241 bool World::Intersection(
248 Chunk *closest_chunk = nullptr;
249 int closest_blkid = -1;
250 float closest_dist = std::numeric_limits<float>::infinity();
251 glm::vec3 closest_normal;
253 for (Chunk &cur_chunk : loaded) {
256 glm::vec3 cur_normal;
257 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(), &cur_blkid, &cur_dist, &cur_normal)) {
258 if (cur_dist < closest_dist) {
259 closest_chunk = &cur_chunk;
260 closest_blkid = cur_blkid;
261 closest_dist = cur_dist;
262 closest_normal = cur_normal;
268 *chunk = closest_chunk;
271 *blkid = closest_blkid;
274 *dist = closest_dist;
277 *normal = closest_normal;
279 return closest_chunk;
283 Chunk &World::Next(const Chunk &to, const glm::vec3 &dir) {
284 const glm::vec3 tgt_pos = to.Position() + dir;
285 for (Chunk &chunk : LoadedChunks()) {
286 if (chunk.Position() == tgt_pos) {
290 for (Chunk &chunk : to_generate) {
291 if (chunk.Position() == tgt_pos) {
296 loaded.emplace_back();
297 loaded.back().Position(tgt_pos);
298 Generate(loaded.back());
299 return loaded.back();
303 void World::Update(int dt) {
306 if (!to_generate.empty()) {
307 Generate(to_generate.front());
308 loaded.splice(loaded.end(), to_generate, to_generate.begin());
313 void World::Render(DirectionalLighting &program) {
314 program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
315 program.SetView(glm::inverse(player.Transform()));
317 for (Chunk &chunk : LoadedChunks()) {
318 program.SetM(chunk.Transform());