Shit WORKS

This commit is contained in:
John 2022-07-01 00:50:21 -05:00
parent 465954d3dd
commit aeff7e396c
2 changed files with 38 additions and 34 deletions

View File

@ -32,17 +32,7 @@ var PEN_LENGTH = 0.8;
// Rotation angle (degrees/second) // Rotation angle (degrees/second)
var A_VELOCITY = 45.0; 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() { function main() {
// Retrieve <canvas> element // Retrieve <canvas> element

View File

@ -1,9 +1,32 @@
// PendulumComponents.js // PendulumComponents.js
// John Breaux 2022-06-30 // John Breaux 2022-06-30
// Classes and data structures for Pendulum.js // Classes, functions, and data structures for Pendulum.js
"use strict" "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 // Classes
// homo2: Stores a 2d vector or point in homog. coords // homo2: Stores a 2d vector or point in homog. coords
class homog2 { class homog2 {
@ -76,17 +99,6 @@ class Anchor {
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); 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 // Write date into the buffers
gl.bufferData(gl.ARRAY_BUFFER, this.vertex_array(), gl.STATIC_DRAW); gl.bufferData(gl.ARRAY_BUFFER, this.vertex_array(), gl.STATIC_DRAW);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.index_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 // Bind the buffers
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); 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 // Pass the rotation matrix to the vertex shader
gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);
// Draw the Anchor // Draw the Anchor
@ -147,25 +165,21 @@ class Polygon {
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); 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 // Write date into the buffers
gl.bufferData(gl.ARRAY_BUFFER, this.vertex_array(), gl.STATIC_DRAW); gl.bufferData(gl.ARRAY_BUFFER, this.vertex_array(), gl.STATIC_DRAW);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.index_array(), gl.STATIC_DRAW); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.index_array(), gl.STATIC_DRAW);
} }
draw(gl, modelMatrix, u_ModelMatrix) { draw(gl, modelMatrix, u_ModelMatrix) {
// Bind the buffers
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); 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 // Pass the rotation matrix to the vertex shader
gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);
// Draw the shape // Draw the shape