From f251b3df76870c171ed32384c156fe29e9745b9e Mon Sep 17 00:00:00 2001 From: John Breaux Date: Tue, 21 Jun 2022 20:49:01 -0500 Subject: [PATCH] Add Program 3: Pendulum and problem statement for Program 2 --- Pendulum/Pendulum.html | 22 ++++++ Pendulum/Pendulum.js | 149 +++++++++++++++++++++++++++++++++++++++++ README.md | 40 ++++++++++- 3 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 Pendulum/Pendulum.html create mode 100644 Pendulum/Pendulum.js diff --git a/Pendulum/Pendulum.html b/Pendulum/Pendulum.html new file mode 100644 index 0000000..792909c --- /dev/null +++ b/Pendulum/Pendulum.html @@ -0,0 +1,22 @@ + + + + + Continually Rotate A Triangle (Button) + + + + + Please use a browser that supports "canvas" + +

+ + +

+ + + + + + + diff --git a/Pendulum/Pendulum.js b/Pendulum/Pendulum.js new file mode 100644 index 0000000..627d111 --- /dev/null +++ b/Pendulum/Pendulum.js @@ -0,0 +1,149 @@ +// RotatingTranslatedTriangle.js (c) 2012 matsuda + +const vertex_shader = ` + attribute vec4 a_Position; + uniform mat4 u_ModelMatrix; + + void main() + { + gl_Position = u_ModelMatrix * a_Position; + } `; + + +const fragment_shader = ` + void main() + { + gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); + } `; + +// Rotation angle (degrees/second) +var ANGLE_STEP = 45.0; + +function main() +{ + // Retrieve 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; + } + + // Write the positions of vertices to a vertex shader + var n = initVertexBuffers(gl); + if (n < 0) + { + console.log('Failed to set the positions of the vertices'); + return; + } + + // Specify the color for clearing + 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; + } + + // Current rotation angle + var currentAngle = 0.0; + + // Model matrix + var modelMatrix = new Matrix4(); + + // Start drawing + var tick = function() + { + currentAngle = animate(currentAngle); // Update the rotation angle + draw(gl, n, currentAngle, modelMatrix, u_ModelMatrix); // Draw the triangle + requestAnimationFrame(tick, canvas); // Request that the browser ?calls tick + }; + tick(); +} + +function initVertexBuffers(gl) +{ + var vertices = new Float32Array ([ + 0, 0.5, -0.5, -0.5, 0.5, -0.5 + ]); + var n = 3; // The number of vertices + + // Create a buffer object + var vertexBuffer = gl.createBuffer(); + if (!vertexBuffer) + { + console.log('Failed to create the buffer object'); + return -1; + } + + // Bind the buffer object to target + gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); + + // Write date into the buffer object + gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); + + // Assign the buffer object to a_Position variable + var a_Position = gl.getAttribLocation(gl.program, 'a_Position'); + if(a_Position < 0) + { + console.log('Failed to get the storage location of a_Position'); + return -1; + } + gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); + + // Enable the assignment to a_Position variable + gl.enableVertexAttribArray(a_Position); + + return n; +} + +function draw(gl, n, currentAngle, modelMatrix, u_ModelMatrix) +{ + // Set the rotation matrix + modelMatrix.setRotate(currentAngle, 0, 0, 1); + modelMatrix.translate(0.35, 0, 0); + + // Pass the rotation matrix to the vertex shader + gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements); + + // Clear + gl.clear(gl.COLOR_BUFFER_BIT); + + // Draw the rectangle + gl.drawArrays(gl.TRIANGLES, 0, n); +} + +// Last time that this function was called +var g_last = Date.now(); +function animate(angle) +{ + // Calculate the elapsed time + var now = Date.now(); + var elapsed = now - g_last; + g_last = now; + // Update the current rotation angle (adjusted by the elapsed time) + var newAngle = angle + (ANGLE_STEP * elapsed) / 1000.0; + return newAngle %= 360; +} + +function up() +{ + ANGLE_STEP += 10; +} + +function down() +{ + ANGLE_STEP -= 10; +} diff --git a/README.md b/README.md index 371f1cf..c2c4f69 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,12 @@ # CSCE 4230 Computer Graphics -## Homework 1 -### Problem Statement +Dr. Doran's in-person hour long commute. + +# Program 1 + +Warm Up + +## Problem Statement Modify the ClickedPoints example as follow: 1. Change the canvas size from 400x400 to 600x400. @@ -15,3 +20,34 @@ Modify the ClickedPoints example as follow: Submit both your HTML and JS on Canvas. Please do NOT put them in Zip archives or similar. This shouldn't take very long at all to do, and is really mainly a warm-up exercise. + +# Program 2 + +Play the Chaos Game + +### Problem Statement: + +Write a WebGL program that implements Michael Barnsley's 'chaos game'. See this link (Links to an external site.) for more information. + +Submit your HTML and js files on Canvas + + +# Program 3 + +Make a rotating Pendulum + +### Problem Statement: + +Write a WebGL program that displays a rotating pendulum. + +The pendulum bob is free to rotate through 360 degrees about an anchor point at the center of the canvas. The pendulum has the following three components: + +1) The anchor point is a green square centered at the origin (0,0), with point size = 5 pixels. + +2) The bob is a blue hexagon of radius r=0.1. Render this with a triangle fan centered at the origin (along with a ModelView matrix that translates and rotates). + +3) The bob is attached to the anchor point by a rigid red wire of length l=0.8. + +Use global variables for the point size of the anchor, the radius of the bob, the length of the wire, and the angular velocity of rotation in degrees per second. Set the initial angular velocity to 45 (degrees per second) and allow an interactive user to increase or decrease the value in multiples of 10 degrees per second with button presses. + +Submit your HTML and JS files on Canvas \ No newline at end of file