]> git.localhorst.tv Git - blank.git/blob - src/chunk.cpp
use player-relative coordinates for rendering
[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                 const AABB bb{{0, 0, 0}, {Width(), Height(), Depth()}};
47                 if (!blank::Intersection(ray, bb, M)) {
48                         return false;
49                 }
50         }
51
52         if (!blkid && !dist && !normal) {
53                 return true;
54         }
55
56         // TODO: should be possible to heavily optimize this
57         int id = 0;
58         int closest_id = -1;
59         float closest_dist = std::numeric_limits<float>::infinity();
60         glm::vec3 closest_normal(0, 1, 0);
61         for (int z = 0; z < Depth(); ++z) {
62                 for (int y = 0; y < Height(); ++y) {
63                         for (int x = 0; x < Width(); ++x, ++id) {
64                                 if (!blocks[id].type->visible) {
65                                         continue;
66                                 }
67                                 float cur_dist;
68                                 glm::vec3 cur_norm;
69                                 glm::vec3 pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
70                                 if (blocks[id].type->shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
71                                         if (cur_dist < closest_dist) {
72                                                 closest_id = id;
73                                                 closest_dist = cur_dist;
74                                                 closest_normal = cur_norm;
75                                         }
76                                 }
77                         }
78                 }
79         }
80
81         if (closest_id < 0) {
82                 return false;
83         }
84
85         if (blkid) {
86                 *blkid = closest_id;
87         }
88         if (dist) {
89                 *dist = closest_dist;
90         }
91         if (normal) {
92                 *normal = closest_normal;
93         }
94         return true;
95 }
96
97 void Chunk::Position(const glm::vec3 &pos) {
98         position = pos;
99 }
100
101 glm::mat4 Chunk::Transform(const glm::vec3 &offset) const {
102         return glm::translate((position - offset) * 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 }