]> git.localhorst.tv Git - blank.git/blob - src/chunk.cpp
split world file
[blank.git] / src / chunk.cpp
1 #include "chunk.hpp"
2
3 #include <limits>
4 #include <glm/gtx/transform.hpp>
5
6
7 namespace blank {
8
9 Chunk::Chunk()
10 : blocks(Size())
11 , model()
12 , transform(1.0f)
13 , dirty(false) {
14
15 }
16
17 Chunk::Chunk(Chunk &&other)
18 : blocks(std::move(other.blocks))
19 , model(std::move(other.model))
20 , transform(other.transform)
21 , dirty(other.dirty) {
22
23 }
24
25 Chunk &Chunk::operator =(Chunk &&other) {
26         blocks = std::move(other.blocks);
27         model = std::move(other.model);
28         transform = other.transform;
29         dirty = other.dirty;
30         return *this;
31 }
32
33
34 void Chunk::Draw() {
35         if (dirty) {
36                 Update();
37         }
38         model.Draw();
39 }
40
41
42 bool Chunk::Intersection(
43         const Ray &ray,
44         const glm::mat4 &M,
45         int *blkid,
46         float *dist,
47         glm::vec3 *normal) const {
48         { // rough check
49                 const AABB bb{{0, 0, 0}, {Width(), Height(), Depth()}};
50                 if (!blank::Intersection(ray, bb, M)) {
51                         return false;
52                 }
53         }
54
55         if (!blkid && !dist && !normal) {
56                 return true;
57         }
58
59         // TODO: should be possible to heavily optimize this
60         int id = 0;
61         int closest_id = -1;
62         float closest_dist = std::numeric_limits<float>::infinity();
63         glm::vec3 closest_normal(0, 1, 0);
64         for (int z = 0; z < Depth(); ++z) {
65                 for (int y = 0; y < Height(); ++y) {
66                         for (int x = 0; x < Width(); ++x, ++id) {
67                                 if (!blocks[id].type->visible) {
68                                         continue;
69                                 }
70                                 float cur_dist;
71                                 glm::vec3 cur_norm;
72                                 glm::vec3 pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
73                                 if (blocks[id].type->shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
74                                         if (cur_dist < closest_dist) {
75                                                 closest_id = id;
76                                                 closest_dist = cur_dist;
77                                                 closest_normal = cur_norm;
78                                         }
79                                 }
80                         }
81                 }
82         }
83
84         if (closest_id < 0) {
85                 return false;
86         }
87
88         if (blkid) {
89                 *blkid = closest_id;
90         }
91         if (dist) {
92                 *dist = closest_dist;
93         }
94         if (normal) {
95                 *normal = closest_normal;
96         }
97         return true;
98 }
99
100 void Chunk::Position(const glm::vec3 &pos) {
101         position = pos;
102         transform = glm::translate(pos * Extent());
103 }
104
105
106 int Chunk::VertexCount() const {
107         int count = 0;
108         for (const auto &block : blocks) {
109                 count += block.type->shape->VertexCount();
110         }
111         return count;
112 }
113
114 void Chunk::Update() {
115         model.Clear();
116         model.Reserve(VertexCount());
117
118         for (size_t i = 0; i < Size(); ++i) {
119                 blocks[i].type->FillModel(ToCoords(i), model);
120         }
121
122         model.Invalidate();
123         dirty = false;
124 }
125
126
127 }