summaryrefslogtreecommitdiffstats
path: root/blockgame/camera.js
blob: f696441414534151ea8db1850db3f7cf3382788f (plain) (blame)
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
export class Camera {
	constructor(gl) {
		const fieldOfView = 45 * Math.PI / 180;   // in radians
		const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
		const zNear = 0.1;
		const zFar = 100.0;
		const projectionMatrix = mat4.create();
		
		// note: glmatrix.js always has the first argument
		// as the destination to receive the result.
		mat4.perspective(projectionMatrix,
		                 fieldOfView,
		                 aspect,
		                 zNear,
		                 zFar);
		
		// Set the drawing position to the "identity" point, which is
		// the center of the scene.
		const modelViewMatrix = mat4.create();
		
		// Now move the drawing position a bit to where we want to
		// start drawing the square.
		
		mat4.translate(modelViewMatrix,     // destination matrix
		               modelViewMatrix,     // matrix to translate
		               [-0.0, 0.0, -6.0]);  // amount to translate

		this.mvmatrix = modelViewMatrix;
		this.pmatrix = projectionMatrix;
	}

	use(gl, shader) {
		gl.uniformMatrix4fv(
		    shader.uniforms.projectionMatrix,
		    false,
		    this.pmatrix);
		gl.uniformMatrix4fv(
		    shader.uniforms.modelViewMatrix,
		    false,
		    this.mvmatrix);
	}
}