Question: please in full webgl code. A. In WebGL, You will create your initials with triangles (3 Letters, SBT) and display them equally spaced across the
please in full webgl code.
A. In WebGL, You will create your initials with triangles (3 Letters, SBT) and display them equally spaced across the screen B. Each letter will have its own color. C. You do NOT have to use transforms for this lab. D. The letters do not have to be 3D.
here is a sample code. please help:
const gl = document.getElementById("glcanvas").getContext("webgl");
// Define vertex shader
const vertexShaderSource = `
attribute vec4 aPosition;
void main() {
gl_Position = aPosition;
}
`;
// Define fragment shader
const fragmentShaderSource = `
precision mediump float;
uniform vec4 uColor;
void main() {
gl_FragColor = uColor;
}
`;
// Compile and link the shaders
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vertexShaderSource);
gl.compileShader(vertexShader);
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fragmentShaderSource);
gl.compileShader(fragmentShader);
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
gl.useProgram(program);
// Define vertices and colors for each letter
const vertices = [
// S
-0.5, 0.5, 0.0,
-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.5, 0.5, 0.0,
-0.5, 0.5, 0.0,
// B
-0.8, 0.5, 0.0,
-0.8, -0.5, 0.0,
-0.2, -0.5, 0.0,
-0.2, -0.5, 0.0,
-0.2, 0.5, 0.0,
-0.8, 0.5, 0.0,
// T
0.2, 0.5, 0.0,
-0.2, 0.5, 0.0,
0.0, -0.5, 0.0,
];
const colors = [
// S is red
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
// B is green
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
// T is blue
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
];
// Create buffer and bind to the appropriate attribute
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
const colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
const aPosition = gl.getAttribLocation(program, "aPosition");
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.vertexAttribPointer(aPosition, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(aPosition);
const uColor = gl.getUniformLocation(program, "uColor");
// Draw the letters
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
// S
gl.uniform4f(uColor, 1.0, 0.0, 0.0, 1.0);
gl.drawArrays(gl.TRIANGLES, 0, 6);
// B
gl.uniform4f(uColor, 0.0, 1.0, 0.0, 1.0);
gl.drawArrays(gl.TRIANGLES, 6, 6);
// T
gl.uniform4f(uColor, 0.0, 0.0, 1.0, 1.0);
gl.drawArrays(gl.TRIANGLES, 12, 3);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
