/
world.d
289 lines (224 loc) · 9.72 KB
/
world.d
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
module brala.dine.world;
private {
import glamour.gl : GLuint, glDrawArrays, GL_TRIANGLES, GL_FLOAT;
import glamour.vbo : Buffer;
import gl3n.linalg : vec3i, mat4;
import brala.dine.chunk : Chunk, Block;
import brala.dine.builder.biomes : BIOMES;
import brala.dine.builder.tessellator : Tessellator, Vertex;
import brala.exception : WorldError;
import brala.engine : BraLaEngine;
import brala.utils.alloc : malloc, realloc, free;
}
private const Block AIR_BLOCK = Block(0);
class World {
static Vertex* tessellate_buffer;
static size_t tessellate_buffer_length;
static this() {
tessellate_buffer_length = width*height*depth*5; // this value is the result of testing!
tessellate_buffer = cast(Vertex*)malloc(tessellate_buffer_length*Vertex.sizeof);
}
static ~this() {
}
const int width = 16;
const int height = 256;
const int depth = 16;
const int zstep = width*height;
const int min_height = 0;
const int max_height = height;
Chunk[vec3i] chunks;
vec3i spawn;
this() {}
this(vec3i spawn) {
this.spawn = spawn;
}
~this() {
remove_all_chunks();
}
// when a chunk is passed to this method, the world will take care of it's memory
// you should also lose all other references to this chunk
//
// old chunk will be cleared
void add_chunk(Chunk chunk, vec3i chunkc) {
if(Chunk* c = chunkc in chunks) {
c.empty_chunk();
}
chunks[chunkc] = chunk;
mark_surrounding_chunks_dirty(chunkc);
}
void remove_chunk(vec3i chunkc)
in { assert(chunkc in chunks); }
body {
chunks[chunkc].empty_chunk();
chunks.remove(chunkc);
mark_surrounding_chunks_dirty(chunkc);
}
void remove_all_chunks() {
foreach(key, chunk; chunks) {
chunk.empty_chunk();
chunks.remove(key);
}
}
Chunk get_chunk(int x, int y, int z) {
return get_chunk(vec3i(x, y, z));
}
Chunk get_chunk(vec3i chunkc) {
if(Chunk* c = chunkc in chunks) {
return *c;
}
return null;
}
void set_block(vec3i position, Block block)
in { assert(position.y >= min_height && position.y <= max_height); }
body {
vec3i chunkc = vec3i(position.x / width, position.y / height, position.z / depth);
Chunk chunk = get_chunk(chunkc);
if(chunk is null) {
throw new WorldError("No chunk available for position " ~ position.toString());
}
vec3i block_position = vec3i(position.x % width, position.y % height, position.z % depth);
uint flat = chunk.to_flat(block_position);
if(chunk[flat] != block) {
chunk[flat] = block;
mark_surrounding_chunks_dirty(chunkc);
}
}
Block get_block(vec3i position)
in { assert(position.y >= min_height && position.y <= max_height); }
body {
Chunk chunk = get_chunk(position.x / width, position.y / height, position.z / depth);
if(chunk is null) {
throw new WorldError("No chunk available for position " ~ position.toString());
}
return chunk[chunk.to_flat(position.x % width, position.y % height, position.z % depth)];
}
Block get_block_safe(vec3i position, Block def = AIR_BLOCK) {
Chunk chunk = get_chunk(position.x / width, position.y / height, position.z / depth);
if(chunk is null) return def;
int x = position.x % width;
int y = position.y % height;
int z = position.z % depth;
if(x >= 0 && x < chunk.width && y >= 0 && y < chunk.height && z >= 0 && z < chunk.depth) {
return chunk[chunk.to_flat(x, y, z)];
} else {
return def;
}
}
void mark_surrounding_chunks_dirty(int x, int y, int z) {
return mark_surrounding_chunks_dirty(vec3i(x, y, z));
}
void mark_surrounding_chunks_dirty(vec3i chunkc) {
mark_chunk_dirty(chunkc.x+1, chunkc.y, chunkc.z);
mark_chunk_dirty(chunkc.x-1, chunkc.y, chunkc.z);
mark_chunk_dirty(chunkc.x, chunkc.y+1, chunkc.z);
mark_chunk_dirty(chunkc.x, chunkc.y-1, chunkc.z);
mark_chunk_dirty(chunkc.x, chunkc.y, chunkc.z+1);
mark_chunk_dirty(chunkc.x, chunkc.y, chunkc.z-1);
}
void mark_chunk_dirty(int x, int y, int z) {
return mark_chunk_dirty(vec3i(x, y, z));
}
void mark_chunk_dirty(vec3i chunkc) {
if(Chunk* c = chunkc in chunks) {
c.dirty = true;
}
}
// rendering
// fills the vbo with the chunk content
// original version from florian boesch - http://codeflow.org/
void tessellate(Chunk chunk, vec3i chunkc, ref Vertex* v, ref size_t length, bool force=false) {
Tessellator tessellator = Tessellator(this, v, length);
if(chunk.vbo is null) {
chunk.vbo = new Buffer();
}
if(chunk.dirty || force) {
int index;
int w = 0;
int y;
int hds = height / 16;
float z_offset, z_offset_n;
float y_offset, y_offset_t;
float x_offset, x_offset_r;
Block value;
Block right_block;
Block front_block;
Block top_block;
Block back_block;
Block left_block;
vec3i wcoords_orig = vec3i(chunkc.x*chunk.width, chunkc.y*chunk.height, chunkc.z*chunk.depth);
vec3i wcoords = wcoords_orig;
// TODO: octree?
foreach(z; 0..depth) {
z_offset = z + 0.5f;
z_offset_n = z + 1.5f;
wcoords.z = wcoords_orig.z + z;
foreach(b; 0..hds) {
if((chunk.primary_bitmask >> b) & 1 ^ 1) continue;
foreach(y_; 0..hds) {
y = b*hds + y_;
y_offset = y+0.5f;
y_offset_t = y+1.5f;
wcoords.x = wcoords_orig.x;
wcoords.y = wcoords_orig.y + y;
value = get_block_safe(wcoords);
tessellator.realloc_buffer_if_needed(1024*(depth-z));
foreach(x; 0..width) {
x_offset = x+0.5f;
x_offset_r = x+1.5f;
wcoords.x = wcoords_orig.x + x;
index = x+y*width+z*zstep;
if(x == width-1) {
right_block = get_block_safe(vec3i(wcoords.x+1, wcoords.y, wcoords.z), AIR_BLOCK);
} else {
right_block = chunk.blocks[index+1];
}
if(z == depth-1) {
front_block = get_block_safe(vec3i(wcoords.x, wcoords.y, wcoords.z+1), AIR_BLOCK);
} else {
front_block = chunk.blocks[index+zstep];
}
if(y == height-1) {
top_block = AIR_BLOCK;
} else {
top_block = chunk.blocks[index+width];
}
tessellator.feed(wcoords, x, y, z,
x_offset, x_offset_r, y_offset, y_offset_t, z_offset, z_offset_n,
value, right_block, top_block, front_block,
BIOMES[chunk.biome_data[x+z*15]]);
value = right_block;
}
}
}
}
chunk.vbo_vcount = tessellator.elements * __traits(allMembers, Vertex).length;
//chunk.vbo_vcount = tessellator.elements / 10;
tessellator.fill_vbo(chunk.vbo);
chunk.dirty = false;
}
}
void bind(BraLaEngine engine, Chunk chunk)
in { assert(chunk.vbo !is null); assert(engine.current_shader !is null, "no current shader"); }
body {
GLuint position = engine.current_shader.get_attrib_location("position");
GLuint normal = engine.current_shader.get_attrib_location("normal");
GLuint texcoord = engine.current_shader.get_attrib_location("texcoord");
GLuint palettecoord = engine.current_shader.get_attrib_location("palettecoord");
//import std.stdio; writefln("%s", palettecoord);
// stride = vertex: x,y,z, normal: xn, xy, xz, texcoords: u, v, palette: s, t
uint stride = Vertex.sizeof;
chunk.vbo.bind(position, GL_FLOAT, 3, 0, stride);
chunk.vbo.bind(normal, GL_FLOAT, 3, Vertex().nx.offsetof, stride);
chunk.vbo.bind(texcoord, GL_FLOAT, 2, Vertex().u_terrain.offsetof, stride);
chunk.vbo.bind(palettecoord, GL_FLOAT, 2, Vertex().u_biome.offsetof, stride);
}
void draw(BraLaEngine engine) {
foreach(chunkc, chunk; chunks) {
tessellate(chunk, chunkc, tessellate_buffer, tessellate_buffer_length, false);
bind(engine, chunk);
engine.model = mat4.translation(chunkc.x*width, chunkc.y*height, chunkc.z*depth);
engine.flush_uniforms();
glDrawArrays(GL_TRIANGLES, 0, chunk.vbo_vcount);
}
}
}