]> git.localhorst.tv Git - blank.git/blob - src/world.cpp
move controller from camera to world
[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                                 glm::vec3 pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
108                                 if (blocks[id].type->shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
109                                         if (cur_dist < closest_dist) {
110                                                 closest_id = id;
111                                                 closest_dist = cur_dist;
112                                                 closest_normal = cur_norm;
113                                         }
114                                 }
115                         }
116                 }
117         }
118
119         if (closest_id < 0) {
120                 return false;
121         }
122
123         if (blkid) {
124                 *blkid = closest_id;
125         }
126         if (dist) {
127                 *dist = closest_dist;
128         }
129         if (normal) {
130                 *normal = closest_normal;
131         }
132         return true;
133 }
134
135 void Chunk::Position(const glm::vec3 &pos) {
136         position = pos;
137         transform = glm::translate(pos * Extent());
138 }
139
140
141 int Chunk::VertexCount() const {
142         int count = 0;
143         for (const auto &block : blocks) {
144                 count += block.type->shape->VertexCount();
145         }
146         return count;
147 }
148
149 void Chunk::Update() {
150         model.Clear();
151         model.Reserve(VertexCount());
152
153         for (size_t i = 0; i < Size(); ++i) {
154                 blocks[i].type->FillModel(ToCoords(i), model);
155         }
156
157         model.Invalidate();
158         dirty = false;
159 }
160
161
162 World::World()
163 : blockType()
164 , blockShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }})
165 , stairShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}, { 0.0f, 0.0f })
166 , slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }})
167 , blockNoise(0)
168 , colorNoise(1)
169 , chunks() {
170         blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &blockShape }); // white block
171         blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &stairShape }); // white stair
172         blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &slabShape }); // white slab
173         blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &blockShape }); // red block
174         blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &stairShape }); // red stair
175         blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &slabShape }); // red slab
176         blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &blockShape }); // green block
177         blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &stairShape }); // green stair
178         blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &slabShape }); // green slab
179         blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &blockShape }); // blue block
180         blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &stairShape }); // blue stair
181         blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &slabShape }); // blue slab
182
183         player.Position({ 4.0f, 4.0f, 4.0f });
184 }
185
186
187 void World::Generate() {
188         for (int z = -2; z < 3; ++z) {
189                 for (int y = -2; y < 3; ++y) {
190                         for (int x = -2; x < 3; ++x) {
191                                 Generate(glm::vec3(x, y, z));
192                         }
193                 }
194         }
195 }
196
197 Chunk &World::Generate(const glm::vec3 &pos) {
198         chunks.emplace_back();
199         Chunk &chunk = chunks.back();
200         chunk.Position(pos);
201         if (pos.x == 0 && pos.y == 0 && pos.z == 0) {
202                 for (size_t i = 1; i < blockType.Size(); ++i) {
203                         chunk.BlockAt(i) = Block(blockType[i]);
204                         chunk.BlockAt(i + 257) = Block(blockType[i]);
205                         chunk.BlockAt(i + 514) = Block(blockType[i]);
206                 }
207         } else {
208                 for (int z = 0; z < Chunk::Depth(); ++z) {
209                         for (int y = 0; y < Chunk::Height(); ++y) {
210                                 for (int x = 0; x < Chunk::Width(); ++x) {
211                                         glm::vec3 block_pos{float(x), float(y), float(z)};
212                                         glm::vec3 gen_pos = (pos * Chunk::Extent() + block_pos) / 64.0f;
213                                         float val = blockNoise(gen_pos);
214                                         if (val > 0.8f) {
215                                                 int col_val = int((colorNoise(gen_pos) + 1.0f) * 2.0f) % 4;
216                                                 chunk.BlockAt(block_pos) = Block(blockType[col_val * 3 + 1]);
217                                         }
218                                 }
219                         }
220                 }
221         }
222         chunk.Invalidate();
223         return chunk;
224 }
225
226 bool World::Intersection(
227                 const Ray &ray,
228                 const glm::mat4 &M,
229                 Chunk **chunk,
230                 int *blkid,
231                 float *dist,
232                 glm::vec3 *normal) {
233         Chunk *closest_chunk = nullptr;
234         int closest_blkid = -1;
235         float closest_dist = std::numeric_limits<float>::infinity();
236         glm::vec3 closest_normal;
237
238         for (Chunk &cur_chunk : chunks) {
239                 int cur_blkid;
240                 float cur_dist;
241                 glm::vec3 cur_normal;
242                 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(), &cur_blkid, &cur_dist, &cur_normal)) {
243                         if (cur_dist < closest_dist) {
244                                 closest_chunk = &cur_chunk;
245                                 closest_blkid = cur_blkid;
246                                 closest_dist = cur_dist;
247                                 closest_normal = cur_normal;
248                         }
249                 }
250         }
251
252         if (chunk) {
253                 *chunk = closest_chunk;
254         }
255         if (blkid) {
256                 *blkid = closest_blkid;
257         }
258         if (dist) {
259                 *dist = closest_dist;
260         }
261         if (normal) {
262                 *normal = closest_normal;
263         }
264         return closest_chunk;
265 }
266
267
268 Chunk &World::Next(const Chunk &to, const glm::vec3 &dir) {
269         const glm::vec3 tgt_pos = to.Position() + dir;
270         for (Chunk &chunk : chunks) {
271                 if (chunk.Position() == tgt_pos) {
272                         return chunk;
273                 }
274         }
275         return Generate(tgt_pos);
276 }
277
278
279 void World::Update(int dt) {
280         player.Update(dt);
281 }
282
283
284 void World::Render(DirectionalLighting &program) {
285         program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
286         program.SetView(glm::inverse(player.Transform()));
287
288         for (Chunk &chunk : LoadedChunks()) {
289                 program.SetM(chunk.Transform());
290                 chunk.Draw();
291         }
292 }
293
294 }