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
| function start(gl) { if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) { console.log('failed to init shaders') return } let n = initVertexBuffers(gl) if (n < 0) { console.log('failed to init vertex buffers') return } gl.clearColor(0, 0, 0, 1) gl.clear(gl.COLOR_BUFFER_BIT) gl.drawArrays(gl.TRIANGLES,0,n) }
function initVertexBuffers(gl) { let verticesColors = new Float32Array([ 0.0, 0.5, 1.0, 0.0, 0.0, -0.5, -0.5, 0.0, 1.0, 0.0, 0.5, -0.5, 0.0, 0.0, 1.0 ]) let n = 3 let FSIZE = verticesColors.BYTES_PER_ELEMENT let a_Position = gl.getAttribLocation(gl.program, 'a_Position') let a_Color = gl.getAttribLocation(gl.program, 'a_Color') if (a_Position < 0 || a_Color < 0) { return -1 } let vertexBuffer = gl.createBuffer() let colorBuffer = gl.createBuffer() gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer) gl.bufferData(gl.ARRAY_BUFFER, verticesColors, gl.STATIC_DRAW) gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 5 * FSIZE, 0) gl.enableVertexAttribArray(a_Position)
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer) gl.bufferData(gl.ARRAY_BUFFER, verticesColors, gl.STATIC_DRAW) gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 5 * FSIZE, 2 * FSIZE) gl.enableVertexAttribArray(a_Color) return n }
|