-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chunk.d
88 lines (71 loc) · 1.77 KB
/
Chunk.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
module Chunk;
import std.container : SList;
import derelict.opengl.gl;
import Block;
import GlobalVariables;
class Chunk {
Block[16][128][16] blocks;
SList!Block blockList;
int[3] pos;
uint vbo;
uint texVBO;
uint vboLen;
bool vboReady = false;
float[] vertexList;
float[] textureCoordList;
void resetVBO() {
vertexList.length = 0;
textureCoordList.length = 0;
vboReady = false;
}
void addBlockToVBO(Block b) {
float coords[2][] = b.getVertArray();
vertexList ~= coords[0];
textureCoordList ~= coords[1];
vboReady = false;
}
void addBlock(int x, int y, int z) {
blocks[x][y][z] = new Block([x, y, z], this);
blockList.insertFront(blocks[x][y][z]);
}
this(int[3] pos) {
this.pos[0] = pos[0];
this.pos[1] = pos[1];
this.pos[2] = pos[2];
resetVBO();
foreach(x; 0..blocks.length)
foreach(y; 0..blocks[x].length)
foreach(z; 0..blocks[x][y].length)
if(pos[1]*1024+(y-groundLevel)*32+noise.generateNoise(pos[0]/32.0f+x/512.0f, pos[1]/16.0f+y/256.0f, pos[2]/32.0f+z/512.0f) < 0)
addBlock(x, y, z);
foreach(block; blockList)
addBlockToVBO(block);
}
void vboify() {
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertexList.length * float.sizeof, vertexList.ptr, GL_STATIC_DRAW);
glGenBuffers(1, &texVBO);
glBindBuffer(GL_ARRAY_BUFFER, texVBO);
glBufferData(GL_ARRAY_BUFFER, textureCoordList.length * float.sizeof, textureCoordList.ptr, GL_STATIC_DRAW);
vboLen = vertexList.length / 3;
vertexList.length = 0;
textureCoordList.length = 0;
vboReady = true;
}
uint getVBO() {
if(!vboReady)
vboify();
return vbo;
}
uint getTexVBO() {
if(!vboReady)
vboify();
return texVBO;
}
uint getVBOLen() {
if(!vboReady)
vboify();
return vboLen;
}
}