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