]> git.localhorst.tv Git - blank.git/blob - src/world.cpp
added distance fog
[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 , generate(0)
15 , chunks(blockType, generate)
16 , player() {
17         BlockType::Faces block_fill = {  true,  true,  true,  true,  true,  true };
18         BlockType::Faces slab_fill  = { false,  true, false, false, false, false };
19         BlockType::Faces stair_fill = { false,  true, false, false, false,  true };
20
21         { // white block
22                 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &blockShape);
23                 type.block_light = true;
24                 type.fill = block_fill;
25                 blockType.Add(type);
26         }
27         { // white slab
28                 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &slabShape);
29                 type.block_light = true;
30                 type.fill = slab_fill;
31                 blockType.Add(type);
32         }
33         { // white stair
34                 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &stairShape);
35                 type.block_light = true;
36                 type.fill = stair_fill;
37                 blockType.Add(type);
38         }
39
40         { // red block
41                 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &blockShape);
42                 type.block_light = true;
43                 type.fill = block_fill;
44                 blockType.Add(type);
45         }
46         { // red slab
47                 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &slabShape);
48                 type.block_light = true;
49                 type.fill = slab_fill;
50                 blockType.Add(type);
51         }
52         { // red stair
53                 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &stairShape);
54                 type.block_light = true;
55                 type.fill = stair_fill;
56                 blockType.Add(type);
57         }
58
59         { // green block
60                 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &blockShape);
61                 type.block_light = true;
62                 type.fill = block_fill;
63                 blockType.Add(type);
64         }
65         { // green slab
66                 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &slabShape);
67                 type.block_light = true;
68                 type.fill = slab_fill;
69                 blockType.Add(type);
70         }
71         { // green stair
72                 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &stairShape);
73                 type.block_light = true;
74                 type.fill = stair_fill;
75                 blockType.Add(type);
76         }
77
78         { // blue block
79                 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &blockShape);
80                 type.block_light = true;
81                 type.fill = block_fill;
82                 blockType.Add(type);
83         }
84         { // blue slab
85                 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &slabShape);
86                 type.block_light = true;
87                 type.fill = slab_fill;
88                 blockType.Add(type);
89         }
90         { // blue stair
91                 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &stairShape);
92                 type.block_light = true;
93                 type.fill = stair_fill;
94                 blockType.Add(type);
95         }
96
97         { // glowing yellow block
98                 BlockType type(true, { 1.0f, 1.0f, 0.0f }, &blockShape);
99                 type.luminosity = 10;
100                 type.block_light = true;
101                 type.fill = block_fill;
102                 blockType.Add(type);
103         }
104
105         generate.Space(0);
106         generate.Solids({ 1, 4, 7, 10 });
107
108         player = &AddEntity();
109         player->Position({ 4.0f, 4.0f, 4.0f });
110
111         chunks.Generate({ -4, -4, -4 }, { 5, 5, 5});
112 }
113
114
115 namespace {
116
117 struct Candidate {
118         Chunk *chunk;
119         float dist;
120 };
121
122 std::vector<Candidate> candidates;
123
124 }
125
126 bool World::Intersection(
127                 const Ray &ray,
128                 const glm::mat4 &M,
129                 Chunk **chunk,
130                 int *blkid,
131                 float *dist,
132                 glm::vec3 *normal) {
133         candidates.clear();
134
135         for (Chunk &cur_chunk : chunks.Loaded()) {
136                 float cur_dist;
137                 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
138                         candidates.push_back({ &cur_chunk, cur_dist });
139                 }
140         }
141
142         if (candidates.empty()) return false;
143
144         Chunk *closest_chunk = nullptr;
145         float closest_dist = std::numeric_limits<float>::infinity();
146         int closest_blkid = -1;
147         glm::vec3 closest_normal;
148
149         for (Candidate &cand : candidates) {
150                 if (cand.dist > closest_dist) continue;
151                 int cur_blkid;
152                 float cur_dist;
153                 glm::vec3 cur_normal;
154                 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player->ChunkCoords()), cur_blkid, cur_dist, cur_normal)) {
155                         if (cur_dist < closest_dist) {
156                                 closest_chunk = cand.chunk;
157                                 closest_blkid = cur_blkid;
158                                 closest_dist = cur_dist;
159                                 closest_normal = cur_normal;
160                         }
161                 }
162         }
163
164         if (chunk) {
165                 *chunk = closest_chunk;
166         }
167         if (blkid) {
168                 *blkid = closest_blkid;
169         }
170         if (dist) {
171                 *dist = closest_dist;
172         }
173         if (normal) {
174                 *normal = closest_normal;
175         }
176         return closest_chunk;
177 }
178
179
180 Chunk &World::PlayerChunk() {
181         return chunks.ForceLoad(player->ChunkCoords());
182 }
183
184 Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
185         const Chunk::Pos tgt_pos = to.Position() + dir;
186         return chunks.ForceLoad(tgt_pos);
187 }
188
189
190 void World::Update(int dt) {
191         for (Entity &entity : entities) {
192                 entity.Update(dt);
193         }
194         chunks.Rebase(player->ChunkCoords());
195         chunks.Update();
196 }
197
198
199 void World::Render(DirectionalLighting &program) {
200         program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
201         // fade out reaches 1/e (0.3679) at 1/fog_density,
202         // gets less than 0.01 at e/(2 * fog_density)
203         // I chose 0.011 because it yields 91 and 124 for those, so
204         // slightly less than 6 and 8 chunks
205         program.SetFogDensity(0.011f);
206         program.SetView(glm::inverse(player->Transform(player->ChunkCoords())));
207
208         for (Chunk &chunk : chunks.Loaded()) {
209                 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
210                 program.SetM(m);
211                 glm::mat4 mvp(program.GetVP() * m);
212                 if (!CullTest(Chunk::Bounds(), mvp)) {
213                         chunk.Draw();
214                 }
215         }
216
217         for (Entity &entity : entities) {
218                 if (entity.HasShape()) {
219                         program.SetM(entity.Transform(player->ChunkCoords()));
220                         entity.Draw();
221                 }
222         }
223 }
224
225 }