4230-hw-1/Pendulum/Pendulum.js

92 lines
2.0 KiB
JavaScript
Raw Normal View History

// 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-07-01 08:49:08 +00:00
"use strict";
const vertex_shader = `
2022-07-01 08:45:53 +00:00
attribute vec4 a_Position;
uniform mat4 u_ModelMatrix;
2022-07-01 08:45:53 +00:00
void main()
{
gl_Position = u_ModelMatrix * a_Position;
gl_PointSize = 5.0;
} `;
const fragment_shader = `
2022-07-01 08:45:53 +00:00
precision mediump float;
uniform vec4 u_Color;
void main()
{
gl_FragColor = u_Color;
} `;
2022-07-01 05:50:21 +00:00
2022-07-01 08:45:53 +00:00
var SIDES_DELTA = 0;
function main() {
2022-07-01 08:45:53 +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
if (!initShaders(gl, vertex_shader, fragment_shader)) {
console.log('Failed to intialize shaders.');
return;
}
// create a pendulum object
var pendulum = new Pendulum({ angle: 0, length: PEN_LENGTH, radius: BOB_RADIUS });
pendulum.init(gl);
// 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');
if (!u_ModelMatrix) {
console.log('Failed to get the storage location of u_ModelMatrix');
return;
}
// Model matrix
var modelMatrix = new Matrix4();
// Set it to the I matrix
modelMatrix.setIdentity();
// Start drawing
var tick = function () {
// Tick the pendulum
pendulum.tick();
// Draw the pendulum
draw(gl, pendulum, modelMatrix, u_ModelMatrix);
// Request that the browser ?calls tick
requestAnimationFrame(tick, canvas);
};
tick();
}
2022-07-01 05:38:54 +00:00
function draw(gl, pendulum, modelMatrix, u_ModelMatrix) {
2022-07-01 08:45:53 +00:00
// Clear <canvas>
gl.clear(gl.COLOR_BUFFER_BIT);
// Draw the pendulum
pendulum.draw(gl, modelMatrix, u_ModelMatrix);
}
function up() {
2022-07-01 08:45:53 +00:00
A_VELOCITY += 10;
}
function down() {
2022-07-01 08:45:53 +00:00
A_VELOCITY -= 10;
}