diff options
author | Jon Santmyer <jon@jonsantmyer.com> | 2021-04-25 02:46:43 +0000 |
---|---|---|
committer | Jon Santmyer <jon@jonsantmyer.com> | 2021-04-25 02:46:43 +0000 |
commit | 9acfb6bfffc31e39dfb273f870a2235e5968ffc9 (patch) | |
tree | 150d4426c9a059c7224b5d73ea57cce54fee28fe /blockgame/camera.js | |
parent | a5f4d66313bb1a455bbc344a671dd16d315a0fdc (diff) | |
download | website-9acfb6bfffc31e39dfb273f870a2235e5968ffc9.tar.gz website-9acfb6bfffc31e39dfb273f870a2235e5968ffc9.tar.bz2 website-9acfb6bfffc31e39dfb273f870a2235e5968ffc9.zip |
add 2D rendering to blockgame
Diffstat (limited to 'blockgame/camera.js')
-rw-r--r-- | blockgame/camera.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/blockgame/camera.js b/blockgame/camera.js new file mode 100644 index 0000000..f696441 --- /dev/null +++ b/blockgame/camera.js @@ -0,0 +1,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); + } +} |