]> git.localhorst.tv Git - blank.git/blob - src/world.cpp
split entity from controller
[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 World::World()
10 : blockType()
11 , blockShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }})
12 , stairShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}, { 0.0f, 0.0f })
13 , slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }})
14 , blockNoise(0)
15 , colorNoise(1)
16 , player()
17 , loaded()
18 , to_generate() {
19         blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &blockShape }); // white block
20         blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &stairShape }); // white stair
21         blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &slabShape }); // white slab
22         blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &blockShape }); // red block
23         blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &stairShape }); // red stair
24         blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &slabShape }); // red slab
25         blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &blockShape }); // green block
26         blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &stairShape }); // green stair
27         blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &slabShape }); // green slab
28         blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &blockShape }); // blue block
29         blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &stairShape }); // blue stair
30         blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &slabShape }); // blue slab
31
32         player.Position({ 4.0f, 4.0f, 4.0f });
33 }
34
35
36 namespace {
37
38 bool ChunkLess(const Chunk &a, const Chunk &b) {
39         return dot(a.Position(), a.Position()) < dot(b.Position(), b.Position());
40 }
41
42 }
43
44 void World::Generate(const glm::tvec3<int> &from, const glm::tvec3<int> &to) {
45         for (int z = from.z; z < to.z; ++z) {
46                 for (int y = from.y; y < to.y; ++y) {
47                         for (int x = from.x; x < to.x; ++x) {
48                                 glm::vec3 pos{float(x), float(y), float(z)};
49                                 if (x == 0 && y == 0 && z == 0) {
50                                         loaded.emplace_back();
51                                         loaded.back().Position(pos);
52                                         Generate(loaded.back());
53                                 } else {
54                                         to_generate.emplace_back();
55                                         to_generate.back().Position(pos);
56                                 }
57                         }
58                 }
59         }
60         to_generate.sort(ChunkLess);
61 }
62
63 void World::Generate(Chunk &chunk) {
64         glm::vec3 pos(chunk.Position());
65         if (pos.x == 0 && pos.y == 0 && pos.z == 0) {
66                 for (size_t i = 1; i < blockType.Size(); ++i) {
67                         chunk.BlockAt(i) = Block(blockType[i]);
68                         chunk.BlockAt(i + 257) = Block(blockType[i]);
69                         chunk.BlockAt(i + 514) = Block(blockType[i]);
70                 }
71         } else {
72                 for (int z = 0; z < Chunk::Depth(); ++z) {
73                         for (int y = 0; y < Chunk::Height(); ++y) {
74                                 for (int x = 0; x < Chunk::Width(); ++x) {
75                                         glm::vec3 block_pos{float(x), float(y), float(z)};
76                                         glm::vec3 gen_pos = (pos * Chunk::Extent() + block_pos) / 64.0f;
77                                         float val = blockNoise(gen_pos);
78                                         if (val > 0.8f) {
79                                                 int col_val = int((colorNoise(gen_pos) + 1.0f) * 2.0f) % 4;
80                                                 chunk.BlockAt(block_pos) = Block(blockType[col_val * 3 + 1]);
81                                         }
82                                 }
83                         }
84                 }
85         }
86         chunk.Invalidate();
87 }
88
89 bool World::Intersection(
90                 const Ray &ray,
91                 const glm::mat4 &M,
92                 Chunk **chunk,
93                 int *blkid,
94                 float *dist,
95                 glm::vec3 *normal) {
96         Chunk *closest_chunk = nullptr;
97         int closest_blkid = -1;
98         float closest_dist = std::numeric_limits<float>::infinity();
99         glm::vec3 closest_normal;
100
101         for (Chunk &cur_chunk : loaded) {
102                 int cur_blkid;
103                 float cur_dist;
104                 glm::vec3 cur_normal;
105                 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(), &cur_blkid, &cur_dist, &cur_normal)) {
106                         if (cur_dist < closest_dist) {
107                                 closest_chunk = &cur_chunk;
108                                 closest_blkid = cur_blkid;
109                                 closest_dist = cur_dist;
110                                 closest_normal = cur_normal;
111                         }
112                 }
113         }
114
115         if (chunk) {
116                 *chunk = closest_chunk;
117         }
118         if (blkid) {
119                 *blkid = closest_blkid;
120         }
121         if (dist) {
122                 *dist = closest_dist;
123         }
124         if (normal) {
125                 *normal = closest_normal;
126         }
127         return closest_chunk;
128 }
129
130
131 Chunk &World::Next(const Chunk &to, const glm::vec3 &dir) {
132         const glm::vec3 tgt_pos = to.Position() + dir;
133         for (Chunk &chunk : LoadedChunks()) {
134                 if (chunk.Position() == tgt_pos) {
135                         return chunk;
136                 }
137         }
138         for (Chunk &chunk : to_generate) {
139                 if (chunk.Position() == tgt_pos) {
140                         Generate(chunk);
141                         return chunk;
142                 }
143         }
144         loaded.emplace_back();
145         loaded.back().Position(tgt_pos);
146         Generate(loaded.back());
147         return loaded.back();
148 }
149
150
151 void World::Update(int dt) {
152         player.Update(dt);
153
154         if (!to_generate.empty()) {
155                 Generate(to_generate.front());
156                 loaded.splice(loaded.end(), to_generate, to_generate.begin());
157         }
158 }
159
160
161 void World::Render(DirectionalLighting &program) {
162         program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
163         program.SetView(glm::inverse(player.Transform()));
164
165         for (Chunk &chunk : LoadedChunks()) {
166                 program.SetM(chunk.Transform());
167                 chunk.Draw();
168         }
169 }
170
171 }