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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/* globals window document mat4 */
const internals = {
// Constants
kEntrypointElementId: 'entrypoint', // The id of the canvas element where we will render
kWidthRatio: 2.76,
kHeightRatio: 1,
kTargetVerticalResolution: 32 + Math.round(Math.random() * 1024),
kFieldOfView: 45,
kNearLimit: 0.1,
kFarLimit: 100,
kBackgroundColor: [0.811, 0.73, 0.48, 1.0],
kRotationValues: [Math.random(), Math.random(), Math.random()],
kFPS: 30,
// Constants: Shaders
kVertexShader: `
attribute vec4 aVertexPosition;
attribute vec4 aColor;
varying vec4 vColor;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
void main() {
gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
vColor = aColor;
}
`,
kFragmentShader: `
precision mediump float;
varying vec4 vColor;
void main() {
gl_FragColor = vColor;
}
`,
// Properties: Animation
currentTime: 0,
// Runs on load, Initializes WebGL and starts the animation loop.
onLoad() {
const canvas = internals.initializeCanvas();
const { gl, programInfo, buffers } = internals.initializeWebGL(canvas);
window.requestAnimationFrame(internals.animate.bind(this, gl, programInfo, buffers));
},
// Gets a canvas element and sets up resizing events.
initializeCanvas() {
const canvas = document.getElementById(internals.kEntrypointElementId);
internals.resizeCanvas(canvas);
window.addEventListener('resize', () => internals.resizeCanvas(canvas));
return canvas;
},
// Given a canvas, it will initialize the webgl context and set up the shaders.
initializeWebGL(canvas) {
const gl = canvas.getContext('webgl', {
preserveDrawingBuffer: true
});
const shaderProgram = internals.initializeShaderProgram(gl, internals.kVertexShader, internals.kFragmentShader);
const programInfo = {
program: shaderProgram,
attribLocations: {
vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition'),
vertexColor: gl.getAttribLocation(shaderProgram, 'aColor')
},
uniformLocations: {
projectionMatrix: gl.getUniformLocation(shaderProgram, 'uProjectionMatrix'),
modelViewMatrix: gl.getUniformLocation(shaderProgram, 'uModelViewMatrix')
}
};
const buffers = internals.initializeBuffers(gl);
return { gl, programInfo, buffers };
},
// Methods: Canvas
resizeCanvas(canvas) {
let width = window.innerWidth;
let height = Math.round(width * internals.kHeightRatio / internals.kWidthRatio);
if (window.innerHeight < height) {
height = window.innerHeight;
width = Math.round(height * internals.kWidthRatio / internals.kHeightRatio);
console.log(width, height);
}
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
canvas.width = internals.kTargetVerticalResolution * internals.kWidthRatio;
canvas.height = internals.kTargetVerticalResolution;
},
// Methods: Shader / GL Related
initializeShaderProgram(gl, vertexShaderSource, fragmentShaderSource) {
const vertexShader = internals.loadShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
const fragmentShader = internals.loadShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
console.error(`Unable to initialize shader program: ${gl.getProgramInfoLog(shaderProgram)}`);
return null;
}
return shaderProgram;
},
loadShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error(`Unable to compile shader: ${gl.getShaderInfoLog(shader)}`);
gl.deleteShader(shader);
return null;
}
return shader;
},
initializeBuffers(gl) {
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
const positions = [
-1.0, 1.0, 1.0, // Front-top-left
1.0, 1.0, 1.0, // Front-top-right
-1.0, -1.0, 1.0, // Front-bottom-left
1.0, -1.0, 1.0, // Front-bottom-right
1.0, -1.0, -1.0, // Back-bottom-right
1.0, 1.0, 1.0, // Front-top-right
1.0, 1.0, -1.0, // Back-top-right
-1.0, 1.0, 1.0, // Front-top-left
-1.0, 1.0, -1.0, // Back-top-left
-1.0, -1.0, 1.0, // Front-bottom-left
-1.0, -1.0, -1.0, // Back-bottom-left
1.0, -1.0, -1.0, // Back-bottom-right
-1.0, 1.0, -1.0, // Back-top-left
1.0, 1.0, -1.0 // Back-top-right
];
gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(positions),
gl.STATIC_DRAW);
return {
position: positionBuffer
};
},
// Methods: Utility
createMatrix() {
const matrix = mat4.create();
mat4.translate(
matrix,
matrix,
[-0.0, 0.0, -6.0]
);
return matrix;
},
// Methods: Animation
rotate(matrix) {
mat4.rotate(internals.modelViewMatrix, internals.modelViewMatrix, internals.kRotationValues[0], [1, 0, 0]);
mat4.rotate(internals.modelViewMatrix, internals.modelViewMatrix, internals.kRotationValues[1], [0, 1, 0]);
mat4.rotate(internals.modelViewMatrix, internals.modelViewMatrix, internals.kRotationValues[2], [0, 0, 1]);
},
drawScene(gl, programInfo, buffers) {
gl.clearColor(...internals.kBackgroundColor);
gl.clearDepth(1.0);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
const fieldOfView = internals.kFieldOfView * Math.PI / 180;
const aspectRatio = internals.kWidthRatio / internals.kHeightRatio;
const projectionMatrix = mat4.create();
mat4.perspective(
projectionMatrix,
fieldOfView,
aspectRatio,
internals.kNearLimit,
internals.kFarLimit
);
internals.modelViewMatrix = internals.modelViewMatrix || internals.createMatrix();
internals.rotate(internals.modelViewMatrix);
{
const numberOfComponents = 3;
const type = gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.position);
gl.vertexAttribPointer(
programInfo.attribLocations.vertexPosition,
numberOfComponents,
type,
normalize,
stride,
offset
);
gl.enableVertexAttribArray(programInfo.attribLocations.vertexPosition);
}
{
// ATTEMPT TO SEND COLOR DATA
const colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
const colors = [
1.0, 0.0, 0.5, 1.0, // Front-top-left
1.0, 0.3, 0.8, 1.0, // Front-top-right
1.0, 1.0, 0.7, 1.0, // Front-bottom-left
0.0, 0.3, 0.5, 1.0, // Front-bottom-right
0.0, 0.3, 0.5, 1.0, // Back-bottom-right
1.0, 0.3, 0.8, 1.0, // Front-top-right
1.0, 0.3, 0.8, 1.0, // Back-top-right
1.0, 0.0, 0.5, 1.0, // Front-top-left
1.0, 0.0, 0.5, 1.0, // Back-top-left
1.0, 1.0, 0.7, 1.0, // Front-bottom-left
1.0, 1.0, 0.7, 1.0, // Back-bottom-left
0.0, 0.3, 0.5, 1.0, // Back-bottom-right
1.0, 0.0, 0.5, 1.0, // Back-top-left
1.0, 0.3, 0.8, 1.0 // Back-top-right
];
gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(colors),
gl.STATIC_DRAW);
const numberOfComponents = 4;
const type = gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.vertexAttribPointer(
programInfo.attribLocations.vertexColor,
numberOfComponents,
type,
normalize,
stride,
offset
);
gl.enableVertexAttribArray(programInfo.attribLocations.vertexColor);
// END ATTEMPT
}
gl.useProgram(programInfo.program);
gl.uniformMatrix4fv(
programInfo.uniformLocations.projectionMatrix,
false,
projectionMatrix
);
gl.uniformMatrix4fv(
programInfo.uniformLocations.modelViewMatrix,
false,
internals.modelViewMatrix
);
{
const offset = 0;
const vertexCount = 14;
gl.drawArrays(gl.TRIANGLE_STRIP, offset, vertexCount);
}
},
animate(gl, programInfo, buffers, time) {
const delta = time - internals.currentTime;
const interval = 1000 / internals.kFPS;
if (delta >= interval) {
internals.drawScene(gl, programInfo, buffers, delta);
internals.currentTime = time;
}
window.requestAnimationFrame(internals.animate.bind(this, gl, programInfo, buffers));
}
};
window.addEventListener('load', internals.onLoad);
|