diff --git a/src/client/main.ts b/src/client/main.ts index 1bd0fae..858d905 100644 --- a/src/client/main.ts +++ b/src/client/main.ts @@ -1,6 +1,8 @@ // @ts-ignore import mat4 from 'gl-mat4'; +let squareRotation = 0.0; + main(); /** @@ -60,7 +62,20 @@ function main() { }; const buffers = initBuffers(gl); - drawScene(gl, programInfo, buffers); + let then = 0; + + /** + * Draws the scene repeatedly + * @param {number} now the current time + */ + function render(now: any) { + now *= 0.001; + const deltaTime = now - then; + then = now; + drawScene(gl, programInfo, buffers, deltaTime); + requestAnimationFrame(render); + } + requestAnimationFrame(render); } /** @@ -160,8 +175,9 @@ function initBuffers(gl: any) { * @param {any} gl the WebGL context * @param {any} programInfo WebGL program information * @param {any} buffers the buffers to draw + * @param {number} deltaTime the difference in time since last call */ -function drawScene(gl: any, programInfo: any, buffers: any) { +function drawScene(gl: any, programInfo: any, buffers: any, deltaTime: number) { gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.clearDepth(1.0); gl.enable(gl.DEPTH_TEST); @@ -201,6 +217,11 @@ function drawScene(gl: any, programInfo: any, buffers: any) { modelViewMatrix, [-0.0, 0.0, -6.0]); + mat4.rotate(modelViewMatrix, + modelViewMatrix, + squareRotation, + [Math.abs(squareRotation % 1.0 - 0.5), 1, 1]); + // Tell WebGL how to pull out the positions from the position // buffer into the vertexPosition attribute. { @@ -258,4 +279,6 @@ function drawScene(gl: any, programInfo: any, buffers: any) { const vertexCount = 4; gl.drawArrays(gl.TRIANGLE_STRIP, offset, vertexCount); } + + squareRotation += deltaTime; }