-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
375 lines (290 loc) · 12.8 KB
/
app.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
glMatrix = require('gl-matrix');
objLoader = require('webgl-obj-loader');
Promise.all([
fetch('vertex.glsl').then(function (response) {
return response.text();
}),
fetch('fragment.glsl').then(function (response) {
return response.text();
}),
fetch('post_vertex.glsl').then(function (response) {
return response.text();
}),
fetch('post_fragment.glsl').then(function (response) {
return response.text();
}),
fetch('dragon.obj').then(function (response) {
return response.text();
}),
new Promise(function (resolve) {
var image = new Image();
image.onload = () => {
resolve(image);
};
image.src = 'dragon_texture.png';
})
]).then(main);
function main(files) {
const canvas = document.getElementById('webgl-canvas');
const gl = canvas.getContext('webgl2');
gl.clearColor(1.0, 1.0, 1.0, 1.0);
gl.clearDepth(1.0);
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.CULL_FACE);
gl.depthFunc(gl.LEQUAL);
const shaderProgram = initShaderProgram(gl, files[0], files[1]);
const programInfo = {
program: shaderProgram,
attribLocations: {
vertexPosition: 0,
texCoord: 1,
normal: 2,
},
uniformLocations: {
modelViewMatrix: gl.getUniformLocation(shaderProgram, 'uModelViewMatrix'),
projectionMatrix: gl.getUniformLocation(shaderProgram, 'uProjectionMatrix'),
lightDirection: gl.getUniformLocation(shaderProgram, 'uLightDirection'),
smoothFactor: gl.getUniformLocation(shaderProgram, 'uSmoothFactor'),
fixedLighting: gl.getUniformLocation(shaderProgram, 'uFixedLighting'),
},
mesh: new objLoader.Mesh(files[4]),
texture: files[5],
}
const buffers = initBuffers(gl, programInfo);
const postShaderProgram = initShaderProgram(gl, files[2], files[3]);
const postProgramInfo = {
program: postShaderProgram,
attribLocations: {
vertexPosition: 0,
texCoord: 1,
},
uniformLocations: {
canvasWidth: gl.getUniformLocation(postShaderProgram, 'uCanvasWidth'),
canvasHeight: gl.getUniformLocation(postShaderProgram, 'uCanvasHeight'),
colorTexture: gl.getUniformLocation(postShaderProgram, 'sColorTexture'),
depthTexture: gl.getUniformLocation(postShaderProgram, 'sDepthTexture'),
},
}
const postBuffers = initPostBuffers(gl);
const textures = loadTextures(gl, programInfo);
drawScene(gl, programInfo, buffers, postProgramInfo, postBuffers, textures);
}
function initShaderProgram(gl, vertexSource, fragmentSource) {
const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vertexSource);
const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fragmentSource);
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
console.log('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram));
return null;
}
return shaderProgram;
}
function loadShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.log('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
function initBuffers(gl, programInfo) {
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(programInfo.mesh.vertices), gl.STATIC_DRAW);
const texCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(programInfo.mesh.textures), gl.STATIC_DRAW);
const normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(programInfo.mesh.vertexNormals), gl.STATIC_DRAW);
const indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(programInfo.mesh.indices), gl.STATIC_DRAW);
return {
vertexBuffer: vertexBuffer,
texCoordBuffer: texCoordBuffer,
normalBuffer: normalBuffer,
indexBuffer: indexBuffer,
};
}
function initPostBuffers(gl) {
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
const vertices = [
-1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
1.0, -1.0, 0.0,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
const texCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
const texCoords = [
0, 1,
0, 0,
1, 1,
1, 0,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(texCoords), gl.STATIC_DRAW);
const indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([0, 1, 2, 3]), gl.STATIC_DRAW);
return {
vertexBuffer: vertexBuffer,
texCoordBuffer: texCoordBuffer,
indexBuffer: indexBuffer,
};
}
function loadTextures(gl, programInfo) {
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA8, 512, 512);
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 512, 512, gl.RGBA, gl.UNSIGNED_BYTE, programInfo.texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
const framebufferTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, framebufferTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 128, 128, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
const depthTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT16, 128, 128, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_SHORT, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
const framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, framebufferTexture, 0);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, depthTexture, 0);
return {
texture: texture,
framebuffer: framebuffer,
framebufferTexture: framebufferTexture,
depthTexture: depthTexture,
};
}
function drawScene(gl, programInfo, buffers, postProgramInfo, postBuffers, textures) {
const projectionMatrix = glMatrix.mat4.create();
const fieldOfView = 45 * Math.PI / 180;
const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
glMatrix.mat4.perspective(projectionMatrix, fieldOfView, aspect, 0.1, 10.0);
window.modelViewMatrix = glMatrix.mat4.create();
glMatrix.mat4.translate(window.modelViewMatrix, window.modelViewMatrix, [0.0, -1.0, -3.5]);
window.then = 0;
drawFrame = (time) => {
// convert time to seconds
time *= 0.001;
const deltaTime = time - window.then;
if (deltaTime < 1.0 / 10.0) {
requestAnimationFrame(drawFrame);
return;
}
window.then = time;
gl.useProgram(programInfo.program);
gl.bindFramebuffer(gl.FRAMEBUFFER, textures.framebuffer);
gl.viewport(0, 0, 128, 128);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
const rotateSpeed = document.getElementById('rotate-speed').value;
glMatrix.mat4.rotate(window.modelViewMatrix, window.modelViewMatrix, rotateSpeed * deltaTime, [0.0, 1.0, 0.0]);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, textures.texture);
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.vertexBuffer);
gl.vertexAttribPointer(
programInfo.attribLocations.vertexPosition,
3,
gl.FLOAT,
false,
0,
0
);
gl.enableVertexAttribArray(programInfo.attribLocations.vertexPosition);
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.texCoordBuffer);
gl.vertexAttribPointer(
programInfo.attribLocations.texCoord,
2,
gl.FLOAT,
false,
0,
0
);
gl.enableVertexAttribArray(programInfo.attribLocations.texCoord);
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.normalBuffer);
gl.vertexAttribPointer(
programInfo.attribLocations.normal,
3,
gl.FLOAT,
false,
0,
0
)
gl.enableVertexAttribArray(programInfo.attribLocations.normal);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers.indexBuffer);
gl.uniformMatrix4fv(programInfo.uniformLocations.modelViewMatrix, false, window.modelViewMatrix);
gl.uniformMatrix4fv(programInfo.uniformLocations.projectionMatrix, false, projectionMatrix);
const xAmount = -document.getElementById('light-direction-x').value;
const yAmount = -document.getElementById('light-direction-y').value;
gl.uniform3fv(programInfo.uniformLocations.lightDirection, new Float32Array([xAmount, yAmount, 2]));
const smoothFactor = document.getElementById('smooth-factor').value;
gl.uniform1f(programInfo.uniformLocations.smoothFactor, smoothFactor);
const fixedLight = document.getElementById('fixed-light').checked;
gl.uniform1i(programInfo.uniformLocations.fixedLighting, fixedLight);
gl.drawElements(gl.TRIANGLES, programInfo.mesh.indices.length, gl.UNSIGNED_SHORT, 0);
gl.disableVertexAttribArray(programInfo.attribLocations.vertexPosition);
gl.disableVertexAttribArray(programInfo.attribLocations.texCoord);
gl.disableVertexAttribArray(programInfo.attribLocations.normal);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.useProgram(postProgramInfo.program);
gl.viewport(0, 0, gl.canvas.clientWidth, gl.canvas.clientHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.uniform1i(postProgramInfo.uniformLocations.colorTexture, 0);
gl.uniform1i(postProgramInfo.uniformLocations.depthTexture, 1);
gl.uniform1f(postProgramInfo.uniformLocations.canvasWidth, 128.0);
gl.uniform1f(postProgramInfo.uniformLocations.canvasHeight, 128.0);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, textures.framebufferTexture);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, textures.depthTexture);
gl.bindBuffer(gl.ARRAY_BUFFER, postBuffers.vertexBuffer);
gl.vertexAttribPointer(
postProgramInfo.attribLocations.vertexPosition,
3,
gl.FLOAT,
false,
0,
0
);
gl.enableVertexAttribArray(postProgramInfo.attribLocations.vertexPosition);
gl.bindBuffer(gl.ARRAY_BUFFER, postBuffers.texCoordBuffer);
gl.vertexAttribPointer(
postProgramInfo.attribLocations.texCoord,
2,
gl.FLOAT,
false,
0,
0
);
gl.enableVertexAttribArray(postProgramInfo.attribLocations.texCoord);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, postBuffers.indexBuffer);
gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_SHORT, 0);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.disableVertexAttribArray(postProgramInfo.attribLocations.vertexPosition);
gl.disableVertexAttribArray(postProgramInfo.attribLocations.texCoord);
requestAnimationFrame(drawFrame);
};
requestAnimationFrame(drawFrame);
}