-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW2D.js
104 lines (84 loc) · 2.59 KB
/
HW2D.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"use strict";
var gl;
var points;
let counter = 0; // Counter to keep track of the current shape
const numShapes = 3; // Total shapes
// Define vertices for different 2D shapes
const triangleVertices = new Float32Array([
0.0, 0.5,
-0.5, -0.5,
0.5, -0.5
]);
const squareVertices = new Float32Array([
-0.5, 0.5,
-0.5, -0.5,
0.5, 0.5,
-0.5, -0.5,
0.5, 0.5,
0.5, -0.5
]);
// For the circle, considering polygon with 360 sides as 1 degree will correpond to 1 segment of polygon
const polygonVertices = [];
const sides = 360;
const radius = 0.5;
for (let i = 0; i < sides; i++) {
const theta = (i / sides) * 2 * Math.PI;
const x = radius * Math.cos(theta);
const y = radius * Math.sin(theta);
polygonVertices.push(x, y);
}
window.onload = function init()
{
var canvas = document.getElementById( "gl-canvas" );
gl = canvas.getContext('webgl2');
if (!gl) { alert( "WebGL 2.0 isn't available" ); }
//
// Configure WebGL
//
gl.viewport( 0, 0, canvas.width, canvas.height );
gl.clearColor( 1.0, 1.0, 1.0, 1.0 );
// Load shaders and initialize attribute buffers
var program = initShaders( gl, "vertex-shader", "fragment-shader" );
gl.useProgram( program );
// Load the data into the GPU
var bufferId = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, bufferId );
// Associate out shader variables with our data buffer
var aPosition = gl.getAttribLocation( program, "aPosition" );
gl.vertexAttribPointer( aPosition, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray( aPosition );
// Initializing the first shape as triangle
updateShape(triangleVertices);
// Handling mouse click events
canvas.addEventListener('click', toggleOnClick);
};
function toggleOnClick() {
counter = (counter + 1) % numShapes;
switch (counter) {
case 0:
updateShape(triangleVertices);
break;
case 1:
updateShape(squareVertices);
break;
case 2:
updateShape(new Float32Array(polygonVertices));
break;
}
}
// Function to update the shape in the WebGL buffer
function updateShape(vertices) {
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
render(vertices);
}
function render(vertices) {
// Clear the canvas
gl.clearColor(1.0, 1.0, 1.0, 1.0);
gl.clear( gl.COLOR_BUFFER_BIT );
// Draw the shape
if (counter === 0 || counter === 1) {
gl.drawArrays(gl.TRIANGLES, 0, vertices.length/2);
} else {
gl.drawArrays(gl.TRIANGLE_FAN, 0, vertices.length/2);
}
}