4 #include <glm/gtx/transform.hpp>
9 const BlockType BlockType::DEFAULT;
10 const CuboidShape BlockType::DEFAULT_SHAPE({{ 0, 0, 0 }, { 1, 1, 1 }});
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 if (blocks[id].type->shape->Intersects(ray, glm::translate(M, glm::vec3(x, y, z)), cur_dist, cur_norm)) {
108 if (cur_dist < closest_dist) {
110 closest_dist = cur_dist;
111 closest_normal = cur_norm;
118 if (closest_id < 0) {
126 *dist = closest_dist;
129 *normal = closest_normal;
134 void Chunk::Position(const glm::vec3 &pos) {
136 transform = glm::translate(pos * Extent());
140 int Chunk::VertexCount() const {
141 // TODO: query blocks as soon as type shapes are implemented
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 if (blocks[i].type->visible) {
155 blocks[i].type->FillModel(ToCoords(i), model);
166 , blockShape({{ 0.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 1.0f }})
167 , stairShape({{ 0.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 1.0f }}, { 0.5f, 0.5f })
168 , slabShape({{ 0.0f, 0.0f, 0.0f }, { 1.0f, 0.5f, 1.0f }})
170 blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &blockShape }); // white block
171 blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &stairShape }); // white stair
172 blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &slabShape }); // white slab
173 blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &blockShape }); // red block
174 blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &stairShape }); // red stair
175 blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &slabShape }); // red slab
176 blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &blockShape }); // green block
177 blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &stairShape }); // green stair
178 blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &slabShape }); // green slab
179 blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &blockShape }); // blue block
180 blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &stairShape }); // blue stair
181 blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &slabShape }); // blue slab
185 void World::Generate() {
186 for (int z = -1; z < 2; ++z) {
187 for (int y = -1; y < 2; ++y) {
188 for (int x = -1; x < 2; ++x) {
189 Generate(glm::vec3(x, y, z));
195 Chunk &World::Generate(const glm::vec3 &pos) {
196 chunks.emplace_back();
197 Chunk &chunk = chunks.back();
199 for (size_t i = 1; i < blockType.Size(); ++i) {
200 chunk.BlockAt(i) = Block(blockType[i]);
201 chunk.BlockAt(i + 257) = Block(blockType[i]);
202 chunk.BlockAt(i + 514) = Block(blockType[i]);
205 chunk.BlockAt(glm::vec3(0, 0, 0)) = Block(blockType[4]);
206 chunk.BlockAt(glm::vec3(0, 0, 1)) = Block(blockType[1]);
207 chunk.BlockAt(glm::vec3(1, 0, 0)) = Block(blockType[5]);
208 chunk.BlockAt(glm::vec3(1, 0, 1)) = Block(blockType[3]);
209 chunk.BlockAt(glm::vec3(2, 0, 0)) = Block(blockType[4]);
210 chunk.BlockAt(glm::vec3(2, 0, 1)) = Block(blockType[1]);
211 chunk.BlockAt(glm::vec3(3, 0, 0)) = Block(blockType[2]);
212 chunk.BlockAt(glm::vec3(3, 0, 1)) = Block(blockType[5]);
213 chunk.BlockAt(glm::vec3(2, 0, 2)) = Block(blockType[4]);
214 chunk.BlockAt(glm::vec3(2, 0, 3)) = Block(blockType[1]);
215 chunk.BlockAt(glm::vec3(3, 0, 2)) = Block(blockType[2]);
216 chunk.BlockAt(glm::vec3(3, 0, 3)) = Block(blockType[5]);
217 chunk.BlockAt(glm::vec3(1, 1, 0)) = Block(blockType[5]);
218 chunk.BlockAt(glm::vec3(1, 1, 1)) = Block(blockType[4]);
219 chunk.BlockAt(glm::vec3(2, 1, 1)) = Block(blockType[3]);
220 chunk.BlockAt(glm::vec3(2, 2, 1)) = Block(blockType[2]);
226 bool World::Intersection(
233 Chunk *closest_chunk = nullptr;
234 int closest_blkid = -1;
235 float closest_dist = std::numeric_limits<float>::infinity();
236 glm::vec3 closest_normal;
238 for (Chunk &cur_chunk : chunks) {
241 glm::vec3 cur_normal;
242 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(), &cur_blkid, &cur_dist, &cur_normal)) {
243 if (cur_dist < closest_dist) {
244 closest_chunk = &cur_chunk;
245 closest_blkid = cur_blkid;
246 closest_dist = cur_dist;
247 closest_normal = cur_normal;
253 *chunk = closest_chunk;
256 *blkid = closest_blkid;
259 *dist = closest_dist;
262 *normal = closest_normal;
264 return closest_chunk;
268 Chunk &World::Next(const Chunk &to, const glm::vec3 &dir) {
269 const glm::vec3 tgt_pos = to.Position() + dir;
270 for (Chunk &chunk : chunks) {
271 if (chunk.Position() == tgt_pos) {
275 return Generate(tgt_pos);