4 #include <glm/gtx/transform.hpp>
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 }})
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
32 player.Position({ 4.0f, 4.0f, 4.0f });
38 bool ChunkLess(const Chunk &a, const Chunk &b) {
39 return dot(a.Position(), a.Position()) < dot(b.Position(), b.Position());
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());
54 to_generate.emplace_back();
55 to_generate.back().Position(pos);
60 to_generate.sort(ChunkLess);
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]);
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);
79 int col_val = int((colorNoise(gen_pos) + 1.0f) * 2.0f) % 4;
80 chunk.BlockAt(block_pos) = Block(blockType[col_val * 3 + 1]);
89 bool World::Intersection(
96 Chunk *closest_chunk = nullptr;
97 int closest_blkid = -1;
98 float closest_dist = std::numeric_limits<float>::infinity();
99 glm::vec3 closest_normal;
101 for (Chunk &cur_chunk : loaded) {
104 glm::vec3 cur_normal;
105 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player.ChunkCoords()), &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;
116 *chunk = closest_chunk;
119 *blkid = closest_blkid;
122 *dist = closest_dist;
125 *normal = closest_normal;
127 return closest_chunk;
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) {
138 for (Chunk &chunk : to_generate) {
139 if (chunk.Position() == tgt_pos) {
144 loaded.emplace_back();
145 loaded.back().Position(tgt_pos);
146 Generate(loaded.back());
147 return loaded.back();
151 void World::Update(int dt) {
154 if (!to_generate.empty()) {
155 Generate(to_generate.front());
156 loaded.splice(loaded.end(), to_generate, to_generate.begin());
161 void World::Render(DirectionalLighting &program) {
162 program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
163 program.SetView(glm::inverse(player.Transform(player.ChunkCoords())));
165 for (Chunk &chunk : LoadedChunks()) {
166 program.SetM(chunk.Transform(player.ChunkCoords()));