WEBGL You will need 3 files since this assignment has only 3 questions, you should call each file by its questions number for example question 2 will be called question2.html, and for questions that...

1 answer below »
Webgl assignment


WEBGL You will need 3 files since this assignment has only 3 questions, you should call each file by its questions number for example question 2 will be called question2.html, and for questions that are not a coding question you are allowed to use text file format only .txt Question 01 (Keypress Animation) For this question, you need to create a triangle in the middle of the CANVAS and use arrow keys to rotate this triangle with respect to the y-axis, if the user presses the left arrow the triangle will rotate left if the user presses the right arrow the triangle will rotate in the opposite direction, the animation should not break your rendering under any condition. Triangle and implementation properties: · ●  You need to use the color buffer as explained in class · ●  For rotation, you need to use the Matrix4() class as explained in class · ●  Every vertex of the three of the triangle should have a different color · ●  The vertex array should be a collection of [x, y, R, B, G] for every vertex Question 02 (Random Color Points Animation) In the previous assignment, you added one point to the canvas and moved it randomly between the walls of the canvas, in this question you will need to randomly move two-points in the canvas with the following requirements: · ●  None of the two points will translate outside of the canvas (exactly like before) · ●  Ignore the z transposition · ●  The two points will change their color randomly every 5 seconds Question 03 (3-D cube) In this question, you will create a 3-D cube as shown below, all sides of this cube should be colored differently (anything of your choice) color should be interpolated from the vertices, in order to show the colors of all sides of the cube you should constantly rotate it around the x and y-axis
Answered Same DayNov 18, 2021

Answer To: WEBGL You will need 3 files since this assignment has only 3 questions, you should call each file by...

Sandeep Kumar answered on Nov 25 2021
155 Votes
3.js"use strict";
let cubeRotation = 0.0;
function main() {
/** @type {HTMLCanvasElement} */
const canvas = document.getElementById("canvas");
/** @type {WebGL2RenderingContext} */
const gl = canvas.getContext("webgl");
if (!gl) {
alert("Please use a modern web browser, as your current one does not support WebGL.");
}

gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
const vertexShaderSource = `
attribute vec4 aVertexPosition;
attribute vec3 aVertexNormal;
at
tribute vec2 aTextureCoord;
uniform mat4 uNormalMatrix;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
varying highp vec2 vTextureCoord;
varying highp vec3 vLighting;
void main(void) {
gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
vTextureCoord = aTextureCoord;
// Apply lighting
highp vec3 ambientLight = vec3(0.3, 0.3, 0.3);
highp vec3 directionalLightColor = vec3(1, 1, 1);
highp vec3 directionalVector = normalize(vec3(0.85, 0.8, 0.75));
highp vec4 transformedNormal = uNormalMatrix * vec4(aVertexNormal, 1.0);
highp float directional = max(dot(transformedNormal.xyz, directionalVector), 0.0);
vLighting = ambientLight + (directionalLightColor * directional);
}
`;
const fragmentShaderSource = `
varying highp vec2 vTextureCoord;
varying highp vec3 vLighting;
uniform sampler2D uSampler;

void main(void) {
highp vec4 texelColor = texture2D(uSampler, vTextureCoord);
gl_FragColor = vec4(texelColor.rgb * vLighting, texelColor.a);
}
`;
// Initialize shader program
const shaderProgram = initShaderProgram(gl, vertexShaderSource, fragmentShaderSource);
const programInfo = {
program: shaderProgram,
attribLocations: {
vertexPosition: gl.getAttribLocation(shaderProgram, "aVertexPosition"),
vertexNormal: gl.getAttribLocation(shaderProgram, "aVertexNormal"),
textureCoord: gl.getAttribLocation(shaderProgram, "aTextureCoord"),
},
uniformLocations: {
modelViewMatrix: gl.getUniformLocation(shaderProgram, "uModelViewMatrix"),
projectionMatrix: gl.getUniformLocation(shaderProgram, "uProjectionMatrix"),
normalMatrix: gl.getUniformLocation(shaderProgram, "uNormalMatrix"),
uSampler: gl.getUniformLocation(shaderProgram, "uSampler"),
}
};
const buffers = initBuffers(gl);
const texture = loadTexture(gl, "./assets/wood-texture.jpg");
let then = 0;
// Draw the scene repeatedly to the gl canvas
function render(now) {
now *= 0.001; // convert to seconds
const deltaTime = now - then;
then = now;
drawScene(gl, programInfo, buffers, texture, deltaTime);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
function initBuffers(/** @type {WebGL2RenderingContext} */gl) {
// Create a buffer for the square's positions.
const positionBuffer = gl.createBuffer();
// Select the positionBuffer as the one to apply buffer operations to from here on.
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Now, create an array of positions for the square.
const positions = [
// Front face
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// Back face
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
// Top face
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,

// Bottom face
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,

// Right face
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,

// Left face
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
];
/*
Now, pass the list of positions into WebGL to build
the shape. We do this by creating a Float32Array from the
JS array, then use it to fill the current buffer.
*/
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array(positions),
gl.STATIC_DRAW
);
// Set up the normals for the vertices, so that we can compute lighting.
const normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
const vertexNormals = [
// Front
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
// Back
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
// Top
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
// Bottom
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
// Right
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
// Left
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0
];
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array(vertexNormals),
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here