]> git.localhorst.tv Git - blank.git/blob - src/world.cpp
add null shape for void blocks
[blank.git] / src / world.cpp
1 #include "world.hpp"
2
3 #include <limits>
4 #include <glm/gtx/transform.hpp>
5
6
7 namespace blank {
8
9 const BlockType BlockType::DEFAULT;
10 const NullShape BlockType::DEFAULT_SHAPE;
11
12 void BlockType::FillVBO(
13         const glm::vec3 &pos,
14         std::vector<glm::vec3> &vertices,
15         std::vector<glm::vec3> &colors,
16         std::vector<glm::vec3> &normals
17 ) const {
18         shape->Vertices(vertices, pos);
19         colors.insert(colors.end(), shape->VertexCount(), color);
20         shape->Normals(normals);
21 }
22
23 void BlockType::FillOutlineVBO(
24         std::vector<glm::vec3> &vertices,
25         std::vector<glm::vec3> &colors
26 ) const {
27         shape->Outline(vertices);
28         colors.insert(colors.end(), shape->OutlineCount(), outline_color);
29 }
30
31
32 BlockTypeRegistry::BlockTypeRegistry() {
33         Add(BlockType::DEFAULT);
34 }
35
36 int BlockTypeRegistry::Add(const BlockType &t) {
37         int id = types.size();
38         types.push_back(t);
39         types.back().id = id;
40         return id;
41 }
42
43
44 Chunk::Chunk()
45 : blocks(Size())
46 , model()
47 , transform(1.0f)
48 , dirty(false) {
49
50 }
51
52 Chunk::Chunk(Chunk &&other)
53 : blocks(std::move(other.blocks))
54 , model(std::move(other.model))
55 , transform(other.transform)
56 , dirty(other.dirty) {
57
58 }
59
60 Chunk &Chunk::operator =(Chunk &&other) {
61         blocks = std::move(other.blocks);
62         model = std::move(other.model);
63         transform = other.transform;
64         dirty = other.dirty;
65         return *this;
66 }
67
68
69 void Chunk::Draw() {
70         if (dirty) {
71                 Update();
72         }
73         model.Draw();
74 }
75
76
77 bool Chunk::Intersection(
78         const Ray &ray,
79         const glm::mat4 &M,
80         int *blkid,
81         float *dist,
82         glm::vec3 *normal) const {
83         { // rough check
84                 const AABB bb{{0, 0, 0}, {Width(), Height(), Depth()}};
85                 if (!blank::Intersection(ray, bb, M)) {
86                         return false;
87                 }
88         }
89
90         if (!blkid && !dist && !normal) {
91                 return true;
92         }
93
94         // TODO: should be possible to heavily optimize this
95         int id = 0;
96         int closest_id = -1;
97         float closest_dist = std::numeric_limits<float>::infinity();
98         glm::vec3 closest_normal(0, 1, 0);
99         for (int z = 0; z < Depth(); ++z) {
100                 for (int y = 0; y < Height(); ++y) {
101                         for (int x = 0; x < Width(); ++x, ++id) {
102                                 if (!blocks[id].type->visible) {
103                                         continue;
104                                 }
105                                 float cur_dist;
106                                 glm::vec3 cur_norm;
107                                 if (blocks[id].type->shape->Intersects(ray, glm::translate(M, glm::vec3(x, y, z)), cur_dist, cur_norm)) {
108                                         if (cur_dist < closest_dist) {
109                                                 closest_id = id;
110                                                 closest_dist = cur_dist;
111                                                 closest_normal = cur_norm;
112                                         }
113                                 }
114                         }
115                 }
116         }
117
118         if (closest_id < 0) {
119                 return false;
120         }
121
122         if (blkid) {
123                 *blkid = closest_id;
124         }
125         if (dist) {
126                 *dist = closest_dist;
127         }
128         if (normal) {
129                 *normal = closest_normal;
130         }
131         return true;
132 }
133
134 void Chunk::Position(const glm::vec3 &pos) {
135         position = pos;
136         transform = glm::translate(pos * Extent());
137 }
138
139
140 int Chunk::VertexCount() const {
141         int count = 0;
142         for (const auto &block : blocks) {
143                 count += block.type->shape->VertexCount();
144         }
145         return count;
146 }
147
148 void Chunk::Update() {
149         model.Clear();
150         model.Reserve(VertexCount());
151
152         for (size_t i = 0; i < Size(); ++i) {
153                 blocks[i].type->FillModel(ToCoords(i), model);
154         }
155
156         model.Invalidate();
157         dirty = false;
158 }
159
160
161 World::World()
162 : blockType()
163 , blockShape({{ 0.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 1.0f }})
164 , stairShape({{ 0.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 1.0f }}, { 0.5f, 0.5f })
165 , slabShape({{ 0.0f, 0.0f, 0.0f }, { 1.0f, 0.5f, 1.0f }})
166 , chunks() {
167         blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &blockShape }); // white block
168         blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &stairShape }); // white stair
169         blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &slabShape }); // white slab
170         blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &blockShape }); // red block
171         blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &stairShape }); // red stair
172         blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &slabShape }); // red slab
173         blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &blockShape }); // green block
174         blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &stairShape }); // green stair
175         blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &slabShape }); // green slab
176         blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &blockShape }); // blue block
177         blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &stairShape }); // blue stair
178         blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &slabShape }); // blue slab
179 }
180
181
182 void World::Generate() {
183         for (int z = -1; z < 2; ++z) {
184                 for (int y = -1; y < 2; ++y) {
185                         for (int x = -1; x < 2; ++x) {
186                                 Generate(glm::vec3(x, y, z));
187                         }
188                 }
189         }
190 }
191
192 Chunk &World::Generate(const glm::vec3 &pos) {
193         chunks.emplace_back();
194         Chunk &chunk = chunks.back();
195         chunk.Position(pos);
196         for (size_t i = 1; i < blockType.Size(); ++i) {
197                 chunk.BlockAt(i) = Block(blockType[i]);
198                 chunk.BlockAt(i + 257) = Block(blockType[i]);
199                 chunk.BlockAt(i + 514) = Block(blockType[i]);
200         }
201         if (false) {
202                 chunk.BlockAt(glm::vec3(0, 0, 0)) = Block(blockType[4]);
203                 chunk.BlockAt(glm::vec3(0, 0, 1)) = Block(blockType[1]);
204                 chunk.BlockAt(glm::vec3(1, 0, 0)) = Block(blockType[5]);
205                 chunk.BlockAt(glm::vec3(1, 0, 1)) = Block(blockType[3]);
206                 chunk.BlockAt(glm::vec3(2, 0, 0)) = Block(blockType[4]);
207                 chunk.BlockAt(glm::vec3(2, 0, 1)) = Block(blockType[1]);
208                 chunk.BlockAt(glm::vec3(3, 0, 0)) = Block(blockType[2]);
209                 chunk.BlockAt(glm::vec3(3, 0, 1)) = Block(blockType[5]);
210                 chunk.BlockAt(glm::vec3(2, 0, 2)) = Block(blockType[4]);
211                 chunk.BlockAt(glm::vec3(2, 0, 3)) = Block(blockType[1]);
212                 chunk.BlockAt(glm::vec3(3, 0, 2)) = Block(blockType[2]);
213                 chunk.BlockAt(glm::vec3(3, 0, 3)) = Block(blockType[5]);
214                 chunk.BlockAt(glm::vec3(1, 1, 0)) = Block(blockType[5]);
215                 chunk.BlockAt(glm::vec3(1, 1, 1)) = Block(blockType[4]);
216                 chunk.BlockAt(glm::vec3(2, 1, 1)) = Block(blockType[3]);
217                 chunk.BlockAt(glm::vec3(2, 2, 1)) = Block(blockType[2]);
218         }
219         chunk.Invalidate();
220         return chunk;
221 }
222
223 bool World::Intersection(
224                 const Ray &ray,
225                 const glm::mat4 &M,
226                 Chunk **chunk,
227                 int *blkid,
228                 float *dist,
229                 glm::vec3 *normal) {
230         Chunk *closest_chunk = nullptr;
231         int closest_blkid = -1;
232         float closest_dist = std::numeric_limits<float>::infinity();
233         glm::vec3 closest_normal;
234
235         for (Chunk &cur_chunk : chunks) {
236                 int cur_blkid;
237                 float cur_dist;
238                 glm::vec3 cur_normal;
239                 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(), &cur_blkid, &cur_dist, &cur_normal)) {
240                         if (cur_dist < closest_dist) {
241                                 closest_chunk = &cur_chunk;
242                                 closest_blkid = cur_blkid;
243                                 closest_dist = cur_dist;
244                                 closest_normal = cur_normal;
245                         }
246                 }
247         }
248
249         if (chunk) {
250                 *chunk = closest_chunk;
251         }
252         if (blkid) {
253                 *blkid = closest_blkid;
254         }
255         if (dist) {
256                 *dist = closest_dist;
257         }
258         if (normal) {
259                 *normal = closest_normal;
260         }
261         return closest_chunk;
262 }
263
264
265 Chunk &World::Next(const Chunk &to, const glm::vec3 &dir) {
266         const glm::vec3 tgt_pos = to.Position() + dir;
267         for (Chunk &chunk : chunks) {
268                 if (chunk.Position() == tgt_pos) {
269                         return chunk;
270                 }
271         }
272         return Generate(tgt_pos);
273 }
274
275 }