Professional Documents
Culture Documents
CS 418: Interactive Computer Graphics Basic Animation: Eric Shaffer
CS 418: Interactive Computer Graphics Basic Animation: Eric Shaffer
Basic Animation
Eric Shaffer
Simple Animation with WebGL
function setupShaders() {
… There are several steps to using
shaderProgram.mvMatrixUniform =
the library and to create a
gl.getUniformLocation(shaderProgram, "uMVMatrix"); matrix to be sent to the shader.
}
1. Include the library in the
function draw() { HTML file using <script> tags
… 2. We create a matrix
mat4.identity(mvMatrix); 3. When we initialize the
mat4.rotateX(mvMatrix, mvMatrix, degToRad(rotAngle));
shader, we get a handle for
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
the Uniform matrix in the
gl.drawArrays(gl.TRIANGLES, 0, vertexPositionBuffer.numberOfItems);
shader
4. In the draw() function, we
}
create a transformation and
use the handle to send it to
the shader
Animation
¤ So, we now need a way to
The number of
function tick() { callbacks is usually
requestAnimFrame(tick); 60 times per
draw(); second, but will
animate();
generally match the
}
display refresh rate.
Animation
function animate() {
With each frame,
we update a
var timeNow = new Date().getTime();
global variable
if (lastTime != 0) { that is used to set
var elapsed = timeNow - lastTime; the rotation matrix
rotAngle= (rotAngle+1.0) % 360; sent to the shader.
}
http://courses.engr.illinois.edu/cs418/inde
x.html
Look for :
HelloAnimation.html
HelloAnimation.js
webgl-utils.js