Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

* Computer Graphics When the user clicks with the left button, a small pentagon with a random color appears where s / he clicked. Each

*Computer Graphics
When the user clicks with the left button, a small pentagon with a random color appears where s/he clicked. Each click adds a new pentagon, with the previous ones remaining
Why is the pentagon not appearing on the canvas even thought the handleMouseClick function is working
pentagon.js
class Pentagon2D {
static vertexPositions =[];
static vertexColors =[];
static shaderProgram =-1;
static positionBuffer =-1;
static colorBuffer =-1;
static aPositionShader =-1;
static aColorShader =-1;
static initialize(){
// Similar to Ellipse2D.initialize, initialize shader program, buffers, and attribute locations
Pentagon2D.shaderProgram = initShaders(gl,"/vshader.glsl","/fshader.glsl");
// Define vertices for a pentagon and assign random colors
const side =0.1;
const goldenRatio =1.6180339887;
const angleOffset = Math.PI /2;
for (let i =0; i 5; i++){
const angle = angleOffset +(i *2* Math.PI)/5;
const x = Math.cos(angle)* side;
const y = Math.sin(angle)* side;
const red = Math.random();
const green = Math.random();
const blue = Math.random();
Pentagon2D.vertexPositions.push(vec2(x, y));
Pentagon2D.vertexColors.push(vec3(red, green, blue));
}
Pentagon2D.positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, Pentagon2D.positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(Pentagon2D.vertexPositions), gl.STATIC_DRAW);
Pentagon2D.colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, Pentagon2D.colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(Pentagon2D.vertexColors), gl.STATIC_DRAW);
Pentagon2D.aPositionShader = gl.getAttribLocation(Pentagon2D.shaderProgram, "aPosition");
Pentagon2D.aColorShader = gl.getAttribLocation(Pentagon2D.shaderProgram, "aColor");
}
constructor(x, y){
if (Pentagon2D.shaderProgram ===-1) Pentagon2D.initialize();
this.translate(x, y);
}
translate(x, y){
for (let i =0; i Pentagon2D.vertexPositions.length; i++){
const position = Pentagon2D.vertexPositions[i];
position[0]+= x;
position[1]+= y;
}
gl.bindBuffer(gl.ARRAY_BUFFER, Pentagon2D.positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(Pentagon2D.vertexPositions), gl.STATIC_DRAW);
}
draw(){
gl.useProgram(Pentagon2D.shaderProgram);
gl.bindBuffer(gl.ARRAY_BUFFER, Pentagon2D.positionBuffer);
gl.vertexAttribPointer(Pentagon2D.aPositionShader, 2, gl.FLOAT, false, 0,0);
gl.bindBuffer(gl.ARRAY_BUFFER, Pentagon2D.colorBuffer);
gl.vertexAttribPointer(Pentagon2D.aColorShader, 3, gl.FLOAT, false, 0,0);
gl.enableVertexAttribArray(Pentagon2D.aPositionShader);
gl.enableVertexAttribArray(Pentagon2D.aColorShader);
gl.drawArrays(gl.TRIANGLE_FAN, 0, Pentagon2D.vertexPositions.length);
gl.disableVertexAttribArray(Pentagon2D.aPositionShader);
gl.disableVertexAttribArray(Pentagon2D.aColorShader);
}
}
app.js
var canvas;
var gl;
var sq;
var sq2;
var sq3;
var sq4;
var sq5;
var sq6;
var triangle;
var circle;
var ellipse;
var pentagons =[];
window.onload = function init(){
canvas = document.getElementById("gl-canvas");
gl = canvas.getContext('webgl2');
if (!gl){ alert("WebGL 2.0 isn't available"); }
gl.viewport(0,0, canvas.width, canvas.height);
gl.clearColor(0.0,0.0,0.0,1.0);
document.addEventListener('keydown', handleKeyDown);
canvas.addEventListener('click', handleMouseClick);
sq = new Square();
sq2= new Square2();
sq3= new Square3();
sq4= new Square4();
sq5= new Square5();
sq6= new Square6();
triangle = new Triangle2D();
circle = new Circle2D();
ellipse = new Ellipse2D();
// Render the initial scene
render();
};
function render(){
gl.clear(gl.COLOR_BUFFER_BIT);
sq.draw();
sq2.draw();
sq3.draw();
sq4.draw();
sq5.draw();
sq6.draw();
triangle.draw();
circle.draw();
ellipse.draw();
}
function handleMouseClick(event){
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = canvas.height -(event.clientY - rect.top);
console.log('Mouse click at:', x, y);
// Create a new pentagon with a random color at the clicked position
const newPentagon = new Pentagon2D(x / canvas.width *2-1, y / canvas.height *2-1);
pentagons.push(newPentagon);
render();
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

3. State the clique-number for the following graph G

Answered: 1 week ago

Question

u = 5 j , v = 6 i Find the angle between the vectors.

Answered: 1 week ago

Question

How can we visually describe our goals?

Answered: 1 week ago

Question

What metaphors might describe how we work together?

Answered: 1 week ago

Question

What are some of the possible scenes from our future?

Answered: 1 week ago