2022-06-22 01:49:01 +00:00
|
|
|
// RotatingTranslatedTriangle.js (c) 2012 matsuda
|
2022-07-01 05:38:54 +00:00
|
|
|
|
|
|
|
// Pendulum.js
|
|
|
|
// John Breaux 2022-06-30
|
|
|
|
// Simulates a pendulum in a frictionless vacuum with no gravity
|
|
|
|
|
2022-06-30 21:10:19 +00:00
|
|
|
"use strict"
|
2022-06-22 01:49:01 +00:00
|
|
|
|
|
|
|
const vertex_shader = `
|
|
|
|
attribute vec4 a_Position;
|
|
|
|
uniform mat4 u_ModelMatrix;
|
2022-06-30 21:10:19 +00:00
|
|
|
|
|
|
|
void main()
|
2022-06-22 01:49:01 +00:00
|
|
|
{
|
|
|
|
gl_Position = u_ModelMatrix * a_Position;
|
2022-07-01 05:38:54 +00:00
|
|
|
gl_PointSize = 5.0;
|
2022-06-22 01:49:01 +00:00
|
|
|
} `;
|
|
|
|
|
|
|
|
|
|
|
|
const fragment_shader = `
|
2022-06-30 21:10:19 +00:00
|
|
|
void main()
|
2022-06-22 01:49:01 +00:00
|
|
|
{
|
|
|
|
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
|
|
|
} `;
|
|
|
|
|
2022-06-30 21:10:19 +00:00
|
|
|
// Point size of the anchor
|
|
|
|
var POINT_SIZE = 5;
|
|
|
|
// Radius of the bob
|
|
|
|
var BOB_RADIUS = 0.1;
|
|
|
|
// length of the wire
|
|
|
|
var PEN_LENGTH = 0.8;
|
2022-06-22 01:49:01 +00:00
|
|
|
// Rotation angle (degrees/second)
|
2022-06-30 21:10:19 +00:00
|
|
|
var A_VELOCITY = 45.0;
|
|
|
|
|
2022-07-01 05:50:21 +00:00
|
|
|
|
2022-06-30 21:10:19 +00:00
|
|
|
|
|
|
|
function main() {
|
2022-06-22 01:49:01 +00:00
|
|
|
// Retrieve <canvas> element
|
|
|
|
var canvas = document.getElementById('webgl');
|
|
|
|
|
|
|
|
// Get the rendering context for WebGL
|
|
|
|
var gl = getWebGLContext(canvas);
|
|
|
|
if (!gl) {
|
|
|
|
console.log('Failed to get the rendering context for WebGL');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize shaders
|
2022-06-30 21:10:19 +00:00
|
|
|
if (!initShaders(gl, vertex_shader, fragment_shader)) {
|
2022-06-22 01:49:01 +00:00
|
|
|
console.log('Failed to intialize shaders.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-30 21:10:19 +00:00
|
|
|
// create a pendulum object
|
2022-07-01 05:38:54 +00:00
|
|
|
var pendulum = new Pendulum({ angle: 0, length: PEN_LENGTH, radius: BOB_RADIUS });
|
|
|
|
init(gl, pendulum);
|
2022-06-22 01:49:01 +00:00
|
|
|
|
|
|
|
// Specify the color for clearing <canvas>
|
|
|
|
gl.clearColor(0, 0, 0, 1);
|
|
|
|
|
|
|
|
// Get storage location of u_ModelMatrix
|
|
|
|
var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix');
|
2022-06-30 21:10:19 +00:00
|
|
|
if (!u_ModelMatrix) {
|
2022-06-22 01:49:01 +00:00
|
|
|
console.log('Failed to get the storage location of u_ModelMatrix');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Model matrix
|
|
|
|
var modelMatrix = new Matrix4();
|
2022-07-01 05:38:54 +00:00
|
|
|
// Set it to the I matrix
|
|
|
|
modelMatrix.setIdentity();
|
2022-06-22 01:49:01 +00:00
|
|
|
|
|
|
|
// Start drawing
|
2022-06-30 21:10:19 +00:00
|
|
|
var tick = function () {
|
2022-07-01 05:38:54 +00:00
|
|
|
// Tick the pendulum
|
|
|
|
pendulum.tick();
|
|
|
|
// Draw the pendulum
|
|
|
|
draw(gl, pendulum, modelMatrix, u_ModelMatrix);
|
|
|
|
// Request that the browser ?calls tick
|
|
|
|
requestAnimationFrame(tick, canvas);
|
2022-06-22 01:49:01 +00:00
|
|
|
};
|
|
|
|
tick();
|
|
|
|
}
|
|
|
|
|
2022-07-01 05:38:54 +00:00
|
|
|
function init(gl, pendulum) {
|
|
|
|
pendulum.init(gl);
|
2022-06-22 01:49:01 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 05:38:54 +00:00
|
|
|
function draw(gl, pendulum, modelMatrix, u_ModelMatrix) {
|
2022-06-22 01:49:01 +00:00
|
|
|
// Clear <canvas>
|
|
|
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
2022-07-01 05:38:54 +00:00
|
|
|
// Draw the pendulum
|
|
|
|
pendulum.draw(gl, modelMatrix, u_ModelMatrix);
|
2022-06-22 01:49:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 21:10:19 +00:00
|
|
|
function up() {
|
|
|
|
A_VELOCITY += 10;
|
2022-06-22 01:49:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 21:10:19 +00:00
|
|
|
function down() {
|
|
|
|
A_VELOCITY -= 10;
|
2022-06-22 01:49:01 +00:00
|
|
|
}
|