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