4 #include <glm/gtx/transform.hpp>
9 const BlockType BlockType::DEFAULT;
11 void BlockType::FillVBO(
13 std::vector<glm::vec3> &vertices,
14 std::vector<glm::vec3> &colors,
15 std::vector<glm::vec3> &normals
17 vertices.emplace_back(pos.x , pos.y , pos.z + 1); // front
18 vertices.emplace_back(pos.x + 1, pos.y , pos.z + 1);
19 vertices.emplace_back(pos.x , pos.y + 1, pos.z + 1);
20 vertices.emplace_back(pos.x + 1, pos.y , pos.z + 1);
21 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z + 1);
22 vertices.emplace_back(pos.x , pos.y + 1, pos.z + 1);
23 vertices.emplace_back(pos.x , pos.y , pos.z ); // back
24 vertices.emplace_back(pos.x , pos.y + 1, pos.z );
25 vertices.emplace_back(pos.x + 1, pos.y , pos.z );
26 vertices.emplace_back(pos.x + 1, pos.y , pos.z );
27 vertices.emplace_back(pos.x , pos.y + 1, pos.z );
28 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z );
29 vertices.emplace_back(pos.x , pos.y + 1, pos.z ); // top
30 vertices.emplace_back(pos.x , pos.y + 1, pos.z + 1);
31 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z );
32 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z );
33 vertices.emplace_back(pos.x , pos.y + 1, pos.z + 1);
34 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z + 1);
35 vertices.emplace_back(pos.x , pos.y , pos.z ); // bottom
36 vertices.emplace_back(pos.x + 1, pos.y , pos.z );
37 vertices.emplace_back(pos.x , pos.y , pos.z + 1);
38 vertices.emplace_back(pos.x + 1, pos.y , pos.z );
39 vertices.emplace_back(pos.x + 1, pos.y , pos.z + 1);
40 vertices.emplace_back(pos.x , pos.y , pos.z + 1);
41 vertices.emplace_back(pos.x , pos.y , pos.z ); // left
42 vertices.emplace_back(pos.x , pos.y , pos.z + 1);
43 vertices.emplace_back(pos.x , pos.y + 1, pos.z );
44 vertices.emplace_back(pos.x , pos.y + 1, pos.z );
45 vertices.emplace_back(pos.x , pos.y , pos.z + 1);
46 vertices.emplace_back(pos.x , pos.y + 1, pos.z + 1);
47 vertices.emplace_back(pos.x + 1, pos.y , pos.z ); // right
48 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z );
49 vertices.emplace_back(pos.x + 1, pos.y , pos.z + 1);
50 vertices.emplace_back(pos.x + 1, pos.y , pos.z + 1);
51 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z );
52 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z + 1);
54 colors.insert(colors.end(), 6 * 6, color);
56 normals.insert(normals.end(), 6, glm::vec3( 0.0f, 0.0f, 1.0f)); // front
57 normals.insert(normals.end(), 6, glm::vec3( 0.0f, 0.0f, -1.0f)); // back
58 normals.insert(normals.end(), 6, glm::vec3( 0.0f, 1.0f, 0.0f)); // top
59 normals.insert(normals.end(), 6, glm::vec3( 0.0f, -1.0f, 0.0f)); // bottom
60 normals.insert(normals.end(), 6, glm::vec3(-1.0f, 0.0f, 0.0f)); // left
61 normals.insert(normals.end(), 6, glm::vec3( 1.0f, 0.0f, 0.0f)); // right
65 BlockTypeRegistry::BlockTypeRegistry() {
66 Add(BlockType::DEFAULT);
69 int BlockTypeRegistry::Add(const BlockType &t) {
70 int id = types.size();
85 Chunk::Chunk(Chunk &&other)
86 : blocks(std::move(other.blocks))
87 , model(std::move(other.model))
88 , transform(other.transform)
89 , dirty(other.dirty) {
93 Chunk &Chunk::operator =(Chunk &&other) {
94 blocks = std::move(other.blocks);
95 model = std::move(other.model);
96 transform = other.transform;
110 bool Chunk::Intersection(
115 glm::vec3 *normal) const {
117 const AABB bb{{0, 0, 0}, {Width(), Height(), Depth()}};
118 if (!blank::Intersection(ray, bb, M)) {
123 if (!blkid && !dist && !normal) {
127 // TODO: should be possible to heavily optimize this
130 float closest_dist = std::numeric_limits<float>::infinity();
131 glm::vec3 closest_normal(0, 1, 0);
132 for (int z = 0; z < Depth(); ++z) {
133 for (int y = 0; y < Height(); ++y) {
134 for (int x = 0; x < Width(); ++x, ++id) {
135 if (!blocks[id].type->visible) {
138 const AABB bb{{x, y, z}, {x+1, y+1, z+1}};
141 if (blank::Intersection(ray, bb, M, &cur_dist, &cur_norm)) {
142 if (cur_dist < closest_dist) {
144 closest_dist = cur_dist;
145 closest_normal = cur_norm;
152 if (closest_id < 0) {
160 *dist = closest_dist;
163 *normal = closest_normal;
168 void Chunk::Position(const glm::vec3 &pos) {
170 transform = glm::translate(pos * Extent());
174 int Chunk::VertexCount() const {
175 // TODO: query blocks as soon as type shapes are implemented
176 return Size() * 6 * 6;
179 void Chunk::Update() {
181 model.Reserve(VertexCount());
183 for (int i = 0; i < Size(); ++i) {
184 if (blocks[i].type->visible) {
185 blocks[i].type->FillModel(ToCoords(i), model);
197 blockType.Add(BlockType(true, glm::vec3(1, 1, 1)));
198 blockType.Add(BlockType(true, glm::vec3(1, 0, 0)));
199 blockType.Add(BlockType(true, glm::vec3(0, 1, 0)));
200 blockType.Add(BlockType(true, glm::vec3(0, 0, 1)));
204 void World::Generate() {
205 for (int z = -1; z < 2; ++z) {
206 for (int y = -1; y < 2; ++y) {
207 for (int x = -1; x < 2; ++x) {
208 Generate(glm::vec3(x, y, z));
214 Chunk &World::Generate(const glm::vec3 &pos) {
215 chunks.emplace_back();
216 Chunk &chunk = chunks.back();
218 chunk.BlockAt(glm::vec3(0, 0, 0)) = Block(blockType[4]);
219 chunk.BlockAt(glm::vec3(0, 0, 1)) = Block(blockType[1]);
220 chunk.BlockAt(glm::vec3(1, 0, 0)) = Block(blockType[2]);
221 chunk.BlockAt(glm::vec3(1, 0, 1)) = Block(blockType[3]);
222 chunk.BlockAt(glm::vec3(2, 0, 0)) = Block(blockType[4]);
223 chunk.BlockAt(glm::vec3(2, 0, 1)) = Block(blockType[1]);
224 chunk.BlockAt(glm::vec3(3, 0, 0)) = Block(blockType[2]);
225 chunk.BlockAt(glm::vec3(3, 0, 1)) = Block(blockType[3]);
226 chunk.BlockAt(glm::vec3(2, 0, 2)) = Block(blockType[4]);
227 chunk.BlockAt(glm::vec3(2, 0, 3)) = Block(blockType[1]);
228 chunk.BlockAt(glm::vec3(3, 0, 2)) = Block(blockType[2]);
229 chunk.BlockAt(glm::vec3(3, 0, 3)) = Block(blockType[3]);
230 chunk.BlockAt(glm::vec3(1, 1, 0)) = Block(blockType[1]);
231 chunk.BlockAt(glm::vec3(1, 1, 1)) = Block(blockType[4]);
232 chunk.BlockAt(glm::vec3(2, 1, 1)) = Block(blockType[3]);
233 chunk.BlockAt(glm::vec3(2, 2, 1)) = Block(blockType[2]);
238 bool World::Intersection(
245 Chunk *closest_chunk = nullptr;
246 int closest_blkid = -1;
247 float closest_dist = std::numeric_limits<float>::infinity();
248 glm::vec3 closest_normal;
250 for (Chunk &cur_chunk : chunks) {
253 glm::vec3 cur_normal;
254 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(), &cur_blkid, &cur_dist, &cur_normal)) {
255 if (cur_dist < closest_dist) {
256 closest_chunk = &cur_chunk;
257 closest_blkid = cur_blkid;
258 closest_dist = cur_dist;
259 closest_normal = cur_normal;
265 *chunk = closest_chunk;
268 *blkid = closest_blkid;
271 *dist = closest_dist;
274 *normal = closest_normal;
276 return closest_chunk;
280 Chunk &World::Next(const Chunk &to, const glm::vec3 &dir) {
281 const glm::vec3 tgt_pos = to.Position() + dir;
282 for (Chunk &chunk : chunks) {
283 if (chunk.Position() == tgt_pos) {
287 return Generate(tgt_pos);