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
| window.onload = function init() { var canvas = document.getElementById("trisquare-canvas"); gl = canvas.getContext("webgl2"); if( !gl ){ alert( "WebGL isn't available" ); }
var vertices1 = new Float32Array([ -0.5, 0.5, -1.0, -0.5, 0.0, -0.5 ]);
var vertices2 = new Float32Array([ 0.0, -0.5, 1.0, -0.5, 1.0, 0.5, 1.0, 0.5, 0.0, 0.5, 0.0, -0.5 ]); gl.viewport(0, 0, canvas.width, canvas.height); gl.clearColor(1.0, 1.0, 1.0, 1.0);
var program1 = initShaders(gl, "vertex-shader", "fragment1-shader"); gl.useProgram(program1);
var bufferId1 = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, bufferId1); gl.bufferData(gl.ARRAY_BUFFER, vertices1, gl.STATIC_DRAW);
var vPosition1 = gl.getAttribLocation(program1, "vPosition"); gl.vertexAttribPointer(vPosition1, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(vPosition1); render1();
var program2 = initShaders( gl, "vertex-shader", "fragment2-shader" ); gl.useProgram( program2 );
var bufferId2 = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, bufferId2); gl.bufferData(gl.ARRAY_BUFFER, vertices2, gl.STATIC_DRAW);
var vPosition2 = gl.getAttribLocation( program2, "vPosition" ); gl.vertexAttribPointer( vPosition2, 2, gl.FLOAT, false, 0, 0 ); gl.enableVertexAttribArray( vPosition2 ); render2(); } function render1() { gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLES, 0, 3); } function render2() { gl.drawArrays(gl.TRIANGLES, 0, 6); }
|