8 const BlockType BlockType::DEFAULT;
10 void BlockType::FillVBO(
12 std::vector<glm::vec3> &vertices,
13 std::vector<glm::vec3> &colors,
14 std::vector<glm::vec3> &normals
16 vertices.emplace_back(pos.x , pos.y , pos.z + 1); // front
17 vertices.emplace_back(pos.x + 1, pos.y , pos.z + 1);
18 vertices.emplace_back(pos.x , pos.y + 1, pos.z + 1);
19 vertices.emplace_back(pos.x + 1, pos.y , pos.z + 1);
20 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z + 1);
21 vertices.emplace_back(pos.x , pos.y + 1, pos.z + 1);
22 vertices.emplace_back(pos.x , pos.y , pos.z ); // back
23 vertices.emplace_back(pos.x , pos.y + 1, pos.z );
24 vertices.emplace_back(pos.x + 1, pos.y , pos.z );
25 vertices.emplace_back(pos.x + 1, pos.y , pos.z );
26 vertices.emplace_back(pos.x , pos.y + 1, pos.z );
27 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z );
28 vertices.emplace_back(pos.x , pos.y + 1, pos.z ); // top
29 vertices.emplace_back(pos.x , pos.y + 1, pos.z + 1);
30 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z );
31 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z );
32 vertices.emplace_back(pos.x , pos.y + 1, pos.z + 1);
33 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z + 1);
34 vertices.emplace_back(pos.x , pos.y , pos.z ); // bottom
35 vertices.emplace_back(pos.x + 1, pos.y , pos.z );
36 vertices.emplace_back(pos.x , pos.y , pos.z + 1);
37 vertices.emplace_back(pos.x + 1, pos.y , pos.z );
38 vertices.emplace_back(pos.x + 1, pos.y , pos.z + 1);
39 vertices.emplace_back(pos.x , pos.y , pos.z + 1);
40 vertices.emplace_back(pos.x , pos.y , pos.z ); // left
41 vertices.emplace_back(pos.x , pos.y , pos.z + 1);
42 vertices.emplace_back(pos.x , pos.y + 1, pos.z );
43 vertices.emplace_back(pos.x , pos.y + 1, pos.z );
44 vertices.emplace_back(pos.x , pos.y , pos.z + 1);
45 vertices.emplace_back(pos.x , pos.y + 1, pos.z + 1);
46 vertices.emplace_back(pos.x + 1, pos.y , pos.z ); // right
47 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z );
48 vertices.emplace_back(pos.x + 1, pos.y , pos.z + 1);
49 vertices.emplace_back(pos.x + 1, pos.y , pos.z + 1);
50 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z );
51 vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z + 1);
53 colors.insert(colors.end(), 6 * 6, color);
55 normals.insert(normals.end(), 6, glm::vec3( 0.0f, 0.0f, 1.0f)); // front
56 normals.insert(normals.end(), 6, glm::vec3( 0.0f, 0.0f, -1.0f)); // back
57 normals.insert(normals.end(), 6, glm::vec3( 0.0f, 1.0f, 0.0f)); // top
58 normals.insert(normals.end(), 6, glm::vec3( 0.0f, -1.0f, 0.0f)); // bottom
59 normals.insert(normals.end(), 6, glm::vec3(-1.0f, 0.0f, 0.0f)); // left
60 normals.insert(normals.end(), 6, glm::vec3( 1.0f, 0.0f, 0.0f)); // right
64 BlockTypeRegistry::BlockTypeRegistry() {
65 Add(BlockType::DEFAULT);
68 int BlockTypeRegistry::Add(const BlockType &t) {
69 int id = types.size();
92 bool Chunk::Intersection(
97 glm::vec3 *normal) const {
99 const AABB bb{{0, 0, 0}, {Width(), Height(), Depth()}};
100 if (!blank::Intersection(ray, bb, M)) {
105 if (!blkid && !dist && !normal) {
109 // TODO: should be possible to heavily optimize this
112 float closest_dist = std::numeric_limits<float>::infinity();
113 glm::vec3 closest_normal(0, 1, 0);
114 for (int z = 0; z < Depth(); ++z) {
115 for (int y = 0; y < Height(); ++y) {
116 for (int x = 0; x < Width(); ++x, ++id) {
117 if (!blocks[id].type->visible) {
120 const AABB bb{{x, y, z}, {x+1, y+1, z+1}};
123 if (blank::Intersection(ray, bb, M, &cur_dist, &cur_norm)) {
124 if (cur_dist < closest_dist) {
126 closest_dist = cur_dist;
127 closest_normal = cur_norm;
134 if (closest_id < 0) {
142 *dist = closest_dist;
145 *normal = closest_normal;
151 int Chunk::VertexCount() const {
152 // TODO: query blocks as soon as type shapes are implemented
153 return Size() * 6 * 6;
156 void Chunk::Update() {
158 model.Reserve(VertexCount());
160 for (int i = 0; i < Size(); ++i) {
161 if (blocks[i].type->visible) {
162 blocks[i].type->FillModel(ToCoords(i), model);