/
gl-lib.js
237 lines (194 loc) · 5.67 KB
/
gl-lib.js
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
'use strict';
function initPrograms(gl) {
const vsSourceFaces = `
attribute vec4 aPosition;
attribute vec4 aColor;
uniform mat4 uMatrix;
varying vec4 vColor;
void main() {
vColor = aColor;
gl_Position = uMatrix * aPosition;
}
`;
const fsSourceFaces = `
varying highp vec4 vColor;
void main() {
gl_FragColor = vColor;
}
`;
const vsSourceLines = `
attribute vec4 aPosition;
uniform mat4 uMatrix;
void main() {
gl_Position = uMatrix * aPosition;
}
`;
const fsSourceLines = `
void main() {
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
`;
return {
faces: loadProgram(gl, vsSourceFaces, fsSourceFaces),
lines: loadProgram(gl, vsSourceLines, fsSourceLines),
};
}
function loadProgram(gl, vsSource, fsSource) {
const vs = loadShader(gl, gl.VERTEX_SHADER, vsSource);
const fs = loadShader(gl, gl.FRAGMENT_SHADER, fsSource);
const program = gl.createProgram();
gl.attachShader(program, vs);
gl.attachShader(program, fs);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
alert('Unable to initialize the shader program: ' + gl.getProgramInfoLog(program));
return null;
}
return program;
}
function loadShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
function initBuffers(gl) {
const widthBlocks = 66;
const lengthBlocks = 66;
const verticesPerRow = widthBlocks * 4;
const dimensions = {
widthBlocks,
lengthBlocks,
verticesPerRow,
totalVertices: verticesPerRow * lengthBlocks,
widthElements: (widthBlocks - 1) * 24,
};
const element = initElementBuffer(gl, dimensions);
return {
dimensions,
element,
position: initPositionBuffer(gl, dimensions),
color: initColorBuffer(gl, dimensions),
lineElement: initLineElementBuffer(gl, dimensions, element.elements),
};
}
function heightAt(a, b) {
return noise.perlin2(a / 25, b / 25) * 50;
}
function initPositionBuffer(gl, dimensions) {
const positionBuffer = gl.createBuffer();
const positions = [];
noise.seed(0.123);
for (let z = 0; z < dimensions.lengthBlocks; z++) {
const z1 = z + 0.5;
for (let x = 0; x < dimensions.widthBlocks; x++) {
const x1 = x + 0.5;
positions.push(
x, heightAt(x, z), z,
x1, heightAt(x1, z), z,
x, heightAt(x, z1), z1,
x1, heightAt(x1, z1), z1
);
}
}
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array(positions),
gl.STATIC_DRAW
);
return positionBuffer;
}
function initColorBuffer(gl, dimensions) {
const colorBuffer = gl.createBuffer();
const colors = [];
for (let i = 0; i < dimensions.totalVertices; i++) {
colors.push(
130, 224, 30
);
}
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(
gl.ARRAY_BUFFER,
new Uint8Array(colors),
gl.STATIC_DRAW
);
return colorBuffer;
}
function initElementBuffer(gl, dimensions) {
const elementBuffer = gl.createBuffer();
const elements = [];
for (let z = 0; z < dimensions.lengthBlocks - 1; z++) {
for (let x = 0; x < dimensions.widthBlocks - 1; x++) {
bufferElements(elements, dimensions, x, z);
}
}
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer);
gl.bufferData(
gl.ELEMENT_ARRAY_BUFFER,
new Uint16Array(elements),
gl.STATIC_DRAW
);
return {
buffer: elementBuffer,
length: elements.length,
elements,
};
}
function bufferElements(elements, dimensions, x, z) {
const i = z * dimensions.verticesPerRow + x * 4;
const ip1 = i + 1;
const ip2 = i + 2;
const ip3 = i + 3;
const ip4 = i + 4;
const ip6 = i + 6;
const j = i + dimensions.verticesPerRow;
const jp1 = j + 1;
const jp4 = j + 4;
elements.push(
ip3, ip1, i,
ip3, ip4, ip1,
ip3, ip6, ip4,
ip3, jp4, ip6,
ip3, jp1, jp4,
ip3, j, jp1,
ip3, ip2, j,
ip3, i, ip2
);
}
function initLineElementBuffer(gl, dimensions, triangleElements) {
const lineElementBuffer = gl.createBuffer();
const elements = [];
for (let z = 0; z < dimensions.lengthBlocks - 1; z += 2) {
for (let x = 0; x < dimensions.widthBlocks - 1; x += 2) {
bufferLineElements(elements, dimensions, triangleElements, x, z);
}
}
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, lineElementBuffer);
gl.bufferData(
gl.ELEMENT_ARRAY_BUFFER,
new Uint16Array(elements),
gl.STATIC_DRAW
);
return {
buffer: lineElementBuffer,
length: elements.length,
};
}
function bufferLineElements(elements, dimensions, triangleElements, x, z) {
const start = z * dimensions.widthElements + x * 24;
const end = start + 24;
for (let i = start; i < end; i += 3) {
const ip1 = i + 1;
const ip2 = i + 2;
elements.push(
triangleElements[i], triangleElements[ip1],
triangleElements[ip1], triangleElements[ip2]
);
}
}