Question
use the following javascript file and the html file to create a graphic by adding additional shapes by: 1) creating functions drawTriangle( vec2, vec2, vec2)
use the following javascript file and the html file to create a graphic by adding additional shapes by:
1) creating functions drawTriangle( vec2, vec2, vec2) and drawSquare (vec2, vec2)
- square takes the upper left and lower right as the two vec2
2) call these functions with different values to draw the shapes necessary to complete your graphic
triangle.js:
var gl; var points; var color;
window.onload = function init() { var canvas = document.getElementById( "gl-canvas" ); gl = WebGLUtils.setupWebGL( canvas ); if ( !gl ) { alert( "WebGL isn't available" ); }
//var vertices = new Float32Array([-1, -1, 0, 1, 1, -1]); points = [vec2(-1,-1),vec2(0,1),vec2(1,-1)] color =[vec4(1,0,0,1),vec4(0,1,0,1),vec4(0,0,1,1)]; // 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 ); gl.bufferData( gl.ARRAY_BUFFER,flatten(points), gl.STATIC_DRAW );
// Associate out shader variables with our data buffer var vPosition = gl.getAttribLocation( program, "vPosition" ); gl.vertexAttribPointer( vPosition, 2, gl.FLOAT, false, 0, 0 ); gl.enableVertexAttribArray( vPosition ); // Load the color data into the GPU var cbufferId = gl.createBuffer(); gl.bindBuffer( gl.ARRAY_BUFFER, cbufferId ); gl.bufferData( gl.ARRAY_BUFFER,flatten(color), gl.STATIC_DRAW );
// Associate out color shader variables with our data buffer var vColor = gl.getAttribLocation( program, "vColor" ); gl.vertexAttribPointer( vColor, 4, gl.FLOAT, false, 0, 0 ); gl.enableVertexAttribArray( vColor ); //setting a uniform variable for color var uColor = vec4(1.0, 1.0, 0.0, 1.0); var colorLoc = gl.getUniformLocation( program, "uColor" ); gl.uniform4fv( colorLoc, flatten(uColor));
render(); };
function render() { gl.clear( gl.COLOR_BUFFER_BIT ); gl.drawArrays( gl.TRIANGLES, 0, 3 ); }
triangle.html:
attribute vec4 vPosition;
attribute vec4 vColor;
varying vec4 fColor;
void
main()
{
gl_Position = vPosition;
fColor = vColor;
}
precision mediump float;
varying vec4 fColor;
uniform vec4 uColor;
void
main()
{
gl_FragColor = uColor;
}
Oops ... your browser doesn't support the HTML5 canvas element
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started