diff options
Diffstat (limited to 'devtools/client/canvasdebugger/test/doc_webgl-drawElements.html')
-rw-r--r-- | devtools/client/canvasdebugger/test/doc_webgl-drawElements.html | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/devtools/client/canvasdebugger/test/doc_webgl-drawElements.html b/devtools/client/canvasdebugger/test/doc_webgl-drawElements.html new file mode 100644 index 000000000..a8ba4a3e8 --- /dev/null +++ b/devtools/client/canvasdebugger/test/doc_webgl-drawElements.html @@ -0,0 +1,225 @@ +<!-- Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ --> +<!doctype html> + +<html> + <head> + <meta charset="utf-8"/> + <title>WebGL editor test page</title> + </head> + + <body> + <canvas id="canvas" width="128" height="128"></canvas> + <script id="shader-fs" type="x-shader/x-fragment"> + precision mediump float; + uniform vec4 mtrColor; + + void main(void) { + gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) * mtrColor; + } + </script> + <script id="shader-vs" type="x-shader/x-vertex"> + attribute vec3 aVertexPosition; + + void main(void) { + gl_PointSize = 5.0; + gl_Position = vec4(aVertexPosition, 1.0); + } + </script> + <script type="text/javascript;version=1.8"> + "use strict"; + + let canvas, gl, shaderProgram; + let triangleVertexPositionBuffer, squareVertexPositionBuffer; + let triangleIndexBuffer; + let squareIndexBuffer, squareStripIndexBuffer, squareFanIndexBuffer + + window.onload = function() { + canvas = document.querySelector("canvas"); + gl = canvas.getContext("webgl", { preserveDrawingBuffer: true }); + gl.viewportWidth = canvas.width; + gl.viewportHeight = canvas.height; + + initShaders(); + initBuffers(); + + gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight); + gl.clearColor(0.0, 0.0, 0.0, 1.0); + gl.disable(gl.DEPTH_TEST); + drawScene(); + } + + function getShader(gl, id) { + var shaderScript = document.getElementById(id); + if (!shaderScript) { + return null; + } + + var str = ""; + var k = shaderScript.firstChild; + while (k) { + if (k.nodeType == 3) { + str += k.textContent; + } + k = k.nextSibling; + } + + var shader; + if (shaderScript.type == "x-shader/x-fragment") { + shader = gl.createShader(gl.FRAGMENT_SHADER); + } else if (shaderScript.type == "x-shader/x-vertex") { + shader = gl.createShader(gl.VERTEX_SHADER); + } else { + return null; + } + + gl.shaderSource(shader, str); + gl.compileShader(shader); + + if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { + alert(gl.getShaderInfoLog(shader)); + return null; + } + + return shader; + } + + function initShaders() { + var fragmentShader = getShader(gl, "shader-fs"); + var vertexShader = getShader(gl, "shader-vs"); + + shaderProgram = gl.createProgram(); + gl.attachShader(shaderProgram, vertexShader); + gl.attachShader(shaderProgram, fragmentShader); + gl.linkProgram(shaderProgram); + + if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { + alert("Could not initialise shaders"); + } + + gl.useProgram(shaderProgram); + + shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition"); + shaderProgram.pMaterialColor = gl.getUniformLocation(shaderProgram, "mtrColor"); + gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute); + } + + function initBuffers() { + // Create triangle vertex/index buffer + triangleVertexPositionBuffer = gl.createBuffer(); + gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer); + var vertices = [ + 0.0, 0.5, 0.0, + -0.5, -0.5, 0.0, + 0.5, -0.5, 0.0 + ]; + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); + triangleVertexPositionBuffer.itemSize = 3; + triangleVertexPositionBuffer.numItems = 3; + + triangleIndexBuffer = gl.createBuffer(); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleIndexBuffer); + var indices = [ + 0, 1, 2 + ]; + gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW); + triangleIndexBuffer.itemSize = 1; + triangleIndexBuffer.numItems = 3; + + // Create square vertex/index buffer + squareVertexPositionBuffer = gl.createBuffer(); + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer); + vertices = [ + 0.8, 0.8, 0.0, + -0.8, 0.8, 0.0, + 0.8, -0.8, 0.0, + -0.8, -0.8, 0.0 + ]; + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); + squareVertexPositionBuffer.itemSize = 3; + squareVertexPositionBuffer.numItems = 4; + + squareIndexBuffer = gl.createBuffer(); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareIndexBuffer); + indices = [ + 0, 1, 2, + 1, 3, 2 + ]; + gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW); + squareIndexBuffer.itemSize = 1; + squareIndexBuffer.numItems = 6; + + squareStripIndexBuffer = gl.createBuffer(); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer); + indices = [ + 0, 1, 2, 3 + ]; + gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW); + squareStripIndexBuffer.itemSize = 1; + squareStripIndexBuffer.numItems = 4; + + } + + function drawScene() { + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT); + + // DrawElements + // -------------- + // draw triangle + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer); + gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); + gl.uniform4f(shaderProgram.pMaterialColor, 1, 1, 1, 1); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareIndexBuffer); + gl.drawElements(gl.TRIANGLES, squareIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0); + + // draw square - triangle strip + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer); + gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); + gl.uniform4f(shaderProgram.pMaterialColor, 0, 1, 0, 1); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer); + gl.drawElements(gl.TRIANGLE_FAN, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0); + + // draw square - triangle fan + gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer); + gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); + gl.uniform4f(shaderProgram.pMaterialColor, 1, 0, 0, 1); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleIndexBuffer); + gl.drawElements(gl.TRIANGLE_FAN, triangleIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0); + + // draw points + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer); + gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); + gl.uniform4f(shaderProgram.pMaterialColor, 1, 0, 1, 1); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer); + gl.drawElements(gl.POINTS, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0); + + // draw lines + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer); + gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); + gl.uniform4f(shaderProgram.pMaterialColor, 0, 0, 1, 1); + gl.lineWidth(8.0); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer); + gl.drawElements(gl.LINES, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0); + + // draw line strip + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer); + gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); + gl.uniform4f(shaderProgram.pMaterialColor, 0.9, 0.6, 0, 1); + gl.lineWidth(3.0); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer); + gl.drawElements(gl.LINE_STRIP, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0); + + // draw line loop + gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer); + gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); + gl.uniform4f(shaderProgram.pMaterialColor, 0, 1, 1, 1); + gl.lineWidth(3.0); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleIndexBuffer); + gl.drawElements(gl.LINE_LOOP, triangleIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0); + + window.requestAnimationFrame(drawScene); + } + </script> + </body> + +</html>
\ No newline at end of file |