-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk.cpp
More file actions
224 lines (211 loc) · 9 KB
/
chunk.cpp
File metadata and controls
224 lines (211 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <vector>
#include <ranges>
#include <algorithm>
#include <print>
#include "chunk.hpp"
#include "display.hpp"
#include <GL/glew.h>
#include "glm/glm.hpp"
#include "glm/gtx/transform.hpp"
LiveChunk::LiveChunk(ChunkData const& block_data):
vertex_count(0), dirty(true) {
this->block_data = block_data;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbop);
glBindBuffer(GL_ARRAY_BUFFER, vbop);
glBufferData(vbop, 0, 0, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3), 0);
glEnableVertexAttribArray(0);
glGenBuffers(1, &vbot);
glBindBuffer(GL_ARRAY_BUFFER, vbot);
glBufferData(vbot, 0, 0, GL_DYNAMIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), 0);
glEnableVertexAttribArray(1);
}
LiveChunk::~LiveChunk() {
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbop);
glDeleteBuffers(1, &vbot);
}
LiveChunk::LiveChunk(LiveChunk&& other): vao(other.vao), vbop(other.vbop),
vbot(other.vbot), vertex_count(other.vertex_count),
position(other.position), dirty(other.dirty), block_data(other.block_data) {
other.vao = 0;
other.vbop = 0;
other.vbot = 0;
}
LiveChunk& LiveChunk::operator=(LiveChunk&& other) {
position = other.position;
vertex_count = other.vertex_count;
vao = other.vao;
vbop = other.vbop;
vbot = other.vbot;
block_data = other.block_data;
dirty = other.dirty;
other.vao = 0;
other.vbop = 0;
other.vbot = 0;
return *this;
}
void draw_chunk(
LiveChunk const& chunk,
Camera const& camera,
unsigned int program
) {
glUseProgram(program);
// TODO: Transform matrix
auto location = glGetUniformLocation(program, "transform");
auto transform = glm::translate(glm::vec3(chunk.position.x, chunk.position.y, chunk.position.z));
glUniformMatrix4fv(location, 1, GL_FALSE, &transform[0][0]);
set_view_matrices(camera, program);
// Draw Calls
glUseProgram(program);
glBindVertexArray(chunk.vao);
glDrawArrays(GL_QUADS, 0, chunk.vertex_count);
}
std::optional<Block> ChunkData::get_block(ssize_t ix, ssize_t iy, ssize_t iz) const {
if (!(ix < static_cast<ssize_t>(sizex) && ix >= 0)) return std::nullopt;
if (!(iy < static_cast<ssize_t>(sizey) && iy >= 0)) return std::nullopt;
if (!(iz < static_cast<ssize_t>(sizez) && iz >= 0)) return std::nullopt;
return blocks[ix][iy][iz];
}
void recompute_mesh(
LiveChunk& c,
std::optional<LiveChunk const*> north,
std::optional<LiveChunk const*> west,
std::optional<LiveChunk const*> south,
std::optional<LiveChunk const*> east,
std::optional<LiveChunk const*> up,
std::optional<LiveChunk const*> down
) {
std::vector<Vector3> vertices;
std::vector<Vector2> vertices_uv;
// Generation
for (int ix = 0; ix < static_cast<int>(sizex); ix ++) {
for (int iy = 0; iy < static_cast<int>(sizey); iy ++) {
for (int iz = 0; iz < static_cast<int>(sizez); iz ++) {
auto block = c.block_data.get_block(ix, iy, iz).value(); // know it has a block
if (block != Air) continue;
// Calculate Corners
auto ox = static_cast<float>(ix);
auto oy = static_cast<float>(iy);
auto oz = static_cast<float>(iz);
Vector3 lsw(ox*blocksize, oy*blocksize, -oz*blocksize);
Vector3 usw(lsw.x, lsw.y+blocksize, lsw.z);
Vector3 lse(lsw.x+blocksize, lsw.y, lsw.z);
Vector3 use(lsw.x+blocksize, lsw.y+blocksize, lsw.z);
Vector3 lnw(ox*blocksize, oy*blocksize, -oz*blocksize-blocksize);
Vector3 unw(lsw.x, lsw.y+blocksize, lsw.z-blocksize);
Vector3 lne(lsw.x+blocksize, lsw.y, lsw.z-blocksize);
Vector3 une(lsw.x+blocksize, lsw.y+blocksize, lsw.z-blocksize);
auto ptexcords = [&vertices_uv](Block block, Cardinal dir){
auto bd = block_lookup[block];
// Bottom Left
auto& bl = bd.side_bl;
if (dir == Up || dir == Down) bl = bd.up_bl;
vertices_uv.emplace_back(Vector2(bl.x+BlockInfo::ts, bl.y+BlockInfo::ts));
vertices_uv.emplace_back(Vector2(bl.x, bl.y+BlockInfo::ts));
vertices_uv.emplace_back(Vector2(bl.x, bl.y));
vertices_uv.emplace_back(Vector2(bl.x+BlockInfo::ts, bl.y));
};
auto get_solid_block = [&](int x, int y, int z) -> std::optional<Block> {
std::optional<Block> block = c.block_data.get_block(x, y, z);
if (block.has_value() && block.value() != Air) return block.value();
// check all the neighbors
// West - East
if (west.has_value() && x < 0)
block = west.value()->block_data.get_block(x+sizex, y, z);
if (east.has_value() && x >= static_cast<int>(sizex))
block = east.value()->block_data.get_block(x-sizex, y, z);
// South - North
if (south.has_value() && z < 0)
block = south.value()->block_data.get_block(x, y, z+sizez);
if (north.has_value() && z >= static_cast<int>(sizez))
block = north.value()->block_data.get_block(x, y, z-sizez);
// Down - Up
if (down.has_value() && y < 0)
block = down.value()->block_data.get_block(x, y+sizey, z);
if (up.has_value() && y >= static_cast<int>(sizey))
block = up.value()->block_data.get_block(x, y-sizey, z);
// Check if we found an alternative - if not we are on the border
if (block.has_value() && block.value() != Air) return block.value();
return std::nullopt;
};
std::optional<Block> neighbor;
// North
neighbor = get_solid_block(ix, iy, iz+1);
if (neighbor.has_value()) {
vertices.emplace_back(une);
vertices.emplace_back(unw);
vertices.emplace_back(lnw);
vertices.emplace_back(lne);
ptexcords(neighbor.value(), North);
}
// West
neighbor = get_solid_block(ix-1, iy, iz);
if (neighbor.has_value()) {
vertices.emplace_back(usw);
vertices.emplace_back(unw);
vertices.emplace_back(lnw);
vertices.emplace_back(lsw);
ptexcords(neighbor.value(), West);
}
// South
neighbor = get_solid_block(ix, iy, iz-1);
if (neighbor.has_value()) {
vertices.emplace_back(use);
vertices.emplace_back(usw);
vertices.emplace_back(lsw);
vertices.emplace_back(lse);
ptexcords(neighbor.value(), South);
}
// East
neighbor = get_solid_block(ix+1, iy, iz);
if (neighbor.has_value()) {
vertices.emplace_back(une);
vertices.emplace_back(use);
vertices.emplace_back(lse);
vertices.emplace_back(lne);
ptexcords(neighbor.value(), East);
}
// Up
neighbor = get_solid_block(ix, iy+1, iz);
if (neighbor.has_value()) {
vertices.emplace_back(une);
vertices.emplace_back(unw);
vertices.emplace_back(usw);
vertices.emplace_back(use);
ptexcords(neighbor.value(), Up);
}
// Down
neighbor = get_solid_block(ix, iy-1, iz);
if (neighbor.has_value()) {
vertices.emplace_back(lne);
vertices.emplace_back(lse);
vertices.emplace_back(lsw);
vertices.emplace_back(lnw);
ptexcords(neighbor.value(), Down);
}
}
}
}
// Upload
// vbop
c.vertex_count = vertices.size();
glBindVertexArray(c.vao);
glBindBuffer(GL_ARRAY_BUFFER, c.vbop);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vector3), vertices.data(),
GL_DYNAMIC_DRAW);
// vbot
glBindBuffer(GL_ARRAY_BUFFER, c.vbot);
glBufferData(GL_ARRAY_BUFFER, vertices_uv.size()*sizeof(Vector2), vertices_uv.data(),
GL_DYNAMIC_DRAW);
c.dirty = false;
}
bool IndexId::operator==(IndexId const& other) const {
return x == other.x && y == other.y && z == other.z;
}
size_t IndexHash::operator()(IndexId const& idx) const {
return std::hash<int>()(idx.x) ^ std::hash<int>()(idx.y << 1) ^ std::hash<int>()(idx.z << 2);
}