-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChunkCpp.hpp
181 lines (147 loc) · 4.81 KB
/
ChunkCpp.hpp
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
#pragma once
#include <cstdint>
#include <vector>
#include "WorldGen.hpp"
class ChunkCpp {
private:
int64_t x, z;
Block blocks[CHUNK_SIZE_X][CHUNK_SIZE_Y][CHUNK_SIZE_Z];
std::vector<float> vertexBuffer;
std::vector<uint32_t> indexBuffer;
public:
inline ChunkCpp(): x(0), z(0), vertexBuffer{}, indexBuffer{} { }
inline ChunkCpp(const int64_t x, const int64_t z): x(x), z(z), vertexBuffer{}, indexBuffer{} { }
inline void* getBuffer() {
return blocks;
}
inline int64_t getX() const {
return x;
}
inline int64_t getZ() const {
return z;
}
inline void generate() {
for(size_t x = 0; x < CHUNK_SIZE_X; x++) {
for(size_t y = 0; y < CHUNK_SIZE_Y; y++) {
for(size_t z = 0; z < CHUNK_SIZE_Z; z++) {
const int64_t ax = this->x * CHUNK_SIZE_X + x;
const int64_t az = this->z * CHUNK_SIZE_Z + z;
blocks[x][y][z] = getBlock(ax, y, az);
// blocks[x][y][z] = y < 60 ? BLOCK_STONE : BLOCK_AIR;
}
}
}
}
inline void generateMesh() {
vertexBuffer.clear();
indexBuffer.clear();
const auto emitQuad = [this](const float *const pos, const float *const norm, const Block block) {
const uint32_t extra = block;
const float fExtra = *reinterpret_cast<const float*>(&extra);
const float newVerts[] {
pos[0], pos[1], pos[2], norm[0], norm[1], norm[2], 0.f, 0.f, fExtra,
pos[3], pos[4], pos[5], norm[0], norm[1], norm[2], 0.f, 1.f, fExtra,
pos[6], pos[7], pos[8], norm[0], norm[1], norm[2], 1.f, 0.f, fExtra,
pos[9], pos[10], pos[11], norm[0], norm[1], norm[2], 1.f, 1.f, fExtra
};
const uint32_t numVerts = vertexBuffer.size() / FLOATS_PER_VERTEX;
const uint32_t newInds[] {
numVerts + 0, numVerts + 1, numVerts + 3,
numVerts + 0, numVerts + 3, numVerts + 2
};
vertexBuffer.insert(vertexBuffer.end(), (float*)newVerts, ((float*)newVerts) + (sizeof(newVerts) / sizeof(float)));
indexBuffer.insert(indexBuffer.end(), newInds, newInds + (sizeof(newInds) / sizeof(uint32_t)));
};
const auto isAir = [](const Block block) {
return block == BLOCK_AIR;
};
const auto isTranslucent = [](const Block block) {
return block == BLOCK_AIR || block == BLOCK_WATER;
};
for(size_t x = 0; x < CHUNK_SIZE_X; x++) {
for(size_t y = 0; y < CHUNK_SIZE_Y; y++) {
for(size_t z = 0; z < CHUNK_SIZE_Z; z++) {
if(isAir(blocks[x][y][z])) continue;
// top face:
if(y >= CHUNK_SIZE_Y-1 || (blocks[x][y][z] != blocks[x][y+1][z])&&isTranslucent(blocks[x][y+1][z])) {
const float normal[] { 0., 1., 0. };
const float vertices[] {
x, y+1, z,
x+1, y+1, z,
x , y+1, z+1,
x+1, y+1, z+1
};
emitQuad(vertices, normal, blocks[x][y][z]);
}
// bottom face:
if(y <= 0 || (blocks[x][y][z] != blocks[x][y-1][z]) && isTranslucent(blocks[x][y-1][z])) {
const float normal[] { 0., -1., 0. };
const float vertices[] {
x, y, z+1,
x+1, y, z+1,
x , y, z,
x+1, y, z
};
emitQuad(vertices, normal, blocks[x][y][z]);
}
// pz face:
if(z >= CHUNK_SIZE_Z-1 || (blocks[x][y][z] != blocks[x][y][z+1]) && isTranslucent(blocks[x][y][z+1])) {
const float normal[] { 0., 0., 1. };
const float vertices[] {
x, y+1, z+1,
x+1, y+1, z+1,
x , y, z+1,
x+1, y, z+1
};
emitQuad(vertices, normal, blocks[x][y][z]);
}
// nz face:
if(z <= 0 || (blocks[x][y][z] != blocks[x][y][z-1]) && isTranslucent(blocks[x][y][z-1])) {
const float normal[] { 0., 0., -1. };
const float vertices[] {
x+1, y+1, z,
x, y+1, z,
x+1, y, z,
x, y, z
};
emitQuad(vertices, normal, blocks[x][y][z]);
}
// px face:
if(x >= CHUNK_SIZE_X-1 || (blocks[x][y][z] != blocks[x+1][y][z]) && isTranslucent(blocks[x+1][y][z])) {
const float normal[] { 1., 0., 0. };
const float vertices[] {
x+1, y+1, z+1,
x+1, y+1, z,
x+1, y, z+1,
x+1, y, z
};
emitQuad(vertices, normal, blocks[x][y][z]);
}
// nx face:
if(x <= 0 || (blocks[x][y][z] != blocks[x-1][y][z]) && isTranslucent(blocks[x-1][y][z])) {
const float normal[] { -1., 0., 0. };
const float vertices[] {
x, y+1, z,
x, y+1, z+1,
x, y, z,
x, y, z+1
};
emitQuad(vertices, normal, blocks[x][y][z]);
}
}
}
}
}
inline float* getVertices() {
return vertexBuffer.data();
}
inline uint32_t* getIndices() {
return indexBuffer.data();
}
inline size_t numVertices() const {
return vertexBuffer.size();
}
inline size_t numIndices() const {
return indexBuffer.size();
}
};