diff --git a/Pendulum/Pendulum.js b/Pendulum/Pendulum.js index d9971df..4c89a71 100644 --- a/Pendulum/Pendulum.js +++ b/Pendulum/Pendulum.js @@ -32,17 +32,7 @@ var PEN_LENGTH = 0.8; // Rotation angle (degrees/second) var A_VELOCITY = 45.0; -// Common functions: -// acquire_buffer: Get a vertex buffer object from gl -function acquire_buffer(gl) { - // Create a buffer object - var vertexBuffer = gl.createBuffer(); - if (!vertexBuffer) { - console.log('Failed to create the buffer object'); - return -1; - } - return vertexBuffer; -} + function main() { // Retrieve element diff --git a/Pendulum/PendulumComponents.js b/Pendulum/PendulumComponents.js index 779373c..414e753 100644 --- a/Pendulum/PendulumComponents.js +++ b/Pendulum/PendulumComponents.js @@ -1,9 +1,32 @@ // PendulumComponents.js // John Breaux 2022-06-30 -// Classes and data structures for Pendulum.js +// Classes, functions, and data structures for Pendulum.js "use strict" +// Common functions: +// acquire_buffer: Get a vertex buffer object from gl +function acquire_buffer(gl) { + // Create a buffer object + var vertexBuffer = gl.createBuffer(); + if (!vertexBuffer) { + console.log('Failed to create the buffer object'); + return -1; + } + return vertexBuffer; +} + +function acquire_VAO(gl, name, size, type) { + // Acquire a reference to named attr + var ret = gl.getAttribLocation(gl.program, name); + if (ret < 0) { + console.log(`Failed to get the storage location of ${name}`); + return -1; + } + gl.vertexAttribPointer(ret, size, type, false, 0, 0); + return ret; +} + // Classes // homo2: Stores a 2d vector or point in homog. coords class homog2 { @@ -76,17 +99,6 @@ class Anchor { gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); - // Acquire a reference to a_Position - 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); - // Write date into the buffers gl.bufferData(gl.ARRAY_BUFFER, this.vertex_array(), gl.STATIC_DRAW); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.index_array(), gl.STATIC_DRAW); @@ -96,6 +108,12 @@ class Anchor { // Bind the buffers gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); + + // get a_Position + var a_Position = acquire_VAO(gl, 'a_Position', 2, gl.FLOAT); + // Enable the assignment to a_Position variable + gl.enableVertexAttribArray(a_Position); + // Pass the rotation matrix to the vertex shader gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements); // Draw the Anchor @@ -147,25 +165,21 @@ class Polygon { gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); - // Acquire a reference to a_Position - 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); - // Write date into the buffers gl.bufferData(gl.ARRAY_BUFFER, this.vertex_array(), gl.STATIC_DRAW); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.index_array(), gl.STATIC_DRAW); } draw(gl, modelMatrix, u_ModelMatrix) { + // Bind the buffers gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); + + // get a_Position + var a_Position = acquire_VAO(gl, 'a_Position', 2, gl.FLOAT); + // Enable the assignment to a_Position variable + gl.enableVertexAttribArray(a_Position); + // Pass the rotation matrix to the vertex shader gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements); // Draw the shape