-
Notifications
You must be signed in to change notification settings - Fork 1
/
Renderer.js
43 lines (37 loc) · 1.33 KB
/
Renderer.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
class Renderer {
constructor(canvas) {
this.canvas = canvas;
this.projectionMatrix = GLMath.matrix4Identity();
this.setupWebGLContext();
this.resizeViewport();
}
setupWebGLContext() {
this.gl = this.canvas.getContext('webgl') || this.canvas.getContext('experimental-webgl');
if (!this.gl) {
throw new Error("WebGL must be supported in order to run this game!");
}
}
resizeViewport() {
this.gl.viewport(0, 0, this.canvas.width, this.canvas.height);
this.gl.colorMask(true, true, true, true);
this.projectionMatrix = GLMath.projectionMatrix(30, 0.1, 1000, this.canvas.width / this.canvas.height);
// this.projectionMatrix = GLMath.orthographicMatrix(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
};
prepareRendering() {
this.gl.clearColor(1.0, 1.0, 1.0, 1.0);
this.gl.clearDepth(1.0);
this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
this.gl.enable(this.gl.DEPTH_TEST);
this.gl.depthFunc(this.gl.LESS);
}
render(models, program, camera, light) {
program.bind(() => {
models[0].geometry.bind(() => {
models.forEach(model => {
program.updateUniforms(this, camera, model, light);
this.gl.drawElements(this.gl.TRIANGLES, model.geometry.indicesBuffer.itemCount, this.gl.UNSIGNED_BYTE, 0);
});
});
});
};
}